function getXMLDoc(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 {
		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";
	}
}

// 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);
var longpause = 4000;
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_pw');
	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 = '<a href="' + imagelink + '"><img border="0" src="' + imageurl + '" /></a>';
	if (currentdrawitem >= 2){
		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_pw');		
		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 = '<a href="' + largeimagelink + '"><img src="http://www.giftsthatthrill.com.au/images/rotator/empty.gif" width="200" height="110" /></a>';
	if (currentclearitem >= 2){
		nextfunctioninterval = setTimeout('DrawSmallImage(1)',longpause);
	} else {
		currentclearitem++;
		nextfunctioninterval = setTimeout('ClearSmallImage(0)',shortpause);
	}
}

