// JavaScript Document

function SlideShow( div_slideshow_id,div_header_id )
{
	this.SlideShow   = window.document.getElementById( div_slideshow_id );
	this.Header	     = div_header_id!='' && div_header_id != null?window.document.getElementById( div_header_id ):null;
	this.HeaderTable = null;
	this.Containers  = new Array();
	
	this.CurrentContainerIndex = 0;
	this.CurrentContainer 	   = null;
	this.CurrentContainerTimer = null;
	this.CurrentContainerIntervalMS = 5000;
	
	this.FadeContainerTimer = null;
	this.FadeContainerPct = 100;
	this.FadeFoward = false;
	this.CallbackShadow = SlideShow_CallbackShadow;
	
	this.AddContainer 	 = SlideShow_AddContainer;
	this.ChangeContainer = SlideShow_ChangeContainer;
	this.CallbackContainer = SlideShow_CallbackContainer;


	this.Display	  	 = SlideShow_Display;
	this.Play			 = SlideShow_Play;
	this.Stop			 = SlideShow_Stop;
	
}

function SlideShow_AddContainer( id )
{
	this.Containers.push( window.document.getElementById( id ) );
}

function SetOpacity(object,opacityPct)
{
  // IE.
  object.style.filter = 'alpha(opacity=' + opacityPct + ')';
  // Old mozilla and firefox
  object.style.MozOpacity = opacityPct/100;
  // Everything else.
  object.style.opacity = opacityPct/100;
}


function SlideShow_CallbackShadow( NewContainerIndex )
{

	SetOpacity( this.CurrentContainer,this.FadeContainerPct );

	if( this.FadeContainerPct == 0 )
	{
		this.CurrentContainer.style.display = 'none';
		this.HeaderTable.rows[0].cells[this.CurrentContainerIndex].className = "unselect";

		this.CurrentContainerIndex = NewContainerIndex;
		this.CurrentContainer 	   = this.Containers[this.CurrentContainerIndex];

		SetOpacity( this.CurrentContainer,0 );
		this.CurrentContainer.style.display = 'block';
		this.HeaderTable.rows[0].cells[this.CurrentContainerIndex].className = "select";
		
		this.FadeFoward = true;
		
	}
	else if( this.FadeContainerPct == 100 && this.FadeFoward )
	{
		this.FadeFoward = false;
		
		window.clearInterval( this.FadeContainerTimer );
		this.FadeContainerTimer = null;
	}

	if( this.FadeFoward )
	{
		this.FadeContainerPct = this.FadeContainerPct + 10;
	}
	else
	{
		this.FadeContainerPct = this.FadeContainerPct - 10;
	}
}

function SlideShow_ChangeContainer( index )
{
	if( this.CurrentContainer != this.Containers[index] )
	{
		var objTHIS = this;
		if( this.FadeContainerTimer != null ) 	
		{
			window.clearInterval( this.FadeContainerTimer );
		}
		
		this.FadeContainerPct = 100;
		this.FadeFoward = false;
		this.FadeContainerTimer = window.setInterval( function() { objTHIS.CallbackShadow(index); },32 );
	}
}

function SlideShow_Display( )
{
	this.HeaderTable = document.createElement("table");
	this.HeaderTable.id = 'slideshow_shortlist';
	var row = this.HeaderTable.insertRow( 0 );
	var objTHIS = this;

	for(PF in this.Containers)
    {
		var cell = row.insertCell( row.cells.length );
		cell.innerHTML = parseInt(PF)+1;
		
		cell.onclick   = function () { objTHIS.ChangeContainer( this.cellIndex ); }

		if( PF == 0 )
		{
			this.CurrentContainer = this.Containers[PF];
			this.Containers[PF].style.display = 'block';
			cell.className = "select";
		}
		else
		{
			this.Containers[PF].style.display = 'none';
			cell.className = "unselect";
		}
    }
	
	var cell = row.insertCell( row.cells.length );
	cell.title = "Play";
	cell.className = "slideshow_shortlist_play";
	cell.onclick = function() { objTHIS.Play( this.CurrentContainerIntervalMS ); }

	this.Header.appendChild( this.HeaderTable );
}

function SlideShow_CallbackContainer()
{
	var nextContainer = (this.CurrentContainerIndex+1)%this.Containers.length;
	this.ChangeContainer( nextContainer );
}

function SlideShow_Play( interval )
{
	if( this.CurrentContainerTimer != null )
	{
		this.Stop( );
	}
	
	var objTHIS = this;
	this.CurrentContainerIntervalMS = interval;
	this.CurrentContainerTimer = window.setInterval( function() { objTHIS.CallbackContainer() },interval );
	
	var ctrl = this.HeaderTable.rows[0].cells[this.HeaderTable.rows[0].cells.length-1];
	ctrl.title = "Stop";
	ctrl.className = "slideshow_shortlist_stop";
	ctrl.onclick = function() { objTHIS.Stop(); }
}

function SlideShow_Stop( )
{
	if( this.CurrentContainerTimer != null )
	{
		window.clearInterval( this.CurrentContainerTimer );
	}
	this.CurrentContainerTimer = null;

	var objTHIS = this;
	var ctrl = this.HeaderTable.rows[0].cells[this.HeaderTable.rows[0].cells.length-1];
	ctrl.title = "Play";
	ctrl.className = "slideshow_shortlist_play";
	ctrl.onclick = function() { objTHIS.Play( objTHIS.CurrentContainerIntervalMS ); }
}
