var crossFadeIVL= 0;
var timeOutIVL = 0;
 
var autocrossFadeSpeed=7000; //time between fades
var firstTimerSpeed=0; //time to start fade intially
var restartTimerSpeed=30000; //time to restart after pausing in ms
var blnAnimRunning=false;
 
$(document).ready(function(){		
			
	//cache background images for IE6
	if (document && document.execCommand) {
		try { document.execCommand("BackgroundImageCache",false,true); }
		catch (e) { }
	}

	//start panel animation
	timeOutIVL=setTimeout("startCrossFade()",firstTimerSpeed); //start delay

	//start mouse over/keyboard actions on tabs
	initPanelLinkEvents();	
	
	preloadImages();
	
				
}); //end document.ready


function startCrossFade() {
	 blnAnimRunning=true;
	 crossFadeIVL=setInterval('crossFade()', autocrossFadeSpeed); //miliseconds
	 $("#animController").fadeIn("slow");
}
 
 function initPanelLinkEvents()
 {
	$("#hpLink1").bind('click focus',function(event){ 
		event.preventDefault();
		clearInterval(crossFadeIVL);
		clearTimeout(timeOutIVL);
		togglePanel(1);
		blnAnimRunning=false;
		if($("#toggleAnim img").attr("src")!="/images/play-off.gif")
		{
			$("#toggleAnim").html('<img src="/images/play-off.gif" width="14" height="14" alt="Play"/>');
		}
		timeOutIVL=setTimeout("startCrossFade()",restartTimerSpeed);
		
	});
 
   $("#hpLink2").bind('click focus',function(event){
		event.preventDefault();
		clearInterval(crossFadeIVL);							
		clearTimeout(timeOutIVL);
		togglePanel(2);
		blnAnimRunning=false;
		if($("#toggleAnim img").attr("src")!="/images/play-off.gif")
		{
			$("#toggleAnim").html('<img src="/images/play-off.gif" width="14" height="14" alt="Play"/>');
		}
		timeOutIVL=setTimeout("startCrossFade()",restartTimerSpeed);
		
	});
   
    $("#hpLink3").bind('click focus',function(event){
		event.preventDefault();
		clearInterval(crossFadeIVL);
		clearTimeout(timeOutIVL);
		togglePanel(3);
		blnAnimRunning=false;
		if($("#toggleAnim img").attr("src")!="/images/play-off.gif")
		{
			$("#toggleAnim").html('<img src="/images/play-off.gif" width="14" height="14" alt="Play"/>');
		}
		timeOutIVL=setTimeout("startCrossFade()",restartTimerSpeed);
		
	});
 
 
	$("#toggleAnim").click(function(event){
		event.preventDefault();
		if(blnAnimRunning)
		{
			clearInterval(crossFadeIVL);
			clearTimeout(timeOutIVL);
			blnAnimRunning=false;
			if($("#toggleAnim img").attr("src")!="/images/play-off.gif")
			{
				$("#toggleAnim").html('<img src="/images/play-off.gif" width="14" height="14" alt="Play"/>');
			}
		}
		else {
			crossFade();
			startCrossFade();
			if($("#toggleAnim img").attr("src")!="/images/pause-off.gif")
			{
			$("#toggleAnim").html('<img src="/images/pause-off.gif" width="14" height="14" alt="Pause"/>');
			}
		}
		
	});

	//animation controller
	$("#toggleAnim").hover(
	  function () {
		$img=$(this).find("img");
		$img[0].src = $img[0].src.replace("-off","-hover");
	  }, 
	  function () {
		$img=$(this).find("img");
		$img[0].src = $img[0].src.replace("-hover","-off");
	  }
	);
 }
 
function togglePanel(panelID)
{
  //removed anim
	for(i=1;i<4;i++)
	{
		if(panelID==i)
		{
			//fade in and remove link background image
			$panel=$("#homepromopanel" + i);
			if($panel.hasClass("panelCurrent"))
			{
				break;
			}
			$panel.fadeIn("fast",function(){
				if(!$.support.opacity) this.style.removeAttribute('filter'); 
			});
			$panel.addClass("panelCurrent");
			$("#promoTabsContainer #hpLink" + i).addClass("hpCurrentLink");
		}
		else {
			//fade out and add in background image
			$panel=$("#homepromopanel" + i);
			$panel.fadeOut("fast",function(){
				if (!$.support.opacity) this.style.removeAttribute('filter'); 
			});
			$panel.removeClass("panelCurrent");
			$("#promoTabsContainer #hpLink" + i).removeClass("hpCurrentLink");
		}
	}
}

function crossFade() {
	//work out current panel 
	//loop through each
	var arrPanels=new Array($("#homepromopanel1"),$("#homepromopanel2"),$("#homepromopanel3"));
	
	//get current
	var panelSelected=0;
	var nextPanel=0;
	for(i=0;i<3;i++)
	{
		if(arrPanels[i].hasClass("panelCurrent")) {
		panelSelected=i;	
		}
	}
	//select next panel;
	if(panelSelected!=2) {nextPanel=panelSelected+1;} else {nextPanel=0;}
	
	togglePanel(nextPanel+1);
}

function hideElem(e,id)
{
	e.preventDefault();
	$("#" + id).hide();
}

