var imgCount = 0, iCurrImg, evt, stopimages;

window.onload = function()
{		
	/* only load sIFT for modern browsers (not IE6) */
	if (window.XMLHttpRequest) 
	{
		ApplySifrHeadings();
	}
	else		
	{
		// if its IE 6
		sfHover();
	}
	
	// If there is a div with an id of masthead, then run the image rotator code.
	
	if(document.getElementById('masthead'))
	{
		var images = document.getElementById('masthead').getElementsByTagName("img");
		
		// Get the total number of images
		imgCount = images.length;
		
		// iCurrentImag is the image currently being displayed, as a default, set it to the first image
		iCurrImg = 1;
		
		if(images.length > 1)
		{			
			showAnimation();
		}
		else
		{
			clearTimeout(evt);
		}
	}
}

function ApplySifrHeadings()
{
	
	if(typeof sIFR == "function")
	{		
		sIFR.bHideBrowserText = true;
		sIFR.setup();
		//sIFR.debug();
		sIFR();
		sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"/swf/calibri.swf", sColor:"#CA263D", sWmode:"transparent", sFlashVars:"offsetTop=-1&textalign=left"}));
	};	
}

function showAnimation()
{	
	var sImg1, sImg2;
	
	// if the current image is lower than the last image then we've gone too far, we need to loop back to the first image
	if(iCurrImg > imgCount)
	{
		iCurrImg = 1;
	}	
	
	// set the current image  (the one we're about to fade out)
	sImg1 = document.getElementById('headimg'+ iCurrImg);
	
	
	// set the new image (the one we're about to fade in)
	if (document.getElementById('headimg'+ (iCurrImg+1)))
	{
		sImg2 = document.getElementById('headimg'+ (iCurrImg+1));
	}
	else
	{
		// if we get to the first image, go back to the first one
		sImg2 = document.getElementById('headimg1');
	}
	
	// fade the images
	Effect.Fade(sImg1, { duration: 3.0});
	Effect.Appear(sImg2, { duration: 3.0});
		
	iCurrImg += 1;		
			
	evt = setTimeout("showAnimation()", 3000);
}

function changeImg(img,url)
{
	clearTimeout(evt);
	
	var thisImg = document.getElementById('headimg' + iCurrImg);

	// set the new image (the one we're about to fade in)
	if (document.getElementById('headimg'+ (iCurrImg+1)))
	{
		sImg2 = document.getElementById('headimg'+ (iCurrImg+1));
		iCurrImg++;
	}
	else
	{
	// if we get to the first image, go back to the first one
		sImg2 = document.getElementById('headimg1');
		iCurrImg = 1;
	}
	

	
	if (thisImg.src.match(url + '$') == null)	// If this is a new image then fade the old one out and fade the new one in
	{
		sImg2.src = url;
		Effect.Fade(thisImg, { duration: 1.0});
		Effect.Appear(sImg2, { duration: 1.0});
	}
	
	thisImg = newImg;
}

function pauseAnimation()
{

}

function unpauseAnimation()
{

}


sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}