window.addEvent('domready', initRotator);

//ROTATOR
var previousImage = 0;
var nextImage = 0;
var imageCount = 0;
function initRotator(){
	var requestItems = new Request({method: 'get', url: 'image-zone-items.php?z=1'});
	requestItems.send();
	requestItems.addEvent('success', setupRotate);	
}
function setupRotate(responseText, responseXML){
	items = responseXML.getElementsByTagName("item");
	for(i=0; i<items.length; ++i){
		thisItem = items[i];
		
		//extract data from xml
		thisImage = thisItem.getElementsByTagName("image")[0].childNodes[0].nodeValue;
		thisTitle = thisItem.getElementsByTagName("title")[0].childNodes[0].nodeValue;
		thisDescription = thisItem.getElementsByTagName("description")[0].childNodes[0].nodeValue;
		thisLink = null;
		if(thisItem.getElementsByTagName("link")[0].childNodes[0] != null){thisLink = thisItem.getElementsByTagName("link")[0].childNodes[0].nodeValue};
		
		//Each image in the rotator needs to have code like this...

		var newImage = document.createElement("img");
		newImage.id = 'home-rotator-item'+i+'-image';
		newImage.src = 'dynamicimage.php?w=550&h=350&src='+thisImage;		
			
		var newItemDiv = document.createElement("div");
		newItemDiv.id = 'home-rotator-item'+i;
		newItemDiv.className = 'home-rotator-item';
		newItemDiv.appendChild(newImage);
		
		//Pass data from XML into the item div object as properties
		newItemDiv.thisTitle = thisTitle;
		newItemDiv.thisDescription = thisDescription;
		newItemDiv.thisLink = thisLink;
		
		$('home-rotator').appendChild(newItemDiv);
		$('home-rotator-item'+i).set('opacity',0);
		
	}
	imageCount = items.length;
	doRotate();
}
var rotateTimer = 0;
function doRotate(){
	$clear(rotateTimer);
	if($('home-rotator-item'+nextImage+'-image').complete == true && windowOpen != true){
		//transition to the next image
		if(previousImage != nextImage){	
			$('home-rotator-item'+previousImage).tween('opacity',0);
		} else {
			//first time, Insert handlers for mouse enter and leave
			$('home-rotator').addEvent('mouseenter',handleMouseOver);
			$('home-rotator').addEvent('mouseleave',handleMouseOut);
			$('home-rotator').addEvent('click',handleClick);
		}
		$('home-rotator-item'+nextImage).tween('opacity',1);
		
		//Bring title & description into textbox, in case it's needed
		$('rotator-title').innerHTML = $('home-rotator-item'+nextImage).thisTitle;
		$('rotator-description').innerHTML = $('home-rotator-item'+nextImage).thisDescription;
		$('rotator-description').thisLink = $('home-rotator-item'+nextImage).thisLink;
		if($('rotator-description').thisLink!=""){$('home-rotator').setStyle('cursor','pointer');}
					
		//go to the next image
		previousImage = nextImage;
		++nextImage;
		if(nextImage >= imageCount){nextImage = 0;}
		rotateTimer = doRotate.delay(5000);
	} else {
		rotateTimer =doRotate.delay(1000);
	}
	
}


function handleMouseOver(){
	$('rotator-textbox').morph({
		'top': '270px',
		'height': '110px',
		'opacity': .85
	});
	$clear(rotateTimer);
	
}
function handleMouseOut(){
	$('rotator-textbox').morph({
		'top':  '340px',
		'height': '1px',
		'opacity': 0
	});
	rotateTimer = doRotate.delay(1000);
}
function handleClick(){
	goto = $('rotator-description').thisLink;
	
	if(goto != null){
		if(goto.substr(0,7)=="http://"){window.open(goto,"new");} else {loadPage(goto);}
	}
}