//homepage promo rotation
function set_next_promo(image, thumbnail){

}

function rotate_promos(next_photo, next_thumbnail){
	var current_photo = $("#promos div.promo.current");
	var current_thumbnail = $("#thumbnails div.thumbnail.current");
	
	if (next_photo == undefined || next_thumbnail == undefined){
		var next_photo = current_photo.next();
		var next_thumbnail = current_thumbnail.next();
	}
	
	if (next_photo.length == 0){
		next_photo = $("#promos div.promo:first");
		next_thumbnail = $("#thumbnails div.thumbnail:first");
	}
	
	current_photo.removeClass("current").addClass("previous");
	current_thumbnail.removeClass("current");
	
	//next_photo.css({opacity:0.0}).addClass("current").animate({opacity:1.0}, 1000, function(){ current_photo.removeClass("previous"); });
	next_photo.css({opacity:0.0}).addClass("current").animate({opacity:1.0}, 10, function(){ current_photo.removeClass("previous"); });
	next_thumbnail.addClass("current").animate({opacity:1.0}, 1000);
	
	if (next_photo.find("#swfPromo").length == 1){
		pause_promos();
		var swf_path = $("#mainpromos #promos .current #swfPromoPath").text();
		var flv_path = $("#mainpromos #promos .current #swfPromoFLVPath").text();
		load_promo_swf(swf_path, flv_path);
		$("#mainpromos #promos .current #swfPromo").show();
	}
	else $("#swfPromo").hide();
}

//sidebar video player playlist and info
function playClip(clip_id, info_id){
	var player = Outlet.getOutletExtension("embeddedPlayer");
	player.playVideo(clip_id);
	
	var current_info = $("#playlist-info div.video.current");
	var current_thumbnail = $("#playlist-thumbnails div.thumbnail.current");
	
	current_info.removeClass("current").addClass("previous");
	current_thumbnail.removeClass("current");
	
	$("#playlist-info #video"+ info_id).css({opacity:0.0}).addClass("current").animate({opacity:1.0}, 200, function(){ current_info.removeClass("previous"); });
	$("#playlist-thumbnails #thumbnail"+ info_id).addClass("current").animate({opacity:1.0}, 1000);
}

var promo_rotate;
//start onload
$(function(){
	//homepage carousel
	$("#swfPromoPath, #swfPromoFLVPath").hide();
	$("#mainpromos #promos :first").addClass("current");
	$("#mainpromos #thumbnails :first").addClass("current");
	start_promos();
	
	if ($("#mainpromos #promos .current #swfPromo").length == 1){
		var swf_path = $("#mainpromos #promos .current #swfPromoPath").text();
		var flv_path = $("#mainpromos #promos .current #swfPromoFLVPath").text();
		load_promo_swf(swf_path, flv_path);
		$("#mainpromos #promos .current #swfPromo").show();
	}
	
	$("#mainpromos #thumbnails .thumbnail").css({"cursor":"pointer"}).click(function(){
		var id = $(this).attr("id").split("_");
		var promo_id = id[1];
		rotate_promos($("#promos div#promo_"+ promo_id), $(this));
		//reset the timer
		pause_promos();
		start_promos();
	});
	
	//homepage bottom rotation
	$("ul#homepage_graphic_rotation li:first").addClass("current");
	start_bottom_promos();
	
	//adjust spacing
	$(".module:not(.sub) .articles .article:first-child, .pressreleases .pressrelease:first-child").css({"border-top":"1px solid #e5e5e5"});
	$("#beinspired .article:first, .module .program:first").css({"padding-top":"0"});
	$(".module .program:first").css({"padding-bottom":"12px"});
	$("#beinspired .article:last-child").css({"border":"0"});
	
	$("#playlist #playlist-info :first").addClass("current");
	$("#playlist #playlist-thumbnails :first").css({"margin-left":0}).addClass("current");
	$("#playlist #playlist-thumbnails :nth-child(3)").css({"margin-left":"4px"});
	
	if ($("#dates").children().length == 7){
		//$("#dates .date-tab:last-child").css({"width":"93px"});//if it's a full date bar, extend the last one 6px (87 to 93) to make it fill the entire bar since it wasn't an even split
		$("#dates .date-tab:nth-child(2n)").css({"width":"89px"});//if it's a full date bar, extend three of then (every other 2) 2px (87 to 89) to make it fill the entire bar since it wasn't an even split
	}
	var current_tab_width = parseInt($("#dates .date-tab.current").css("width"));
	$("#dates :first-child.current").css({"border-right":"1px solid #0169a8", "width":current_tab_width-1+"px"});
	$("#dates :last-child.current").css({"border-left":"1px solid #0169a8", "width":current_tab_width-1+"px"});
	$("#dates .current").not("#dates :first-child, #dates :last-child").css({"border-right":"1px solid #0169a8", "border-left":"1px solid #0169a8", "width":current_tab_width-2+"px"});
	
	
	// PROGRAMMING
	$(".tab").click(function(){
		//get the day ID out of the div ID
		var day_id = $(this).attr("id").split("_")[1];
		
		//remove current class from all tabs
		$(".tab").removeClass("current");
		//add current class to the tab clicked
		$("#tab_"+ day_id).addClass("current");
		
		//hide all the days
		$(".day").hide();
		//show only the one I clicked
		$("#day_"+ day_id).show();
		//hide all the features
		$(".feature").hide();
		//show only the one I clicked
		$("#feature_"+ day_id).show();
	});
	//$(".tab:nth-child(2n)").css({"width":"89px"});
		
	//nav dropdowns
    $("ul.dropdown li").hover(
		function(){
	        $(this).addClass("hover");
	        $('ul:first',this).css('visibility', 'visible');
	    }, function(){
	        $(this).removeClass("hover");
	        $('ul:first',this).css('visibility', 'hidden');
    	}
	);
    
    //$("ul.dropdown li ul li:has(ul)").find("a:first").append(" Test ");
	
	//auto fit nav items
	//adjustNavSpacing(660);
	
	//search box text
	var default_search_text = 'SEARCH';
	$("#search_box").focus(function(){
		if ($(this).val() == default_search_text) $(this).val("");
	});
	$("#search_box").blur(function(){
		if ($(this).val() == '') $(this).val(default_search_text);
	});
});
//end onload

function pause_promos(){
	clearInterval(promo_rotate);
}

function start_promos(){
	promo_rotate = setInterval("rotate_promos()", 5000);
}

function promo_swf_started(){
	//alert("The video started.");
	pause_promos();
}

function promo_swf_ended(){
	//alert("The video ended.");
	start_promos();
}

function load_promo_swf(path, flv){
	var so = new SWFObject(path,"MainPromo", "456", "292", "9.0.115", "#000000");
	so.addVariable('quality', 'high');
	so.addParam('menu', 'false');
	so.addParam('scale', 'noscale');
	so.addParam('align', 'top');
	so.addParam('salign', 'lt');
	//so.addVariable('link_url', 'http://www.tuesday.com');
	so.addVariable('flv_url', flv);
	so.write("swfPromo");
}


//functions for homepage bottom image rotation

var promo_bottom_rotate;

function pause_bottom_promos(){
	clearInterval(promo_bottom_rotate);
}

function start_bottom_promos(){
	promo_bottom_rotate = setInterval("rotate_bottom_promos()", 5000);
}

function rotate_bottom_promos(){
	var current_bottom_image = $("ul#homepage_graphic_rotation li.current");
	
	if (next_bottom_image == undefined) var next_bottom_image = current_bottom_image.next();
	
	if (next_bottom_image.length == 0){
		next_bottom_image = $("ul#homepage_graphic_rotation li:first");
	}
	
	current_bottom_image.removeClass("current").addClass("previous");
	
	next_bottom_image.css({opacity:0.0}).addClass("current").animate({opacity:1.0}, 500, function(){ current_bottom_image.removeClass("previous"); });
}

