/*
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*/


if(document.all){
	document.onmouseover = evento_pressed0;
	document.onmouseout = evento_pressed0;
	document.onkeydown = evento_pressed0;
	document.onkeyup = evento_pressed0;
	document.onkeypress = evento_pressed0;
	document.onmouseup = evento_pressed0;
	document.onmousedown = evento_pressed0;
	document.onmousemove = evento_pressed0;
	document.onmouseclick = evento_pressed0;
	document.onchange =  evento_pressed0;
	document.ondblclick =  evento_pressed0;
	document.onclick =  evento_pressed0;

}
else{
	document.onmouseout = evento_pressed1;
	document.onmouseover= evento_pressed1;
	document.onkeydown = evento_pressed1;
	document.onkeyup = evento_pressed1;
	document.onkeypress = evento_pressed1;
	document.onmouseup = evento_pressed1;
	document.onmousedown = evento_pressed1;
	document.onmousemove = evento_pressed1;
	document.onmouseclick = evento_pressed1;
	document.onchange =  evento_pressed1;
	document.ondblclick =  evento_pressed1;
	document.onclick =  evento_pressed1;
	 
}

/****FIREFOX*/
function evento_pressed1(e) {

  var E = Array();	
  E['target']=e.target; //Object destino
  E['id']= E['target'].id ;
  E['type']=e.type; //tipo evento
  E['scrollLeft']=parseInt(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  E['scrollTop']=parseInt(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

  E['x']=e.clientX; //pos. em rel ao topo da tela
  E['y']=e.clientY; //pos. em rel ao topo da tela 
  E['rx']=e.pageX; //pos. em rel ao topo dao documento
  E['ry']=e.pageY;  //pos. em rel ao topo dao documento
//	if(E['type']=='mousedown') alert(E['target'].parentNode.id);
  E['parent']= E['target'].parentNode ? E['target'].parentNode : -1;

try{
  E['px']=E['parent']!=null ? E['rx']-E['parent'].offsetLeft : -1; //em relacao ao container pai 
  E['py']=E['parent']!=null ? E['ry']-E['parent'].offsetTop : -1; //em relacao ao container pai
  /*if(E['type']=='mousedown') alert(E['parent'].id);*/

}catch(erro){}
  E['cx']=E['x']+E['scrollLeft']-E['target'].offsetLeft; //em relacao ao container 
  E['cy']=E['y']+E['scrollTop']-E['target'].offsetTop; //em relacao ao container 

  E['button']=(e.button?e.button:-1);	//botao apertado. (0-esq, 1-meio, 2-dir, -1 nenhum) 
  E['altKey']=e.altKey;
  E['ctrlKey']=e.ctrlKey;
  E['shiftKey']=e.shiftKey;
  E['key']=e.which;
  E['related']=e.relatedTarget;


 if(EventoExecutado(E) ==  -1){ return false;}



if(eventer.movendo!=null){
  if (E['type']=='mousemove')
	return false;
  if (E['type']=='mousedown')
	return false;
}


}

/****IE*/
function evento_pressed0() {
  var e = event;
  var E = Array();	

  E['target']=e.srcElement;
  E['id']= E['target'].id;
  E['type']=e.type;

	

  E['scrollLeft']=parseInt(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  E['scrollTop']=parseInt(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

  E['x']=e.clientX;
  E['y']=e.clientY;  

  E['rx']=e.clientX+E['scrollLeft'];
  E['ry']=e.clientY+E['scrollTop'];


  E['parent']= E['target'].parentNode ? E['target'].parentNode : null;	


  E['cx']=e.offsetX; //em relacao ao container 
  E['cy']=e.offsetY; //em relacao ao container 

try{
  E['px']=E['parent']!=null ? E['cx'] + E['target'].offsetLeft : -1; //em relacao ao container pai 
  E['py']=E['parent']!=null ? E['cy'] + E['target'].offsetTop : -1; //em relacao ao container pai
}catch(erro){}

  E['button']=(e.button==4 ? 1: e.button==1 ? 0: e.button==2 ? 2: -1);	
  E['altKey']=e.altKey;
  E['ctrlKey']=e.ctrlKey;
  E['shiftKey']=e.shiftKey;
  E['key']=e.keyCode;
  E['related']=e.fromElement |e.toElement ;

  if(EventoExecutado(E) == -1) return false;

if(eventer.movendo!=null){
  if (E['type']=='mousemove')
	return false;
  if (E['type']=='mousedown')
	return false;
}


}
/*
function nada(){return false;}
document.oncontextmenu=nada;
*/
function EventoExecutado(E){
	return eventer.trata(E);
}

function EventoAgente(){
 this.movendo=null;
 this.entidade=null;
 this.lastDown = null;
 this.lastUp = null;
 this.x = 0;
 this.y = 0;
 this.disabled=false;

 this.disable=function(){this.disabled=true;}
 this.enable=function(){this.disabled=false;}


 this.Obj = Array();
 this.registrar= function (obj, evento, acao){
	this.Obj[obj+"/"+evento] = acao;	
 }
 this.unregistrar= function (obj, evento){
	this.Obj[obj+"/"+evento] = null;	
 }
 this.trata=function(E){	
		if (this.disabled) return;

		if(E['type']=="mousemove"){
			this.x = E['x'];
			this.y = E['y'];
		}				
		if((E['type']=="mousemove" || E['type']=="mouseup" ) && this.movendo!=null ){
			E['target'] = this.movendo.ob;			
			try{E['id'] = E['target'].id;}catch( xd){E['id']=''; }	
			if(E['type']!="mouseup"){ 			 
				if(this.entidade.onMove!=null){  
					this.entidade.onMove(E);
				}
			}
		}

		if(E['type']=='keydown' && (E['id']=='' || E['id']==null) ){  E['id']='document'; }
		if(E['type']=='keyup' && (E['id']=='' || E['id']==null) ){  E['id']='document'; }
		if(E['type']=='keypress' && (E['id']=='' || E['id']==null) ){  E['id']='document'; }

		if(E['id'] && this.Obj[E['id']+"/"+E['type']]!=null){
			if(E['type']=="mousedown")
				this.lastDown = E['target'];
			if(E['type']=="mouseup"){
				this.lastUp = E['target'];
				if (this.movendo!=null)	{
					objectManager.execHover(this.entidade, "uphere", E);
				}
			}
			return this.Obj[E['id']+"/"+E['type']](E);
		}else{
			if(E['parent']!=null){
				E['target'] = E['parent'];
  				E['parent']= E['target'].parentNode ? E['target'].parentNode : null;
			}else{
				E['target']= null;
			}
			try{
				if(E['parent']!=null){	
					if (isIE()){
						E['cx']=E['rx']-realLeftIE(E['target']); //em relacao ao container 
						E['cy']=E['ry']-realTopIE(E['target']); //em relacao ao container 
						E['px']=E['cx']+E['target'].offsetLeft;//em relacao ao container pai 
						E['py']=E['cy']+E['target'].offsetTop; //em relacao ao container pai
					}else{
						E['cx']=E['rx']-E['target'].offsetLeft; //em relacao ao container 
						E['cy']=E['ry']-E['target'].offsetTop; //em relacao ao container 
						E['px']=E['rx']-E['parent'].offsetLeft;//em relacao ao container pai 
						E['py']=E['ry']-E['parent'].offsetTop; //em relacao ao container pai
					}
				}else{ E['px']=-1; E['py']=-1; }
			}catch(erro){}

			try{E['id'] = E['target'].id;}catch( xd){E['id']=''; }
			if(E['target']!=null) this.trata(E);
		}
 }

}



function WindowControl(){
	this.getW=function(){
		if (!isIE())
			return parseInt(window.innerWidth) + parseInt(document.body.scrollLeft);
		return parseInt(document.body.offsetWidth) + parseInt(document.body.scrollLeft);
	}
	this.getH=function(){
		if (!isIE())
			return parseInt(window.innerHeight) + parseInt(document.body.scrollTop);
		return parseInt(document.body.offsetHeight) + parseInt(document.body.scrollTop);
	}
}



function ObjectManager(){
	this.listener = Array();
	this.object = Array();
	this.n_obj=0;
	this.addObject = function(div){
		this.object[this.n_obj++] = div;
	}	
	this.add = function(id, oq, acao){
		this.addObject(new Div($(id)));
		this.listener[id+"/"+oq] = acao;
	}
	this.execHover = function(ator, oq, E){ 
		var onde=null;
		var ix=-1;
		for(var x in this.object){
			if (this.object[x].hover()){ 
				var tix = this.object[x].getZ();
				if(tix >= ix){
					ix = tix; onde = this.object[x];
				}				
			}
		}		
		var id="";
		if (onde != null){ id = onde.ob.id; }
		if (this.listener[id+"/"+oq]!=null){
			this.listener[id+"/"+oq](E,ator, onde);
		}
	}

}

/***
Manipulador de uma unidade movivel. 
Permite inclusao de handles para fazer o movimento, porem o movimento será pertencente ao div informado.
*/


function Moveable(div, handle){
	this.handle = handle;
	this.div = div;
	this.div.ob.style.position='absolute';

	this.movendo=false;
	this.sx = 0;
	this.sy = 0;
	this.beforeMove=null;
	this.onMove=null;
	this.afterMove=null;
	this.afterMoveSkip=false;


	this.onstart = function(){
		var obj=this;
		var acao = function(E){

			if(obj.movendo==false){
				obj.movendo=true;
				if(obj.beforeMove!=null) obj.beforeMove(E);
			}
			obj.sx = E['rx'];
			obj.sy = E['ry'];	
			if(obj.movendo){
				eventer.movendo=new Div(E['target']);
				eventer.entidade = obj;
			}
		}
		if(handle != null){ 	
			for(var x in this.handle){
				eventer.registrar(this.handle[x].ob.id, "mousedown", acao);
			}			
		}else		
			eventer.registrar(this.div.ob.id,  "mousedown",  acao);
	}
	this.onmove = function(){
		var obj=this;
		var acao = function(E){
			if(obj.movendo){
				//obj.div.ob.style.backgroundColor='red';
				var nx = E['rx'];
				var ny = E['ry'];

				var dx = obj.div.getIX() + nx - obj.sx;
				var dy = obj.div.getIY() + ny - obj.sy;
				if (nx >= 0 && nx <= windowControl.getW())
					obj.div.setX(dx);
				if (ny >= 0 && ny <= windowControl.getH())
					obj.div.setY(dy);
				obj.sx=nx;
				obj.sy=ny;			
			}
		}
		if(handle != null){
			for(var x in this.handle){
				eventer.registrar(this.handle[x].ob.id, "mousemove", acao);
			}			
		}else		
			eventer.registrar(this.div.ob.id,  "mousemove",  acao);
	}
	this.onend = function(){
		var obj=this;
		var acao = function(E){
			if(obj.movendo){
				obj.movendo=false;
				var nx = E['rx'];
				var ny = E['ry'];
				var dx = obj.div.getIX() + nx - obj.sx;
				var dy = obj.div.getIY() + ny - obj.sy;
				if (nx >= 0 && nx <= windowControl.getW())
					obj.div.setX(dx);
				if (ny >= 0 && ny <= windowControl.getH())
					obj.div.setY(dy);
				//obj.div.ob.style.backgroundColor='white';
				eventer.movendo=null;
				if(obj.afterMove!=null){
					if (obj.afterMoveSkip==false) obj.afterMove(E);
					obj.afterMoveSkip=false;
				}
			}
		}
		if(handle != null){	

			for(var x in this.handle){
				eventer.registrar(this.handle[x].ob.id, "mouseup", acao);
			}			
		}else		
			eventer.registrar(this.div.ob.id,  "mouseup",  acao);
	}

	this.remover = function(){
		if(handle != null){
			for(var x in this.handle){
				eventer.unregistrar(this.handle[x].ob.id, "mouseup");
				eventer.unregistrar(this.handle[x].ob.id, "mousemove");
				eventer.unregistrar(this.handle[x].ob.id, "mousedown");
			}			
		}else{		
			eventer.unregistrar(this.div.ob.id,  "mouseup");
			eventer.unregistrar(this.div.ob.id,  "mousemove");
			eventer.unregistrar(this.div.ob.id,  "mousedown");
		}
	}

	this.onstart();
	this.onmove();
	this.onend();
}

 
function moveableId(div, ids){
 
	if(ids==null)	
		return new Moveable(div, null);
	var han = Array();
	for(var x in ids){
		han[x]=new Div($(ids[x]));
	}
	return new Moveable(div, han);
}

/**
Permite agendar execucoes repetitivas de uma mesma funcao function(s, depoisDeCadaPasso). 
s=null force STOP;
**/
function Scheduler(n, acao, arg, after, timer){

	this.size = n;
	this.param = Array();
	this.n_para =0;
	this.next = null;
	this.funcao = null;
	this.cmdLine = null;
	this.handle=null;
	this.block=0;
	this.after =after;
	this.next=arg; 		this.funcao=acao;
	var toexec="obj.next, function(){obj.block=0;}";	
	this.cmdLine = "obj.funcao("+toexec+")";
	var obj=this;		
	this.handle = setInterval(function(){ obj.sync(obj); }, timer ? timer : 40 );

	this.sync=function(obj){
		if(obj.block==1) return;
		obj.block=1;
		if(obj.size > 0){			
			obj.size--;  obj.next = eval(obj.cmdLine);
			if (obj.next==null){ obj.size =0; clearInterval(obj.handle); obj.handle=null;  if (obj.after!=null) obj.after(obj);}
		}else{		
			clearInterval(obj.handle); obj.handle=null; if (obj.after!=null) obj.after(obj);	
		}
	}
}

/**
permite registrar acoes mais complexas e especificas para serem executadas pelo manipulador de eventos em momentos oportunos
*/
var objectManager =  new ObjectManager();

/**
controla informacoes da janela como tamannho, posicao, etc
*/
var windowControl =  new WindowControl();

/**
manipulador de eventos. Permite registrar eventos simples
*/
var eventer = new EventoAgente();


var INFINITO = 100000;
