// global flag
var isMarqueeIE = 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);

var longpause = 5100;
var shortpause = 270;

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 = '<a href="' + imagelink + '" target="_blank"><img border="0" src="' + imageurl + '" /></a>';
	
	if (currentdrawitem >= 3){
		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 = '<a href="' + largeimagelink + '" target="_blank"><img src="http://www.blue4you.be/images/portfolio/empty.gif" width="254" height="165" /></a>';
	
	if (currentclearitem >= 3)
	{
		nextfunctioninterval = setTimeout('DrawSmallImage(1)',longpause);
	}
	else
	{
		currentclearitem++;
		nextfunctioninterval = setTimeout('ClearSmallImage(0)',shortpause);
	}
}

