/*
Part of JJLIB.
Copyright (c) 2009, Wisebusiness Ltd. All rights reserved. Unauthorised re-use or redistribution of any part of this file is strictly prohibited.
Developer: James Mistry
*/

FADE_DIRECTION_OUT = 0;
FADE_DIRECTION_IN = 1;

FADING_OUT = 0;
FADED_OUT = 1;
FADING_IN = 2;
FADED_IN = 3;

function jjlib_fader(idtofade, fadedirection, fadespeed) {
	
	if (fadespeed == null) fadespeed = 5;
	
	this.target = idtofade;
	this.direction = fadedirection;
	this.fadespeed = (fadespeed * 5);
	this.autodirect = true;
	this.ttafade = 0;
	this.cback = null;
	
	this.GetDirection = function() {
		
		return this.direction;
		
	}
	
	this.SetDirection = function(NewDirection) {
		
		this.direction = NewDirection;
		
	}
	
	this.GetSpeed = function() {
		
		return this.fadespeed / 5;
		
	}
	
	this.SetSpeed = function(NewSpeed) {
		
		this.fadespeed = NewSpeed * 5;
		
	}
	
	this.GetAutoDirection = function() {
		
		return this.autodirect;
		
	}
	
	this.SetAutoDirection = function(AutoDir) {
		
		this.autodirect = AutoDir;
		
	}
	
	this.SetCallback = function(NewCback) {
		
		this.cback = NewCback;
		
	}
	
	var tgt = document.getElementById(this.target);
	
	if (tgt == null) return false;
	
	if (!this.autodirect) {
		
		if (this.direction == FADE_DIRECTION_OUT) this.state = FADED_IN;
		else this.state = FADED_OUT;
		
	} else {
		if (((tgt.style.filter == null) && ((tgt.style.opacity == null))) || ((tgt.style.filter == "alpha(opacity=100)") || (tgt.style.opacity == "1"))) this.state = FADED_IN;
		else this.state = FADED_OUT;
		
	}
	
	this.Fade = function(NoDisplayWhenFaded) {
		
		if (NoDisplayWhenFaded == null) NoDisplayWhenFaded = false;
		
		var el = document.getElementById(this.target);
		
		if (el == null)return false;
		
		if ((this.state == FADING_OUT) || (this.state == FADING_IN)) {
			
			this.state = this.state == FADING_IN ? FADING_OUT : FADING_IN;
			this.ttafade = this.fadespeed - this.ttafade;
			
		} else {
			
			this.state = this.state == FADED_IN ? FADING_OUT : FADING_IN;
			this.ttafade = this.fadespeed;
			var pobj = this;
			var fadetimerhandle = setInterval(function() {
				
				if (pobj.ttafade <= 0) {
					
					el.style.opacity = pobj.state == FADING_IN ? 1 : 0;
					el.style.filter = "alpha(opacity=" + (pobj.state == FADING_IN ? 100 : 0) + ")";
					pobj.state = pobj.state == FADING_IN ? FADED_IN : FADED_OUT;
					if ((NoDisplayWhenFaded) && (pobj.state == FADED_OUT)) el.style.display = "none";
					else if ((NoDisplayWhenFaded) && (pobj.state == FADED_IN)) el.style.display = "block";
					clearInterval(fadetimerhandle);
					pobj.ttafade = 0;
					if (this.cback != null) cback();
					return;
					
				}
				
				pobj.ttafade--;
				var fpos = pobj.ttafade / pobj.fadespeed;
				if (pobj.state == FADING_IN) fpos = 1 - fpos;
				
				el.style.opacity = fpos;
				el.style.filter = "alpha(opacity=" + (fpos * 100) + ")";
				
			}, 25);
			
		}
		
	}
	
	return true;
	
}