// JavaScript Document

// define some initial variables
var numberOfHeadings = 0;
var currentHeading = 0;
var firstTimeThrough = true;

/**********************************/
/* Start of Background fader code */
/**********************************/
// function to load the xml
function loadXML(uri)
{
	// prevent xml caching
	var timestamp = new Date();
	if (uri == undefined)
	{
		uri = "xml/home.xml";
	}
	var uniqueURI = uri + (uri.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
	try
	{
		if (window.ActiveXObject)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(uniqueURI);
			getHomeBkgd();
		}
		else if(window.XMLHttpRequest)
		{
			var errorHappendHere = "Error handling XMLHttpRequest request";
			var d = new XMLHttpRequest();
			d.open("GET", uniqueURI, false);
			d.send(null);
			xmlDoc=d.responseXML;
			getHomeBkgd();
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.async=false;
			xmlDoc.load(uniqueURI);
			xmlDoc.onload=getHomeBkgd();
		}
	}	
	catch(e)
	{
		//alert(errorHappendHere);
		document.getElementById('headingContainer').innerHTML = "<img src=\"/images/homeBkgd/hp2.jpg\" alt=\"background image\" name=\"noScript\" id=\"noScript\" />"
	}
}

//function to load the images
// we do not recommend editing this function
var getHomeBkgd = function getHomeBkgd()
{
	var imageMode=xmlDoc.getElementsByTagName("node")[0].getAttribute('imageMode');
	// check to see which image mode is set
	if(imageMode=="rotate"){
		// array of all of the image paths to be loaded
		var images=new Array;
		// variable to build all of the heading images within the innerHTML
		var topHeadingCode="";
		// create an object with all of the image nodes
		var xmlTopNodes=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading"); 
		// find out how many top nodes there are
		var numTopNodes=xmlTopNodes.length; 
		// Pull in a random number based on the number of nodes
		var nImage=Math.floor(Math.random()*numTopNodes); // remove first comment to randomize first image
		// assign the number of nodes found to a global level variable
		numberOfHeadings = numTopNodes;
		// loop through the images
		for(var x=0; x<numberOfHeadings; x++){
			// get the value of the image source
			var newImage = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[nImage].getElementsByTagName("location")[0].childNodes[0].nodeValue;
			// get the value of the image alt text
			var newAlt = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[nImage].getElementsByTagName("alt")[0].childNodes[0].nodeValue;
			// add to the final code
			topHeadingCode+='<img src="' + newImage + '" border="0" id="heading'+x+'" alt="'+newAlt+'" /><br />';	
			// populate the images array
			images[x]=newImage;
			nImage++;
			if (nImage == numberOfHeadings){
				nImage = 0;
			}
		}
		// set the global currentProfile number to the randome number generated
		currentHeading = 0;
		// write div to contain loading image
		var loadingText = '<div id="backgroundLoader"><img src="images/ajax-loader.gif" alt="loading" width="32" height="32" id="loadingAnimation" /></div><img src="images/homeBkgd/loader.gif" alt="loading" class="show" />';
		// put everything together and create the image
		document.getElementById('headingContainer').innerHTML = loadingText+topHeadingCode;
//		document.getElementById('learnMoreLink').href=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[0].getElementsByTagName("href")[0].childNodes[0].nodeValue;
		// Preloader of large images
		new Asset.images(images, {
			onComplete: function(){
				document.getElementById('loadingAnimation').style.display = "none";
				timedCount();
			}
		});
	}else{  // this else fires off if imageMode is set to refresh
		// create an object with all of the image nodes
		var xmlTopNodes=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading"); 
		// find out how many top nodes there are
		var numTopNodes=xmlTopNodes.length; 
		// Pull in a random number based on the number of nodes
		var randomNumber=Math.floor(Math.random()*numTopNodes);
		// get the value of the image source
		var newImage = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[randomNumber].getElementsByTagName("location")[0].childNodes[0].nodeValue;
		// get the value of the image alt text
		var newAlt = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[randomNumber].getElementsByTagName("alt")[0].childNodes[0].nodeValue;
		// add to the final code
		topHeadingCode+='<img src="' + newImage + '" border="0" id="headingRefresh" alt="'+newAlt+'" /><br />';	
		// add the html to the page
		if (uri == "xml/home.xml")
		{
			document.getElementById('headingContainer').innerHTML = topHeadingCode;
		}
		else
		{
			document.getElementById('imgDiv').innerHTML = topHeadingCode;
		}
//		document.getElementById('learnMoreLink').href=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[randomNumber].getElementsByTagName("href")[0].childNodes[0].nodeValue;
	}
}
// starting z-index
var counter=2;
// timed loop for heading images
function timedCount()
{	
	// determine browser type
	var browser=navigator.appName;
	var transitionDuration = 1000;// set timing for transitions
	var transitionTiming = 3000; // set the time in between transitions
	// redefine the learn more button's href value
//	document.getElementById('learnMoreLink').href=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[currentHeading].getElementsByTagName("href")[0].childNodes[0].nodeValue;
	// time in between loops
	setTimeout("timedCount()",transitionTiming);
	// determin which heading to work with
	var newId = "heading" + currentHeading;
	// make sure opacity starts at 0
	if (browser=="Netscape"){
		document.getElementById(newId).style.opacity = '0';
	}else if(browser=="Microsoft Internet Explorer"){
		document.getElementById(newId).style.filter='alpha(opacity=00)';
	}
	// set the z index of the new heading to be on top
	document.getElementById(newId).style.zIndex=counter;
	// only chang the first heading if this is the first time through the loop
	if(firstTimeThrough){
		var myElementsEffects = new Fx.Elements($$('#headingContainer img#heading0'),{duration: transitionDuration});
		myElementsEffects.start({
			'0': { //let's change the first element's opacity
				'opacity': [0,1]
			}
		});
		firstTimeThrough = false;
	}else{
		// hide loader graphic
		document.getElementById('loadingAnimation').style.display = "none";
		// set the new heading's opacity from 0 to 1
		var myElementsEffects = new Fx.Elements($$('#headingContainer img#heading'+currentHeading),{duration: transitionDuration});
		myElementsEffects.start({
			'0': { //change the headings's opacity
				'opacity': [0,1]
			}
		});
	}
	// reset the current Heading value appropriately if it reaches the end of the images or not
	if(currentHeading == numberOfHeadings-1){
		currentHeading = 0;
	}else{
		currentHeading++;	
	}
	counter++;
}

/*Set initial background image for search box*/
function searchBkgd(){
	document.getElementById('qt').style.background="#fff url('/images/searchBkgd.gif') 8px 4px no-repeat";
}
function startFocus(){
	document.getElementById('qt').style.backgroundImage = 'none'; 
	//document.getElementById('quickLink').className=document.getElementById('quickLink').className.replace(' over', '');	
}
function replaceSearchGraphic(){
	if(document.getElementById('qt').value == ""){
		document.getElementById('qt').style.background="#fff url('/images/searchBkgd.gif') 8px 4px no-repeat";
	}
}