// -----------------------------------------------------------------------------------
// Constructor de Ventana															  
// -----------------------------------------------------------------------------------
// Objeto: Window
// Comentario: Es el archivo principal. Se encarga de construir la ventana
// Programado por Ivan Mattoni (Dic-2008)
// Version 1.0
// -----------------------------------------------------------------------------------

var Window={
	init:{
		root: "",
		skin: "skin_default",
		include: "",
		width: "400px",
		height: "350px",
		left: "0px",
		top: "0px",	
		title: "Example Window",
		resizer_topX: 170,
		resizer_topY: 110
	},
	
	configure:{
		resizable: true,
		windowmove: true,
		align_title: 'left',
		align_window: 'custom',
		buttons:{
			minimize:true,
			maximize:true
		},
		wallpaper:{
			enabled: true,
			background_color: '#ffffff',
			opacity: 5
		}
	},
	
	_nodeScript1: false,
	_nodeScript2: false,
	_nodeCSS: false,
	working: false,
	onload: Function(),

	load: function(){
		var headID = document.getElementsByTagName("head")[0];
		if( !headID ) return;
		
		this.working=true;
		
		// Open CSS
		var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = this.init.root+"/skins/"+this.init.skin+"/style.css";
			headID.appendChild(cssNode);				
			this._nodeCSS=cssNode;

		// Show Frame Opacity
		if( this.configure.wallpaper.enabled ){
			GenFunc.frame_opacity.show();
		}

		// Build Window
		var div = document.createElement("DIV");
			div.id = "element_window";
			div.style.position = "absolute";
			div.style.width = this.init.width;
			div.style.height = this.init.height;
			div.style.backgroundColor = "#cccccc";
			div.style.border = "none";
			div.style.zIndex=201;
					
		// Location Window		
		this.configure.align_title = (this.configure.align_title!="left" && this.configure.align_title!="center" && this.configure.align_title!="right") ? "left" : this.configure.align_title;
		this.configure.align_window = (this.configure.align_window!="custom" && this.configure.align_window!="left" && this.configure.align_window!="right" && this.configure.align_window!="center" && this.configure.align_window!="middle") ? "custom" : this.configure.align_window;
		
		if( this.configure.align_window!="custom" ){
			switch( this.configure.align_window ){
			case "left":
				div.style.left="0px";
				div.style.top = "0px";
			break;
			case "center":
				div.style.left = ((GenFunc.get_browser_width()/2) - (parseInt(div.style.width)/2))+"px";
				div.style.top = "0px";
			break;
			case "right":
				div.style.left = (GenFunc.get_browser_width() - parseInt(div.style.width))+"px";
				div.style.top = "0px";
			break;
			case "middle":
				div.style.left = ((GenFunc.get_browser_width()/2) - (parseInt(div.style.width)/2))+"px";
				div.style.top = ((GenFunc.get_browser_height()/2) - (parseInt(div.style.height)/2))+"px";
			break;				
			}
		}else{
			div.style.left = this.init.left;
			div.style.top = this.init.top;			
		}	
		
		// Add new Object
		document.body.appendChild(div);
		
		// Show Skin
		GenFunc.Ajax.XMLHttp = GenFunc.Ajax.create_instance_ajax();
		if( GenFunc.Ajax.XMLHttp ){
			GenFunc.Ajax.XMLHttp.onreadystatechange = function(){GenFunc.Ajax.changing_state_ajax('element_window');}
			
			param1 = (this.configure.windowmove) ? "yes" : "no";
			param2 = (this.configure.buttons.minimize) ? "yes" : "no";
			param3 = (this.configure.buttons.maximize) ? "yes" : "no";
			
			GenFunc.Ajax.XMLHttp.open("GET", this.init.root+"/skins/body.php?width="+(parseInt(this.init.width))+"&height="+(parseInt(this.init.height))+"&root="+this.init.root+"/skins/"+this.init.skin+"&title="+this.init.title+"&aligntitle="+this.configure.align_title+"&include="+this.init.include+"&windowmove="+param1+"&view_minimize="+param2+"&view_maximize="+param3, true);
			GenFunc.Ajax.XMLHttp.send(null);
			return;
		}
		
		// Disabled Selection
		GenFunc.disabled_selection("element_window");
		
	},	
	
	show_skin: function(id, html){
		div = GenFunc.$(id);
		div.innerHTML = html;
		//GenFunc.disabled_selection("element_skintable");
		GenFunc.disabled_selection("element_title")
		GenFunc.disabled_selection("element_pad");
		if( id=="element_window" && Window.configure.resizable ) Window.make_borders();
		this.working=false;
		this.onload();
	},
	
	make_borders: function(id){
		var show=0;
		var divTop = document.createElement("DIV");
			divTop.id = "element_border_top";
			divTop.style.position = "absolute";
			divTop.style.left = "10px";
			divTop.style.top = "0px";
			divTop.style.width = (parseInt(this.init.width)-20)+"px";
			divTop.style.height = "5px";
			divTop.style.cursor = "n-resize";
			divTop.style.overflow="hidden";
			if( show==1 ) divTop.style.background = "red";

		var divBottom = document.createElement("DIV");
			divBottom.id = "element_border_bottom";
			divBottom.style.position = "absolute";
			divBottom.style.left = "10px";
			divBottom.style.top = (parseInt(this.init.height)-5)+"px";
			divBottom.style.width = (parseInt(this.init.width)-20)+"px";
			divBottom.style.height = "5px";
			divBottom.style.cursor = "s-resize";
			divBottom.style.overflow="hidden";
			if( show==1 ) divBottom.style.background = "red";

		var divLeft = document.createElement("DIV");
			divLeft.id = "element_border_left";
			divLeft.style.position = "absolute";
			divLeft.style.left = "0";
			divLeft.style.top = "10px";
			divLeft.style.width = "5px";
			divLeft.style.height = (parseInt(this.init.height)-20)+"px";
			divLeft.style.cursor = "w-resize";
			divLeft.style.overflow="hidden";
			if( show==1 ) divLeft.style.background = "red";

		var divRight = document.createElement("DIV");
			divRight.id = "element_border_right";
			divRight.style.position = "absolute";
			divRight.style.left = (parseInt(this.init.width)-5)+"px";
			divRight.style.top = "10px";
			divRight.style.width = "5px";
			divRight.style.height = (parseInt(this.init.height)-20)+"px";
			divRight.style.cursor = "e-resize";
			divRight.style.overflow="hidden";
			if( show==1 ) divRight.style.background = "red";

		var divEsqNO = document.createElement("DIV");
			divEsqNO.id = "element_border_esqNO";
			divEsqNO.style.position = "absolute";
			divEsqNO.style.left = "0";
			divEsqNO.style.top = "0";
			divEsqNO.style.width = "10px";
			divEsqNO.style.height = "10px";
			divEsqNO.style.cursor = "nw-resize";
			divEsqNO.style.overflow="hidden";
			if( show==1 ) divEsqNO.style.background = "blue";

		var divEsqNE = document.createElement("DIV");
			divEsqNE.id = "element_border_esqNE";
			divEsqNE.style.position = "absolute";
			divEsqNE.style.left = (parseInt(this.init.width)-10)+"px";
			divEsqNE.style.top = "0";
			divEsqNE.style.width = "10px";
			divEsqNE.style.height = "10px";
			divEsqNE.style.cursor = "ne-resize";
			divEsqNE.style.overflow="hidden";
			if( show==1 ) divEsqNE.style.background = "blue";

		var divEsqSO = document.createElement("DIV");
			divEsqSO.id = "element_border_esqSO";
			divEsqSO.style.position = "absolute";
			divEsqSO.style.left = "0";
			divEsqSO.style.top = (parseInt(this.init.height)-10)+"px";
			divEsqSO.style.width = "10px";
			divEsqSO.style.height = "10px";
			divEsqSO.style.cursor = "sw-resize";
			divEsqSO.style.overflow="hidden";
			if( show==1 ) divEsqSO.style.background = "blue";

		var divEsqSE = document.createElement("DIV");
			divEsqSE.id = "element_border_esqSE";
			divEsqSE.style.position = "absolute";
			divEsqSE.style.left = (parseInt(this.init.width)-10)+"px";
			divEsqSE.style.top = (parseInt(this.init.height)-10)+"px";
			divEsqSE.style.width = "10px";
			divEsqSE.style.height = "10px";
			divEsqSE.style.cursor = "se-resize";
			divEsqSE.style.overflow="hidden";
			if( show==1 ) divEsqSE.style.background = "blue";
			
		var div=GenFunc.$("element_window");			
			div.appendChild(divTop);
			div.appendChild(divBottom);
			div.appendChild(divLeft);
			div.appendChild(divRight);			
			div.appendChild(divEsqNO);
			div.appendChild(divEsqNE);
			div.appendChild(divEsqSO);
			div.appendChild(divEsqSE);
			
			GenFunc.disabled_selection(divTop.id);
			GenFunc.disabled_selection(divBottom.id);
			GenFunc.disabled_selection(divLeft.id);
			GenFunc.disabled_selection(divRight.id);
			GenFunc.disabled_selection(divEsqNO.id);
			GenFunc.disabled_selection(divEsqNE.id);
			GenFunc.disabled_selection(divEsqSO.id);
			GenFunc.disabled_selection(divEsqSE.id);
		
	},
	
	create_frame_movible: function(id, cursor){
		var div = document.createElement("DIV");
		var divWindow = GenFunc.$(id);
			
			div.id = "element_frame_movible";
			div.style.position = "absolute";
			div.style.float = "left";
			div.style.width = divWindow.style.width;
			div.style.height = divWindow.style.height;
			div.style.left = divWindow.style.left;
			div.style.top = divWindow.style.top;
			div.style.backgroundColor = "black";
			div.style.border = "none";
			div.style.cursor = cursor;			
			div.style.zIndex=202;
		document.body.appendChild(div);
		GenFunc.set_opacity(div, 3);
		
		return div;
	},

	remove_frame_movible: function(id){
		divFrame = GenFunc.$("element_frame_movible");
		if( divFrame ){
			var divWindow = GenFunc.$(id);
				divWindow.style.left = divFrame.style.left;
				divWindow.style.top = divFrame.style.top;
				divWindow.style.width = divFrame.style.width;
				divWindow.style.height = divFrame.style.height;
			
			if( id=="element_window" ){
				this.location_elements();
				
			}else{
				GenFunc.$("element_pad2").style.left = (parseInt(divWindow.style.width)-71)+"px";				
			}
			GenFunc.remove_element(divFrame);
		}
	},
	
	location_elements: function(){
		var divWindow = GenFunc.$("element_window");
		GenFunc.$("element_cell_height").style.height = (parseInt(divWindow.style.height)-29)+"px";
		GenFunc.$("element_window_include").style.width = (parseInt(divWindow.style.width)-12)+"px";
		GenFunc.$("element_window_include").style.height = (parseInt(divWindow.style.height)-30)+"px";
		GenFunc.$("element_pad").style.left = (parseInt(divWindow.style.width)-108)+"px";
	},

	pad: {
		_width: '',
		_height: '',
		_left: '',
		_top: '',
		_overflow: '',

		minimize: function(){			
			GenFunc.$("element_window").style.display="none";			

			var divWindow2 = document.createElement("DIV");
				divWindow2.id = "element_window_minimizer";
				divWindow2.style.position = "absolute";
				divWindow2.style.width = "200px";
				divWindow2.style.height = "24px";
				divWindow2.style.left = "1px";
				var top = (!document.all) ? GenFunc.get_browser_height()-24 : GenFunc.get_browser_height()-39;
				divWindow2.style.top = top+"px";
				divWindow2.style.backgroundColor = "#cccccc";
				divWindow2.title = Window.init.title;
			
			document.body.appendChild(divWindow2);
			
			GenFunc.Ajax.XMLHttp = GenFunc.Ajax.create_instance_ajax();
			if( GenFunc.Ajax.XMLHttp ){
				GenFunc.Ajax.XMLHttp.onreadystatechange = function(){GenFunc.Ajax.changing_state_ajax('element_window_minimizer');}
				GenFunc.Ajax.XMLHttp.open("GET", Window.init.root+"/skins/body_minimizer.php?root="+Window.init.root+"/skins/"+Window.init.skin+"&title="+Window.init.title+"&aligntitle="+Window.configure.align_title+"&width="+parseInt(divWindow2.style.width), true);
				GenFunc.Ajax.XMLHttp.send(null);
			}
			
			GenFunc.disabled_selection("element_window_minimizer");
		},
		
		maximize: function(){
			divWindow = GenFunc.$("element_window");
			this._width = divWindow.style.width;
			this._height = divWindow.style.height;
			this._left = divWindow.style.left;
			this._top = divWindow.style.top;
			this._overflow = document.body.style.overflow;

			document.body.style.overflow="hidden";
	
			divWindow.style.width=GenFunc.get_browser_width()+"px";
			divWindow.style.height=GenFunc.get_browser_height()+"px";
			divWindow.style.left="0";
			divWindow.style.top="0";
			
			GenFunc.$("element_button_restore").style.position="relative";
			GenFunc.$("element_button_restore").style.display="";
			GenFunc.$("element_button_maximize").style.position="absolute";
			GenFunc.$("element_button_maximize").style.display="none";

			Window.location_elements();

			WindowMove.bloqued=true;
		},
		
		restore: function(){
			divWindow=GenFunc.$("element_window");
			divWindow.style.width=this._width;
			divWindow.style.height=this._height;
			divWindow.style.left=this._left;
			divWindow.style.top=this._top;
			document.body.style.overflow=this._overflow;
			
			GenFunc.$("element_button_restore").style.position="absolute";
			GenFunc.$("element_button_restore").style.display="none";
			GenFunc.$("element_button_maximize").style.position="relative";
			GenFunc.$("element_button_maximize").style.display="";

			Window.location_elements();
			
			WindowMove.bloqued=false;
		},
		
		restore2: function(){
			GenFunc.$("element_window").style.display="";			
			GenFunc.remove_element(GenFunc.$("element_window_minimizer"));
		},
		
		close: function(){
			GenFunc.remove_element(GenFunc.$("element_window"));
			GenFunc.remove_element(GenFunc.$("element_window_minimizer"));
			if( Window.configure.wallpaper.enabled ) GenFunc.remove_element(GenFunc.$("element_container"));
			document.onmousedown=null;
			document.onmouseup=null;
			document.onmousemove=null;
			GenFunc.remove_element(Window._nodeScript1);
			GenFunc.remove_element(Window._nodeScript2);
			GenFunc.remove_element(Window._nodeCSS);			
		}
	}
}