/* define some variables */
var last_id = 'k_A'; //quick lookup
var cycle_current_id = 0; //recipe slideshow

(function(){
	if(typeof(Cufon) != 'undefined'){
		//Cufon.replace('.replace, #main-navigation li *, #secondary-content aside>h1, aside>hgroup>h1, aside>header>h1');
		//for lifeStyle:
		Cufon.replace('h1',{hover: true});
		Cufon.replace('h2',{hover: true});
		Cufon.replace('h3',{hover: true});
		Cufon.replace('#pull-quote blockquote',{hover: true});
		Cufon.replace('.link-quote',{hover: true});
		Cufon.replace('#main-navigation li',{hover: true});
		Cufon.replace('.keyboard li',{hover: true});
		Cufon.replace('.breadcrumbs');
		Cufon.replace('.schedule-links');
		Cufon.replace('.print-link');
		Cufon.replace('.schedule article .series, .schedule article .episodes, .schedule article .subtitles');
		Cufon.replace('#login a',{hover: true});
		Cufon.replace('#menu_principal li a',{hover: true});
		Cufon.replace('#menu_principal li');

		Cufon.replace('#ampliar a',{hover: true});
		Cufon.replace('#ampliar2 a',{hover: true});
		
		Cufon.replace('.submenu_pare_actiu');
		Cufon.replace('.submenu_pare_inactiu',{hover: true});
		Cufon.replace('.submenu_fill_actiu');
		Cufon.replace('.submenu_fill_inactiu',{hover: true});
		Cufon.replace('.univers_33_taronja',{hover: true});	
		Cufon.replace('.univers_20_taronja',{hover: true});			
		
		
		
		


	}

	if(typeof(jQuery) == 'undefined'){ // jQuery hasn't loaded
		return;
	}
	
	/* some rollovers need js because of cufon */
	$('#clips a').each(function(index) {
				$(this).hover( 
					function () {Cufon.replace('#clips a#'+index+' h3',{color: '#FFFFFF'});} ,
					function () {Cufon.replace('#clips a#'+index+' h3',{color: '#d0e125'});}
				);
	});
	
	
	/* home carousel */
	if ($('#home-carousel').length > 0) $('#home-carousel').jcarousel();
	
	/* recipe slideshow */
	if ($('#cycle-pager li').length > 0)
		$('#cycle-pager li').each(function(index) {
					$(this).click(function() {cycleGoNext(index)});
		})
	
	//lets put "teaser" in front of dark label
	//if we use the darklabel inside each item well see an ugly flash effect on fadings
	//therefore dark label is in the pager and we need to put the teaser in front of this.
	$('#cycle-pager .teaser').html($('#cycle-list #i_0 .teaser').html());
	
	/* automatic slideshow */
	//timer = setInterval('cycleGoNext()',3000);
	
	/** quick lookup **/
	if (typeof $().movingBlock == 'function') {
		$('.keyboard').movingBlock({
					holder:'#container',
					topDif:405,
					bottomDif:0,
					toEl:false,
					//cssTop:'marginTop',
					cssTop:'top',
					duration:200
				});
	}
		
	$('.keyboard li').each(function(index) {
		$(this).click(function() {
			if (!$(this).hasClass('disabled')) {
				var current_id = $(this).attr('id');
				var section_id = 's_' + current_id.substr(2,1); 
				
				$.scrollTo('#'+section_id, 800 , {offset : -50} );
				
				$(this).addClass('active');
				Cufon.replace('#'+current_id,{color:'#FFFFFF'});
				
				$('#'+last_id).removeClass('active');
				Cufon.replace('#'+last_id,{color:'#333333'});
				
				last_id = $(this).attr('id');
			}
		});
	});

	/** signup dialog **/
	if (typeof $().jqm  == 'function' ) 
		$('#signup-dialog').jqm(
			{
			ajax :'@href' , 
			modal : false , 
			overlay : 80,
			trigger : '#login a',
			onLoad : function(hash){Cufon.replace('.modal-signup h1');},
			onShow : function(hash){ hash.w.fadeIn(500);}
			});

		$('#country-selection').jqm(
			{
			ajax :'@href' , 
			modal : false , 
			overlay : 100,
			overlayClass : 'country-select-overlay',
			trigger : '#country a',
			onLoad : function(hash){
						$('#country-selection .selector').mouseenter(function() {$('#country-selection .items').show();});
						$('#country-selection .items').mouseleave(function() {$('#country-selection .items').hide();});
			},
			onShow : function(hash){ hash.w.fadeIn(500);}
			});
			
	var root = $('#masthead > h1 a').attr('href');

})();

function cycleGoNext(forcedId) {
	
	if(forcedId != null) { if (cycle_current_id == forcedId) return false; cycle_next_id = forcedId;} 
	else cycle_next_id = cycle_current_id + 1;
	
	if ($('#cycle-list #i_' + cycle_next_id).length <= 0 ) cycle_next_id = 0; 
		
	$('#cycle-list #i_' + cycle_current_id).fadeOut(500);
	$('#cycle-list #i_' + cycle_next_id).fadeIn(500);
	
	//lets put "teaser" in front of dark label
	//if we use the darklabel inside each item well see an ugly flash effect on fadings
	//therefore dark label is in the pager and we need to put the teaser in front of this.
	$('#cycle-pager .teaser').html($('#cycle-list #i_' + cycle_next_id + ' .teaser').html());
	
	$('#cycle-pager #p_' + cycle_current_id).removeClass('active');
	$('#cycle-pager #p_' + cycle_next_id).addClass('active');
	
	cycle_current_id = cycle_next_id;
}






