//ROTATOR
function Rotator(mainDivName,imageZone){
	var previousImage = 0;
	var nextImage = 0;
	var imageCount = 0;
	window.addEvent('domready', initRotator);
	function initRotator(){
		var requestItems = new Request({method: 'get', url: 'image-zone-items.php?z='+imageZone});
		requestItems.send();
		requestItems.addEvent('success', setupRotate);	
		 $(mainDivName+"-player").fade("hide");
	}
	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;
			thisLinkType = thisItem.getElementsByTagName("linktype")[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 = mainDivName + '-item'+i+'-image';
			newImage.src = '/dimages/561/316/'+thisImage;		
				
			var newItemDiv = document.createElement("div");
			newItemDiv.id = mainDivName + '-item'+i;
			newItemDiv.className = mainDivName + '-item';
			newItemDiv.appendChild(newImage);
			
			//Pass data from XML into the item div object as properties
			newItemDiv.thisTitle = thisTitle;
			/*newItemDiv.thisDescription = thisDescription;*/
			newItemDiv.thisLink = thisLink;
			newItemDiv.thisLinkType = thisLinkType;
			
			$(mainDivName).appendChild(newItemDiv);
			$(mainDivName+'-item'+i).set('opacity',0);
			$(mainDivName+'-item'+i+"-image").addEvent('click',handleClick);
			if(thisLink != null){$(mainDivName+'-item'+i+"-image").setStyle('cursor','pointer');}
			
			var newBoxDiv = document.createElement("div");
			newBoxDiv.id = mainDivName+'-box'+i;
			newBoxDiv.className = 'rotator-boxes rotator-box-off';
			$(mainDivName+'-boxes').appendChild(newBoxDiv);
			$(mainDivName+'-box'+i).addEvent('click', boxclick.pass(i));
		}
		imageCount = items.length;
		doRotate();
	}
	function boxclick(whichbox){
		
		if(playerActive == true){
			//destroy video box, if active
			playerActive = false;
			var fadeOutVideo = new Fx.Tween(mainDivName+"-player")
			fadeOutVideo.start('opacity',0);
			fadeOutVideo.addEvent('complete',function(){$(mainDivName+"-player").empty(); $(mainDivName+"-player").setStyle("display","none");});
		}
		
		nextImage = whichbox;
		$clear(rotateTimer);
		doRotate();
	}
	
	var rotateTimer = 0;
	var CurrentLink = null;
	var CurrentLinkType = null;
	function doRotate(){
		$clear(rotateTimer);
		if($(mainDivName+'-item'+nextImage+'-image').complete == true && playerActive == false){
			//transition to the next image
			if(previousImage != nextImage){	
				$(mainDivName + '-item'+previousImage).tween('opacity',0);
				$(mainDivName+'-box'+previousImage).removeClass('rotator-box-on');
				$(mainDivName+'-box'+previousImage).addClass('rotator-box-off');
			} else {
				//first time, Insert handlers for mouse enter and leave
				$(mainDivName).addEvent('mouseenter',handleMouseOver);
				$(mainDivName).addEvent('mouseleave',handleMouseOut);
			}
			$(mainDivName+'-item'+nextImage).tween('opacity',1);
			$(mainDivName+'-box'+nextImage).removeClass('rotator-box-off');
			$(mainDivName+'-box'+nextImage).addClass('rotator-box-on');
			
			//Bring title & description into textbox, in case it's needed
			$(mainDivName+'-title').innerHTML = $(mainDivName + '-item'+nextImage).thisTitle;
			CurrentLink = $(mainDivName + '-item'+nextImage).thisLink;
			CurrentLinkType = $(mainDivName + '-item'+nextImage).thisLinkType;
			
			//$('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(100);
		}
	
	}
	
	
	function handleMouseOver(){
		
		$clear(rotateTimer);
		
	}
	function handleMouseOut(){
		$clear(rotateTimer);
		rotateTimer = doRotate.delay(5000);
	}
	var playerActive = false;
	function handleClick(){

		if(CurrentLink != null && CurrentLink != ""){
			if(CurrentLinkType == "internal" || CurrentLinkType == "external"){
				window.location.href = (CurrentLink);
			}
			if(CurrentLinkType == "video"){
				playerActive = true;
				Extension = CurrentLink.split(".");
				Extension = Extension[1];
				if(Browser.Plugins.Flash.version > 0){
					//user has flash, use flowplayer
					flowplayer("home-rotator-player", {src: '/flowplayer.commercial-3.1.2.swf', wmode: 'opaque'}, { 
						key: '#$67e192b470c2432e8af', 
						canvas: {
							backgroundGradient: 'none',
							backgroundColor: '#000000'
						},
						contextMenu: [ 
					 
							'New Life Church', 
							{'Go to newlifechurch.tv': function() { 
								location.href = 'http://newlifechurch.tv'; 
							}} 
						],
						clip: {  
							url: Extension + ':newlifechurch/' + CurrentLink,  
							provider: 'simplecdn',  
							autoPlay: true
						}, 
						// streaming plugins are configured under the plugins node  
						plugins: { 
							simplecdn: {  
								url: 'flowplayer.rtmp-3.1.1.swf',  
								netConnectionUrl: 'rtmpt://e1f1.simplecdn.net/play'  
							},
							controls: {
								autoHide: 'always',
								backgroundGradient: 'low',
								backgroundColor: '#000000'
							}
						},
						onBeforeFinish: function(){
							var fadeOutVideo = new Fx.Tween(mainDivName+"-player")
							fadeOutVideo.start('opacity',0);
							fadeOutVideo.addEvent('complete',function(){
							  $(mainDivName+"-player").empty(); 
							  $(mainDivName+"-player").setStyle("display","none"); 
							  playerActive = false;
							  $clear(rotateTimer);
							  rotateTimer = doRotate.delay(3000);
							});
							return false;
						}
					});
				} else {
					//no flash, just give user the video file
					$(mainDivName+"-player").innerHTML = '<video width="100%" height="100%" controls="true" src="http://media.newlifechurch.tv/' + CurrentLink + '"><a href="http://media.newlifechurch.tv/' + CurrentLink + '">Watch Video</a></video>';
				}
				$(mainDivName+"-player").setStyle("display","block");
				$(mainDivName+"-player").fade("in");
			}
		}
	}
	function handlePlayerDone(){
		playerActive = false;
	}
}

