//////////////////////////////////////// ventana.js //////////////////

function Draggable(elt){

	var self=this;
	var draggin=false;
	var ioffset={"x":0,"y":0};
		elt.onmousedown=function(e){
				if (!e) var e = window.event;
				draggin=true;
				this.style.cursor="move"
				if (e.layerX && e.layerY)
					ioffset={"x":e.layerX,
						"y":e.layerY};
				if (e.offsetX && e.offsetY)
					ioffset={"x":e.offsetX,
						"y":e.offsetY};
		
				//alert("start");
		}
		elt.onmouseup=function(e){
			if (draggin) this.style.cursor=""
				draggin=false;
				ioffset={"x":0,"y":0};
			
		}
		elt.onmouseexit=elt.onmouseup;
		elt.onmouseenter=elt.onmouseup;
		elt.onmousemove=function(e){
			if (!e) var e = window.event;
			if (draggin){
			  try{	
				function offset(e){
				if (e.pageX || e.pageY){
						x = e.pageX;
						y = e.pageY;
				}else if (e.clientX || e.clientY){
					x = e.clientX + document.body.scrollLeft;
					y = e.clientY + document.body.scrollTop;
				}
					return {"x":x,"y":y};
				}
				//if (IE) alert("move"+ioffset.y);
				this.style.position="absolute";
				this.style.zIndex="2000";
				try{
				this.parentNode.removeChild(this);
				app(this,document.body);
				}catch(e){}
				
				this.style.top=(offset(e).y-ioffset.y)+"px";
				this.style.left=(offset(e).x-ioffset.x)+"px";
			   }catch(e){/* @todo: IE */}		
			}
			
		}
}




var Ventana= new function(){
	try{
	var ventana=add("DIV");
	new Draggable(ventana);
	var barra=add("DIV");
	var frame=add("DIV");
	 //app(barra,ventana);
	 app(frame,ventana);
	
	//hide(ventana);

		ventana.className="ventana";
		frame.className="frameventana";
		barra.className="barra";

	this.attach=function(doc){
		app(ventana,doc);
		hide(ventana);
	}
	this.show=function(){
		show(ventana);
	}
	this.hide=function(){
		hide(ventana);
	}
	this.ventana=ventana;
	var self=this;
	var move=false;
	var manejador=function(e){
			var x;var y;
		if (!e) var e = window.event;
	
		if (e.clientX || e.clientY){
		x = e.clientX + document.body.scrollLeft;
		y = e.clientY + document.body.scrollTop;
		}else if (e.pageX || e.pageY){
		
		x = e.pageX;
		y = e.pageY;
		}
		if (move){
			self.setAttribute("top",(y-50)+"px");
			self.setAttribute("left",(x+15)+"px");
		}
			//self.printmsg(x,", ",y);
	}
	var listening=false;
	this.seguirRaton=function(){
		
		this.setAttribute("width","300px");
		this.minimiza();
		move=true;
		if (!listening){
		try{
		document.body.addEventListener("mousemove",manejador
		,true);
		}catch(e){
			//printobj(getById("body"))
			document.body.onmousemove=manejador;	
		}
		}
	}

	this.fijarRaton=function(){
		move=false;
	}

	this.centrar=function(){}
	var bt_cerrar=addButton("cerrar");
		bt_cerrar.onclick=function(){
			self.hide();
		}
	this.popup=function(text){
		this.fijarRaton();
		this.clear();
		ventana.className="popup";
		app(textNode(text),frame);
		app(bt_cerrar,ventana);
		this.show();
	}
	this.clear=function(){
		this.setAttribute("top","");
		this.setAttribute("left","");
		this.setAttribute("width","");
		del(frame,ventana);
		frame=add("DIV");
		frame.className="frameventana";
		app(frame,ventana);
	}
	this.app=function(elt){
		app(elt,frame);
	}
	var text;
	this.maximiza=function(){
		this.fijarRaton();
		this.setAttribute("top","");
		this.setAttribute("left","");
		this.setAttribute("width","");
		ventana.className="ventanamax";
		
	}
	this.minimiza=function(){
		this.setAttribute("top","");
		this.setAttribute("left","");
		this.setAttribute("width","");
		ventana.className="ventana";
		del(bt_cerrar,ventana);
	}
	this.printmsg=function(){
			var out="";	
			for (var i=0; i<arguments.length;i++)
				out+=arguments[i];
		// No altera la posicion de la ventana..
		this.clear();
		app(textNode(out),frame);
		app(bt_cerrar,ventana);
		this.show();
		//frame.textContent=msg;
	}
	this.setAttribute =function(attr,valor){
		try{
		eval("ventana.style."+attr+"='"+valor+"'");
		}catch(e){alert("setAttr: "+e);}
	}
		
		
	}catch(e){
		alert("Error:"+e);
	}	
}();



