	var OBJECT_TO_MOVE	= "";
	var startX 			= 0;
	var startY 			= 0;
	var tempX 			= 0;
	var tempY 			= 0;
	var lastTop			= 10;
	var Ojanelas		= new Array();
	
	if (!document.all)	document.captureEvents(Event.MOUSEMOVE);
	
	document.onmousemove = getMouseXY;
	
	function getMouseXY(e) {
		if (document.all) { //for IE
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
		} else { //for true browsers
			tempX = e.pageX;
			tempY = e.pageY;
		}
		if (tempX < 0){tempX = 0;}
		if (tempY < 0){tempY = 0;}
		
		if (OBJECT_TO_MOVE !== ""){
			moveObjectTo(OBJECT_TO_MOVE,tempX,tempY);
		}
		return true;
	}
	
	function moveObjectTo(objectId,posX,posY){
		var objeto = document.getElementById(objectId);
		var estilo = getStyle(objeto);
		
		// retira valores top, left e z-index antigos
		var basicStyle = estilo.replace(/(.*)(\stop\s*:\s*[0-9]+px;|%;)(.*)/,'$1$3');
		var basicStyle = basicStyle.replace(/(.*)(\sleft\s*:\s*[0-9]+px;|%;)(.*)/,'$1$3');
		var basicStyle = basicStyle.replace(/(.*)(\sz\-index\s*:\s*[0-9]+;?)/,'$1');
		
		var newX = posX - startX;
		var newY = posY - startY;
		if (newX <= 0) newX = 1;
		if (newY <= 0) newY = 1;
		
		var newStyle = basicStyle+"top:"+newY+"px; left:"+newX+"px; z-index: "+lastTop;
		//alert(objectId);
		setStyle(document.getElementById(objectId),newStyle);
	}

	function closeWindow(objectId){
		var bodys = document.getElementsByTagName('body');
		//bodys[0].appendChild(nodeNewWindow);

		//var pai		= document.getElementById('conteudo');
		var filho	= document.getElementById('container'+objectId);
		bodys[0].removeChild(filho);
	}
	
	function praFrentePorra(objectId){
		lastTop++;
		
		var objeto = document.getElementById(objectId);
		var estilo = getStyle(objeto);
		var basicStyle = estilo.replace(/(.*)(\s*z\-index\s*:\s*[0-9]+;?)/,'$1');
		var newStyle = basicStyle+"z-index:"+lastTop+";";
		setStyle(document.getElementById(objectId),newStyle);
	}
	
	function moveWindow(objectId){
		if (windowState(objectId) == "maximizado") return false;
	
		OBJECT_TO_MOVE = objectId;
	
		var objeto = document.getElementById(objectId);
		var estilo = getStyle(objeto);

		var objectX = estilo.replace(/(.*left\s*:\s*)([0-9]+)(px;|%;)(.*)/,'$2');
		var objectY = estilo.replace(/(.*top\s*:\s*)([0-9]+)(px;)(.*)/,'$2');

		startX = tempX - objectX;
		startY = tempY - objectY;
	}

	// função para desenhar a janela
	function createWindow(idName,conteudo,titulo,x,y,w,h){
		var theContent	= idName;
		var nameSufix	= ""; //Math.random()*5 + "ABCDEFG"; // + A para transformar em string;
		//nameSufix		= nameSufix.substring(0,10);
		//idName			+= nameSufix;
		var saida = "";
		if (document.all){
			h = h - 35;
		}
		
		visibility = "visible";
		lastTop++;
		var zIndex = lastTop;
		var position = "absolute";
		
		var style		= "visibility:"+visibility+";position:"+position+";width:"+w+"px;height:"+h+"px;top:"+y+"px;left:"+x+"px; z-index:"+zIndex+";";				
		var windowName	= "window"+idName;
		var styleTransp = "filter:alpha(opacity=80);opacity:.80;";

		Ojanelas[Ojanelas.length] = new Array (idName,style,"normal");
		//0=nome do objeto 1=estilo 2=normal|maximizado
		var nodeNewWindow	= document.createElement("div");
		nodeNewWindow.setAttribute("class","window");
		nodeNewWindow.setAttribute("id","container"+windowName);

		saida += "<table border=\"0\" id=\""+windowName+"\" onMouseDown=\"praFrentePorra('"+windowName+"');\" style=\""+style+"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
		saida += "<tr><td width=\"6\" height=\"2\" align=\"right\"><div style=\"width:100%\" align=\"right\"><img src=\"comps/aero/top-left.png\" width=\"6\" height=\"26\"></div></td>";
		saida += "<td width=\"100%\" background=\"comps/aero/top.png\"><div style=\"width:100%\" onMouseUp=\"OBJECT_TO_MOVE=''\"  onDblClick=\"maximizarJanela('"+windowName+"')\" onMouseDown=\"if (checkEvent(event) == 1) moveWindow('"+windowName+"') \" id=\"titulo\" class=\"windowTitle\">"+titulo+"</div></td>";
		saida += "<td width=\"1\" align=\"right\" background=\"comps/aero/top.png\">";
		saida += "<div align=\"right\">";
		saida += "<img style=\"cursor:pointer;\" onclick=\"maximizarJanela('"+windowName+"')\" onmouseOver=\"this.src='comps/aero/min-hover.gif'\" onmouseOut=\"this.src='comps/aero/min.gif'\" src=\"comps/aero/min.gif\"  width=\"26\" height=\"19\"></div></td>";
		saida += "<td  background=\"comps/aero/top.png\"><div align=\"right\"><img style=\"cursor:pointer;\" onclick=\"closeWindow('"+windowName+"')\" onmouseOver=\"this.src='comps/aero/close-hover.gif'\" onmouseOut=\"this.src='comps/aero/close.gif'\" src=\"comps/aero/close.gif\" width=\"43\" height=\"19\"></div></td>";
		saida += "<td width=\"6\"><img src=\"comps/aero/top-right.png\" width=\"6\" height=\"26\"></td>";
		saida += "</tr><tr><td background=\"comps/aero/left.png\">&nbsp;</td><td valign=\"top\"  background=\"comps/fundo_blah.png\" style=\"height:100%\" colspan=\"3\" >";
		saida += "<table class=\"windowContent\" style=\"overflow:scroll\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\"><tr><td valign=\"top\">";
		saida += "<div id=\""+idName+"\"></div></td></tr></table></td><td background=\"comps/aero/right.png\">&nbsp;</td></tr><tr>";
		saida += "<td height=\"6\" align=\"right\"><img src=\"comps/aero/bottom-left.png\" width=\"6\" height=\"6\"></td>";
		saida += "<td height=\"6\" colspan=\"3\" background=\"comps/aero/bottom.png\"><img src=\"comps/aero/bottom.png\" width=\"1\" height=\"6\"></td>";
		saida += "<td height=\"6\"><img style=\"cursor:se-resize;\" src=\"comps/aero/bottom-right.png\" width=\"6\" height=\"6\"></td></tr></table>";
		
		nodeNewWindow.innerHTML = saida;
		
		var bodys = document.getElementsByTagName('body');
		bodys[0].appendChild(nodeNewWindow);
		//document.getElementById('conteudo').
	

		if (conteudo == ""){
			ajaxGet(theContent+".php",document.getElementById(idName),true);
		} else {
			//document.getElementById(idName).innerHTML = "conteudo personalizado";
			document.getElementById(idName).innerHTML = conteudo;
		}
		
		if (estado == "maximizado"){
			changeStateWindow(idName,"maximizado");
		} else {
			//changeStateWindow(idName,"maximizado");
		}
	}
	
	function checkEvent(evt){
		evt = (evt) ? evt : (window.event) ? window.event : ""
		if (evt){
			var thingPressed = "";
			var elem = (evt.target) ? evt.target : evt.srcElement;
			if (evt.which) {
				thingPressed = evt.which;
			} else {				
				thingPressed = evt.button;
			}	
		}
		return thingPressed;
	}
	
	// =FUNÇÃO QUE RESTAURA A JANELA AO ESTADO ORIGINAL (EM QUE FOI CRIADA)===========================//
	function restaurarJanela(objectId){
		var estilo = windowStyle(objectId);
		setStyle(document.getElementById(objectId),estilo);
	}

	function windowContentName(objectId){
		for (var cont = 0; cont < Ojanelas.length; cont++){
			if ("window"+Ojanelas[cont][0] == objectId){ 
				return Ojanelas[cont][0];
			}
		}
	}

	function windowStyle(objectId){
		for (var cont = 0; cont < Ojanelas.length; cont++){
			if ("window"+Ojanelas[cont][0] == objectId){ 
				return Ojanelas[cont][1];
			}
		}
	}

	// =FUNÇÃO PARA VERIFICAR O ESTADO DA JANELA =====================================================//
	function windowState(objectId){
		for (var cont = 0; cont < Ojanelas.length; cont++){
			if ("window"+Ojanelas[cont][0] == objectId){ 
				return Ojanelas[cont][2];
			}
		}
	}
	
	function windowOffset(objectId){
		for (var cont = 0; cont < Ojanelas.length; cont++){
			if ("window"+Ojanelas[cont][0] == objectId){ 
				return cont;
			}
		}
	}

	function changeStateWindow(objectId,state){
		for (var cont = 0; cont < Ojanelas.length; cont++){
			if ("window"+Ojanelas[cont][0] == objectId){ 
				Ojanelas[cont][2] = state;
			}
		}
	}

	function maximizarJanela(objectId,conteudo){
		var namePrefix	= objectId.substring(0,objectId.length - 10);	// retira o numero aleatório
		namePrefix = namePrefix.substring(6,namePrefix.length) ;		// retira o prefixo window

		if (windowState(objectId) == "maximizado"){ // se já estiver maximizada, restaure
			restaurarJanela(objectId);
			
			var pagina = namePrefix+"-min.php";
			//ajaxGet(namePrefix+"-min.php",document.getElementById(windowContentName(objectId)),true);
			
			changeStateWindow(objectId,"normal");
		} else if (windowState(objectId) == "normal"){
			// maximiza a janela
			objeto				= document.getElementById(objectId);

			objeto.style.top 	= "0px";
			objeto.style.left	= "0px";
			objeto.style.width	= "100%";
			objeto.style.height = "400px";
			
			window.scrollTo(0,0);
			
			changeStateWindow(objectId,"maximizado");
		}
	}

	function getStyle(node){
		// procura somente as propriedades interessantes para nosso caso
		// fuck the microsoft
		//if (!document.all){
		//	return node.getAttribute("style");
		//} else {
			var arrayStyle = node.getAttribute("style");
			var saida = "";
			if (arrayStyle.position !== '') saida += "position:"	+ arrayStyle.position + ";";
			if (arrayStyle.top !== '') saida += "top:" + arrayStyle.top + ";";
			if (arrayStyle.left !== '') saida += "left:"	+ arrayStyle.left + ";";
			if (arrayStyle.width !== '') saida += "width:" + arrayStyle.width + ";";
			if (arrayStyle.height !== '') saida += "height:" + arrayStyle.height + ";";
			if (arrayStyle.visibility !=='') saida += "visibility:" + arrayStyle.visibility + ";";
			if (arrayStyle.zIndex !== '') saida += "z-index:" + arrayStyle.zIndex + ";";			
			return saida;
		//}
	}
	
	function setStyle(node,estilo){
		// infelizmente não consegui fazer isso com expressões regulares mas o importante é o resultado final
		// fuck the microsoft
		//if (!document.all){
		//	node.setAttribute("style",estilo);
		//} else {
			
			// substitui todos as propriedades do tipo font-family, z-index por fontFamily, zIndex
				var pos = estilo.indexOf('-');
				var strtoupper;
				while (pos !== -1){ // enquanto encontrar esse caractere
					strtoupper = estilo.substr(pos+1,1);	
					estilo = estilo.substr(0,pos) + strtoupper.toUpperCase() + estilo.substr(pos+2,estilo.length-(pos+1));
					pos = estilo.indexOf('-');
				} 
			// --------------------------------------------------------------------
	
			// separa cada par de propriedade e valor em uma celula do vetor
				var vetorSaida = new Array();
				var pos = estilo.indexOf(';');

				while (pos !== -1){ // enquanto encontrar esse caractere
					vetorSaida[vetorSaida.length] = estilo.substr(0,pos);
					estilo = estilo.substr(pos+1,estilo.length-pos);
					pos = estilo.indexOf(';');
				} 
			// ---------------------------------------------------------------------
		
			// pega cada celula do vetor, separa a propriedade do valor e ajusta o estilo do objeto
				var seletor = "";
				var valor	= "";
				for(var cont = 0; cont < vetorSaida.length; cont++){
					pos 	= vetorSaida[cont].indexOf(':');
					seletor	= vetorSaida[cont].substr(0,pos);
					valor	= vetorSaida[cont].substr(pos+1,vetorSaida[cont].length-pos);
					
					if (valor !== "undefined") // olha que feio! mas assim funciona bem no IEca e nos navegadores então... foda-se!
						eval("node.style."+seletor+"=valor");
				}
			// ---------------------------------------------------------------------
		//}
	}
