//xmlhttp request handler
var xmlh = false;

//counter for stepping through images in xml file
var counter=0;

//flag for preventing a fade on the first time through
var firsttime = true;

//global variable that contains the settimeout ID
var setTimeoutID = false;

//set window.onload event handler
addOnload(initAll);

//flag that prevents user from stepping slideshow while change is in progress
var changeFlag = false;

//function that handles multiple functions for the window.onload event
function addOnload(newFunction) {

	//get current onload function
	var oldOnload = window.onload;
	
	if(typeof oldOnload == "function") {
		
		//run both the old function and the new function when page loads
		window.onload = function() {
			if(oldOnload) {
				oldOnload();
			}
			newFunction();
		}
	} else {
		//run the new funciton
		window.onload = newFunction;
	}
}

//function that makes the xmlhttp request
function initAll() {

	if (window.XMLHttpRequest) { //Normal browsers
		xmlh = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) { //#$%^%#$% Internet Explorer....
			try {
				xmlh = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
		
	if (xmlh) { //if the request was succcessful, set up the slideshow, else do nothing (leave the static image and caption)
		PrepSlideShow();
	}
	
}

//funciton that initiazes XML request
function PrepSlideShow() {

	//request xml data asynchronously
	xmlh.open("GET", '/images/frontpage/index.xml', true);
	
	//when data is loaded, run the stepslideshow function
	xmlh.onreadystatechange = StepSlideShow;
	
	//send request
	xmlh.send(null);
	
	//Do this every ten seconds
	setTimeoutID = setTimeout(PrepSlideShow, 10*1000);
}

//function that advances the slideshow
function StepSlideShow() {

	if(xmlh.readyState == 4) { //only run after the data is fully loaded
		if(xmlh.status == 200) { //make sure the file is present
		
			if(!firsttime) { //fade the old image/caption out and change to new image/caption
			
				//set flag to prevent other changes
				changeFlag=true;
			
				//fade
				opacity("sswrapper",100,0);
				
				//after 550 ms, run changepicture function
				setTimeout(ChangePicture,550);
				
				//increment image counter
				counter++;
				
			} else { //the first time the script tuns through, just set the image
			
				//define xml objects and document objects
				var xmlimagename = xmlh.responseXML.documentElement.getElementsByTagName("file");
				var xmlimagetype = xmlh.responseXML.documentElement.getElementsByTagName("filetype");
				var xmlimagetitle = xmlh.responseXML.documentElement.getElementsByTagName("title");
				var xmlimagecaption = xmlh.responseXML.documentElement.getElementsByTagName("caption");
				var xmlimagelink = xmlh.responseXML.documentElement.getElementsByTagName("link");
				var ssimage = document.getElementById("slideshowimage");
				var sscaption = document.getElementById("caption");
				var sstitle = document.getElementById("sstitle");
				var sslink = document.getElementById("sslink");
				
				//set document objects to the appropriate xml objects
				ssimage.src = "/images/frontpage/" + xmlimagename[counter].childNodes[0].nodeValue + "." + xmlimagetype[counter].childNodes[0].nodeValue;
				ssimage.alt = xmlimagetitle[counter].childNodes[0].nodeValue;
				sscaption.innerHTML = xmlimagecaption[counter].childNodes[0].nodeValue;	
				sstitle.innerHTML = xmlimagetitle[counter].childNodes[0].nodeValue;
				sslink.href = xmlimagelink[counter].childNodes[0].nodeValue;
			
				//it's not the first time anymore!
				firsttime = false;
				
			}
		} else {
		
			//Report HTTP status (404, 403, etc.) if something goes wrong
			document.getElementById("caption").innerHTML = xmlh.status;
			
		}
	}

	return;
}

//funciton that parses and displays XML data
function ChangePicture() {

	//"wraparound" counter when it exceeds the total number of images in XML file
	if(counter >= xmlh.responseXML.documentElement.getElementsByTagName("image").length) {
		counter = 0;
	} else if (counter < 0){ //or wrap around counter the other way if negative
		counter = xmlh.responseXML.documentElement.getElementsByTagName("image").length - 1;
	}

	//define xml objects and document objects
	var xmlimagename = xmlh.responseXML.documentElement.getElementsByTagName("file");
	var xmlimagetype = xmlh.responseXML.documentElement.getElementsByTagName("filetype");
	var xmlimagetitle = xmlh.responseXML.documentElement.getElementsByTagName("title");
	var xmlimagecaption = xmlh.responseXML.documentElement.getElementsByTagName("caption");
	var xmlimagelink = xmlh.responseXML.documentElement.getElementsByTagName("link");
	var ssimage = document.getElementById("slideshowimage");
	var sscaption = document.getElementById("caption");
	var sstitle = document.getElementById("sstitle");
	var sslink = document.getElementById("sslink");
	
	//set document objects to the appropriate xml objects
	ssimage.src = "/images/frontpage/" + xmlimagename[counter].childNodes[0].nodeValue + "." + xmlimagetype[counter].childNodes[0].nodeValue;
	ssimage.alt = xmlimagetitle[counter].childNodes[0].nodeValue;
	sscaption.innerHTML = xmlimagecaption[counter].childNodes[0].nodeValue;	
	sstitle.innerHTML = xmlimagetitle[counter].childNodes[0].nodeValue;
	sslink.href = xmlimagelink[counter].childNodes[0].nodeValue;
	
	//wait 150 ms, then fade the new content in
	setTimeout("opacity('sswrapper',0,100)",150);
	
	//reset change flag after the image has faded in
	setTimeout(resetFlag,700);
	
	return;
	
}

//function that does the fading
function opacity(id1, opacStart, opacEnd) {

	//timer that controls the fade
	var timer = 0;
	
	//determine the direction for the blending, if start and end are the same nothing happens
	//5 ms per 1% opacity change for a total of 500 ms fade time. Can change to adjust speed.
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id1 + "')",timer*5);
// 			setTimeout("changeOpac(" + i + ",'" + id2 + "')",timer*5);
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++){
			setTimeout("changeOpac(" + i + ",'" + id1 + "')",timer*5);
// 			setTimeout("changeOpac(" + i + ",'" + id2 + "')",timer*5);
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	//select the document object
	var object = document.getElementById(id).style;
	
	//mozilla >1.6
	object.opacity = (opacity / 100);
	
	//mozilla <= 1.6
	object.MozOpacity = (opacity / 100);
	
	//Konqueror :)
	object.KhtmlOpacity = (opacity / 100);
	
	//%#&*@)$% Internet Explorer....
	object.filter = "alpha(opacity=" + opacity + ")";
}

//function that resets changeFlag to false
function resetFlag() {
	changeFlag= false;
}

//function that manually steps slideshow; takes an integer dir that tells it which direction to step
function manualStep(dir) {

	if(!changeFlag) { //make sure a change is not in progress

		//stop the current slideshow
		clearTimeout(setTimeoutID);
	
		//change the counter: should be by either 0 (to go forward 1) or -2 (to go back 1). This is because the counter itself is incremented by one in the stepslideshow function, which is later called
		counter += dir;
	
		//begin the process of changing the picture.
		PrepSlideShow();
	}
	
	//return false so that the link doesn't do anything
	return false;
}