// JavaScript Document

var SetEvent = function ( Element , Kind , Func ) {
	if ( window.attachEvent ) {
		Element.attachEvent ( "on" + Kind , Func );
	} else if ( window.addEventListener ) {
		Element.addEventListener ( Kind , Func , true );
	}
};

function LoadFade ( ObjectRoot , LoadObjects ) {
	// Defaults
	this.FadeAmount = 100;
	this.FadeLength = 32;
	this.FadeTimer = 3000;
	
	// Globals
	this.FadeCurrentObject = 0;
	this.FadeObjectRoot = ObjectRoot;
	this.FadeLoadObjects = LoadObjects;
	
	this.FadeObject = null;
	this.FadePreviousObject = null;
	this.FadeTime = null;
	this.FadeTimeout = null;
	this.FadeDirection = null;
	
	// Collect self
	this.id = LoadFade.Instances.length;
	LoadFade.Instances [ this.id ] = this;
	
	this.SineInOut = function ( t , b , c , d ) {
		return -c / 2 * ( Math.cos ( Math.PI * t / d ) - 1 ) + b;
	};
	
	// Iteration move.
	this.IntervalFade = function ( e ) {
		// Sinusoidal wave movement	
		this.FadeOffset = this.SineInOut ( this.FadeTime , 0 , this.FadeAmount , this.FadeLength );

		// Position div.
		this.FadeObject.style.filter = "Alpha(Opacity="+this.FadeOffset+")";
		this.FadeObject.style.mozOpacity = this.FadeOffset/100;
		this.FadeObject.style.opacity = this.FadeOffset/100;
		
		// Update time counter	
		this.FadeTime ++;
		
		// Anymore to do? Ok do another.
		if ( this.FadeTime <= this.FadeLength  ) {
			this.FadeTimeout = setTimeout ( 'LoadFade.Instances['+this.id+'].IntervalFade()' , 17 );
		} else {
			// Setup next fade.
			this.FadeTimeout = setTimeout ( 'LoadFade.Instances['+this.id+'].StartFade()' , this.FadeTimer );
		}
	};
	
	// Sets up the slider.
	this.StartFade = function ( Direction ) {
		// Reset previous
		this.FadeTime = 0;
		if ( this.FadePreviousObject ) {
			this.FadePreviousObject.parentNode.removeChild ( this.FadePreviousObject );
		}
		if ( this.FadeObject ) {
			this.FadePreviousObject = this.FadeObject;
			this.FadePreviousObject.style.zIndex = 100;
		}
		
		// Next object
		this.FadeCurrentObject ++;
		if ( this.FadeCurrentObject >= this.FadeLoadObjects.length ) {
			this.FadeCurrentObject = 1;
		}
		
		// Setup a way to connect to IntervalFade
		eval ( "function InitFade () {LoadFade.Instances["+this.id+"].IntervalFade()}" );
		
		// Grab object ie. InspiringThumbnail0
		if ( ! this.FadeLoadObjects [ this.FadeCurrentObject ] [ "Object" ] ) {
			// Load it in
			this.FadeObject = document.createElement ( "img" );
			this.FadeObject.src = this.FadeLoadObjects [ this.FadeCurrentObject ] [ "Src" ]+"?"+Math.floor(Math.random()*10000+1);
			this.FadeObject.alt = this.FadeLoadObjects [ this.FadeCurrentObject ] [ "Title" ];
			document.getElementById ( this.FadeObjectRoot ).appendChild ( this.FadeObject );
			SetEvent ( this.FadeObject , 'load' , InitFade );	
			this.FadeObject.style.position = "absolute";
			this.FadeObject.style.filter = "Alpha(Opacity=0)";
			this.FadeObject.style.mozOpacity = 0;
			this.FadeObject.style.opacity = 0;
			this.FadeObject.style.zIndex = 200;
			this.FadeLoadObjects [ this.FadeCurrentObject ] [ "Object" ] = this.FadeObject;
		} else {
			// Re-use
			this.FadeObject = this.FadeLoadObjects [ this.FadeCurrentObject ] [ "Object" ];	
			this.FadeObject.style.filter = "Alpha(Opacity=0)";
			this.FadeObject.style.mozOpacity = 0;
			this.FadeObject.style.opacity = 0;
			this.FadeObject.style.zIndex = 200;
			document.getElementById ( this.FadeObjectRoot ).appendChild ( this.FadeObject );
			InitFade ( );
		}
	};	
}
LoadFade.Instances = [ ];
