// 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()
{
	// prevent xml caching
	var timestamp = new Date();
	var 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.onload=getHomeBkgd();
		}
	}	
	catch(e)
	{
		alert(errorHappendHere);
	}
}

//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; 
		// 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")[x].getElementsByTagName("location")[0].childNodes[0].nodeValue;
			// get the value of the image alt text
			var newAlt = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[x].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;
		}
		// 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/homeBkgd/ajax-loader.gif" alt="loading" width="32" height="32" id="loadingAnimation" /></div>';
		// put everything together and create the image
		document.getElementById('headingContainer').innerHTML = loadingText+topHeadingCode;
		// Preloader of large images
		new Asset.images(images, {
			onComplete: function(){
				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
		document.getElementById('headingContainer').innerHTML = topHeadingCode;
	}
}
// starting z-index
var counter=2;
// timed loop for heading images
function timedCount()
{	
	// determine browser type
	var browser=navigator.appName;
	var transitionDuration = 1500;// set timing for transitions
	var transitionTiming = 900; // set the time in between transitions
	// time in between loops
	var timeout=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;
		clearTimeout(timeout);
	}else{
		currentHeading++;	
	}
	counter++;
}

/**********************************/
/* Profiles Section               */
/**********************************/
// function to load the xml
function loadXML2()
{
	// prevent xml caching
	var timestamp = new Date();
	var uri = "xml/homeProfiles.xml";
	var uniqueURI = uri + (uri.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
	try
	{
		if (window.ActiveXObject)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc2 = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc2.async=false;
			xmlDoc2.load(uniqueURI);
			getProfiles();
		}
		else if(window.XMLHttpRequest)
		{
			var errorHappendHere = "Error handling XMLHttpRequest request";
			var d = new XMLHttpRequest();
			d.open("GET", uniqueURI, false);
			d.send(null);
			xmlDoc2=d.responseXML;
			getProfiles();
		} else {
			var errorHappendHere = "Error.";
			xmlDoc2 = document.implementation.createDocument("","",null);
			xmlDoc2.async=false;
			xmlDoc2.onload=getProfiles();
		}
	}	
	catch(e)
	{
		alert(errorHappendHere);
	}
}
/*Parse Profile XML Data and load into page*/
function getProfiles(){
	// create an object with all of the profile nodes
	var xmlTopNodes=xmlDoc2.getElementsByTagName("node")[0].getElementsByTagName("profile"); 
	// find out how many top nodes there are
	var numTopNodes=xmlTopNodes.length; 
	// assign the number of nodes found to a global level variable
	//numberOfProfiles = numTopNodes;
	// generate a random number
	var x = Math.floor(Math.random()*numTopNodes)
	// get the value of the image source
	var newImage = xmlDoc2.getElementsByTagName("node")[0].getElementsByTagName("profile")[x].getElementsByTagName("imagePath")[0].childNodes[0].nodeValue;
	// get the value of the image alt text
	var newAlt = xmlDoc2.getElementsByTagName("node")[0].getElementsByTagName("profile")[x].getElementsByTagName("name")[0].childNodes[0].nodeValue;
	// get name
	var newName = xmlDoc2.getElementsByTagName("node")[0].getElementsByTagName("profile")[x].getElementsByTagName("name")[0].childNodes[0].nodeValue;
	// get description
	var newDescription = xmlDoc2.getElementsByTagName("node")[0].getElementsByTagName("profile")[x].getElementsByTagName("description")[0].childNodes[0].nodeValue;
	// get title
	var newTitle = xmlDoc2.getElementsByTagName("node")[0].getElementsByTagName("profile")[x].getElementsByTagName("title")[0].childNodes[0].nodeValue;
	// get link path
	var newLink = xmlDoc2.getElementsByTagName("node")[0].getElementsByTagName("profile")[x].getElementsByTagName("linkPath")[0].childNodes[0].nodeValue;
	// add to the final code
	topImageCode='<a href="' + newLink +'"><img src="' + newImage + '" border="0" id="heading'+x+'" alt="'+newAlt+'" /></a>';
	topProfileCode='<a href="' + newLink+'"><strong><span id="profileName">'+newName+'</span>'+ newDescription+'</strong></a>'+newTitle;
	// set the global currentProfile number to the randome number generated
	currentHeading = 0;
	// put everything together and create the full profile
	document.getElementById('profileContainer').innerHTML = topImageCode+topProfileCode;
}

