// -----------------------------------------------------------------------------------
// Constructor de Ventana															  
// -----------------------------------------------------------------------------------
// Objeto: WindowMove
// Comentario: Se encarga de mover y redimencionar la ventana
// -----------------------------------------------------------------------------------

var WindowMove={

	moz: document.getElementById && !document.all,
	dragging: false,
	resizeRight: false,
	resizeLeft: false,
	resizeTop: false,
	resizeBottom: false,
	dobj: false,
	posX: 0,
	posY: 0,
	posX2: 0,
	posY2: 0,
	bloqued: false,
	object_tomove: '',	

	PressingButton: function(e) {
		if( this.bloqued ) return;	
		var fobj = this.moz ? e.target : event.srcElement;

		if( fobj.tagName.toLowerCase() != "html" && fobj.id.indexOf("element_")==-1 ) return;

		
		if( Window.configure.resizable ){
			 while( fobj.tagName.toLowerCase() != "html" && fobj.id!="element_window" && fobj.id.indexOf("element_border")==-1 && fobj.id!="element_button_minimize" && fobj.id!="element_button_maximize" && fobj.id!="element_button_restore" && fobj.id!="element_button_close" && fobj.id!="element_window_minimizer" && fobj.id!="element_button_restore2" && fobj.id!="element_button_close2" && fobj.id!="element_window_include" ) {
				fobj = this.moz ? fobj.parentNode : fobj.parentElement;
			 }
		}else{		 
			 while( fobj.tagName.toLowerCase() != "html" && fobj.id!="element_window" && fobj.id!="element_button_minimize" && fobj.id!="element_button_maximize" && fobj.id!="element_button_restore" && fobj.id!="element_button_close" && fobj.id!="element_window_minimizer" && fobj.id!="element_button_restore2" && fobj.id!="element_button_close2" && fobj.id!="element_window_include" ) {
				fobj = this.moz ? fobj.parentNode : fobj.parentElement;
			 }		 
		}					
		
		 	  		
		  if( Window.configure.windowmove && fobj.id == "element_window" || fobj.id == "element_window_minimizer" ){
			this.dragging = true;

			X = this.moz ? e.clientX : event.clientX;
			Y = this.moz ? e.clientY : event.clientY;

			X = X - parseInt(!isNaN(fobj.style.left) ? 0 : fobj.style.left);
			Y = Y - parseInt(!isNaN(fobj.style.top) ? 0 : fobj.style.top);
			
			if( X < 0 ) X=0;
			else if( X > parseInt(fobj.style.width) ) X=parseInt(fobj.style.width);
			if( Y < 0 ) Y=0;
			else if( Y > parseInt(fobj.style.height) ) Y=parseInt(fobj.style.height);
			
			this.posX = X;
			this.posY = Y;
						
			this.dobj = Window.create_frame_movible(fobj.id, "move");			
			this.object_tomove = fobj.id;
			
			this.stopEvent(e);
			return false;
		  }
		  
		  if( Window.configure.resizable && fobj.id.indexOf("element_border")>-1 ){		  
		  	if( fobj.id == "element_border_right" ) this.resizeRight=true;
		  	else if( fobj.id == "element_border_left" ) this.resizeLeft=true;
		  	else if( fobj.id == "element_border_top" ) this.resizeTop=true;
		  	else if( fobj.id == "element_border_bottom" ) this.resizeBottom=true;
		  	else if( fobj.id == "element_border_esqSE" ) {
				this.resizeRight=true;
				this.resizeBottom=true;
			}
		  	else if( fobj.id == "element_border_esqSO" ) {
				this.resizeLeft=true;
				this.resizeBottom=true;
			}
		  	else if( fobj.id == "element_border_esqNE" ) { 
				this.resizeRight=true;
				this.resizeTop=true;
			}
		  	else if( fobj.id == "element_border_esqNO" ) { 
				this.resizeLeft=true;
				this.resizeTop=true;
			}

			X = this.moz ? e.clientX : event.clientX;
			Y = this.moz ? e.clientY : event.clientY;

			this.posX2 = X;
			this.posY2 = Y;
			
			this.dobj = Window.create_frame_movible("element_window", fobj.style.cursor);
			this.object_tomove = "element_window";
			
			document.body.style.cursor=fobj.style.cursor;

			this.stopEvent(e);			
			return false;
		 }		 
	},
	
	DragMouse: function(e){
		// MOVE
		if( this.dragging ) {	
			newLeft = this.moz ? e.clientX : event.clientX;
			newTop = this.moz ? e.clientY : event.clientY;
							
			this.dobj.style.left = (newLeft - this.posX) + "px";
			this.dobj.style.top = (newTop - this.posY) + "px";
						
			return false;
		}
		
		// RIGHT
		if( this.resizeRight ){			
			X = this.moz ? e.clientX : event.clientX;			
			posX = X-parseInt(this.dobj.style.left);
			
			if( posX > Window.init.resizer_topX ){			
				this.dobj.style.width = (X-parseInt(this.dobj.style.left)) + "px";
			}
		}

		// BOTTOM
		if( this.resizeBottom ){
			Y = this.moz ? e.clientY : event.clientY;			
			posY = Y-parseInt(this.dobj.style.top);
			if( posY > Window.init.resizer_topY ){
				this.dobj.style.height = (Y - parseInt(this.dobj.style.top)) + "px";
			}
		}
		
		// TOP
		if( this.resizeTop ){
			Y = this.moz ? e.clientY : event.clientY;
			posY = (parseInt(this.dobj.style.top)+parseInt(this.dobj.style.height))-Y;
			
			if( posY > Window.init.resizer_topY ){				
				this.dobj.style.top = Y + "px";			
				y=this.posY2;
				direction=this.get_direction_y(Y)
				
				if( direction == "down" ){
					i = Y-y;
					this.dobj.style.height = (parseInt(this.dobj.style.height)-parseInt(i))+"px";				
				}else if( direction == "up" ){
					i = y-Y;				
					this.dobj.style.height = (parseInt(this.dobj.style.height)+parseInt(i))+"px";				
				}
			}
		}
		
		// LEFT
		if( this.resizeLeft ){
			X = this.moz ? e.clientX : event.clientX;
			posX = (parseInt(this.dobj.style.left)+parseInt(this.dobj.style.width))-X;
			
			if( posX > Window.init.resizer_topX ){			
				this.dobj.style.left = X + "px";			
				x=this.posX2;
				direction=this.get_direction_x(X)
				
				if( direction == "right" ){
					i = X-x;
					this.dobj.style.width = (parseInt(this.dobj.style.width)-parseInt(i))+"px";
				}else if( direction == "left" ){
					i = x-X;				
					this.dobj.style.width = (parseInt(this.dobj.style.width)+parseInt(i))+"px";
				}									 			
			}
		}
		
		if( this.resizeRight || this.resizeLeft ){
			GenFunc.$("element_border_right").style.left=(parseInt(this.dobj.style.width)-5)+"px";
			GenFunc.$("element_border_top").style.width=(parseInt(this.dobj.style.width)-20)+"px";
			GenFunc.$("element_border_bottom").style.width=(parseInt(this.dobj.style.width)-20)+"px";
			GenFunc.$("element_border_esqNE").style.left=(parseInt(this.dobj.style.width)-10)+"px";
			GenFunc.$("element_border_esqSE").style.left=(parseInt(this.dobj.style.width)-10)+"px";
		}
		
		if( this.resizeBottom || this.resizeTop ){
			GenFunc.$("element_border_bottom").style.top=(parseInt(this.dobj.style.height)-5)+"px";
			GenFunc.$("element_border_left").style.height=(parseInt(this.dobj.style.height)-20)+"px";
			GenFunc.$("element_border_right").style.height=(parseInt(this.dobj.style.height)-20)+"px";
			GenFunc.$("element_border_esqSO").style.top=(parseInt(this.dobj.style.height)-10)+"px";
			GenFunc.$("element_border_esqSE").style.top=(parseInt(this.dobj.style.height)-10)+"px";			
		}

	},
	
	ReleaseButton: function(){
		this.dragging=false;
		this.resizeRight=false;
		this.resizeLeft=false;
		this.resizeTop=false;
		this.resizeBottom=false;
		if( this.object_tomove!="" ){
			Window.remove_frame_movible(this.object_tomove);
			document.body.style.cursor="";
		}
	},
	
	stopEvent: function(e){
		if( this.moz ){
			e.stopPropagation();
			e.preventDefault(); 				
		}else{
			event.cancelBubble = true; 
			event.returnValue = false;
		}		
	},
	
	get_direction_y: function(y){
		if( y < this.posY2 ){
			r="up";
		}else if( y > this.posY2 ){
			r="down";				
		}else{
			r="igual";
		}
		this.posY2=y;
		return r;
	},

	get_direction_x: function(x){
		if( x < this.posX2 ){
			r="left";
		}else if( x > this.posX2 ){
			r="right";				
		}else{
			r="igual";
		}
		this.posX2=x;
		return r;
	}
	
		
}
document.onmousedown = Function("e","WindowMove.PressingButton(e)");
document.onmouseup = Function("WindowMove.ReleaseButton()");
document.onmousemove = Function("e","WindowMove.DragMouse(e)");		
