// JavaScript Document
var date = "";
var news_height = "";

function mycarousel_initCallback(carousel){
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

$(document).ready(function() {
	$('#slider_principal').innerfade({ speed: 'slow', timeout: 5000, type: 'random', containerheight: '475px' });					   
					   
	$("#agenda_disciplines").slideUp();
	$("#agenda_places").slideUp();		
	$("#agenda_calendar").slideUp();		
	
	$("#agenda_disciplines a").tooltip({showURL: false });				
	$("#agenda_scroll li img").tooltip({showURL: false });	

	if( $('#agenda').css("display") != 'none'){
		jQuery('#agenda_scrolling').jcarousel({
			wrap: 'circular',
			vertical: true,
			scroll:3,
			initCallback: mycarousel_initCallback
		});
	}
			
	//Eventos
	$('#event_list a.scrollup').mouseover(function () {$('#event_list_content').scrollTo(0,5000);});
	$('#event_list a.scrollup').mouseout(function () {$('#event_list_content').stop();});
	
	$('#event_list a.scrolldown').mouseover(function () {$('#event_list_content').scrollTo("#last_element",5000);});
	$('#event_list a.scrolldown').mouseout(function () {$('#event_list_content').stop();});
	
	// lista de lugares
	$('#agenda_places a.scrollup_white').mouseover(function () {$('#agenda_places #list').scrollTo(0,5000);});
	$('#agenda_places a.scrollup_white').mouseout(function () {$('#agenda_places #list').stop();});
	
	$('#agenda_places a.scrolldown_white').mouseover(function () {$('#agenda_places #list').scrollTo('#places_last',5000);});
	$('#agenda_places a.scrolldown_white').mouseout(function () {$('#agenda_places #list').stop();});
		
	$('#agenda_disciplines a').click(function () {
		$('#event_list_content').scrollTo($(this).attr('href'), 800);//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		return false; //Discard the link default behavior
	});
	
	$('#agenda_places a.place_link').click(function () {
		$('#event_list_content').scrollTo($(this).attr('href'), 800);//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		return false; //Discard the link default behavior
	});	
	
	$('#agenda_calendar a.selected').click(function () {
		$('#event_list_content').scrollTo($(this).attr('href'), 800);//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		return false; //Discard the link default behavior
	});
	
	//// NOTICIAS ////
	
	news_height = $('#panel-1').height() != 0 ? $('#panel-1').height() : 480;
	$('#mask').height(news_height);	 //Get the height of the first item
	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt(520 * $('#panel div').length));

	$('#panel div').width($('#mask').css('width')); //Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
		var panelheight = $($(this).attr('href')).height(); //Get the height of the sub-panel
		
		$('a[rel=panel]').removeClass('selected'); //Set class for the selected item
		$(this).addClass('selected');
		
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});//Resize the height			
		$('#mask').scrollTo($(this).attr('href'), 800, {axis:'x'});//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
	
		return false; //Discard the link default behavior
	});
	
	$('.news_pager a.num').mouseover(function(){$('#news_title').html($(this).attr('title'));}); // titulo, en el over de la numeracion
	 
});
	
function close_all() {
	$("#agenda_disciplines").slideUp();
	$("#agenda_places").slideUp();		
	$("#agenda_calendar").slideUp();			
}
	
	function toggle(id) {
		close_all();
		
		var main = $('#agenda').css("display");
		/*
		if(main == 'none') {
			$('#agenda').fadeIn();
			
			jQuery('#agenda_scrolling').jcarousel({
				auto: 10,
				wrap: 'circular',
				vertical: true,
				scroll:5,
				initCallback: mycarousel_initCallback
			});
		}
		*/
	
		var visible = $(id).css("display");
		if (visible=="none") {
			$(id).slideDown("slow");
			load_event_list(id);
			$('#principal').fadeOut();
		} else if(id == "#agenda_all"){
			load_event_list(id);
			$('#principal').fadeOut();	
		} else if(id == "#agenda_other"){
			var filter = $('#other').val();
			load_event_list(id, filter);
			$('#principal').fadeOut();	
		} else {
			$(id).slideUp("slow");
			$('#event_list').fadeOut();
			if($('#agenda').attr('class') != 'agenda_inner'){$('#principal').fadeIn(); } else {$('#agenda').fadeOut();} 
		}
	}

	/*function set_all() { toggle(id)  }*/
	function set_other() { close_all(); }

	function load_event_list(order, filter){
		$('#event_list').fadeIn();
		$.ajax({
		  url: '/scripts/schedule_list.php',
		  type: 'POST',
		  async: true,
		  data: 'order='+order+'&filter='+filter,
		  beforeSend:function(response){$('#event_list_content').html("<img src='/img/ajax-loader.gif' class='loading' style='width:auto; height:auto;'>");},
		  success: function(response){
			  $('#event_list_content').html(response);
			  $('#event_list_content').scrollTo(0);
			}
		});
	}
	
	jQuery.easing.easeOutQuart = function (x, t, b, c, d) {return -c * ((t=t/d-1)*t*t*t - 1) + b;};