/*
Thiago amaral soalheiro

Adekz.com

Copyright © 2006.

É expressamente proibida a copia parcial ou total de qualquer parte desse arquivo.

Versao 2.0 - 23/10/2006*/



function isIE(){
	var browser=navigator.appName;
	if (browser=="Microsoft Internet Explorer")
		return true;
	return false;
}
function isFF(){
	return !isIE();
}
//getElement por ID
function $(n){return document.getElementById(n); }
//style
function $sty(n){return $(n).style; }
//valor
function $val(n){ return $(n).value; }
//innerHTML
function $inn(n){return $(n).innerHTML; }

function realTopIE(el){
	var tp = 0;
	while(el.offsetParent){ tp += el.offsetTop; el = el.offsetParent; }
	return tp;
}
function realLeftIE(el){
	var tp = 0;
	while(el.offsetParent){ tp += el.offsetLeft; el = el.offsetParent; }
	return tp;
}


function Vetor(){
	this.V = Array();
}




function appendDiv(div, id){
	$(id).appendChild(div);
}


function removeDiv(div, id){
	$(id).removeChild(div);
}

function newDiv(id){
	var x = document.createElement("DIV");
	x.setAttribute("id", id);
	x.style.border='solid red 1px;';
	return x;
}
function createDiv(id){
	var x = document.createElement("DIV");
	x.setAttribute("id", id);
	return x;
}

function newDivJanela(id, w, h, x, y){
	var xt = document.createElement("DIV");
	xt.setAttribute("id", id);
	xt.style.border='solid blue 1px;';
	xt.style.position='absolute';

	var xt2 = new Div(xt);
	xt2.setW(w);
	xt2.setH(h);
	xt2.setX(x);
	xt2.setY(y);
	return xt;
}


function Janela(divid, w, h, x, y){
	this.type='janela';
	this.div = newDivJanela(divid, w, h, x, y);
	this.cont = new Array();
	this.n_cont=0;

	this.addContainer = function(cont){
		if(cont.type!='container') alert("FAZ DIREITO");
		this.cont[this.n_cont++] = cont;
		cont.pai = this;
		this.div.appendChild(cont.div);
	}
	this.dropContainer =function(cont){
		if(cont.type!='container') alert("FAZ DIREITO");
		if(cont.pai != this) return;
		var temp=0;
		for(var i=0; i<this.n_cont-1; i++){
			if(this.cont[i]==cont) temp=1;
			if(temp==1){
				this.cont[i]=this.cont[i+1];
			}
		}
		this.n_cont--;
		this.div.removeChild(cont.div);
	}

}

function Container(divid){
	this.type='container';
	this.div = newDiv(divid);
	this.pai = null;
	this.fil = new Array();
	this.n=0;

	this.addContainer = function(cont){
		if(cont.type!='container') alert("FAZ DIREITO");
		this.fil[this.n++] = cont;
		cont.pai = this;
		this.div.appendChild(cont.div);
	}
	this.setConteudo = function(cont){
		this.div.innerHTML = cont;
	}
	this.dropContainer =function(cont){
		if(cont.type!='container') alert("FAZ DIREITO");
		if(cont.pai != this) return;
		var temp=0;
		for(var i=0; i<this.n-1; i++){
			if(this.fil[i]==cont) temp=1;
			if(temp==1){
				this.fil[i]=this.fil[i+1];
			}
		}
		this.n--;
		this.div.removeChild(cont.div);
	}
	this.dropContainerAll =function(){
		for(var i=0; i<this.n; i++){
			this.div.removeChild(this.fil[i].div);
		}
		this.n=0;
	}
}


function executa(){

	var c = new Janela('pai');
	
 
	var d = new Container('filho');
	d.setConteudo("Filho");
	c.addContainer(d);

	var e = new Container('subfilho');
	e.setConteudo("SubFilho");
	c.addContainer(e);

	c.dropContainer(d);

	appendDiv(c.div, 'ADK_main');

	/*
	var handle=Array();
	var c = new Container('titulo');
	var pai.addContainer(c);
	handle[0]=c;
	var acao1 = function(e){
		alert(e.X);
	}
	Evento.register("comecar_arrastar", pai, acao1, handle);
	Evento.register("terminar_arrastar", div, acao2);
	Evento.register("arrastando", div, acao3);
	Evento.register("click", div, funcao);

*/

}




function Div(div){
		this.ob = div;
		if(!this.ob.style||this.ob.style==null) this.ob.style="";
		this.setAbsolute = function (){			this.ob.style.position='absolute';		}
		this.getX = function (){return isIE() ? parseInt(realLeftIE(this.ob)):parseInt(this.ob.offsetLeft);}
		this.getY = function (){return isIE() ? parseInt(realTopIE(this.ob)):parseInt(this.ob.offsetTop);}

		this.getIX = function (){return parseInt(this.ob.style.left);}
		this.getIY = function (){return parseInt(this.ob.style.top);}

		this.getW = function (){return parseInt(this.ob.offsetWidth);		}
		this.getH = function (){return parseInt(this.ob.offsetHeight);		}
		this.setX = function (v){			this.ob.style.left=Math.round(v)+'px';		}
		this.setY = function (v){			this.ob.style.top=Math.round(v)+'px';		}
		this.setW = function (v){			this.ob.style.width=Math.round(v)+'px';		}
		this.setH = function (v){			this.ob.style.height=Math.round(v)+'px';		}
		this.catX = function (v){			this.ob.style.left=this.getX()+Math.round(v);		}
		this.catY = function (v){			this.ob.style.top=this.getY()+Math.round(v);		}
		this.catW = function (v){			this.ob.style.width=this.getW()+(v);		}
		this.catH = function (v){			this.ob.style.height=this.getH()+(v);		}
		this.setAlpha = function (v){
			if(v<0) v=0; if(v>100) v=100;
			if(!isIE()) this.ob.style.opacity = v/100; 
			else this.ob.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+v+")";
		}
		this.getAlpha = function (){
			if(!isIE()){
				if(!this.ob.style.opacity) return 100;
				if(this.ob.style.opacity*1<0) return 0;				
				if(this.ob.style.opacity*1>100) return 100;				
				return parseInt(parseFloat(this.ob.style.opacity)*100);
			}else{
				try{
					var nro = this.ob.style.filter;
					if (nro.indexOf("=")==-1){ return 100; }
					var x = nro.indexOf("="); var n = parseInt(nro.substr(x+1, nro.indexOf(")", x))); 
					if (n>100) return 100; if (n<0) return 0; return n;
				}catch(e){ return 100; }
			}
		}
		this.getZ = function (){ if(!this.ob.style.zIndex||this.ob.style.zIndex==null) return 0; return parseInt(this.ob.style.zIndex);		}
		this.setZ = function (v){			this.ob.style.zIndex=Math.round(v);		}
		this.show= function(){ this.ob.style.display=''; }
		this.hide= function(){ this.ob.style.display='none'; }
		this.showHide = function(){ if(this.ob.style !=null && this.ob.style.display!=null && this.ob.style.display != 'none') this.hide(); else this.show();  }
		this.visivel = function(){ try{ if ( this.ob.style.display=='none') return false; }catch(er){} return true;}
		this.hover= function(){
			if (eventer.x >= this.getX() && eventer.x <= this.getX() + this.getW())
				if (eventer.y >= this.getY() && eventer.y <= this.getY() + this.getH()){
					return true;
				}
			return false;
		}
		this.hoverDiv= function(div){
			if (div.getX() >= this.getX() && div.getX()  <= this.getX() + this.getW())
				if (div.getY() >= this.getY() && div.getY() <= this.getY() + this.getH()){
					return true;
				}

			if (div.getX()+div.getW() >= this.getX())
				if (div.getY()+div.getH() >= this.getY() && (div.getY()+div.getH() >= this.getY() + this.getH()||div.getY()+div.getH() <= this.getY() + this.getH())){
					return true;
				}

			return false;
		}

		this.hoverRect = function(x0,y0,x,y){
			if(this.getX()>= x0 &&  this.getW()+this.getX() <= x)
				if(this.getY()>= y0 &&  this.getH()+this.getY() <= y)
					return true;
			return false;
		}

		this.setClip = function (x,y,w,h){
			this.ob.style.clip = "rect("+parseInt(x)+"px "+parseInt(y)+"px "+parseInt(w)+"px "+parseInt(h)+"px)";
		}
		this.setClipA = function (r){
			this.ob.style.clip = "rect("+parseInt(r[0])+"px "+parseInt(r[1])+"px "+parseInt(r[2])+"px "+parseInt(r[3])+"px)";
		}
		this.getClip = function (){
			var ret = Array(4);
			if (this.ob.style.clip){
				var tx = this.ob.style.clip;
				var v1 = tx.split("ect("); tx = v1[1];
				var v1 = tx.split(")"); tx = v1[0];
				var v1 = tx.split(" "); 
				ret[0] = parseInt(v1[0]); ret[1] = parseInt(v1[1]); ret[2] = parseInt(v1[2]); ret[3] = parseInt(v1[3]); 
			}else{
				ret[0]=0; ret[3]=0; ret[1]=this.getW(); ret[2]=this.getH();
			}
			return ret;
		}
		this.append = function (div){
			this.ob.appendChild(div);
		}


	}

	function Animacao(ob){
		this.ob  = new Div(ob);
		this.handle_move=null;
		this.handle_resize=null;
		this.handle_alpha=null;



		this.resize_aux = function (xo, x, t, yo, y, max, pos){
			if (t>max)	{ this.handle_resize=null; if (pos != null){ pos(this);}  return; }
			this.ob.setW(this.funcao(xo, x, max, t));
			this.ob.setH(this.funcao(yo, y, max, t));
			var obj = this;
			//this.ob.ob.innerHTML = this.ob.getW()+" x " + this.ob.getH();
			if (this.handle_resize!=null)
				this.handle_resize=setTimeout(function(){ obj.resize_aux(xo, x, t+1, yo, y, max, pos); }, 40);
		}
		this.move_aux = function (xo, x, t, yo, y, max, pos){
			if (t>max)	{this.handle_move=null; eventer.enable(); if (pos != null){  pos(this);}  return; }
			this.ob.setX(this.funcao(xo, x, max, t));
			this.ob.setY(this.funcao(yo, y, max, t));
			var obj = this;
			//this.ob.ob.innerHTML = this.ob.getX()+" x " + this.ob.getY();
			if (this.handle_move!=null)
				this.handle_move=setTimeout(function(){ obj.move_aux(xo, x, t+1, yo, y, max, pos); }, 40);
		}
		this.clip_aux = function (orig, dest, t, max, pos){
			if (t>max){this.handle_clip=null; if (pos != null){  pos(this);}  return; }
			var to = Array(4);
			for(var i=0; i<4; i++)	to[i] = parseInt(this.funcao(orig[i], dest[i], max, t));	
			//alert(to);
			this.ob.setClip(to[0], to[1], to[2], to[3]);
			var obj = this;
			//this.ob.ob.innerHTML = this.ob.getX()+" x " + this.ob.getY();
			if (this.handle_clip!=null)
				this.handle_clip=setTimeout(function(){ obj.clip_aux(orig, dest, t+1, max, pos); }, 40);
		}
		this.alpha_aux = function (xo, x, t, max, pos){
			if (t>max)	{ this.handle_alpha=null; if (pos != null){ pos(this);}  return; }
			this.ob.setAlpha(this.funcao(xo, x, max, t));
			var obj = this;
			//this.ob.ob.innerHTML = this.ob.getAlpha();
			if (this.handle_alpha!=null)
				this.handle_alpha=setTimeout(function(){ obj.alpha_aux(xo, x, t+1, max ,pos); }, 40);
		}

		/** width, height , velocidade em pixels/40mseg, funcao depois do resize*/
		this.resize = function (x, y, t, pos){
			if (this.handle_resize!=null){ clearTimeout(this.handle_resize); this.handle_resize=null; }
			var segm = (Math.abs(this.ob.getW()-x) <Math.abs(this.ob.getH()-y)) ? this.ob.getH()-y : this.ob.getW()-x;
			var tt = parseInt(Math.abs(segm) / t); 
			if(tt==0) tt=1;
			var xo = this.ob.getW();
			var yo = this.ob.getH();
			var obj = this;
			this.handle_resize=setTimeout(function(){ obj.resize_aux(xo, x, 1, yo, y, tt, pos); }, 40);
		}

		/** left, top , velocidade em pixels/40mseg, funcao depois do move*/
		this.move = function (x, y, t, pos){
			if (this.handle_move!=null){ clearTimeout(this.handle_move); this.handle_move=null; }
			else { eventer.disable(); }
			var segm = (Math.abs(this.ob.getX()-x) <Math.abs(this.ob.getY()-y)) ? this.ob.getY()-y : this.ob.getX()-x;
			var tt = parseInt(Math.abs(segm) / t); 
			if(tt==0) tt=1;
			var xo = this.ob.getX();
			var yo = this.ob.getY();
			var obj = this; 
			this.handle_move=setTimeout(function(){ obj.move_aux(xo, x, 1, yo, y, tt, pos); }, 40);
		}

		/** alpha, velocidade em pts/40mseg, funcao depois do alpha*/
		this.alpha = function (a, t, pos){
			if (this.handle_alpha!=null){ clearTimeout(this.handle_alpha); this.handle_alpha=null; }
			var al = this.ob.getAlpha();
			var dx = Math.abs(al - a);
			var tt = parseInt(Math.abs(dx) / t); 
			if(tt==0) tt=1;
			var obj = this;
			this.handle_alpha=setTimeout(function(){ obj.alpha_aux(al, a, 1, tt, pos); }, 40);  
		}

		//parabola
		this.funcao = function (xo, x, max, t){ 
			var p = 5/18;
			var d = (x+xo)/2;
			var c = d-xo+p*p*(xo-x);
			var e = max*p*(1-p);
			var b = c/e;
			var a = (x - max*b -xo)/(max*max);			
			var y = a*t*t+b*t+xo;
			return y;
		}

		//parabolaINV
		this.funcao3 = function (xo, x, max, t){ 
			var p = 13/18;
			var d = (x+xo)/2;
			var c = d-xo+p*p*(xo-x);
			var e = max*p*(1-p);
			var b = c/e;
			var a = (x - max*b -xo)/(max*max);			
			var y = a*t*t+b*t+xo;
			return y;
		}


		//reta
		this.funcao2 = function (xo, x, max, t){ 
			var d = (x-xo)/max;
			var y = xo+d*t;
			return y;
		}

		this.aparecer = function (freq, pos){
			if (this.ob.visivel()) return ;
			this.ob.setAlpha(0);
			this.ob.show();
			this.alpha(100, freq, pos);
		}
		this.desaparecer = function (freq, pos){
			if (!this.ob.visivel()) return ; 
			var obj=this;
			var tm = function(ob){
				obj.ob.hide();
				if(pos!=null)	pos(ob);
			}
			this.alpha(0, freq, tm);
		}

		this.mover_rel = function (x, y, t, pos){
			this.move(this.ob.getX()+x, this.ob.getY()+y, t, pos);
		}
		this.mover_fix= function (x, y, pos){
			this.ob.setX(x); this.ob.setY(y);  if (pos!=null) pos(this);
		}
		this.mover_rel_fix= function (x, y, pos){
			this.ob.setX(this.ob.getX()+x); this.ob.setY(this.ob.getY()+y);  if (pos!=null) pos(this);
		}

		this.tremerH = function (freq, nro, dir, pos){
			if(nro<=1){	if(pos!=null) pos(this);	return;	}
			var dep = function(ob){ ob.tremerH(freq, dir==0 ? nro : nro-2, 1-dir, pos); 	}
			this.mover_rel((dir==0) ? nro : -nro, 0, freq, dep);
		}

		this.tremerV = function (freq, nro, dir, pos){
			if(nro<=1){	if(pos!=null) pos(this);	return;	}
			var dep = function(ob){	ob.tremerV(freq, dir==0 ? nro : nro-2, 1-dir, pos); 	}
			this.mover_rel(0, (dir==0) ? nro : - nro,  freq, dep);
		}

		/** Array(top, right, bottom, left), velocidade em pixels/40mseg, funcao depois do clipping*/
		this.clipping = function (dest, t, pos){
			if (this.handle_clip!=null){ clearTimeout(this.handle_clip); this.handle_clip=null; }
			var orig = this.ob.getClip();
			var segm =0;
			for(var i=0; i<4; i++) if (segm<Math.abs(orig[i]-dest[i])) segm = Math.abs(orig[i]-dest[i]);

			var tt = parseInt(Math.abs(segm) / t); 
			if(tt==0) tt=1;
			var obj = this; 
			this.handle_clip=setTimeout(function(){ obj.clip_aux(orig, dest, 1, tt, pos); }, 40); 
		}	


	}
	function DO(){		
		var ob = document.getElementById('nm');
		var an = new Animacao(ob);
		an.move(200, 220, 5, function(o){ o.resize(150, 130, 5, function(p){ p.alpha(10, 5); });});
	}
	function DO1(){		
		var ob = document.getElementById('nm');
		var an = new Animacao(ob);
		an.aparecer(2);
	}
	function DO2(){		
		var ob = document.getElementById('nm');
		var an = new Animacao(ob);
		an.desaparecer(2);
	}
	function DO3(){		
		var ob = document.getElementById('nm');
		var an = new Animacao(ob);
		an.tremerH(5, 10, 1);
	}

	
	function getSelectCampo(tabela, selec, selec2){

		//alert(tabela);return;

		var ad = new AjaxData();
		ad.add("acao", "selectCampo");
		ad.add("tabela", tabela);

		var toDo = function(o){

			var opt = o.getA('option');
			
			var ret = "<select name='join_campo' id='join_campo'>\n";

			var ret2 = "<select name='join_mostra' id='join_mostra'>\n";

			if(opt!=null && opt.length>0){
				for(var i=0; i<opt.length; i++){
					ret += "<option value='"+opt[i]+"'  "+(opt[i]==selec?"selected":"")+">"+opt[i]+"</option>";
					ret2 += "<option value='"+opt[i]+"' "+(opt[i]==selec2?"selected":"")+">"+opt[i]+"</option>";
				}
			}
			
			ret += "</select>";
			ret2 += "</select>";

			$('SelectCampoRepos').innerHTML = ret;

			$('SelectCampoRepos2').innerHTML = ret2;
			 

		}
		//DEBUGANDO = 1;
		linkwork("ajax", "load.php", ad, toDo);	
	}
