/* sciTools-Library :: sciEffects.js */

	/*******************************************************************************
	 * Copyright (c) 2007 SCI-Design.at Dr. Günther Rezniczek, 1030 Vienna, Austria
	 *******************************************************************************/ 



function sciEffectsObject()
{
}

sciEffectsObject.prototype.visibleById = function (id,visible) {
	/*******************************************************************************************************************
    function: sciEffects.visibleById

    Description:
		Sets visibility of an element
		id: id of the elemnt
		visible: true = visible, false = hidden
	*********************************************************************************************************************/
	
	var el = document.getElementById(id);
	if (el)
	{
		el.style.display = visible ? "block" : "none";
	}
};

sciEffectsObject.prototype.visibleByClass = function (className,visible) {
	/*******************************************************************************************************************
    function: sciEffects.visibleByClass

    Description:
		Sets visibility of all elements with a certain class
		class: name of the class
		visible: true = visible, false = hidden
	*********************************************************************************************************************/
	
	var els = document.getElementsByClassName(className);
	if (els) 
	{
		for (var i = 0; i < els.length; i++)
		{
			els[i].style.display = visible ? "block" : "none";
		}
	}
};


sciEffectsObject.prototype.mouseFadeInit = function () {
	/*******************************************************************************************************************
    function: sciEffects.mouseFadeInit

    Description:
    	Sets up mouse fade effects on elements with the 
		
			mousefade="out,in,speed"
			
		attribute.
	*********************************************************************************************************************/
	
 	var els = document.getElementsByTagName("*");
	for (var i = 0; i < els.length; i++)
	{
		var el = els[i];
		var mf = el.getAttribute("mousefade");
		if (mf) {
			if (!el.id)
			{
				el.id = "mouseFadeElement_" + i;
			}
			var vals = mf.split(",");
			el.mouseFadeOut = vals[0] ? vals[0] : 80;
			el.mouseFadeOver = vals[1] ? vals[1] : 100;
			el.mouseFadeSpeed = (typeof(vals[2]) != "undefined") ? vals[2] : 500;
			el.onmouseover_PreMouseFade = mf.onmouseover ? mf.onmouseover : function() {};
			el.onmouseout_PreMouseFade = mf.onmouseout ? mf.onmouseout : function() {};
			el.onmouseover = new Function("this.onmouseover_PreMouseFade(); sciEffects.fade(this, this.mouseFadeOver, this.mouseFadeSpeed);");
			el.onmouseout = new Function("this.onmouseout_PreMouseFade(); sciEffects.fade(this, this.mouseFadeOut, this.mouseFadeSpeed);");
			sciEffects.setOpacity(el, el.mouseFadeOut);
		}
	}
};










sciEffectsObject.prototype.getOpacity = function(id) {
	/*******************************************************************************************************************
    function: sciEffects.getOpacity

    Description:
		Gets the opacity of an element.
		id: The id of the element (usually an <img> or <div>).
		Returns: The opacity of the element (in %).
	*********************************************************************************************************************/

	var obj = (typeof(id) == "string") ? document.getElementById(id) : id;
	
	if (obj) {
		var o = obj.style.opacity;
		if (typeof(o) == "undefined") {
			return 100;
		}
		else if (o!= "0" && o == "") {
			return 100;
		}
		else {
			return o * 100;
		}
	}
	else {
		return 100;
	}
	
};


sciEffectsObject.prototype.setOpacity = function(id, opacity) {
	/*******************************************************************************************************************
    function: sciEffects.setOpacity

    Description:
		Sets the opacity of an element.
		id: The id of the element (usually an <img> or <div>).
		opacity: The opacity (as %, 0-100) the element should be set to.
	*********************************************************************************************************************/

	var obj = (typeof(id) == "string") ? document.getElementById(id) : id;
	
	if (obj) {
		obj.style.opacity = opacity / 100;
		obj.style.MozOpacity = opacity / 100;
		obj.style.KhtmlOpacity = opacity / 100;
		obj.style.filter = "alpha(opacity=" + opacity + ")";
	}
};


sciEffectsObject.prototype.fade = function(id, targetOpacity, speed) {
	/*******************************************************************************************************************
    function: sciEffects.fade

    Description:
		Fades an element.
		id: The id of the element (usually an <img> or <div>).
		targetOpacity: The opacity the element should have.
		speed: The time in milliseconds a fade from 0% to 100% should take.
	*********************************************************************************************************************/

	var obj = (typeof(id) == "string") ? document.getElementById(id) : id;
	
	if (obj) {
		var v = Math.round(speed / 100);
		var t = 0;
		var startOpacity = sciEffects.getOpacity(obj);
		id = obj.id;
		
		if (startOpacity > targetOpacity) {
			for (var i = startOpacity; i >= targetOpacity; i--) {
				setTimeout("sciEffects.setOpacity('" + id + "', " + i + ")", v * t);
				t++;
			}
		}
		else if (startOpacity < targetOpacity) {
			for (var i = startOpacity; i <= targetOpacity; i++) {
				setTimeout("sciEffects.setOpacity('" + id + "', " + i + ")", v * t);
				t++;
			}
		}
	}
};


sciEffectsObject.prototype.toggleOpacity = function (id, speed) {
	/*******************************************************************************************************************
    function: sciEffects.toggleOpacity

    Description:
		Fades an element to 0 or 100%, depending on current state.
		id: The id (or object reference) of the element (usually an <img> or <div>).
		speed: The time in milliseconds a fade from 0% to 100% should take.
	*********************************************************************************************************************/

	var obj = (typeof(id) == "string") ? document.getElementById(id) : id;
	
	if (obj) {
		if (sciEffects.getOpacity(obj) == 0) {
			sciEffects.fade(obj, 100, speed);
		}
		else {
			sciEffects.fade(obj, 0, speed);
		}
	}
};



sciEffectsObject.prototype.crossfade = function (divId, imgId, imgUrl, speed) {
	/*******************************************************************************************************************
    function: sciEffects.crossfade

    Description:
		Fades an image into another.
		divId: The id (or object reference) of the <div> containing the <img>.
		imgId: The id (or object reference) of the <img>.
		imgUrl: The URL of the image to fade to.
		speed: The time in milliseconds a fade from 0% to 100% should take.
	*********************************************************************************************************************/
	
	var objDiv = (typeof(divId) == "string") ? document.getElementById(divId) : divId;
	var objImg = (typeof(imgId) == "string") ? document.getElementById(imgId) : imgId;
	
	if (objDiv && objImg) {
		objDiv.style.backgroundImage = "url(" + objImg.src + ")";
		objDiv.style.backgroundRepeat = "no-repeat";
		sciEffects.setOpacity(objImg, 0);
		objImg.src = imgUrl;
		sciEffects.fade(objImg, 100, speed);
	}
};

var sciEffects = new sciEffectsObject;