<?xml version="1.0" encoding="UTF-8"?>
<application-components>
	<!--
		CoreWeb Version:
			This version expects the following global variable exist:
				g_application_path : the Web application path (eg: / or /CoreWeb/)
	-->
	
	<application-component id="window">
		<![CDATA[
		GetWindowContainer : function(){
			var oE = this.GetEngine();
			return (oE ? oE.engine_element : document.body);
		},
		GetEngine : function(){
			return org.cote.js.engine.EngineService.getEngine(this.getReferenceId());
		},
		GetWindowManager : function(){
			var oE = this.GetEngine(), oM;
			if(oE && (oM = oE.getObjectByName("manager"))) return oM.object;
			else if(g_manager) return g_manager;
			return 0;
		},
		component_init:function(){

			var oManager, _p = this.getPointers(),_s = this.getStatus();
			
			oManager = this.GetWindowManager();
			if(!oManager){
				alert("Could not find manager");
				this.destroy();
				return;
			}
			_p.manager = oManager;

			this.getContainer().className = "window_gizmo";
			_p.active_frame = document.createElement("div");
			_p.active_frame.className = "window_gizmo_frame";
			this.GetWindowContainer().appendChild(_p.active_frame);
			
			_p.title_bar = document.createElement("div");
			_p.title_bar.onselectstart = function(){return false;};
			_p.title_bar.className = "window_title";
			_p.title_label = document.createElement("span");
			_p.title_bar.appendChild(_p.title_label);
			
			this.createHandler("title_mousedown",0,0,1);
			this.createHandler("frame_mouseup",0,0,1);
			this.createHandler("frame_mousemove",0,0,1);
			this.createHandler("minimize_click",0,0,1);
			this.createHandler("maximize_click",0,0,1);
			this.createHandler("close_click",0,0,1);
			
			_p.active_frame.onmouseup = this._prehandle_frame_mouseup;
			_p.active_frame.onmousemove = _p.manager._prehandle_mousemove;
			 _p.title_bar.onmousedown = this._prehandle_title_mousedown;
			 

			 _p.close_button = document.createElement("div");
			 _p.close_button.setAttribute("is-button","1");
			 _p.close_button.appendChild(document.createTextNode(" "));
			 _p.close_button.className = "button_close";
			 _p.title_bar.appendChild(_p.close_button);
			 _p.close_button.onclick = this._prehandle_close_click;

			 _p.maximize_button = document.createElement("div");
			 _p.maximize_button.setAttribute("is-button","1");
			 _p.maximize_button.className = "button_maximize";
			 _p.title_bar.appendChild(_p.maximize_button);
			 _p.maximize_button.onclick = this._prehandle_maximize_click;

			 _p.minimize_button = document.createElement("div");
			 _p.minimize_button.setAttribute("is-button","1");
			 _p.minimize_button.className = "button_minimize";
			 _p.title_bar.appendChild(_p.minimize_button);
			 _p.minimize_button.onclick = this._prehandle_minimize_click;
			
			_s.lostFocusIndex = 10;
			_s.setFocusIndex = 16;
			_s.resizeFocusIndex = 18;
			_s.moveFocusIndex = 18;
			_s.dialogFocusIndex = 16;
			_s.stackFocusIndex = 9;
			_s.is_moving = 0;
			_s.can_move = 1;
			_s.is_resizing = 0;
			_s.can_resize = 1;
			_s.can_restore = 1;
			_s.is_bound = 1;
			_s.can_minimize = 1;
			_s.is_minimized = 0;
			_s.can_maximize = 1;
			_s.is_maximized = 0;
			_s.mod_x = 0;
			_s.mod_y = 0;
			_s.change_count = 0;
			_s.is_modal = 0;
			_s.is_dialog = 0;
			_s.is_closed = 0;
			_s.hide_on_close = 0;
			_s.manage_minimize = 1;
			_s.manage_maximize = 1;
			_s.opacity = 100;
			_s.mark_opacity = -1;

			_s.minimize_type = "tile";
			_p.minimize_marker = 0;
			
			this.getContainer().appendChild(_p.title_bar);
			
		},
		component_post_init:function(){
			var _p = this.getPointers();
			var a = this.getContainer().getElementsByTagName("div");
			for(var i = 0; i < a.length; i++){
				if(a[i].getAttribute("is-body")){
					_p.body = a[i];
					break;
				}
			}
			if(!_p.body){
				alert("Invalid content structure.  Expecting to find <div is-body=\"1\" />");
				return;
			}
			
			_p.status_bar = document.createElement("div");
			_p.status_bar.onselectstart = function(){return false;};
			_p.status_bar.className = "window_status";
			_p.status_label = document.createElement("span");
			_p.status_bar.appendChild(_p.status_label);

			this.getContainer().appendChild(_p.status_bar);
			_p.body.className = "window_body";
			
			this.setTitle("[ title ]");
			this.setStatus("[ status ]");
			
			 _p.resize_button = document.createElement("div");
			 _p.resize_button.setAttribute("is-button","1");
			 _p.resize_button.appendChild(document.createTextNode(" "));
			 _p.resize_button.className = "button_resize";
			 _p.status_bar.appendChild(_p.resize_button);

			/*
			var ig = document.createElement("img");
			ig.setAttribute("src","Graphics/xp_resize_ico.gif");
			ig.setAttribute("width","10");
			ig.setAttribute("height","10");
			_p.resize_button.appendChild(ig);
			*/
			
			 this.createHandler("resize_mousedown",0,0,1);
			_p.resize_button.onmousedown = this._prehandle_resize_mousedown;
			
			_p.body.style.height = (this.getContainer().clientHeight - _p.status_bar.offsetHeight - _p.title_bar.offsetHeight) + "px";
      
			_p.manager.RegisterWindow(this.getObjectId());

			var sTemplate = this.getContainer().getAttribute("template");
			this.loadTemplate(sTemplate);
		},
		
		center : function(){
			var _s = this.getStatus(), _p = this.getPointers();
			if(_s.is_maximized || _s.is_minimized) return 0;
			_p.manager.CenterWindow(this);
		},

		getTemplateContainer : function(){
		  return this.getPointers().body;
		},
		getMinimizeType : function(){
				return this.getStatus().minimize_type;
		},
		getMinimizeMarker : function(){
				return this.getPointers().minimize_marker;
		},
		setMinimizeMarker : function(o){
				this.getPointers().minimize_marker = o;
		},
		setMinimizeType : function(s){
				this.getStatus().minimize_type = s;
		},
		setManageMinimize : function(b){
		  this.getStatus().manage_minimize = b;
		},
		setManageMaximize : function(b){
		  this.getStatus().manage_maximize = b;
		},

 		absorb : function(a,b){
			var i = 0, l = a.length;
			for(;i<l;)
				b[b.length] = a[i++];
		},

		setIsModal : function(b){
		  this.getStatus().is_modal = b;
		  if(b && !this.getIsMinimized()) this.getPointers().manager.RequestScreen(this);
		  else if(!this.getIsMinimized()) this.getPointers().manager.ReturnScreen();
		  this.setCanResize(!b);
		  this.setCanMove(!b);
		  // this.restore();
		},
	    
		hideButtons : function(){
		  var _p = this.getPointers();
		  _p.minimize_button.style.display = "none";
		  _p.close_button.style.display = "none";
		  _p.maximize_button.style.display = "none";
		},
		showButtons : function(){
		  var _p = this.getPointers();
		  _p.minimize_button.style.display = "block";
		  _p.close_button.style.display = "block";
		  _p.maximize_button.style.display = "block";
		},
		setCanMaximize : function(b){
				this.getStatus().can_maximize = b;
		},
		setCanMinimize : function(b){
				this.getStatus().can_minimize = b;
		},
		setCanMove : function(b){
		  this.getStatus().can_move = b;
		},
		setCanResize : function(b){
		  this.getStatus().can_resize = b;
		  this.getPointers().resize_button.style.display = (b ? "block" : "none");
		},
		_handle_maximize_click : function(e){
				var _s = this.getStatus();
				if(_s.is_maximized){
					this.restore();
				}
				else{
					this.maximize();
				}
				org.cote.js.dom.event._gevt_cancel(org.cote.js.dom.event._gevt(e));
		  return false;
		},
		maximize : function(){
    		var _s = this.getStatus(), _p = this.getPointers();
				if(!_s.can_maximize || _s.is_maximized) return 0;
				if(_s.is_minimized){
							b = _s.manage_minimize;	
							_s.manage_minimize = 0;
							this.restore(1);
							_s.manage_minimize = b;
				}
		  if(_s.is_modal) this.getPointers().manager.ReturnScreen();
				_s.is_maximized = 1;
 				_s.last_height = this.getContainer().clientHeight;
				_s.last_width = this.getContainer().clientWidth;
		  _s.last_left = this.getContainer().offsetLeft;
		  _s.last_top = this.getContainer().offsetTop;
	      
		  _p.maximize_button.style.backgroundImage = "url(" + g_application_path + "Graphics/xp_norm_ico.gif)";
	      
		  if(this.local_handle_maximize) this.local_handle_maximize(this);
	      
		  if(_s.manage_maximize){
			_p.manager.ManageMaximize(this);
		  }

		},
		_handle_minimize_click : function(e){
			var _s = this.getStatus();
			if(_s.is_minimized){
				this.getStatus().late_focus = _s.manage_minimize;
				this.restore(_s.manage_minimize);
			}
			else{
				this.minimize();
			}
		  org.cote.js.dom.event._gevt_cancel(org.cote.js.dom.event._gevt(e));
		  return false;
		},
	    minimize : function(){

			var _s = this.getStatus(), _p = this.getPointers();
			if(!_s.can_minimize || _s.is_minimized) return 0;
			if(_s.is_maximized){
						b = _s.manage_maximize;	
						_s.manage_maximize = 0;
						this.restore(1);
						_s.manage_maximize = b;
			}
			if(_s.is_modal) this.getPointers().manager.ReturnScreen();

			_s.is_minimized = 1;
			_s.last_height = this.getContainer().clientHeight;
			_s.last_width = this.getContainer().clientWidth;
			_s.last_left = this.getContainer().offsetLeft;
			_s.last_top = this.getContainer().offsetTop;
			_p.body.style.display = "none";
			_p.status_bar.style.display = "none";
			this.getContainer().style.height = _p.title_bar.offsetHeight + "px";
			_p.minimize_button.style.backgroundImage = "url(" + g_application_path + "Graphics/xp_norm_ico.gif)";

			if(this.local_handle_minimize) this.local_handle_minimize(this);

			if(_s.manage_minimize){
				_p.manager.ManageMinimize(this);
			}
		},
		
		restore : function(b){
			var _s = this.getStatus(), _p = this.getPointers();

			if((_s.is_minimized || _s.is_maximized) && _s.can_restore){
				var b_was_min = _s.is_minimized;
				var b_was_max = _s.is_maximized;
				if(b_was_max) this.setCanResize(1);
				
				_s.is_minimized = 0;
				_s.is_maximized = 0;
				if(!b){
					// document.title = "Get Focus " + (new Date()).getTime();
					this.getFocus();
					if(_s.is_modal) this.getPointers().manager.RequestScreen(this);
				}

				//this.getContainer().style.width = _s.last_width + "px";
				//this.getContainer().style.height = _s.last_height + "px";
				_p.body.style.display = "block";
				_p.status_bar.style.display = "block";
				_p.minimize_button.style.backgroundImage = "url(" + g_application_path + "Graphics/xp_min_ico.gif)";
				_p.maximize_button.style.backgroundImage = "url(" + g_application_path + "Graphics/xp_max_ico.gif)";
				if(this.local_handle_restore) this.local_handle_restore(this);
				if(_s.manage_minimize && b_was_min){
				  _p.manager.Tile();
					this.animate(this.getContainer().offsetLeft,this.getContainer().offsetTop,_s.last_left,_s.last_top,0,0,0,1,_s.last_width,_s.last_height);
		        }
				else if(b_was_min){
							this.resizeTo(_s.last_width,_s.last_height);
							this.moveTo(_s.last_left + "px",_s.last_top + "px");
				}
				if(_s.manage_maximize && b_was_max){
							this.animate(this.getContainer().offsetLeft,this.getContainer().offsetTop,_s.last_left,_s.last_top,0,0,0,1,_s.last_width,_s.last_height);
				}
				else if(b_was_max){
							this.resizeTo(_s.last_width,_s.last_height);
							this.moveTo(_s.last_left + "px",_s.last_top + "px");
				}
        
			}
		},
		open : function(){
		  if(this.getStatus().hide_on_close && this.getStatus().is_closed){
			this.getContainer().style.display = "block";
			this.getStatus().is_closed = 0;
			return;
		  }
		},
		_handle_close_click : function(e){
			this.Close();
			org.cote.js.dom.event._gevt_cancel(org.cote.js.dom.event._gevt(e));
			return false;
		},
	    Close : function(){
			// allow implementer to override closing
			//
			if(typeof this.local_handle_close == "function" && this.local_handle_close(this)) return;
      
			var _p = this.getPointers(),o = this.getContainer();
			this.getStatus().is_closed = 1;
			if(this.getStatus().is_modal) this.getPointers().manager.ReturnScreen();
			this.blur();
      
			if(this.getStatus().manage_minimize) _p.manager.Tile();
			if(this.getStatus().hide_on_close){
				this.getContainer().style.display = "none";
				return;
			}

			// Make sure to remove the active frame, since it was added outside the scope of this XHTML element.
			_p.manager.UnRegisterWindow(this.getObjectId());

			this.GetWindowContainer().removeChild(_p.active_frame);
			
			this.destroy();
			o.parentNode.removeChild(o);
		},

		getIsMinimized : function(){
		  return this.getStatus().is_minimized;
		},
		getIsMaximized : function(){
		  return this.getStatus().is_maximized;
		},
		setHideOnClose :function(b){
		   this.getStatus().hide_on_close = b;
		},
		getHideOnClose : function(){
		  return this.getStatus().hide_on_close;
		},
		getIsClosed : function(){
		  return this.getStatus().is_closed;
		},
		setTitle : function(s){
			org.cote.js.xml.setInnerXHTML(this.getPointers().title_label,s);

		},
		setStatus : function(s){
			org.cote.js.xml.setInnerXHTML(this.getPointers().status_label,s);
		},
		clearContents : function(){
			var _p = this.getPointers();
			if(_p.child_engine) org.cote.js.engine.EngineService.clearEngine(_p.child_engine);
			org.cote.js.xml.removeChildren(_p.body);
		},
		getIsBound:function(){
			return this.getStatus().is_bound;
		},
		setIsBound:function(b){
			this.getStatus().is_bound = b;
			var _p = this.getPointers();
			_p.active_frame.onmouseup = null;
			_p.active_frame.onmousemove = null;
			_p.active_frame.parentNode.removeChild(_p.active_frame);
			_p.active_frame = document.createElement("div");
			_p.active_frame.className = "window_gizmo_frame";
			
			_p.active_frame.onmouseup = this._prehandle_frame_mouseup;
			_p.active_frame.onmousemove = _p.manager._prehandle_mousemove;
			
			if(b) this.GetWindowContainer().appendChild(_p.active_frame);
			else document.body.appendChild(_p.active_frame);

		},
		moveTo:function(x, y, b){
		
			 if(typeof x == "number" || typeof x == "string") this.getContainer().style.left = x;
			 if(typeof y == "number" || typeof y == "string") this.getContainer().style.top = y;
		},
		_handle_frame_mouseup:function(e){
			var _p = this.getPointers();
			
			if((this.getStatus().can_resize && this.getStatus().is_resizing)|| (this.getStatus().can_move && this.getStatus().is_moving)){
				_p.active_frame.style.zIndex=this.getStatus().setFocusIndex;
				if(this.getStatus().is_resizing){
					this.getStatus().is_resizing = 0;
					this.resizeTo(_p.active_frame.clientWidth, _p.active_frame.clientHeight);
				}	
				if(this.getStatus().is_moving){
					this.moveTo(_p.active_frame.offsetLeft + "px",_p.active_frame.offsetTop + "px");
					this.getStatus().is_moving = 0;
				}
				org.cote.js.dom.event.disableMotionCapture(_p.active_frame);
				_p.active_frame.style.display = "none";
				_p.manager.ReturnFrameScreen();
				if(this.getStatus().mark_opacity >= 0){
					this.setOpacity(this.getStatus().mark_opacity);
					this.getStatus().mark_opacity = -1;
				}
			}
	
		},
		_handle_resize_mousedown:function(e){
			var evt = org.cote.js.dom.event._gevt(e);
			
			var _s = this.getStatus(),_p = this.getPointers();
      if(!_s.can_resize) return;
      
			var oS = org.cote.js.dom.event._gevt_src(e);
			if(!oS || (oS.getAttribute("is-button") != "1" && oS.parentNode.getAttribute("is-button") != "1")){
        return;
       }
			
			this.getFocus();
			
			_s.track_x = evt.clientX - this.getContainer().offsetLeft;
			_s.track_y = evt.clientY - this.getContainer().offsetTop;	
			_s.is_resizing = 1;
			
			this.translateDimensions(_p.active_frame,this.getContainer());

			_p.active_frame.style.zIndex = this.getStatus().moveFocusIndex;
			_p.active_frame.style.display = "block";	

			org.cote.js.dom.event.enableMotionCapture(_p.active_frame,_p.manager._prehandle_mousemove,this._prehandle_frame_mouseup);

		},
		_handle_title_mousedown:function(e){
			var evt = org.cote.js.dom.event._gevt(e);
			var _s = this.getStatus(),_p = this.getPointers();

      if(!_s.can_move) return;
			
			var oS = org.cote.js.dom.event._gevt_src(e);
			if(oS && oS.getAttribute("is-button") == "1") return;
			
			this.getFocus();			
			
			_s.track_x = evt.clientX - this.getContainer().offsetLeft;
			_s.track_y = evt.clientY - this.getContainer().offsetTop;	
			_s.is_moving = 1;
			
			
		
			this.translateDimensions(_p.active_frame,this.getContainer());

			_p.active_frame.style.zIndex = this.getStatus().moveFocusIndex;
			_p.active_frame.style.display = "block";	
			
			/// none / block is a fix for text selections in Mozilla
			this.getContainer().style.display = "none";
			_p.manager.RequestFrameScreen();
			this.getContainer().style.display = "block";
			this.getStatus().mark_opacity = this.getStatus().opacity;
			this.setOpacity(50);
			
			org.cote.js.dom.event.enableMotionCapture(_p.active_frame,_p.manager._prehandle_mousemove,this._prehandle_frame_mouseup);

		},
		resizeTo:function(x, y){
			this.getContainer().style.width = x + "px";
			this.getContainer().style.height = y + "px";
			this.flushBody();
			
		},
		flushBody : function(){
			
			try{
				var iH = (this.getContainer().clientHeight - this.getPointers().status_bar.offsetHeight - this.getPointers().title_bar.offsetHeight);
				if(iH > 0) this.getPointers().body.style.height = iH + "px";
				
			}
			catch(e){
				this.setStatus("Error adjusting interface dimensions");
				//(e.message ? e.message : e.description));
			}
			if(this.local_handle_resize) this.local_handle_resize(this);

			
		},
		translateDimensions:function(x,y){
			x.style.left = (!this.getIsBound() ? org.cote.js.dom.css.getAbsoluteLeft(this.getContainer()) : y.offsetLeft) + "px";
			x.style.top = (!this.getIsBound() ? org.cote.js.dom.css.getAbsoluteTop(this.getContainer()) : y.offsetTop) + "px";
			// document.title = x.style.left + ", " + x.style.top;
			x.style.width = y.clientWidth + "px";
			x.style.height = y.clientHeight + "px";
			
		},
		getIsDialog:function(){
			return this.getStatus().is_dialog;
		},
		setIsDialog:function(i){
				this.getStatus().is_dialog = i;

		},
		component_destroy:function(){
			this.getPointers().manager = null;
		},
		getFocus:function(){
			// if(this.getPointers().manager.getTopWindow() && this.getPointers().manager.getTopWindow()  != this && !this.getPointers().manager.getTopWindow().getIsDialog()) this.getPointers().manager.getTopWindow()._handle_lost_focus();
			if(!this.getPointers().manager.getTopWindow() || !this.getPointers().manager.getTopWindow().getIsDialog()){
				this.getPointers().manager.setFocus(this);
			}
			this.ToggleControls(1);
			this.getPointers().title_bar.className = "window_title window_title_focus";
			this.getPointers().status_bar.className = "window_status window_status_focus";
			this.getContainer().className = "window_gizmo window_gizmo_focus";
			if(this.local_handle_focus) this.local_handle_focus(this);
		},
		blur:function(){
			if(this.getPointers().manager.getTopWindow() == this) this.getPointers().manager.setFocus(null);
		},
		_handle_lost_focus:function(){
			// hide all controls that will bleed through; select, iframe
			this.ToggleControls(0);
			this.getPointers().title_bar.className = "window_title";
			this.getPointers().status_bar.className = "window_status";
			this.getContainer().className = "window_gizmo";
			if(this.local_handle_blur) this.local_handle_blur(this);
			
			
		},
		ToggleControls : function(b){
			var s = (!b ? "hidden" : "visible");
			var a = [];
			this.absorb(this.getPointers().body.getElementsByTagName("iframe"),a);
			this.absorb(this.getPointers().body.getElementsByTagName("select"),a);
			for(var i = 0; i < a.length; i++){
				a[i].style.visibility = s;
			}
		},
    animate : function(iCX,iCY,iDX,iDY,bHide,bFadeOut,bFadeIn,bScale,bScaleToWidth,bScaleToHeight,f){
      if(f) this.getPointers().local_handle_animation_complete = f;
      if(!this.getStatus().is_minimized){
				this.getPointers().body.style.display = "none";
			}
			this.hideButtons();
      this.getPointers().manager.animateMove(this, iCX, iCY, iDX, iDY, bHide,bFadeOut,bFadeIn,bScale,bScaleToWidth,bScaleToHeight);
    },
    _handle_animation_complete : function(){
			if(!this.getStatus().is_minimized){
				this.getPointers().body.style.display = "block";
				this.flushBody();
				// if(this.getStatus().is_modal && this.getPointers().manager.getTopWindow() == this) this.getPointers().manager.RequestScreen(this);
			}
			this.showButtons();
			if(this.getStatus().is_maximized || this.getStatus().late_focus){
				// if(this.getStatus().is_modal) this.getPointers().manager.RequestScreen(this);
				this.getFocus();
				this.getStatus().late_focus = 0;
			}
			
      if(this.getPointers().local_handle_animation_complete) this.getPointers().local_handle_animation_complete(this);
    },
    getBody : function(){
      return this.getPointers().body;
    },
    setOpacity : function(b){
		this.getStatus().opacity = b;
    	this.getContainer().style.filter = "Alpha(Opacity=" + b + ")";
		this.getContainer().style.opacity = (b/100); 
    },
		GetChangeCount : function(){
			return this.getStatus().change_count;
		},
		IncrementChangeCount : function(){
			this.getStatus().change_count++;
		},
		SetChangeCount : function(i){
			this.getStatus().change_count = i;
		}
		]]>
	</application-component>
</application-components>
