/*
#VERSION_HISTORY_START

V1.00 - 26/07/10 AC
- File Created

#VERSION_HISTORY_END

---- Example 1 ----
	var InstanceName = new Fade();
	InstanceName.SetElementsArray('div1,div2,div3');
	InstanceName.SetTimeToFade(10);
	InstanceName.SetTimeToFadeLoop(30);
	InstanceName.FadeLoop();
	
---- Example 2 ----
	var InstanceName = new Fade();
	InstanceName.SetFadeElement('div1');
	InstanceName.FadeIn();
*/

//allows an element to be faded (In/out)
function Fade() {
	this.ElementCount=0;
	this.timer;
	this.fadetimer;
	this.TimeToFadeLoop = 10000;
	this.TimeToFade = 40;
	this.FadeOutState = 100;
	this.FadeInState = 0;
	
	this.ElementsArray = new Array();
	
	this.ActiveDiv = "";
	this.DeactiveDiv = "";
	
	this.FadeLoop = FadeLoop;
	this.FadeOut = FadeOut;
	this.FadeIn = FadeIn;
	
	this.SetElementsArray = SetElementsArray;
	this.SetTimeToFadeLoop = SetTimeToFadeLoop;
	this.SetTimeToFade = SetTimeToFade;
	this.ClearElementsArray = ClearElementsArray;
}

//sets the time inteval for the fade loop
function SetFadeElement(elementid){
	this.ActiveDiv = elementid;
	this.DeactiveDiv = elementid;
}

//defins an array of element for the fade loop
function SetElementsArray(elements){
	this.ElementsArray = elements.split(",");
}

//sets the time inteval for the fade loop
function SetTimeToFade(milliseconds){
	this.TimeToFade = parseInt(milliseconds);
}

//set the display time for the fade loop
function SetTimeToFadeLoop(seconds){
	this.TimeToFadeLoop = parseInt(seconds)*1000;
}

//clears the array of elements for the fade loop
function ClearElementsArray(){
	this.ElementsArray.clear();
}

//loops through elements in the elements array
function FadeLoop(){
	if(this.ElementsArray != null){
		var pointer = this;
		this.DeactiveDiv = this.ActiveDiv;
		this.ActiveDiv = this.ElementsArray[this.ElementCount];
		
		if(this.DeactiveDiv != null){	this.FadeOut();} //if a deactive element exists then fade out
		if(this.ActiveDiv != null){this.FadeIn();} //if a active element exists then fade in
		
		this.ElementCount=this.ElementCount+1;
		if(this.ElementCount >= this.ElementsArray.length){this.ElementCount = 0;}

		if(this.ElementsArray.length > 1){
			this.timer = setTimeout(function(){pointer.FadeLoop();}, this.TimeToFadeLoop+(this.TimeToFade*20));
		}
	}
}

//fades out an element
function FadeOut(){
	var element = document.getElementById(this.DeactiveDiv); //set element
	if(element == null){return;} //if element dosnt exist exit
	
	this.FadeOutState = this.FadeOutState-5; //de-increment the fade out state
	
	if(this.FadeOutState > 0){ //fade out the element
		element.style.display="block";
		element.style.filter = 'alpha(opacity = ' + this.FadeOutState + ')';
		element.style.opacity = Math.round(this.FadeOutState/10)/10;
		element.style.MozOpacity = Math.round(this.FadeOutState/10)/10;
		var pointer = this;
		this.fadetimer = setTimeout(function(){pointer.FadeOut();}, this.TimeToFade); //loop fade out until opacity under 0
	}else{
		this.FadeOutState = 100;
		element.style.filter = 'alpha(opacity = 0)';
		element.style.opacity = 0.0;
		element.style.MozOpacity = 0.0;
		element.style.display="none"; //hide element on fade out
	}
}

//fades in an element
function FadeIn(){
	var element = document.getElementById(this.ActiveDiv); //set element
	if(element == null){return;} //if element dosnt exist exit
	
	element.style.display="block"; //make sure the element is visible
	this.FadeInState = this.FadeInState+5; //increment the fade in state
	
	if(this.FadeInState < 100){ //fade in the element
		element.style.filter = 'alpha(opacity = ' + this.FadeInState + ')';
		element.style.opacity = Math.round(this.FadeInState/10)/10;
		element.style.MozOpacity = Math.round(this.FadeInState/10)/10;
		//this.fadetimer = setTimeout(function() { this.FadeIn(); }, 10); //loop fade in until opacity over 100
		var pointer = this;
		this.fadetimer = setTimeout(function(){pointer.FadeIn();}, this.TimeToFade); //loop fade out until opacity under 0
	}else{
		this.FadeInState = 0;
		element.style.filter = 'alpha(opacity = 100)';
		element.style.opacity = 1.0;
		element.style.MozOpacity = 1.0;
	}
}