var preloaded = new Array();
function preloadImages(data) {
	rotateNodes = data.getElementsByTagName('rotate');
	//alert("loaded xml : length = " + rotateNodes.length);
	n = 0;
	for (i=0;i<rotateNodes.length;i++) {
		for (j=0;j<rotateNodes[i].childNodes.length;j++) {
			theNodeName = rotateNodes[i].childNodes[j].nodeName;
			if (theNodeName == 'head_image'){// || theNodeName == 'txt_image') {
				theNodeValue = rotateNodes[i].childNodes[j].firstChild.nodeValue;
				//document.write('<img src="images/home/' + theNodeValue + '" />' + theNodeValue + '<br />');
				preloaded[n] = document.createElement('img');
        		preloaded[n].setAttribute('src','images/'+theNodeValue);
				//alert(theNodeValue);
				n++;
			}
		}
	}
}

var xmlhttp;
function loadXMLDoc(url) {
	xmlhttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		alert("Your browser does not support XMLHTTP.");
	}
}

function state_Change() {
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
		// if "OK"
		//alert(xmlhttp.status);
		// is this '200' if statement required?
		//if (xmlhttp.status==200) {
			preloadImages(xmlhttp.responseXML);
		//}
		//else {
			//alert("Problem retrieving XML data");
		//}
	}
}


var idx;
var rotate;

function image_populate(whichWay) {

	//$("#txt_image_div").empty();
	$("#head_image_div").empty();
	

	if(!idx){
		idx = 0;
		whichOne = 0;
	}
	
	if(whichOne > 0 && whichWay == -1){
		whichOne --;
	} else if (whichWay == -1){
		whichOne = idx;	
	}
	
	if(whichOne < idx && whichWay == 1){
		whichOne ++;
	} else if (whichWay == 1){
		whichOne = 0;	
	}
	

	$.get("index_rotator.xml", {}, function (xml) {
	
		idx = $("rotate", xml).length - 1;
		rotate = $("rotate", xml).get(whichOne);
		//textimage = '<img src="images/home/' + $("txt_image", rotate).text() + '" alt="' +$("txt_image_alt", rotate).text() + '" width="318" height="168" />';
		imagelink = '<a  href="' + $("url", rotate).text() + '"><img  src="images/' + $("head_image", rotate).text() + '"' + 'alt="' + $("head_image_alt", rotate).text() + '" width="550" height="300" style="border:none;" \/><\/a>';

		//$("#txt_image_div").html(textimage);
		$("#head_image_div").html(imagelink);
		
		
		

	});
}

/*Set Interval for Image rotation*/
$(document).ready(function () {
	image_populate(0);
	rotaterID = setInterval("image_populate(1)",10000);
	
});