// global flag
var isIE = false;

// global request and XML document objects
var req;
 
//
// Work our way through the xml and start rotating the images.
//

var grouparray;	// array of groups. Get count by grouparray.length
var currentgroup = 0;	// current group counter.

var currentdrawitem = 0;
var currentclearitem = 0;
var nextfunctioninterval;

var draworder = new Array();
draworder[0] = new Array(1,2,3,4,5,6,7,8,9);

var longpause = 5100;
//var shortpause = 270;
var shortpause = 500;

var largeimageurl;
//var largeimagelink;

var hover = 0;

// handle onreadystatechange event of req object
function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// Do something since the Javascript has been loaded correctly!
			InitializeRotation();
		 } else {
			alert("There was a problem retrieving the XML data:\n" +
				req.status + '_' + req.statusText);
		 }
	}
}


function InitializeRotation(){

	// Collect Array of DisplayGroup Entities

	grouparray = req.responseXML.getElementsByTagName('displaygroup');
	currentgroup = Math.floor(Math.random() * grouparray.length);

	largeimageurl = getElementTextNS("", "largeimage", grouparray[currentgroup], 0);
	//largeimagelink = grouparray[currentgroup].getElementsByTagName('largeimage')[0].getAttribute('url');

	element = document.getElementById('marquee');
	element.style.backgroundImage = 'url(' + largeimageurl + ')';

	ClearSmallImage(1); // This will set up links for the back image and we can get into the flow; ClearSmallImage will call DrawSmallImage, etc.

}


function DrawSmallImage(init){

	if (init){
		currentdrawitem = 0;
		randomval = Math.floor(Math.random() * draworder.length);
		localdraworder = draworder[randomval];
	}
	
	
	element = document.getElementById('area' + localdraworder[currentdrawitem]);

	var smallimagearray = grouparray[currentgroup].getElementsByTagName('smallimage');
	var imageurl = smallimagearray[currentdrawitem].firstChild.nodeValue;
	//var imagelink = smallimagearray[currentdrawitem].getAttribute('url');
	
	element.innerHTML = '<img border="0" src="' + imageurl + '" />';
	
	if (currentdrawitem >= 8){
		nextfunctioninterval = setTimeout('ClearSmallImage(1)',longpause);
	} else {
		currentdrawitem++;
		nextfunctioninterval = setTimeout('DrawSmallImage(0)',shortpause);
	}

}


function ClearSmallImage(init){

	if (init){
		currentclearitem = 0;
		currentgroup++;
		if (currentgroup >= grouparray.length){
			currentgroup = 0;
		}
			
		largeimageurl = getElementTextNS('','largeimage',grouparray[currentgroup], 0);
		//largeimagelink = grouparray[currentgroup].getElementsByTagName('largeimage')[0].getAttribute('url');
		
		element = document.getElementById('marquee');		
		element.onmouseover = '';
		element.onmouseout = '';
		element.style.backgroundImage = 'url(' + largeimageurl + ')';

		randomval = Math.floor(Math.random() * draworder.length);
		localclearorder = draworder[randomval];	
	
	}
	
	element = document.getElementById('area' + localclearorder[currentclearitem]);
	element.innerHTML = '<img border="0" src="../images/aboutus/grid/127x127/empty.gif" width="127" height="127" />';
	
	if (currentclearitem >= 8){
		nextfunctioninterval = setTimeout('DrawSmallImage(1)',longpause);
	} else {
		currentclearitem++;
		nextfunctioninterval = setTimeout('ClearSmallImage(0)',shortpause);
	}

}

function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}


// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
	var result = "";
	if (prefix && isIE) {
		// IE/Windows way of handling namespaces
		result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
	} else {
		// the namespace versions of this method 
		// (getElementsByTagNameNS()) operate
		// differently in Safari and Mozilla, but both
		// return value with just local name, provided 
		// there aren't conflicts with non-namespace element
		// names
		result = parentElem.getElementsByTagName(local)[index];
	}
	if (result) {
		// get text, accounting for possible
		// whitespace (carriage return) text nodes 
		if (result.childNodes.length > 1) {
			return result.childNodes[1].nodeValue;
		} else {
			return result.firstChild.nodeValue;			
		}
	} else {
		return "n/a";
	}
}
