// JavaScript Document

// define some initial variables
var numberOfProfiles = 0;
var currentProfile = 0;
var currentProfileIndex=2;
var numberOfHeadings = 0;
var currentHeading = 0;
var firstTimeThrough = true;
//variables for left hand menu functions
var oldVisibleList1="";
var oldVisibleList2="";
// function to load the xml
function loadXML()
{
	// prevent xml caching
	var timestamp = new Date();
	var uri = "/hastings/xml/homePrograms.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);
			getNav();
		}
		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;
			getNav();
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.async=false;
			xmlDoc.load(uniqueURI);
			xmlDoc.onload=getNav();
		}
	}	
	catch(e)
	{
		//Commented out alert to prevent error popups within the CMS
		//alert(errorHappendHere);
	}
}
/**************************************************************************/
/*HOMEPAGE HEADING CODE                                                   */
/**************************************************************************/
//function to load the images
// do not edit this function
var getHomeHeadings = function getHomeHeadings()
{
	// array of all of the image paths to be loaded
	var images=new Array;
	// variable to build all of the heading images with in 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;
		// get the value of the image link path
		var newLinkPath = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[x].getElementsByTagName("linkPath")[0].childNodes[0].nodeValue;
		// put the links and/or images together
		if(newLinkPath != "none"){
			topHeadingCode+='<a class="profileLink" href="' + newLinkPath + '" title="' + newAlt + '"><img src="' + newImage + '" border="0" id="heading'+x+'" /></a><br />';
		}else{
			topHeadingCode+='<img src="' + newImage + '" border="0" id="heading'+x+'" /><br />';	
		}
		// populate the images array
		images[x]=newImage;
	}
	// set the global currentProfile number to the randome number generated
	currentHeading = 0;
	// put everything together and create the image
	document.getElementById('headingContainer').innerHTML = topHeadingCode;
	// Preloader of large images
	new Asset.images(images, {
		onComplete: function(){
			timedCount();
		}
	});
}
// starting z-index
var counter=2;
// timed loop for heading images
// do not edit this function
function timedCount()
{	
	// time in between loops
	setTimeout("timedCount()",8000);
	// determin which heading to work with
	var newId = "heading" + currentHeading;
	// 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: 1500});
		myElementsEffects.start({
			'0': { //let's change the first element's opacity
				'opacity': [0,1]
			}
		});
		firstTimeThrough = false;
	}else{
		// set the new heading's opacity from 0 to 1
		var myElementsEffects = new Fx.Elements($$('#headingContainer img#heading'+currentHeading),{duration: 1500});
		myElementsEffects.start({
			'0': { //change the headings's opacity
				'opacity': [0,1]
			}
		});
		// change the opacity of the old heading from 1 to 0
		if(currentHeading == 0){
			var myElementsEffectsOld = new Fx.Elements($$('#headingContainer img#heading'+(numberOfHeadings-1)),{duration: 8000});
		}else{
			var myElementsEffectsOld = new Fx.Elements($$('#headingContainer img#heading'+(currentHeading-1)),{duration: 8000});	
		}
		myElementsEffectsOld.start({
			'0': { //change the headings's opacity
				'opacity': [1,0]
			}
		});
	}
	// 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++;
}
/**************************************************************************/
// function to create needed homepage profiles from the loadXML function.
// do not edit this function
/**************************************************************************/
var getNav = function getImages()
{
	// create an object with all of the image nodes
	var xmlTopNodes=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image"); 
	// 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;
	// keep all data for the new images together
	var newProfileData="";
	
	
	for(var x=0; x<numberOfProfiles; x++){
		// get the value of the randomly selected image source
		var newImage = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image")[x].getElementsByTagName("location")[0].childNodes[0].nodeValue;
		// get the value of the randomly selected image alt text
		var newAlt = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image")[x].getElementsByTagName("alt")[0].childNodes[0].nodeValue;
		// get the value of the randomly selected image link path
		var newLinkPath = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image")[x].getElementsByTagName("linkPath")[0].childNodes[0].nodeValue;
		// put the image together
		newProfileData += '<a href="' + newLinkPath + '" title="' + newAlt + '"><img class="profileImage" id="profile'+x+'" src="' + newImage + '" border="0" /></a>';
	}
	// add the two invisible buttons
	newProfileData += '<a href="javascript:profileLeft();"><img src="/hastings/images/spacer.gif" id="profileLeft" /></a>'+
    	'<a href="javascript:profileRight();"><img src="/hastings/images/spacer.gif" id="profileRight" /></a>';
	// set the global currentProfile number to the first image
	currentProfile = 0;
	// put everything together and create the image
	document.getElementById('profileContainer').innerHTML = newProfileData;
	// put the first image on top
	document.getElementById('profile0').style.zIndex = 2;
	getHomeHeadings();
}

// functions to switch between profiles on the homepage
function profileRight(){
	if(currentProfile == numberOfProfiles-1){
		currentProfile = 0;
	}else{
		currentProfile++;	
	}
	currentProfileIndex++;
	document.getElementById(('profile'+currentProfile)).style.zIndex = currentProfileIndex;
}
function profileLeft(){
	if(currentProfile == 0){
		currentProfile = numberOfProfiles-1;
	}else{
		currentProfile--;	
	}
	currentProfileIndex++;
	document.getElementById(('profile'+currentProfile)).style.zIndex = currentProfileIndex;
}

/*****************************************/
/* SEARCH BOX 
// do not edit this function
/*****************************************/
// function to clear text from search box if default text exists
function clearSearch(){
	if(document.getElementById('searchBox').value == "Search"){
		document.getElementById('searchBox').value = "";
	}
}

// function to add default text back if text box is blank
function addSearch(){
	if(document.getElementById('searchBox').value == ""){
		document.getElementById('searchBox').value = "Search";
	}
}
/**********************/
/*show lists functions*/
/**********************/
function showList(curList, curLevel){
	//select the first button
	var button1 = $(curList);
	alert($(curList).get('height'));
	if(curLevel=='1'){
		if(oldVisibleList1 != ""){
			document.getElementById(oldVisibleList1).style.display = 'none';
			if(oldVisibleList2 != ""){
				document.getElementById(oldVisibleList2).style.display = 'none';
			}
			oldVisibleList1 = curList;
			document.getElementById(curList).style.display = 'block';
		}else{
			oldVisibleList1 = curList;
			document.getElementById(curList).style.display = 'block';
		}
	}else{
		if(oldVisibleList2 != ""){
			document.getElementById(oldVisibleList2).style.display = 'none';
			oldVisibleList2 = curList;
			document.getElementById(curList).style.display = 'block';
		}else{
			oldVisibleList2 = curList;
			document.getElementById(curList).style.display = 'block';
		}
	}
}
// do not remove this function even though it is blank
function blank(){
	
}
// *************************************
// do not edit anything below this line
// *************************************
window.addEvent('domready', function() {
	// HERE IS WHERE YOU READ IN JS CODE
	var accordion = new Accordion('a.atStart', 'ul.atStart', {
	opacity: true,
	alwaysHide:true,
	show:500
	}, $('leftNavContainer'));
	var accordion2 = new Accordion('a.atStart2', 'ul.atStart2', {
	onActive: function(toggler, element){
		var parent = toggler.getParent();
		while (!parent.hasClass('atStart')) {
			parent = parent.getParent();
		}
		parent.setStyle('height', '');
	},
	onBackground: function(toggler, element){
		var parent = toggler.getParent();
		while (!parent.hasClass('atStart')) {
			parent = parent.getParent();
		}
		parent.setStyle('height', '');
	},
	opacity: true,
	alwaysHide:true,
	show:500
	}, $('leftNavContainer'));
});