/**
 * To manage opacity of dom objects
 *
 * @version 1.0
 * @copyright STERNWERK 2008
 *
 * @author Helmut Wandl <helmut@wandls.net>
 *
 */
var opacity=
{
	objects:[],

	/**
	 * To fade in or fade out a dom object
	 *
	 * @param mixed obj A dom object or the id attribute of a dom object
	 * @param mixed opacity The new opacity as an integer (0-100) or an array with start opacity as first and final opacity as second value
	 * @param int speed Duration (milliseconds) of a whole fade (from 0 to 100 percent)
	 * @param string evalstr A javascript executable string which have to execute after the end of fade
	 *
	 * @version  1.0
	 *
	 * @author   Helmut Wandl <helmut@wandls.net>
	 */
	fade:function(obj, opacity, speed, evalstr)
	{
		// initialize and validate obj
		if (typeof (obj=this._getobject(obj)) != 'object') return false;

		// remove interval if set
		if (obj.opacity_fade_parameters) window.clearInterval(obj.opacity_fade_parameters.interval);

		// initialize opacity
		if (typeof opacity == 'object') { var from=opacity[0]; var to=opacity[1]; }
		if (typeof opacity == 'number') { var from=this.get(obj); var to=opacity; }

		// validate opacity
		if (typeof from != 'number' || !(from >= 0) || !(from <= 100)) return false;
		if (typeof to != 'number' || !(to >= 0) || !(to <= 100)) return false;
		if (from == to) return false;

		// initialize and validate speed
		speed=(typeof speed == 'number' && speed >= 0 ? speed : 250);

		// initialize evalstr parameter
		if (!evalstr) evalstr='';

		// create parameter object
		o={};

		o.start_opacity=from;
		o.final_opacity=to;
		o.evalstr=evalstr;

		o.difference=(to - from);

		o.steps=Math.round((speed*(from > to ? from-to : to-from)/100)/50);
		o.current_step=0;
		o.id=this.objects.length;

		o.interval=window.setInterval('opacity._fade_step(opacity.objects['+o.id+']); ', 50);

		// insert parameter object
		obj.opacity_fade_parameters=o;
		this.objects.push(obj);

		// make the first step
		//this._fade_step(obj);
	},

	/**
	 * (private) to manage one step of a fade
	 *
	 * @param mixed obj A dom object or the id attribute of a dom object
	 *
	 * @version  1.0
	 *
	 * @author   Helmut Wandl <helmut@wandls.net>
	 */
	_fade_step:function(obj)
	{
		// get parameter object
		var o=obj.opacity_fade_parameters;
		o.current_step++;

		// get new opacity
		var newopacity=o.start_opacity+(Math.round(o.difference*o.current_step/o.steps));

		// set opacity if isn't finished
		if (o.steps > o.current_step)
			return this.set(obj, newopacity);

		// set final opacity and remove interval
		this.set(obj, o.final_opacity);
		window.clearInterval(o.interval);

		// run eval string
		if (o.evalstr != "") eval(o.evalstr);
	},

	/**
	 * to set opacity of a dom object
	 *
	 * @param mixed obj A dom object or the id attribute of a dom object
	 * @param int opacity The new opacity as an integer (0-100)
	 *
	 * @version  1.0
	 *
	 * @author   Helmut Wandl <helmut@wandls.net>
	 */
	set:function(obj, opacity)
	{
		if (!(obj=this._getobject(obj)) || !(opacity >= 0) || !(opacity <= 100)) return false;

		// set visibility attribute
		if (opacity == 0) obj.style.visibility='hidden';
		else obj.style.visibility='visible';

		// W3C
		if (obj.style.opacity != undefined) obj.style.opacity=(opacity/100);

		// MOZ
		if (obj.style.MozOpacity != undefined) obj.style.MozOpacity=(opacity/100);

		// IE
		if (obj.filters != undefined)
		{
			if (obj.filters.alpha != undefined) obj.filters.alpha.opacity=opacity;
			else obj.style.filter+=' Alpha(opacity='+opacity+')';
		}
	},

	/**
	 * to get opacity of a dom object
	 *
	 * @param mixed obj A dom object or the id attribute of a dom object
	 *
	 * @version  1.0
	 *
	 * @author   Helmut Wandl <helmut@wandls.net>
	 */
	get:function(obj)
	{
		if (!(obj=this._getobject(obj))) return false;

		var opacity=false;

		if (obj.style.visibility == 'hidden') return 0;

		// W3C
		if (obj.style.opacity != undefined) opacity=Math.round((obj.style.opacity == '' ? 1 : obj.style.opacity)*100);

		// MOZ
		if (obj.style.MozOpacity != undefined) opacity=Math.round((obj.style.MozOpacity == '' ? 1 : obj.style.MozOpacity)*100);

		// IE
		if (obj.filters != undefined)
		{
			if (obj.filters.alpha && obj.filters.alpha.opacity != undefined) opacity=obj.filters.alpha.opacity;
			else opacity=100;
		}

		return opacity;
	},

	/**
	 * (private) to get the dom object of a given id or the object itself
	 *
	 * @param mixed obj A dom object or the id attribute of a dom object
	 *
	 * @return object The dom object
	 *
	 * @version  1.0
	 *
	 * @author   Helmut Wandl <helmut@wandls.net>
	 */
	_getobject:function(obj)
	{
		if (typeof obj == 'object') return obj;
		if (typeof obj == 'string') return document.getElementById(obj);
		return false;
	}
};
