// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

function teaser(target1,target2,selector,selectorActive,selectorsClass,captionID,playButton,pauseButton,productLinkID)
{
	oTeaser = this;
	
	this.target1 = target1;
	this.target2 = target2;
	this.selector = selector;
	this.selectorActive = selectorActive;
	this.selectorsClass = selectorsClass;
	this.captionID = captionID;
	this.productLinkID = productLinkID;

	this.playButton = playButton;
	this.pauseButton = pauseButton;

	this.timer = '';
	this.timing = false;
	this.timeBetweenFades = 10000;

	this.maxTimeUp = false;
	this.maxTimeBeforeLoadTimer = '';
	this.maxTimeBeforeLoad = 20000;
	this.totalImages = 0;
	this.totalLoaded = 0;
	
	this.header_images = new Array();
	this.product_images = new Array();
	this.product_links = new Array();
	this.captions = new Array();

	this.imageData = new Array();
	
	this.current_teaser = null;

	this.imageToLoad = '';

	this.add_loaded = function() {

		if( this.maxTimeUp == false )
		{
			this.totalLoaded++;
			if( this.totalLoaded >= this.totalImages)
			{				
				this.kill_maxTimeBeforeLoadTimer();

				this.timer = setTimeout("oTeaser.maxTimeOut();",this.timeBetweenFades);
			}
		}

	}

	this.kill_maxTimeBeforeLoadTimer = function()
	{
		clearTimeout(oTeaser.maxTimeBeforeLoadTimer);
	}

	this.set_valid_index = function(checkVal)
	{
		if( (checkVal > (oTeaser.imageData.length - 1)) || ( checkVal > 5 ) )
		{
			return 0;
		}
		else
		{
			return checkVal;
		}
	}

	this.start_preload_count = function()
	{
		index = this.set_valid_index(1);
		this.maxTimeBeforeLoadTimer = setTimeout("oTeaser.maxTimeOut();",this.maxTimeBeforeLoad);
		this.load(0,'',true);
	}

	this.maxTimeOut = function()
	{		
		this.maxTimeUp = true;
		this.timing = true;
		index = this.set_valid_index(1);
		this.load(index,'timed');
	}
	
	this.add_teaser = function(data) {		
		this.imageData.push(data);
		this.totalImages++;
	}

	this.preload_images = function() {
		
	}
		
	this.load_random = function() {

		this.imageData = this.imageData.shuffle();
		this.load(0,'timed');
	}

	this.load = function(index,timed,firstLoad)
	{
		//load the image we want to load into the background
		$(target2).src = this.imageData[index].header_src;
		$(target2).style.display = 'block';

		//fade the top image so you see the image we just loaded into the background
		new Effect.Fade(oTeaser.target1,{duration:0.8,afterFinish: oTeaser.afterFade});
		
		if( $('teaserProductImage') )
		{
			$('teaserProductImage').src = this.imageData[index].product_src;
			$(oTeaser.productLinkID).href = oTeaser.imageData[index].link;
			try { $('teaserProductButtonLink').href = oTeaser.imageData[index].link; } catch (e) {}
			try { $('teaserProductButtonLinkImage').src = oTeaser.imageData[index].button_src; } catch (e) {}
			try { $('teaserProductButtonLinkImage').width = oTeaser.imageData[index].button_width; } catch (e) {}
		}
		
		this.current_teaser = index;
		this.redrawSelectors();		

		if( timed )
		{
			index++;

			index = this.set_valid_index(index);

			oTeaser.timer = setTimeout("oTeaser.load('"+index+"','timed')",this.timeBetweenFades);
		}
		else if( firstLoad )
		{
		}
		else
		{			
			oTeaser.stop();
		}
	}

	this.stop = function()
	{			
		clearTimeout(oTeaser.maxTimeBeforeLoadTimer);
		clearTimeout(oTeaser.timer);
		oTeaser.timing = false;
		$('rotatingTeaserButton').src = oTeaser.playButton;		
	}

	this.start = function()
	{
		oTeaser.load(oTeaser.current_teaser,'timed');
		oTeaser.timing = true;
		$('rotatingTeaserButton').src = oTeaser.pauseButton;
	}

	this.playPause = function()
	{		
		this.kill_maxTimeBeforeLoadTimer();

		if( oTeaser.timing == true ) {
			oTeaser.stop();
		} else {
			oTeaser.start();
		}
		
		this.redrawSelectors();
	}

	this.afterFade = function()
	{
		//set the background to the new foreground
		$(target1).src = $(target2).src;
		new Effect.Opacity(target1,{duration:0.1,from:0.0,to:1.0});
		$(target1).style.display = 'block';
		$(oTeaser.captionID).innerHTML = oTeaser.imageData[oTeaser.current_teaser].caption
	}
	
	this.redrawSelectors = function () {
		selectorsHTML = '';
		count = 0;
		controlButton = oTeaser.pauseButton;
		controlText = "Pause slide show"

		if( oTeaser.timing == false ) {
			controlButton = oTeaser.playButton
			controlText = "Play slide show"
		}

		// selectorsHTML += '<img id="rotatingTeaserButton" class="selectors" onclick="oTeaser.playPause();" src="'+controlButton+'" width="9" height="9" />';
		selectorsHTML += '<a href="#" id="rotatingTeaserButton" class="selectors" onclick="oTeaser.playPause();return false;">'+controlText+'</a>';
		
		this.imageData.each(
			function(data) {
				
				if ( count >= this.totalImages )
				{
					return false;
				}
				else
				{
					count++;
				}
					
				if (oTeaser.imageData[oTeaser.current_teaser].header_src == data.header_src) {
					selectorsHTML += '<img class="selectors" onclick="oTeaser.load(' + oTeaser.imageData.indexOf(data) + ',\'\',\'\');" src="' + oTeaser.selectorActive + '" width="9" height="9" />';
				} else {
					selectorsHTML += '<img class="selectors" onclick="oTeaser.load(' + oTeaser.imageData.indexOf(data) + ',\'\',\'\');" src="' + oTeaser.selector + '" width="9" height="9" />';
				}
			}
		)
		
		$('teaserSelector').innerHTML = selectorsHTML;
	}
	
}