var default_height = 500;

// MAIN
$(document).ready(function()
{
	// display HTML code
	$('#event-description-par').html($('#event-description-par textarea').text());

	// Initialize map //
	var data = evalJson($('#flashmap').attr('class'));

	if (data != 'undefined')
	{
		try
		{
			$('#flashmap').flash({
				swf:'/imgs/flash/map.swf',
				width:630,
				height:260,
				hasVersion: 9,
				flashvars:
				{
					Latitud: data.geopoint.lat,
					Longitud: data.geopoint.lng,
					key: data.apikey,
					language: data.lang,
					FechaHora: data.date_ini,
					Kml: data.kml,
					Url: data.link,
					Nombre: data.title,
					Lugar: data.location,
					browser: data.browser,
					target: '_self'
				},
				params:{allowFullScreen:true}
			});
		}
		catch(e){}
	}

    // read more... //
    var par_height = $('#event-description-par').height(); // pick up the correct height before starting animations
    if(par_height > default_height)
    {
        $('#event-description-par').css({
            'height': default_height
        });
        $('#readmore').show();
    }
    $('#readmore').click(function()
    {
        if(par_height <= default_height)
		{
			return false;
		}

        if (parseInt($('#event-description-par').css('height')) == default_height)
		{
			$('#event-description-par').animate({
				height: par_height
			},
			'slow',
			'swing',
			function()
			{
				$('div.dissolve-bottom').hide();
				$('#readmore').empty().append(msg.HIDE_TEXT);
			});
		}
		else
		{
			$('div.dissolve-bottom').show();
			$('#event-description-par').animate({
				height: default_height
			},
			'slow',
			'swing',
			function()
			{
				$('#readmore').empty().append(msg.READ_MORE);
			});
		}

        return false;
    });

	//Init event comments list
	reload_event_comments();

	//Handle comments
	$('#event_comments_form').yav({
		errorPosition: 'after',
		onOk: function(the_form){
			$('.error', the_form).remove();
			$.post(noCache('/event_detail/add_comment'), $('#event_comments_form').serialize(), function(response)
			{
				var data = evalJson(response);
				if(data.stat == 'OK')
				{
					$.post(noCache('/event_detail/do_rpx_comment'), {
						long_url: document.location.href,
						comment: $('#event_comment').val()
					});
					$('#event_comments_form textarea').val($('#event_comments_form textarea').attr('placeholder'));
					msgbox_success(msg.SUCCESS);
					reload_event_comments();
				}
				else
				{
					$('#error_div_comments').empty().append($('<p></p>').addClass('error').text(msg[data.msg]));
				}
			});
			return false;
		}
	});
});

function reload_event_comments(ruta)
{
	if(ruta == 'undefined' || !ruta)
	{
		ruta = '/event_detail/get_comments_list/1';
	}
	$('#event_comments_list').load(noCache(ruta), {
		event_id: $('#eventid').val()
	}, function(){
		$('#event_comments_list .pagination a').click(function(){
			reload_event_comments($(this).attr('href').replace('#', '/'));
			return false;
		});
		$('#event_comments_list .comment a').click(function()
		{
			$.post(noCache('/event_detail/delete_comment'),
			{
				comment_id: $(this).attr('rel')
			},
			function(response)
			{
				var data = evalJson(response);
				if(data.stat == 'OK')
				{
					reload_event_comments();
				}
				else
				{
					$('<p>').addClass('error').text(msg[data.msg]).insertBefore('#event_comments_list');
				}
			});

			return false;
		});
	});
}

