/**
 * alphaAPI library for WebDDM, aids in transparency transitions and effects.
 * 
 * @version: 0.1.1
 * @starteC: 02/02/2005
 * @copyright: Copyright (c) 2005 JPortal, All Rights Reserved
 * @website: www.cortex-creations.com/webddm
 * @license: GPL vs2.0
 * @subversion: $IC: alphaAPI.WebDDM.js 134 2006-05-02 21:04:50Z josh $
 */
 
/**
 * alphaAPI
 * Original Author: chrisken
 * Original Url: http://www.cs.utexas.edu/users/chrisken/alphaapi.html
 *
 * Modified by dallen and Josh
 */
function alphaAPI(element, fadeInDelay, fadeOutDelay, startAlpha, stopAlpha, offsetTime, deltaAlpha)
{
	this.element = typeof(element) == 'object' ? element : document.getElementById(element);
	this.fadeInDelay = fadeInDelay || 40;
	this.fadeOutDelay = fadeOutDelay || this.fadeInDelay;
	this.startAlpha = startAlpha;
	this.stopAlpha = stopAlpha;
	// make sure a filter exists so an error is not thrown
	if (typeof(this.element.filters) == 'object')
	{
		if (typeof(this.element.filters.alpha) == 'undefined')
		{
			this.element.style.filter += 'alpha(opacity=100)';
		}
	}

	this.offsetTime = (offsetTime || 0) * 1000;
	this.deltaAlpha = deltaAlpha || 10;
	this.timer = null;
	this.paused = false;
	this.started = false;
	this.cycle = false;
	this.command = function() {};
	return this;
} // End function alphaAPI


/**
 * Sets "cycle" property.
 */
alphaAPI.prototype.repeat = function(repeat)
{
	this.cycle = repeat ? true : false;
};


/**
 * Adds passed deltaAlpha to current alpha.
 */
alphaAPI.prototype.setAlphaBy = function(deltaAlpha)
{
	this.setAlpha(this.getAlpha() + deltaAlpha);
};


/**
 * Switch between stopped/paused/unpaused statuses.
 */
alphaAPI.prototype.toggle = function()
{
	if (!this.started)
	{
		this.start();
	}
	else if (this.paused)
	{
		this.unpause();
	}
	else
	{
		this.pause();
	}
}; // End function alphaAPI.prototype.toggle()


/**
 * Sets a timeout.
 */
alphaAPI.prototype.timeout = function(command, delay)
{
	this.command = command;
	this.timer = setTimeout(command, delay);
}; // End function alphaAPI.prototype.timeout()


/**
 * Set the alpha of an element.
 */
alphaAPI.prototype.setAlpha = function(opacity)
{
	if (typeof(this.element.filters) == 'object')
	{
		this.element.filters.alpha.opacity = opacity;
		return;
	}
	else if (this.element.style.setProperty)
	{
		// Make sure opacity does not go above 99, to fix Firefox bugs
		// I thought this had been fixed, but I can still notice it in FF1.0PR
		opacity = opacity > 99 ? 99 : opacity;
		
		// css3
		this.element.style.setProperty('opacity', opacity / 100, '');
		// handle the case of mozilla < 1.7
		this.element.style.setProperty('-moz-opacity', opacity / 100, '');
		// handle the case of old kthml
		this.element.style.setProperty('-khtml-opacity', opacity / 100, '');
	}
}; // End function alphaAPI.prototype.setAlpha()


/**
 * Returns current alpha status of element.
 */
alphaAPI.prototype.getAlpha = function()
{
	if (typeof(this.element.filters) == 'object')
	{
		return this.element.filters.alpha.opacity;
	}
	else if (this.element.style.getPropertyValue)
	{
		var opacityValue = this.element.style.getPropertyValue('opacity');
		// handle the case of mozilla < 1.7
		if (opacityValue == '')
		{
			opacityValue = this.element.style.getPropertyValue('-moz-opacity');
		}

		// handle the case of old khtml
		if (opacityValue == '')
		{
			opacityValue = this.element.style.getPropertyValue('-khtml-opacity');
		}

		return opacityValue * 100;
	}

	return 100;
}; // End function alphaAPI.prototype.getAlpha()


/**
 * Starts fading.
 */
alphaAPI.prototype.start = function()
{
	this.started = true;
	this.setAlpha(this.startAlpha);
	// determine direction
	if (this.startAlpha > this.stopAlpha)
	{
		var instance = this;
		this.timeout(function() { instance.fadeOut(); }, this.offsetTime);
	}
	else
	{
		var instance = this;
		this.timeout(function() { instance.fadeIn(); }, this.offsetTime);
	}
}; // End function alphaAPI.prototype.start()


/**
 * Stops fading.
 */
alphaAPI.prototype.stop = function()
{
	this.started = false;
	this.setAlpha(this.stopAlpha);
	this.stopTimer();
	this.command = function() {};
}; // End function alphaAPI.prototype.stop()


/**
 * Resets the fader to its original settings.
 */
alphaAPI.prototype.reset = function()
{
	this.started = false;
	this.setAlpha(this.startAlpha);
	this.stopTimer();
	this.command = function() {};
}; // End function alphaAPI.prototype.reset()


/**
 * Freeze fading.
 */
alphaAPI.prototype.pause = function()
{
	this.paused = true;
	this.stopTimer();
}; // End function alphaAPI.prototype.pause()


/**
 * Unpauses fader and commences to fade in/out element.
 */
alphaAPI.prototype.unpause = function()
{
	this.paused = false;
	if (!this.started)
	{ 
		this.start();
	}
	else
	{
		this.command(); 
	}
}; // End function alphaAPI.prototype.unpause()


/**
 * Stops fading in/out.
 */
alphaAPI.prototype.stopTimer = function()
{
	clearTimeout(this.timer);
	this.timer = null;
}; // End function alphaAPI.prototype.stopTimer()


/**
 * alphaAPI object to fade out elements. 
 */
alphaAPI.prototype.fadeOut = function()
{
	this.stopTimer();
	if (this.getAlpha() > this.stopAlpha)
	{
		this.setAlphaBy(-1 * this.deltaAlpha);
		var instance = this;
		this.timeout(function() { instance.fadeOut(); }, this.fadeOutDelay);
	}
	else
	{
		if (this.cycle)
		{
			var instance = this;
			this.timeout(function() { instance.fadeIn(); }, this.fadeInDelay);
		}
		else
		{
		   this.started = false;
		   this.WebDDM_object.transitionCompleted(this.element, false, this.element.id);
		}
	}
}; // End function alphaAPI.prototype.fadeOut()


/**
 * alphaAPI object to fade in elements. 
 */
alphaAPI.prototype.fadeIn = function()
{
	this.stopTimer();
	if (this.getAlpha() < this.startAlpha)
	{
		this.setAlphaBy(this.deltaAlpha);
		var instance = this;
		this.timeout(function() { instance.fadeIn(); }, this.fadeInDelay);
	}
	else
	{
		if (this.cycle)
		{
			var instance = this;
			this.timeout(function() { instance.fadeOut(); }, this.fadeOutDelay);
		}
		else
		{
			this.started = false;
			this.WebDDM_object.transitionCompleted(this.element, true, this.element.id);
		}
	}
};
