﻿function setOpacity(obj, valor){
	try{
		obj.style.filter = "alpha(opacity=" + valor * 100 + ")";
		obj.style.opacity = valor;
		obj.style.MozOpacity = valor;
		obj.style.KhtmlOpacity = valor;
	}catch(e){
		showError(e, "setOpacity");
	}
}

function getOpacity(obj){
	if (obj.style.opacity) return obj.style.opacity;
	else if(obj.style.MozOpacity) return obj.style.MozOpacity;
	else if(obj.style.KhtmlOpacity) return obj.style.KhtmlOpacity;
	else if(obj.style.filter) return parseInt(obj.style.filter.substr(obj.style.filter.indexOf("=") + 1)) / 100;
	else return 1;
}

function fadeIn(obj, valor, timeout, fator, onfinish){
	try{
	    if (obj == "undefined") return;
	    if ((obj._ft) && (obj._ft != null)) clearTimeout(obj._ft);
		if (valor == null) valor = 0;
		if (timeout == null) timeout = 10;
		if (fator == null) fator = 0.05;
		
		if (Math.abs(parseFloat(getOpacity(obj)) - fator) <= valor){
			if (onfinish != null){ onfinish(obj);}
			setOpacity(obj, valor);
			obj._ft = null;
		}else{
			setOpacity(obj, parseFloat(getOpacity(obj)) - fator);
			document.objfade = obj;
			obj._ft = setTimeout(function(){fadeIn(obj, valor, timeout, fator, onfinish)}, timeout);
		}
	}catch(e){
		showError(e, "fadeIn");
	}
}
function fadeOut(obj, valor, timeout, fator, onfinish){
	try{
	    if (obj == "undefined") return;
	    if ((obj._ft) && (obj._ft != null)) clearTimeout(obj._ft);
		if (valor == null) valor = 1;
		if (timeout == null) timeout = 10;
		if (fator == null) fator = 0.05;
		
		if (parseFloat(getOpacity(obj)) + fator >= valor){
			if (onfinish != null){ onfinish(obj);}
			setOpacity(obj, valor);
			obj._ft = null;
		}else{
			setOpacity(obj, parseFloat(getOpacity(obj)) + fator);
			document.objfade = obj;
			obj._ft = setTimeout(function(){fadeOut(obj, valor, timeout, fator, onfinish)}, timeout);
		}
	}catch(e){
		showError(e, "fadeOut");
	}
}