/**
 * @author	Nicholas Almeida
 * @version	2.0
*/

var fxFolded = {};
// -------------------------------------------------- Easing
function easing(ini,end,speed){
	return ini+=(end-ini)/speed;
}
function getHeight(w){
	return $(w).style.height.slice(0, $(w).style.height.length-2);
}
// -------------------------------------------------- FADE
var isRunningAlpha = [];
function fxAlpha(w,i,e,f,t){
	if(isRunningAlpha[w]) return;
	clearInterval(aplhaInt);
	aplhaInt = null;
	var spd = 30;
	t = (t*2) || 4;
	var ini = i;
	var end = e;
	var aplhaInt = setInterval(
		function(){
			isRunningAlpha[w] = true;
			ini = easing(ini,end,t);
			if((Math.round(ini*100)/100) < end){ // Show
				if(ini >= end) {
					ini = end;
					clearInterval(aplhaInt);
					isRunningAlpha[w] = false;
					fxFolded[w] = false;
					if(f)f();
				};
			} else{ // Hide
				if (ini <= end || ini < .05) {
					ini = end;
					clearInterval(aplhaInt);
					isRunningAlpha[w] = false;
					fxFolded[w] = true;
					if(f)f();
				}
			};
			if (window.ActiveXObject && $(w)) { // IE
				$(w).style.filter = "alpha(opacity=" + ini*100 + ")";
			} else if($(w)){ // FF
				$(w).style.opacity = ini;
			};
		}
	, spd);
}
function fadeInDiv(w,f){
	if(is.ie && is.mac){
		sDiv(w);
		if(f)eval(f)();
		return;
	};
	fxAlpha(w,0,1,f);
};
function fadeOutDiv(w,f){
	if(is.ie && is.mac){
		hDiv(w);
		if(f)eval(f)();
		return;
	};
	fxAlpha(w,1,0,f);
};
function highlightDiv(w,f){
	fxAlpha(w,1,0,function(){fxAlpha(w,0,1,function(){fxAlpha(w,1,0,function(){fxAlpha(w,0,1,f, 15);},15)}, 15);},15);
};
function tgFadeDiv(w,f){
	if(fxFolded[w] == false || !fxFolded[w]) fadeOutDiv(w,f);
	else fadeInDiv(w,f);
};

// -------------------------------------------------- FOLD
var isRunningFold = {};
function changeHeight(w, closeDiv, f){
	if(isRunningFold[w]) return;
	var foldInt = setInterval(
		function(){
			isRunningFold[w] = true;
			$(w).style.overflow = 'hidden';
			if(closeDiv){ // Fecha
				$(w).style.height = easing($(w).offsetHeight, 0, 3.7531597777) + 'px';
				if($(w).offsetHeight<=1){
					$(w).style.height = '1px';
					hDiv(w);
					clearInterval(foldInt);
					fxFolded[w] = false;
					isRunningFold[w] = false;
					if(f)eval(f)();
				}
			} else { // Abre
				if($(w).offsetHeight==0) $(w).style.height = '1px';
				sDiv(w);
				if($(w).offsetHeight < $(w).scrollHeight){
					if($(w).offsetHeight == 0) $(w).style.height = '1px';
					$(w).style.height = easing($(w).offsetHeight+.7, $(w).scrollHeight, 3.7531597777) + 'px';
					if(Math.abs(getHeight(w)-$(w).scrollHeight)<=3.7){
						$(w).style.height = $(w).scrollHeight+'px';
						clearInterval(foldInt);
						fxFolded[w] = true;
						isRunningFold[w] = false;
						if(f)eval(f)();
					};
				}else {
					$(w).style.height = $(w).scrollHeight+'px';
					clearInterval(foldInt);
					fxFolded[w] = true;
					isRunningFold[w] = false;
					if(f)eval(f)();
				}
			};
		}
	, 30);
}
function foldDiv(w,f){
	if(is.ie && is.mac){
		if(f)eval(f)();
		return;
	};
	changeHeight(w,true,f);
};
function unfoldDiv(w,f){
	if(is.ie && is.mac){
		if(f)eval(f)();
		return;
	};
	changeHeight(w,false,f);
};
function tgFoldDiv(w,f){
	if(fxFolded[w] == true || ($(w).offsetHeight >1)) {
		foldDiv(w,f);
	} else {
		unfoldDiv(w,f);
	}
};

// -------------------------------------------------- FOLD FADE
function foldFadeDiv(w,f){
	foldDiv(w);
	fadeOutDiv(w, f);
}
function unfoldFadeDiv(w,f){
	unfoldDiv(w);
	fadeInDiv(w, f);
}
function tgFoldFadeDiv(w,f){
	if(fxFolded[w] == true || ($(w).offsetHeight >1)) {
		foldFadeDiv(w,f);
	} else {
		unfoldFadeDiv(w,f);
	}
}

