var focused = false;

function onBlur() {
	//document.body.className = 'blurred';
	focused = false;
};
function onFocus(){
	//document.body.className = 'focused';
	focused = true;
	$.getJSON("http://www.fln.org/flplayer/update_streamers.php?&t=" + Math.random() + "&jsonp_callback=?", 
		function(data){
			$("#streamers").data("fln", data.fln); 
			$("#streamers").data("resound", data.resound); 
			$("#streamers").data("gentle", data.gentle);
			$("#streamers").data("gospel", data.gospel); 	
			update_station(station_key);
		});
};
 
if (/*@cc_on!@*/false) { // check for Internet Explorer
	document.onfocusin = onFocus;
	document.onfocusout = onBlur;
} else {
	window.onfocus = onFocus;
	window.onblur = onBlur;
}
 

	function update_station(station_key){
	
		if(typeof(station_key) == 'undefined') 
		{
			station_key = $("#streamers").data("current_station");
		}
		
		dDate = new Date();
		dTime = dDate.getTime();
		howOld = dTime - $("#streamers").data("last_update");
		if( howOld > 60000 )
		{
			fetch_streamer_data(station_key);
			$("#streamers").data("last_update", dTime);
		
		} else
		{
			var station = $("#streamers").data(station_key);
			
			if(station_key == "fln") 		$("#current_station").css("background-position","0px 0px");
			if(station_key == "resound") 	$("#current_station").css("background-position","-200px 0px");
			if(station_key == "gentle") 	$("#current_station").css("background-position","-400px 0px");
			if(station_key == "gospel") 	$("#current_station").css("background-position","-600px 0px");
		
			if( $("#streamers").data("current_artist") != station.artist && $("#streamers").data("current_title") != station.title )
			{
				$('#current_artist').html(station.artist);
				$('#current_title').html(station.title);
				$('#current_album_art').attr("src", station.album);
			}
	
			$("#streamers").data("current_station", station_key);
			$("#streamers").data("current_artist", station.artist);
			$("#streamers").data("current_title", station.title);
			$("#streamers").data("current_album_art", station.album);
		}
	}
	
	function fetch_streamer_data(station_key){
		if (focused)
		{
			$.getJSON("http://www.fln.org/flplayer/update_streamers.php?&t=" + Math.random() + "&jsonp_callback=?", 
			function(data){
				$("#streamers").data("fln", data.fln); 
				$("#streamers").data("resound", data.resound); 
				$("#streamers").data("gentle", data.gentle);
				$("#streamers").data("gospel", data.gospel); 	
				update_station(station_key);
			});
		}
	}

	$(function(){
			   
		$("#util #links li:last, #nav ul li:last").addClass('last');
		$(".day li h4").truncate(45);                 
		$("#upnext div.day.first").remove();
		$("#upnext .day li").click(function(){
			window.location = $(this).find("a").attr("href"); 
		});
		$("#nav li").click(function(){
			window.location = $(this).find("a").attr("href"); 
		});
			
		$('#rotator_slides')
		.cycle({ 
			fx:     'fade', 
			speed:  500, 
			timeout: 7000, 
			pager:  '#rotator_nav',
			next: '#rotator_next',
			prev: '#rotator_prev'
		}).show();
		
		$('#sponsors')
		.cycle({ 
			fx:     'fade', 
			speed:  500, 
			timeout: 7000,
			cleartypeNoBg: true
		}).show();
		
		$('#podcast_features')
		.cycle({ 
			fx:     'fade', 
			speed:  500, 
			timeout: 7000,
			cleartypeNoBg: true,
			pager: '#podcast_features_nav',
			pagerClick: function(i, s){

			}
		}).show();
		
		
		
		$("#streamers").data("current_station", "fln");
		$("#streamers").data("last_update", 0);
		setInterval(function(){
			update_station();
		}, 15000);
	
		$("#streamer_family_life").mouseenter(function(){
			update_station("fln");
		}).mouseenter();
		
		$("#streamer_resound").mouseenter(function(){
			update_station("resound");
		});
		
		$("#streamer_gentle_praise").mouseenter(function(){
			update_station("gentle");
		});
		
		$("#streamer_gospel").mouseenter(function(){
			update_station("gospel");
		});
		
				
		//$("#homenav_media, #media_submenu").hover(
		//	function(e){
		//		$('#rotator_slides').cycle("pause").hide();
		//		$("#creative_arts_submenu, #resourcing_submenu").clearQueue().stop().hide();
		//		$submenu_links = ($(this).attr("id") == "homenav_media") ? $("#media_submenu").find(".submenu_links").find("a").css({opacity:"0",top:"10px"}) : null;
		//		$("#media_submenu").clearQueue().stop().css({opacity: 1, left: "0px", top: "0px"}).show();
		//		$submenu_links.each(function(i){
		//			$(this).delay(i*50).animate({opacity: 1, top: "0px"}, 250);
		//		});
		//	},
		//	function(e){
		//		$("#media_submenu").queue(function(){
		//			$(this).delay(1000).animate({opacity: 0, top: "0px"}, 500, function(){ 												
		//				$(this).hide();
		//				$("#rotator_slides").show().cycle("resume");						
		//			});
		//			$(this).dequeue();	
		//		});
		//	}
		//);
		
		//$(".submenu_close").click(function(){								
		//	$(this).closest("div").hide();
		//	$("#rotator_slides").show().cycle("resume");
		//});

		//$("#homenav_creative-arts, #creative_arts_submenu").hover(
		//	function(e){
		//		$('#rotator_slides').cycle("pause").hide();
		//		$("#media_submenu, #resourcing_submenu").clearQueue().stop().hide();
		//		$submenu_links = ($(this).attr("id") == "homenav_creative-arts") ? $("#creative_arts_submenu").find(".submenu_links").find("a").css({opacity:"0",top:"10px"}) : null;
		//		$("#creative_arts_submenu").clearQueue().stop().css({opacity: 1, left: "0px", top: "0px"}).show();
		//		$submenu_links.each(function(i){
		//			$(this).delay(i*50).animate({opacity: 1, top: "0px"}, 250);
		//		});
		//	},
		//	function(e){
		//		$("#creative_arts_submenu").queue(function(){
		//			$(this).delay(1000).animate({opacity: 0, top: "0px"}, 500, function(){ 												
		//				$(this).hide();
		//				$("#rotator_slides").show().cycle("resume");						
		//			});
		//			$(this).dequeue();	
		//		});
		//	}
		//);
		
		//$("#homenav_resourcing, #resourcing_submenu").hover(
		//	function(e){
		//		$('#rotator_slides').cycle("pause").hide();
		//		$("#media_submenu, #creative_arts_submenu").clearQueue().stop().hide();
		//		$submenu_links = ($(this).attr("id") == "homenav_resourcing") ? $("#resourcing_submenu").find(".submenu_links").find("a").css({opacity:"0",top:"10px"}) : null;
		//		$("#resourcing_submenu").clearQueue().stop().css({opacity: 1, left: "0px", top: "0px"}).show();
		//		$submenu_links.each(function(i){
		//			$(this).delay(i*50).animate({opacity: 1, top: "0px"}, 250);
		//		});
		//	},
		//	function(e){
		//		$("#resourcing_submenu").queue(function(){
		//			$(this).delay(1000).animate({opacity: 0, top: "0px"}, 500, function(){ 												
		//				$(this).hide();
		//				$("#rotator_slides").show().cycle("resume");						
		//			});
		//			$(this).dequeue();	
		//		});
		//	}
		//);
		
		$(".submenu_links").find("a").hover(
			function(e){
				$(this).clearQueue().stop().css({opacity: 1, left: "0px", top: "0px"}).animate({left: "-10px"}, 100);
				position = $(this).position();
				$("#submenu_arrow").insertAfter(this).css("top", position.top);
			},
			function(e){
				$("#submenu_arrow").css("top", "-9999px");
				$(this).clearQueue().stop().css({left: "0px"});
			}
		);
			
		$("#link_media, #link_creative_arts, #link_resourcing").hover(
			function(e){
				position = $(this).position();
				$("#submenu_arrow").insertAfter(this).css({"top": "15px"});
			},
			function(e){
				$("#submenu_arrow").css({"top": "-9999px"});
			}
		);
			
	
	});

