<?xml version="1.0" encoding="UTF-8"?>
<application-components>
	<!--
		CoreWeb Version:
			This version expects the following global variable exist:
				Hemi.hemi_base : the Web application path (eg: / or /CoreWeb/)
	-->
	
	<application-component id="window">
		<![CDATA[
		GetWindowContainer : function(){
			var oE = this.GetSpace();
			return (oE ? oE.space_element : document.body);
		},
		GetSpace : function(){
			return Hemi.app.space.service.getSpace(this.getReferenceId());
		},
		GetWindowManager : function(){
			var oM = Hemi.registry.service.getObject("manager");
			if(!oM){
				Hemi.app.createApplicationComponent("manager", 0, Hemi.app.space.service.getPrimarySpace(), "manager");
				oM = Hemi.registry.service.getObject("manager");
			}
			return oM;
		},
		component_init:function(){

			var oManager, _p = this.getObjects(),_s = this.getProperties();

			
			oManager = this.GetWindowManager();
			if(!oManager){
				alert("Could not find manager");
				this.destroy();
				return;
			}

			Hemi.include("hemi.css");
			var oS = Hemi.css.loadStyleSheet("Styles/GizmoStyles.css","GizmoStyles");

			_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.scopeHandler("title_mousedown",0,0,1);
			this.scopeHandler("frame_mouseup",0,0,1);
			this.scopeHandler("frame_mousemove",0,0,1);
			this.scopeHandler("minimize_click",0,0,1);
			this.scopeHandler("maximize_click",0,0,1);
			this.scopeHandler("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 = "window_button 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 = "window_button 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 = "window_button 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.getObjects();
			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){
				_p.body = document.createElement("div");
				_p.body.setAttribute("is-body","1");
				_p.body.appendChild(document.createTextNode("[ ... awaiting content ... ]"));
				this.getContainer().appendChild(_p.body);
			}

			_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 = "window_button button_resize";
			 _p.status_bar.appendChild(_p.resize_button);
			
			 this.scopeHandler("resize_mousedown",0,0,1);
			_p.resize_button.onmousedown = this._prehandle_resize_mousedown;

			var iH = (this.getContainer().clientHeight - _p.status_bar.offsetHeight - _p.title_bar.offsetHeight);
			if(iH <= 0){
				setTimeout("Hemi.registry.service.getObject('" + this.getObjectId() + "').flushBody()",50);
			}
			else _p.body.style.height = iH + "px";
			_p.manager.RegisterWindow(this.getObjectId());
			var sTemplate = this.getContainer().getAttribute("template");
			this.loadTemplate(sTemplate);

		},
		
		center : function(){
			var _s = this.getProperties(), _p = this.getObjects();
			if(_s.is_maximized || _s.is_minimized) return 0;
			_p.manager.CenterWindow(this);
		},

		getTemplateContainer : function(){
		  return this.getObjects().body;
		},
		getMinimizeType : function(){
				return this.getProperties().minimize_type;
		},
		getMinimizeMarker : function(){
				return this.getObjects().minimize_marker;
		},
		setMinimizeMarker : function(o){
				this.getObjects().minimize_marker = o;
		},
		setMinimizeType : function(s){
				this.getProperties().minimize_type = s;
		},
		setManageMinimize : function(b){
		  this.getProperties().manage_minimize = b;
		},
		setManageMaximize : function(b){
		  this.getProperties().manage_maximize = b;
		},
		getManageMinimize : function(){
		  return this.getProperties().manage_minimize;
		},
		getManageMaximize : function(){
		  return this.getProperties().manage_maximize;
		},
 		absorb : function(a,b){
			var i = 0, l = a.length;
			for(;i<l;)
				b[b.length] = a[i++];
		},

		setIsModal : function(b){
		  this.getProperties().is_modal = b;
		  if(b && !this.getIsMinimized()) this.getObjects().manager.RequestScreen(this);
		  else if(!this.getIsMinimized()) this.getObjects().manager.ReturnScreen();
		  this.setCanResize(!b);
		  this.setCanMove(!b);
		  // this.restore();
		},
	    
		hideButtons : function(){
		  var _p = this.getObjects();
		  _p.minimize_button.style.display = "none";
		  _p.close_button.style.display = "none";
		  _p.maximize_button.style.display = "none";
		},
		showButtons : function(){
		  var _p = this.getObjects();
		  _p.minimize_button.style.display = "block";
		  _p.close_button.style.display = "block";
		  _p.maximize_button.style.display = "block";
		},
		setCanMaximize : function(b){
				this.getProperties().can_maximize = b;
		},
		setCanMinimize : function(b){
				this.getProperties().can_minimize = b;
		},
		setCanMove : function(b){
		  this.getProperties().can_move = b;
		},
		setCanResize : function(b){
		  this.getProperties().can_resize = b;
		  this.getObjects().resize_button.style.display = (b ? "block" : "none");
		},
		_handle_maximize_click : function(e){
				var _s = this.getProperties();
				if(_s.is_maximized){
					this.restore();
				}
				else{
					this.maximize();
				}
				Hemi.event.cancelEvent(Hemi.event.getEvent(e));
		  return false;
		},
		maximize : function(){
    		var _s = this.getProperties(), _p = this.getObjects();
				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.getObjects().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(" + Hemi.hemi_base + "Graphics/xp_norm_ico.gif)";
			_p.maximize_button.className = "window_button button_maximize button_normalize";
	      
		  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.getProperties();
			if(_s.is_minimized){
				this.getProperties().late_focus = _s.manage_minimize;
				this.restore(_s.manage_minimize);
			}
			else{
				this.minimize();
			}
		  Hemi.event.cancelEvent(Hemi.event.getEvent(e));
		  return false;
		},
	    minimize : function(){

			var _s = this.getProperties(), _p = this.getObjects();
			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.getObjects().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(" + Hemi.hemi_base + "Graphics/xp_norm_ico.gif)";
			_p.minimize_button.className = "window_button button_minimize button_normalize";

			if(this.local_handle_minimize) this.local_handle_minimize(this);

			if(_s.manage_minimize){
				_p.manager.ManageMinimize(this);
			}
		},
		
		restore : function(b){
			var _s = this.getProperties(), _p = this.getObjects();

			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.getObjects().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.className = "window_button button_minimize";
				_p.maximize_button.className = "window_button button_maximize";
				//_p.minimize_button.style.backgroundImage = "url(" + Hemi.hemi_base + "Graphics/xp_min_ico.gif)";
				//_p.maximize_button.style.backgroundImage = "url(" + Hemi.hemi_base + "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.getProperties().hide_on_close && this.getProperties().is_closed){
			this.getContainer().style.display = "block";
			this.getProperties().is_closed = 0;
			return;
		  }
		},
		_handle_close_click : function(e){
			this.Close();
			Hemi.event.cancelEvent(Hemi.event.getEvent(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.getObjects(),o = this.getContainer();
			this.getProperties().is_closed = 1;

			if(this.getProperties().is_modal) this.getObjects().manager.ReturnScreen();
			this.blur();
      
			if(this.getProperties().manage_minimize) _p.manager.Tile();
			if(this.getProperties().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.getProperties().is_minimized;
		},
		getIsMaximized : function(){
		  return this.getProperties().is_maximized;
		},
		setHideOnClose :function(b){
		   this.getProperties().hide_on_close = b;
		},
		getHideOnClose : function(){
		  return this.getProperties().hide_on_close;
		},
		getIsClosed : function(){
		  return this.getProperties().is_closed;
		},
		setTitle : function(s){
			Hemi.xml.setInnerXHTML(this.getObjects().title_label,s);

		},
		setStatus : function(s){
			Hemi.xml.setInnerXHTML(this.getObjects().status_label,s);
		},
		clearContents : function(){
			var _p = this.getObjects();
			if(_p.child_engine) Hemi.app.space.service.clearEngine(_p.child_engine);
			Hemi.xml.removeChildren(_p.body);
		},
		getIsBound:function(){
			return this.getProperties().is_bound;
		},
		setIsBound:function(b){
			this.getProperties().is_bound = b;
			var _p = this.getObjects();
			_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.getObjects();
			
			if((this.getProperties().can_resize && this.getProperties().is_resizing)|| (this.getProperties().can_move && this.getProperties().is_moving)){
				_p.active_frame.style.zIndex=this.getProperties().setFocusIndex;
				if(this.getProperties().is_resizing){
					this.getProperties().is_resizing = 0;
					this.resizeTo(_p.active_frame.clientWidth, _p.active_frame.clientHeight);
				}	
				if(this.getProperties().is_moving){
					this.moveTo(_p.active_frame.offsetLeft + "px",_p.active_frame.offsetTop + "px");
					this.getProperties().is_moving = 0;
				}
				Hemi.event.disableMotionCapture(_p.active_frame);
				_p.active_frame.style.display = "none";
				_p.manager.ReturnFrameScreen();
				if(this.getProperties().mark_opacity >= 0){
					this.setOpacity(this.getProperties().mark_opacity);
					this.getProperties().mark_opacity = -1;
				}
			}
	
		},
		_handle_resize_mousedown:function(e){
			var evt = Hemi.event.getEvent(e);
			
			var _s = this.getProperties(),_p = this.getObjects();
      if(!_s.can_resize) return;
      
			var oS = Hemi.event.getEventSource(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.getProperties().moveFocusIndex;
			_p.active_frame.style.display = "block";	

			Hemi.event.enableMotionCapture(_p.active_frame,_p.manager._prehandle_mousemove,this._prehandle_frame_mouseup);

		},
		_handle_title_mousedown:function(e){
			var evt = Hemi.event.getEvent(e);
			var _s = this.getProperties(),_p = this.getObjects();

      if(!_s.can_move) return;
			
			var oS = Hemi.event.getEventSource(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.getProperties().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.getProperties().mark_opacity = this.getProperties().opacity;
			this.setOpacity(50);
			
			Hemi.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.getObjects().status_bar.offsetHeight - this.getObjects().title_bar.offsetHeight);
				if(iH > 0) this.getObjects().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() ? Hemi.css.getAbsoluteLeft(this.getContainer()) : y.offsetLeft) + "px";
			x.style.top = (!this.getIsBound() ? Hemi.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.getProperties().is_dialog;
		},
		setIsDialog:function(i){
				this.getProperties().is_dialog = i;

		},
		component_destroy:function(){
			this.getObjects().manager = null;
		},
		getFocus:function(){
			// if(this.getObjects().manager.getTopWindow() && this.getObjects().manager.getTopWindow()  != this && !this.getObjects().manager.getTopWindow().getIsDialog()) this.getObjects().manager.getTopWindow()._handle_lost_focus();
			if(!this.getObjects().manager.getTopWindow() || !this.getObjects().manager.getTopWindow().getIsDialog()){
				this.getObjects().manager.setFocus(this);
			}
			this.ToggleControls(1);
			this.getObjects().title_bar.className = "window_title window_title_focus";
			this.getObjects().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.getObjects().manager.getTopWindow() == this) this.getObjects().manager.setFocus(null);
		},
		_handle_lost_focus:function(){
			// hide all controls that will bleed through; select, iframe
			this.ToggleControls(0);
			this.getObjects().title_bar.className = "window_title";
			this.getObjects().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.getObjects().body.getElementsByTagName("iframe"),a);
			this.absorb(this.getObjects().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.getObjects().local_handle_animation_complete = f;
      if(!this.getProperties().is_minimized){
				this.getObjects().body.style.display = "none";
			}
			this.hideButtons();
      this.getObjects().manager.animateMove(this, iCX, iCY, iDX, iDY, bHide,bFadeOut,bFadeIn,bScale,bScaleToWidth,bScaleToHeight);
    },
    _handle_animation_complete : function(){
			if(!this.getProperties().is_minimized){
				this.getObjects().body.style.display = "block";
				this.flushBody();
				// if(this.getProperties().is_modal && this.getObjects().manager.getTopWindow() == this) this.getObjects().manager.RequestScreen(this);
			}
			this.showButtons();
			if(this.getProperties().is_maximized || this.getProperties().late_focus){
				// if(this.getProperties().is_modal) this.getObjects().manager.RequestScreen(this);
				this.getFocus();
				this.getProperties().late_focus = 0;
			}
			
      if(this.getObjects().local_handle_animation_complete) this.getObjects().local_handle_animation_complete(this);
    },
    getBody : function(){
      return this.getObjects().body;
    },
    setOpacity : function(b){
		this.getProperties().opacity = b;
    	this.getContainer().style.filter = "Alpha(Opacity=" + b + ")";
		this.getContainer().style.opacity = (b/100); 
    },
		GetChangeCount : function(){
			return this.getProperties().change_count;
		},
		IncrementChangeCount : function(){
			this.getProperties().change_count++;
		},
		SetChangeCount : function(i){
			this.getProperties().change_count = i;
		}
		]]>
	</application-component>
</application-components>
