/*
  Objeto EFFECT
  Suporte: IE, FF, NN, Chrome, Opera, Safari
  Dependência: engine.js
  Autor: Fábio Luz Nogueira
  Data: 26/DEZ/2008
*/
var EFFECT={};
EFFECT.resize=function(el, r1, r2, f1, f2)
{
	function executeResize(el, f1, f2)
	{		
		var stoped=false;
		el.itop=el.itop+el.stepTop;
		el.ileft=el.ileft+el.stepLeft;
		el.iwidth=el.iwidth+el.stepWidth;
		el.iheight=el.iheight+el.stepHeight;
		
		el.style.top    = el.itop;
		el.style.left   = el.ileft;
		el.style.width  = el.iwidth;
		el.style.height = el.iheight;	
		
		if (el.stepTop<0) if (el.itop<=el.endTop) stoped=true;
		if (el.stepTop>0) if (el.itop>=el.endTop) stoped=true;
		
		if (el.stepLeft<0) if (el.ileft<=el.endLeft) stoped=true;
		if (el.stepLeft>0) if (el.ileft>=el.endLeft) stoped=true;
		
		if (el.stepWidth<0) if (el.iwidth<=el.endWidth) stoped=true;
		if (el.stepWidth>0) if (el.iwidth>=el.endWidth) stoped=true;
		
		if (el.stepHeight<0) if (el.iheight<=el.endHeight) stoped=true;
		if (el.stepHeight>0) if (el.iheight>=el.endHeight) stoped=true;
		
		if (stoped)
		{
			if (f1) f1();
		}
		else
		{
			if (f2) f2();
			setTimeout(function(){executeResize(el, f1, f2)}, 20)
		}
	}
	
	el.step = 25;
	
	el.startTop=r1.top;
	el.startLeft=r1.left;
	el.startWidth=r1.width;
	el.startHeight=r1.height;
	
	el.endTop=r2.top;
	el.endLeft=r2.left;
	el.endWidth=r2.width;
	el.endHeight=r2.height;
	
	var ma=0, mi=0;
	var t=el.endTop-el.startTop;       ma=t; mi=t;
	var l=el.endLeft-el.startLeft;     if (l>ma) ma=l; if (l<mi) mi=l;
	var w=el.endWidth-el.startWidth;   if (w>ma) ma=w; if (w<mi) mi=w;
	var h=el.endHeight-el.startHeight; if (h>ma) ma=h; if (h<mi) mi=h;
	var	p=el.step*100/ma;
			
	el.stepTop   = t*p/100;
	el.stepLeft  = l*p/100;
	el.stepWidth = w*p/100;
	el.stepHeight= h*p/100;
	
	el.style.position = 'absolute';
	el.itop=el.style.top    = el.startTop;
	el.ileft=el.style.left   = el.startLeft;
	el.iwidth=el.style.width  = el.startWidth;
	el.iheight=el.style.height = el.startHeight;
	executeResize(el, f1, f2);
};

EFFECT.move=function(el, r1, r2, f1, f2)
{
	function executeMove(el, f1, f2)
	{		
		var stoped=true;
			
		if (stoped)
		{
			if (f1) f1();
		}
		else
		{
			if (f2) f2();
			setTimeout(function(){executeMove(el, f1, f2)}, 20)
		}
	}
	
	el.step = 25;
	
	el.startTop=r1.top;
	el.startLeft=r1.left;
	
	el.endTop=r2.top;
	el.endLeft=r2.left;
	
	//verifica a direção
	el.dtop = (r1.top<r2.top) ? 'bottom' : 'top';
	el.dleft= (r1.left<r2.left) ? 'right' : 'left';
	
	var ma=0, mi=0;
	var t=el.endTop-el.startTop;       ma=t; mi=t;
	var l=el.endLeft-el.startLeft;     if (l>ma) ma=l; if (l<mi) mi=l;
	var	p=el.step*100/ma;
			
	el.stepTop   = t*p/100;
	el.stepLeft  = l*p/100;
		
	el.style.position = 'absolute';
	//el.itop=el.style.top    = el.startTop;
	//el.ileft=el.style.left   = el.startLeft;
	executeMove(el, f1, f2);
};

EFFECT.fade=function(el, start, end, f1, f2)
{  
	function executeFade(el, f1, f2)
	{
	  var stoped=false;
		if (start>end)
		{
			el.p-=el.step;
			if (el.p<=end){ el.p=end; stoped=true;}
		}
		else
		{
			el.p+=el.step;
			if (el.p>=end){ el.p=end; stoped=true;}
		}		
		EFFECT.setOpacity(el, el.p);
		if (stoped)
		{
			if (f1)
			  f1();
		}
		else
		{
			if (f2)
			  f2();
			setTimeout(function(){executeFade(el, f1, f2)}, el.sleep);
		}
	}
	
	el.p=start;
	el.sleep = el.sleep ? el.sleep : 50;
	el.step  = el.step  ? el.step  : 0.2;
	el.style.display='';
	EFFECT.setOpacity(el, el.p);
	setTimeout(function(){executeFade(el, f1, f2)}, el.sleep);
};

/* float between 0.0 (transparent) and 1.0 (opaque) */
EFFECT.setOpacity=function(node, opacity, dontFixOpacity)
{
	if(!dontFixOpacity){
		if( opacity >= 1.0){
			if(Browser.ie){
				node.style.filter = "";
				//node.clearOpacity(node);
				return;
			}else{
				opacity = 0.999999;
			}
		}else if( opacity < 0.0){ opacity = 0; }
	}

  if(Browser.ie){
		if(node.nodeName.toLowerCase() == "tr"){
			// FIXME: is this too naive? will we get more than we want?
			var tds = node.getElementsByTagName("td");
			for(var x=0; x<tds.length; x++){
				tds[x].style.filter = "Alpha(Opacity="+opacity*100+")";
			}
		}
		node.style.filter = "Alpha(Opacity="+opacity*100+")";
	}else if(Browser.moz){
		node.style.opacity = opacity; // ffox 1.0 directly supports "opacity"
		node.style.MozOpacity = opacity;
	}else if(Browser.safari){
		node.style.opacity = opacity; // 1.3 directly supports "opacity"
		node.style.KhtmlOpacity = opacity;
	}else{
		node.style.opacity = opacity;
	}
};
