// We don't need to do much preloading as most images are in divs faded in when the page has fully loaded (onload)
preloadImg1 = new Image();
preloadImg1.src = "images/background-texture.png";
preloadImg2 = new Image();
preloadImg2.src = "images/bubble/bubble.gif";
preloadImg3 = new Image();
preloadImg3.src = "images/bubble/bubble_filler.gif";
// Just for map page which does not wait for onload to fade in images
preloadImg4 = new Image();
preloadImg4.src = "images/navigation/navigation-tableft.gif";
preloadImg5 = new Image();
preloadImg5.src = "images/navigation/navigation-tabright.gif";

// ONLY works if referring page was a click on a link on an actual served up WEB page (not a local drive)
function WasReferrerWithinSite()
{
	var sPageReferrer = document.referrer + "";// add so it's a string
	var sThisPageUrl = document.location + "";

	return (sPageReferrer.toLowerCase().substring(0, 18) == sThisPageUrl.toLowerCase().substring(0, 18));
}

// Load faster if we are already moving in the site, rather than arriving for the first time
var bPageReferrerWasWithinSite = WasReferrerWithinSite();
//bPageReferrerWasWithinSite = false;//testing

var aImages = new Array();
function SetupImages()
{
	var iCount = 0;

	aImages[iCount] = "navigation-logo";
	iCount++;
	aImages[iCount] = "navigation-tabs";
	iCount++;
	aImages[iCount] = "body-copy-frame-inner";// don't use the inner div which contains text as in IE the bold font doesn't like the opacity change
	iCount++;
	aImages[iCount] = "body-image";
	iCount++;
	aImages[iCount] = "footer-motto";
	iCount++;
	aImages[iCount] = "navigation-contact-email-image";
	iCount++;
	aImages[iCount] = "navigation-contact-phone-image";
	iCount++;
	aImages[iCount] = "navigation-contact-mobile-image";
	iCount++;
	aImages[iCount] = "navigation-contact-address-image";
	iCount++;

	for(var i = 0; i < aImages.length; i++)
	{
		document.write("<style type='text/css'>#" + aImages[i] + " {visibility:hidden;}</style>");
	}
}

SetupImages();


function Fade_InitImages()
{
	for(var i = 0; i < aImages.length; i++)
	{
		imageId = aImages[i];
		image = document.getElementById(imageId);

		setOpacity(image, 0);
		image.style.visibility = "visible";

		var delay = 0;
		var rate = 0;

		if(i == 0)// logo
		{
			rate = 8;
		}
		else if(i == 1)// navigation tabs
		{
			rate = 5;
		}
		else if(i == 2)// main copy
		{
			rate = 5;
		}
		else if(i == 3)// main image
		{
			delay = 1000;
			rate = 5;
		}
		else if(i == 4)// motto
		{
			delay = 1500;
			rate = 5;
		}
		else// contact buttons
		{
			delay = 2500;
			rate = 10;
			if(i == 5)// contact button 1
			{
				delay += 250;
			}
			else if(i == 6)// contact button 2
			{
				delay += 500;
			}
			else if(i == 7)// contact button 3
			{
				delay += 750;
			}
			else if(i == 8)// contact button 4
			{
				delay += 1000;
			}
		}
		
		if(bPageReferrerWasWithinSite)
		{
			rate *= 2;
			delay /= 2;
		}

		
		window.setTimeout("fadeIn('"+imageId+"', 0, " + rate + ")", delay);
	}
}
function fadeIn(objId, opacity, rate) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += rate;
			window.setTimeout("fadeIn('" + objId + "'," + opacity + "," + rate + ")", 100);
		}
	}
}
function setOpacity(obj, opacity) {
	opacity = (opacity == 100) ? 99.999 : opacity;
	
	//###############################################
	// This is buggy in IE - see note on bubble-tt separate divs
	// as child items (eg tooltips) which inherit the opacity stay partially hidden
	//###############################################
	// IE/Win
//	if (typeof obj.style.filter.opacity=="number") //if IE6+
//		obj.style.filter.opacity = opacity;
//	else //else if IE5.5-
		obj.style.filter = "alpha(opacity:" + opacity + ")";


//	obj.style.zoom = "1"; /* problematic IE */
		
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

// We put this in each html file at the end so we can customise for the map page
//window.onload = function() {Fade_InitImages()}

