MainImageAutoRotate = Class.create();

/** auto image rotate feature **/
var galleryManagerActive;
Object.extend(MainImageAutoRotate.prototype, {
	initialize: function(mainImages) {
		// set up the local vars
		this.mainImages = mainImages;
		this.imageLoaders = new Array();
		this.currentLoaderIndex = 0;
		// currentImageIndex is just an index number used to access the correct array item
		this.currentImageIndex = 0;
		// nextImage is an actual image obj, used for preloading
		this.nextImage = new Image();
		this.totalImages = mainImages.length;
		this.imageHolder = $('mainImageHolder');
		this.imageHolder2 = $('mainImageHolder2');
		this.currentVisible = "imageHolder";
		this.rotateInterval = null;
		this.intervalLength = 12000;
		// used for image preloading
		this.imagesLoaded = false;
		this.intervalKey;
		// start the other processes
		// this.randomize();   disabled to allow order to be set on homepage rotations  
		this.processLoading();
		//set initial image
			
		if(MODEL_ID==""){ 
			//fireTag('2118.4',{'<model_name>':'Home Page','<hero_image_name>':this.mainImages[this.currentImageIndex].source.split("/")[this.mainImages[this.currentImageIndex].source.split("/").length-1]}); 
		} else { 
			//fireTag('2118.4',{'<model_name>':MODEL_ID,'<hero_image_name>':this.mainImages[this.currentImageIndex].source.split("/")[this.mainImages[this.currentImageIndex].source.split("/").length-1]}); 
		}
		//this.imageHolder.style.background = "transparent url('" + this.mainImages[this.currentImageIndex].source + "') no-repeat";			
		//$('mainImageLink').href = this.mainImages[this.currentImageIndex].action;
		//$('mainImageLink').onclick = this.mainImages[this.currentImageIndex].action;
		this.toggleLink(this.mainImages[this.currentImageIndex].action);
		
		if (this.currentImageIndex==parseInt(this.mainImages.length-1)) {this.currentImageIndex=0;} else {this.currentImageIndex++;}
			
		this.rotate();
	},
	
	/** randomizes the mainImages array and calls the rotate function to start the rotation **/
	randomize: function() {
		var i = this.mainImages.length;
		if (i == 0) return;
		while (--i) {
			var j = Math.floor(Math.random()*(i+1));
			var tmp1 = this.mainImages[i];
			var tmp2 = this.mainImages[j];
			this.mainImages[i] = tmp2;
			this.mainImages[j] = tmp1;
		}
	},
	processLoading: function(){
		if (this.currentLoaderIndex < this.mainImages.length) {
			var imgLoader = new SlideShowImage();
			imgLoader.setOnload(function(){
				this.loaded = true;
			}.bind(imgLoader));
			imgLoader.image.src = this.mainImages[this.currentLoaderIndex].source;
			this.imageLoaders.push(imgLoader);
			this.currentLoaderIndex++;
			this.processLoading();
		} else {
			this.imagesLoaded = true;
		}
	},

	/** rotates the images based on the currentImageIndex index **/
	rotate: function() {
	
	
		if(this.mainImages.length <= 1) return;
		if(this.rotateInterval == null)
		{
			// bind the rotate function so you don't loose scope -- or, replace with PeriodicalExecuter?
			this.rotateInterval = setInterval(this.rotate.bind(this), this.intervalLength);
		}
		if(!window.pauseImageRotation) {
			this.displayNext();
		}
	},
	
	/** Shows the next image in the stack and resets the tracking vars **/
	displayNext: function() {
		// check to see if all the images are loaded.  If so, continue, 
		// if not check to see if the next image is loaded.  
		// If it is, start loading the following image and display it.  
		// if it is not or if gallery is loaded, don't try to display it.  Set up a delay and check again later.
		window.clearInterval(this.intervalKey);
		// galleryManagerActive is set in gallery.js
		if((this.imageLoaders[this.currentImageIndex] && !this.imageLoaders[this.currentImageIndex].loaded) || galleryManagerActive == true)
		{
			// set up the timer to try again later
			this.intervalKey = setInterval(function(){this.displayNext.bind(this)}.bind(this),500);
			// reset the main timer, so that the rotate intervals stay the same
			window.clearInterval(this.rotateInterval);
			this.rotateInterval = setInterval(this.rotate.bind(this), this.intervalLength);
			return;
		}
		// image should be loaded and ready to go
		// update the image and do the tweens
		var outgoing = null;
		var incoming = null;
		
		if(this.currentVisible == "imageHolder")
		{
			// transition from imageHolder to imageHolder2
			outgoing = this.imageHolder;
			incoming = this.imageHolder2;
			this.currentVisible = "imageHolder2";
		} else {
			// transition from imageHolder2 to imageHolder
			outgoing = this.imageHolder2;
			incoming = this.imageHolder;
			this.currentVisible = "imageHolder";
		}
		
		if(incoming)
		{
			//var scriptAc = new Effect;
			if(Element.getOpacity(incoming) == 1.0) new Element.setOpacity(incoming, 0);
			if(MODEL_ID==""){ 
				//fireTag('2118.4',{'<model_name>':'Home Page','<hero_image_name>':this.mainImages[this.currentImageIndex].source.split("/")[this.mainImages[this.currentImageIndex].source.split("/").length-1]}); 
			} else { 
				//fireTag('2118.4',{'<model_name>':MODEL_ID,'<hero_image_name>':this.mainImages[this.currentImageIndex].source.split("/")[this.mainImages[this.currentImageIndex].source.split("/").length-1]}); 
			}
			incoming.style.background = "transparent url('" + this.mainImages[this.currentImageIndex].source + "') no-repeat";
		
			var lastDisc=this.currentImageIndex-1;
			if(this.currentImageIndex==0){lastDisc=this.mainImages.length-1;}
			try{
			document.getElementById('discGroup'+lastDisc).style.display='none';
			document.getElementById('discGroup'+this.currentImageIndex).style.display='block';
			} catch(err){}
			$('mainImageLink').href = this.mainImages[this.currentImageIndex].action;
			$('mainImageLink').onclick = new Function(this.mainImages[this.currentImageIndex].onclick);
			this.toggleLink(this.mainImages[this.currentImageIndex].action);
			incoming.display = "block";
			
			new Effect.Opacity(outgoing,
			{ 
				duration: 1.0, 
				transition: Effect.Transitions.linear, 
				from: 1, to: 0
			});
			new Effect.Opacity(incoming,
			{ 
				duration: 1.0, 
				transition: Effect.Transitions.linear, 
				from: 0, to: 1
			});
			
			// reset the currentImageIndex var
			if( this.currentImageIndex >= (this.totalImages-1) )
			{
				this.currentImageIndex = 0;
			} else {
				this.currentImageIndex++;
			}
		} else {
			//shouldn't happen
		}
	},
	
	/** Fast-forwards to the next image and restarts the timer **/
	fastForward: function() {
		this.displayNext();
		if (this.rotateInterval == null) {
			// bind the rotate function so you don't loose scope
			this.rotateInterval = setInterval(this.rotate.bind(this), this.intervalLength);
		}
		else {
			// restart the timer
			window.clearInterval(this.rotateInterval);
			this.rotateInterval = setInterval(this.rotate.bind(this), this.intervalLength);
		}
	},
	
	toggleLink: function(action) {
		$('mainImageLinkHolder').style.display = (action == "") ? "none" : "block";
	}
	
});


function SlideShowImage() {
	this.image = new Image();
	this.loaded = false;
	this.intervalKey;
	
	this.setOnload = function(func) {
		this.image.onload = function() {
			if(this.image.complete) {
				func();
			}
			else {
				this.intervalKey = setInterval(function(){this.intervalCallback(func)}.bind(this),500);		
			}
		}.bind(this)		
	}
	this.intervalCallback = function(func) {
	   if(this.image.complete) {
	   	  setTimeout(func,2500);
	   	  clearInterval(this.intervalKey);
	   }	
	}
	this.clearOnload = function() {
		this.image.onload = function() {}
	}
}