/*======================================================================*\
|| #################################################################### ||
|| # ÜberNet Javascript Libary                                        # ||
|| # ---------------------------------------------------------------- # ||
|| # All Javascript in this file is ©2000-2007 ÜberNet.               # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # --------------- THIS FILE IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.ubernet.biz  |  http://www.ubernet.biz/license.unc    # ||
|| #################################################################### ||
\*======================================================================*/

//Create a new class object on which we will build the fadeout class
var FadeOut = Class.create();

FadeOut.prototype = {
	
	//This is the contructor to the fade class
	initialize: function( elementToFadeOut, startIn, lengthOfFade )
	{
		lengthOfFade = lengthOfFade || 1; //Default 1 second fade
		startIn = startIn || 5 //Default starts in 5 seconds
		
		//Grab the element we are working with
		this.element = $(elementToFadeOut);
		
		//Length this fade will alst for
		this.len = lengthOfFade * 1000;
		
		//Set a timeout to start the fade & then create a timeout to initilse the fade
		var pointer = this;
		this.start = setTimeout( function() { pointer.startFade(); }, startIn * 1000 );
		
		//There is no interval timer yet, so set this to null
		this.interval = null;
	},
	
	//If this is called, the class will clear all timers & reset the object
	cancel: function()
	{
		//Reset the opacity to 100%
		this.setOpacity( 100 );
		
		//If the countdown is running clear it
		if (this.start != null)
			clearTimeout( this.start );
		
		//If the fade is running - clear it
		if (this.interval != null)
			clearInterval( this.interval );
	},
	
	//Internal function to change the Opacity of an object
	setOpacity: function( level )
	{
		if (this.element.filters) // internet explorer
		{
			try
			{
				this.element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = level;
			} catch (e) { 
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+level+')';
			}
		}
		else // other browsers
		{
			this.element.style.opacity = (level / 100);
		}
	},
	
	//This function is called by the timer. It starts the fade
	startFade: function()
	{
		this.start = null; //The timer no longer exists, so remove it
		this.time = 10; //Start at 10 (0 would give a devide of zero error)
		
		//Create a pointer back to this object and create an interval to run the fade
		var pointer = this;
		this.interval = setInterval(function () { pointer.runFade(); }, 10);
	},
	
	//This function is called by the interval and does the change in opacity
	runFade: function()
	{
		//Set the opacity to the correct level for this part of the time
		this.setOpacity( 100 - ((this.time / this.len) * 100) );
		
		//If we have finsih clear the interval, otherwise add 10 to the time counter
		if (this.time == this.len)
		{
			this.element.style.display = 'none';
			this.setOpacity( 100 );
			clearInterval( this.interval );
		} else {
			this.time += 10;
			return false;	
		}
	}
}

//Create a new class object on which we will build the fadeout class
var FadeIn = Class.create();

FadeIn.prototype = {
	
	//This is the contructor to the fade class
	initialize: function( elementToFadeIn, startIn, lengthOfFade )
	{
		lengthOfFade = lengthOfFade || 1; //Default 1 second fade
		startIn = startIn || 0 //Default starts in 0 seconds
		
		//Grab the element we are working with
		this.element = $(elementToFadeIn);
		
		//Length this fade will alst for
		this.len = lengthOfFade * 1000;
		
		//Set a timeout to start the fade & then create a timeout to initilse the fade
		var pointer = this;
		this.start = setTimeout( function() { pointer.startFade(); }, startIn * 1000 );
		
		//There is no interval timer yet, so set this to null
		this.interval = null;
	},
	
	//If this is called, the class will clear all timers & reset the object
	cancel: function()
	{
		//Reset the opacity to 100% and hide the element
		this.element.style.display = 'none';
		this.setOpacity( 100 );
		
		
		//If the countdown is running clear it
		if (this.start != null)
			clearTimeout( this.start );
		
		//If the fade is running - clear it
		if (this.interval != null)
			clearInterval( this.interval );
	},
	
	//Internal function to change the Opacity of an object
	setOpacity: function( level )
	{
		if (this.element.filters) // internet explorer
		{
			try
			{
				this.element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = level;
			} catch (e) { 
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+level+')';
			}
		}
		else // other browsers
		{
			this.element.style.opacity = (level / 100);
		}
	},
	
	//This function is called by the timer. It starts the fade
	startFade: function()
	{
		this.start = null; //The timer no longer exists, so remove it
		this.time = 10; //Start at 10 (0 would give a devide of zero error)
		
		//Set the opacity to 0% and allow the browser to render to object
		this.setOpacity( 0 );
		this.element.style.display = '';
		
		//Create a pointer back to this object and create an interval to run the fade
		var pointer = this;
		this.interval = setInterval(function () { pointer.runFade(); }, 10);
	},
	
	//This function is called by the interval and does the change in opacity
	runFade: function()
	{
		//Set the opacity to the correct level for this part of the time
		this.setOpacity( ((this.time / this.len) * 100) );
		
		//If we have finsih clear the interval, otherwise add 10 to the time counter
		if (this.time == this.len)
		{
			clearInterval( this.interval );
		} else {
			this.time += 10;
			return false;	
		}
	}
}