<?xml version="1.0" encoding="UTF-8"?>
<application-components>
  <!--
			Reference: http://www.imnmotion.com/documents/html/technical/dhtml/mdiOverview.html
			The 'manager' component's mousemove code was adapted from the MDI CContainer class.
			The 'window' component's behavior and delegation to 'manager' was modeled after the MDI CWindow class.
			Much of the MDI coding guff was completely dropped

	-->
  <application-component id="manager">
    <![CDATA[
			component_init:function(){
				// bug note: getContainerId is coming through as an object because the component is being tied directly to the engine object
				// but, the getContainer() correctly points to the engine HTML element
				//
				if(typeof this.getContainerId() != "object"){
					alert("Invalid container reference.  terminating object");
					this.destroy();
					return;
				}
				
				var _s = this.getStatus();
				this.setTopWindow(null);
				this.createHandler("mousemove",0,0,1);
				
				org.cote.js.dom.event.addEventListener(this.getContainer(),"mousemove",this._prehandle_mousemove);

				_s.mod_x = 0;
				_s.mod_y = 0;
				
				_s.animate_delay = 10;
				_s.animate_step = 40;
				_s.animate_count = 0;
        
        _s.tile_top = 10;
        _s.tile_left = 10;
        _s.tile_width = 200;
        _s.tile_height = 20;
        
        _s.cascade_height = 250;
        _s.cascade_width = 450;
        
        this.getPointers().frame_screening = 0;
        this.getPointers().frame_screen = 0;
        this.getPointers().screen = 0;
				this.getPointers().screen_owner = 0;
        this.getPointers().windows = [];
        
        _s.screening = 0;
        this.createHandler("window_resize",0,0,1);
        org.cote.js.dom.event.addEventListener(window,"resize",this._prehandle_window_resize);
			},
      _handle_window_resize : function(){
        this.RepositionScreen();
        this.Tile();
      },
      RegisterWindow : function(i){
        var a = this.getPointers().windows;
        a[a.length] = i;
      },
      UnRegisterWindow : function(d){
				var x = -1, i = 0, _p = this.getPointers();
				for(; i < _p.windows.length;i++ ){
					if(_p.windows[i] == d){
						x = i;
						break;
					}
				}
				var w = _p.windows;
				if(x >= 0){
					w.splice(x,1);
				}
      },
      
      ReturnFrameScreen: function(b){
        var _p = this.getPointers();
        this.getStatus().frame_screening = 0;
        if(!b && _p.frame_screen) _p.frame_screen.style.display = "none";
      },
      RequestFrameScreen : function(){
        var _p = this.getPointers();
        this.ReturnFrameScreen(1);
        if(!_p.frame_screen){
          _p.frame_screen = document.createElement("div");
          _p.frame_screen.style.position = "absolute";
          _p.frame_screen.style.display = "block";
          _p.frame_screen.style.zIndex = 17;
          _p.frame_screen.style.top = "0px";
          _p.frame_screen.style.left = "0px";
          // _p.frame_screen.style.backgroundColor = "#CFCFCF";
          _p.frame_screen.onselectstart = function(){return false;};
          _p.frame_screen.onmousedown = function(){return false;};
	        document.body.appendChild(_p.frame_screen);
        }
        _p.frame_screen.style.display = "block";
        this.getStatus().frame_screening = 1;
        this.RepositionFrameScreen();
      
      },
      
      RepositionFrameScreen : function(){
        if(!this.getStatus().frame_screening) return;
        var _p = this.getPointers();
        var de = document.documentElement;
        _p.frame_screen.style.width = "1px";
        _p.frame_screen.style.height = "1px";
        _p.frame_screen.style.width = ((de.scrollWidth > de.offsetWidth ? de.scrollWidth : de.offsetWidth) - 20) + "px";
        _p.frame_screen.style.height = ((de.scrollHeight > de.offsetHeight ? de.scrollHeight : de.offsetHeight) - 20) + "px";

      },
	    ReturnScreen: function(b){
        var _p = this.getPointers();
        this.getStatus().screening = 0;
        if(!b && _p.screen) _p.screen.style.display = "none";
        _p.screen_owner = 0;
      },
      RequestScreen : function(o){
        var _p = this.getPointers();
        this.ReturnScreen(1);
        _p.screen_owner = o;
        //o.getContainer().style.display = "none";
        if(!_p.screen){
          _p.screen = document.createElement("div");
          _p.screen.className = "gizmo_screen";
          _p.screen.style.display = "block";
          // _p.screen.style.width = "100%";
          // _p.screen.style.height = "100%";
          _p.screen.bound = o.getIsBound();
          if(o.getIsBound()) this.getContainer().appendChild(_p.screen);
          else document.body.appendChild(_p.screen);
        }
        _p.screen.style.display = "block";
        //o.getContainer().style.display = "block";
        this.getStatus().screening = 1;
        this.RepositionScreen();
        
      },
      RepositionScreen : function(){
        if(!this.getStatus().screening) return;
        
        var _p = this.getPointers();
        var de = document.documentElement;
        _p.screen.style.width = "1px";
        _p.screen.style.height = "1px";
        var o = de;
        if(_p.screen.bound) o = this.getContainer();
        _p.screen.style.width = (o.scrollWidth > o.offsetWidth ? o.scrollWidth : o.offsetWidth) + "px";
        _p.screen.style.height = (o.scrollHeight > o.offsetHeight ? o.scrollHeight : o.offsetHeight) + "px";

      },
			_handle_mousemove:function(e){
						
				var evt=org.cote.js.dom.event._gevt(e);
				
				if(this.getTopWindow() && (this.getTopWindow().getStatus().is_moving || this.getTopWindow().getStatus().is_resizing)){
					
					var iX = evt.clientX;
					var iY = evt.clientY;
					var oObj = this.getContainer();
					
					var oWin = this.getTopWindow();
					// var oCont= oWin.getContainer();
					var oFr = oWin.getPointers().active_frame;
					if(oWin.getStatus().is_resizing){
						var oTemp= (oWin.getIsBound() ? oObj : document.documentElement);
						var iNewW=iX - oFr.offsetLeft - oTemp.offsetLeft + document.documentElement.scrollLeft;
						var iNewH=iY - oFr.offsetTop - oTemp.offsetTop + document.documentElement.scrollTop;
						if(oWin.getStatus().is_bound){
							var iTop3 = oObj.offsetTop + oObj.offsetHeight +  this.getStatus().mod_y;
							var iLeft3 = oObj.offsetLeft + oObj.offsetWidth +  this.getStatus().mod_x;
							if(iY >= iTop3 || iX >= iLeft3) return;
						}
						if(iNewH > 50 && iNewW > 50){
							oFr.style.height = iNewH + "px";
							oFr.style.width = iNewW + "px";
						}
						
					}
					if(oWin.getStatus().is_moving){
						/*
							Unbound windows should be sized against their parent.
						*/
						var oTemp= (oWin.getIsBound() ? oObj : document.documentElement);

						/*
							Bound or unbound, windows should not be able to be moved into a position where the 
							user can't "grab" the title bar.  In this case, the window can't be moved beyond the point where
							the mouse pointer exits the frame.
						*/

						var iX2 = org.cote.js.dom.event.getAbsoluteLeft(oTemp) + oTemp.clientWidth;
						var iY2 = org.cote.js.dom.event.getAbsoluteTop(oTemp) + oTemp.clientHeight;

						iX=(iX < 0)?0:(iX > iX2)?iX2:iX;
						iY=(iY < 0)?0:(iY > iY2)?iY2:iY;
						var iNewX=iX - oWin.getStatus().track_x;
						var iNewY=iY - oWin.getStatus().track_y;
						
						if(!oWin.getIsBound()){
							iNewY=(iNewY<0)?0:iNewY;
							oFr.style.left = iNewX + "px";
							oFr.style.top = iNewY + "px";
						}
						else{
						
							var iLeft1=oFr.offsetLeft + oObj.offsetLeft;
							var iLeft2=oObj.offsetLeft;
							var iLeft3=iLeft1 + oFr.offsetWidth;
							var iLeft4=iLeft2 + oObj.offsetWidth;
							var iTop1=oFr.offsetTop + oObj.offsetTop;
							var iTop2=org.cote.js.dom.event.getAbsoluteTop(oObj);
							var iTop3=iTop1 + oFr.offsetHeight;
							var iTop4=iTop2 + oObj.offsetHeight;
							// window.status=iTop4;		
							if((iLeft1>=iLeft2)&&(iLeft3<=iLeft4)){
								var iCheckFarLeft=oObj.offsetWidth - oFr.offsetWidth - this.getStatus().mod_x;
								if(iNewX<0){
									iNewX=0;
								}
								if(iNewX>iCheckFarLeft){
									iNewX=iCheckFarLeft;
								}
								oFr.style.left = iNewX + "px";
							}
							else{
								if(iLeft3>iLeft4){
									oFr.style.left = (iLeft4 - iLeft2 - oFr.offsetWidth) + "px";
								}
							}
							if((iTop1>=iTop2)&&(iTop3<=iTop4)){
								var iCheckFarTop=oObj.offsetHeight - oFr.offsetHeight - this.getStatus().mod_y;
								if(iNewY<0){
									iNewY = 0;
								}
								if(iNewY>iCheckFarTop){
									iNewY = iCheckFarTop;
								}
								oFr.style.top = iNewY + "px";
							}
							else{
								if(iTop3>iTop4){
									oFr.style.top=(iTop4 - iTop2 - oFr.offsetHeight) + "px";
								}
							}			
					 } // end if bound
					} // end if is moving
				} // end if valid window object

			},
			getTopWindow:function(){
				return this.getPointers().top_window;
			},
			setTopWindow:function(o){
			  this.getPointers().top_window = o;
			},
			setFocus:function(o){
				if(this.getTopWindow() && this.getTopWindow() != o){
					var y = this.getTopWindow()
					y.getContainer().style.zIndex = y.getStatus().lostFocusIndex;
					y._handle_lost_focus();
				}
				this.setTopWindow(o);
				if(o)
					o.getContainer().style.zIndex = (o.getStatus().is_dialog ? o.getStatus().dialogFocusIndex : o.getStatus().setFocusIndex);
				
			},
			
			Cascade : function(){
				var _p = this.getPointers(), o, i=0, iC = 0, iL = 1,l;
				var iDT = (document.body.offsetHeight > document.documentElement.offsetHeight ? document.body.offsetHeight : document.documentElement.offsetHeight);
				var iOT = this.getContainer().offsetHeight;
				
 				for(; i < _p.windows.length; i++){
					o = org.cote.js.registry.ObjectRegistry.getObject(_p.windows[i]);
					if(!o.getStatus().can_move) continue;
					var iUT = o.getIsBound() ? iOT : iDT;
					var iT = (iC * 50);
					l = (iL * 25) + (iC * 25);
					o.blur();
					if(o.getIsMinimized()){
						b = o.getStatus().manage_minimize;	
						o.getStatus().manage_minimize = 0;
						o.restore(1);
						o.getStatus().manage_minimize = b;
					}
					

					// o.resizeTo(250,250);
					if((iT + this.getStatus().cascade_height) > iUT){
						iL += 5;
						iC = 0;
						iT = 0;
						l = (iL * 25) + (iC * 25);
						
					}
					iC++;
					
					o.animate(o.getContainer().offsetLeft,o.getContainer().offsetTop,l,iT,0,0,0,1,this.getStatus().cascade_width,this.getStatus().cascade_height);
					
				}

          // _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);
      },
      ManageMaximize : function(o){
        var iDT = (document.body.offsetHeight > document.documentElement.offsetHeight ? document.body.offsetHeight : document.documentElement.offsetHeight);
        var iDW = (document.body.offsetWidth > document.documentElement.offsetWidth ? document.body.offsetWidth : document.documentElement.offsetWidth);
        
        var iOT = this.getContainer().offsetHeight;
        var iOW = this.getContainer().offsetWidth;
        
        var iUW = o.getIsBound() ? iOW : iDW;
        var iUT = o.getIsBound() ? iOT : iDT;
        this.animateMove(o,o.getContainer().offsetLeft,o.getContainer().offsetTop,0,0,0,0,0,1,iUW,iUT);
      },
      
      ManageMinimize : function(o){
        this.Tile();
      },
      
      TileAll : function(){
				var _p = this.getPointers(), i = 0, o, b;
				for(; i < _p.windows.length; i++){
					o = org.cote.js.registry.ObjectRegistry.getObject(_p.windows[i]);
					if(!o.getIsMinimized() && o.getStatus().can_minimize){
						b = o.getStatus().manage_minimize;
						o.getStatus().manage_minimize = 0;
						o.minimize();
						o.getStatus().manage_minimize = b;
					}
				}
				this.Tile();
      },
      Tile : function(){
        var a = [], _p = this.getPointers(), o, i = 0, _sw, iC = 0, iCT=0;
        var iDT = (document.body.offsetHeight > document.documentElement.offsetHeight ? document.body.offsetHeight : document.documentElement.offsetHeight);
        var iDW = (document.body.offsetWidth > document.documentElement.offsetWidth ? document.body.offsetWidth : document.documentElement.offsetWidth);
        
        var iOT = this.getContainer().offsetHeight;
        var iOW = this.getContainer().offsetWidth;
        for(; i < _p.windows.length; i++){
          o = org.cote.js.registry.ObjectRegistry.getObject(_p.windows[i]);
          var iUT = o.getIsBound() ? iOT : iDT;
          var iUW = o.getIsBound() ? iOW : iDW;
          _sw = o.getStatus();
          if(o.getIsMinimized() && o.getStatus().can_move && !o.getStatus().is_closed && _sw.manage_minimize){
            var iL = this.getStatus().tile_left + (iC * (this.getStatus().tile_width + 5));
            var iT = iUT - ((this.getStatus().tile_top + o.getContainer().offsetHeight) * (iCT + 1));
            if((iL + o.getContainer().offsetWidth) > iUW){
							iCT++;
							iC = 0;
	            iL = this.getStatus().tile_left + (iC * (this.getStatus().tile_width + 5));
	            iT = iUT - ((this.getStatus().tile_top + o.getContainer().offsetHeight) * (iCT + 1));
            }
            this.animateMove(o,o.getContainer().offsetLeft,o.getContainer().offsetTop,iL,iT,0,0,0,1,this.getStatus().tile_width,this.getStatus().tile_height);
            iC++;
          }
        }
        
      },
			

       animateObjectTo : function(o,iX,iY){

	      var iN = Math.random();
	      iN = 1; // (iN < .5 ? -1 : 1);
	      var iN2 = Math.random();
	      iN2 = 1; //(iN2 < .5 ? -1 : 1);
	      var iTX = (Math.random() * document.documentElement.offsetWidth)  * iN;
	      var iTY = (Math.random() * document.documentElement.offsetHeight) * iN2;
	      this.animateMove(o,iTX,iTY,iX,iY);
      },

      animateMove : function(o,iCX,iCY,iDX,iDY,bHide,bFadeOut,bFadeIn,bScale,bScaleToWidth,bScaleToHeight){
       
         if(!org.cote.js.registry.ObjectRegistry.isRegistered(o)) return;
	     
         var iDistX=iDX - iCX;
	      var iDistY=iDY - iCY;
	      var iDist=Math.sqrt(Math.pow(iDistX,2) + Math.pow(iDistY,2));
	     var iSNum=iDist/this.getStatus().animate_step;
	      var iMX=iDistX/iSNum;
	      var iMY=iDistY/iSNum;
      	
				o.getStatus().expected_x = iDX;
				o.getStatus().expected_y = iDY;
        o.getStatus().expected_h = bScaleToHeight;
        o.getStatus().expected_w = bScaleToWidth;
	      this.getStatus().animate_count++;
	      this.animateMotion(o.getObjectId(),iMX,iMY,iDX,iDY,iCX,iCY,iDist,bHide,bFadeOut,bFadeIn,bScale,bScaleToWidth,bScaleToHeight);
        
      },


    animateMotion : function(sId,iMX,iMY,iDX,iDY,iCX,iCY,iOrgDist,bHide,bFadeOut,bFadeIn,bScale,bScaleToWidth,bScaleToHeight){

 			var o = org.cote.js.registry.ObjectRegistry.getObject(sId);
			if(!o || o.getReadyState() != 4 || typeof o.getContainer != "function" || o.getContainer() == null) return;

	    var iPerc = parseInt((Math.sqrt(Math.pow(iDX - iCX,2) + Math.pow(iDY - iCY,2)) / iOrgDist) * 100);

	    if(

		    Math.floor(Math.abs(iMX)) < Math.floor(Math.abs(iDX - iCX))
		    ||
		    Math.floor(Math.abs(iMY)) < Math.floor(Math.abs(iDY - iCY))
	    ){
		    iCX+=iMX;
		    iCY+=iMY;
		    //vWin.moveTo(iCX,iCY);
		    o.getContainer().style.top = iCY + "px";
		    o.getContainer().style.left = iCX + "px";
		    if(bFadeOut || bFadeIn){
					o.setOpacity((bFadeIn ? 100 - iPerc : iPerc));
					/*
			    o.getContainer().style.filter = "Alpha(Opacity=" + (bFadeIn ? 100 - iPerc : iPerc) + ")";
			    o.getContainer().style.opacity = ((bFadeIn ? (100 - iPerc) : iPerc)/100); 
			    */
		    }
		    if(bScale){
          var iWD = o.getContainer().offsetWidth - bScaleToWidth;
          var iHD = o.getContainer().offsetHeight - bScaleToHeight;
          //document.title = o.getContainer().offsetHeight - (iHD * (iPerc/100));
			    o.getContainer().style.width = ((o.getContainer().offsetWidth >= bScaleToWidth ? o.getContainer().offsetWidth - (iWD * (iPerc/100)) : bScaleToWidth * ((100 - iPerc)  /100))) + "px";
			    o.getContainer().style.height = ((o.getContainer().offsetHeight >= bScaleToHeight ? o.getContainer().offsetHeight - (iHD * (iPerc/100)): bScaleToHeight * ((100 - iPerc)  /100))) + "px";
    			
		    }
				
		    if(bFadeOut){
			    if(iCY > document.documentElement.offsetHeight || iCY < 0
			    ||
			    iCX > document.documentElement.offsetWidth || iCX < 0
			    ){
				    if(bScale){
					    o.getContainer().style.width = bScaleToWidth + "px";
					    o.getContainer().style.height = bScaleToHeight + "px";
				    }
				    if(bHide)
				    {
					    o.getContainer().style.display = "none";
				    }
				    else if(bFadeIn){
							o.setOpacity(100);
				    /*
					    o.getContainer().style.filter = "Alpha(Opacity=100)";
					    o.getContainer().style.opacity = 1; 
					   */
				    }
				    this.getStatus().animate_count--;
				    if(typeof o._handle_animation_complete == "function") o._handle_animation_complete();
				    return;
			    };
		    }
 				o.getStatus().animated = 1;
        var sAFP="org.cote.js.registry.ObjectRegistry.getObject('" + this.getObjectId() + "').animateMotion('" + sId + "'," + iMX + "," + iMY + "," + iDX + "," + iDY + "," + iCX + "," + iCY + "," + iOrgDist + "," + bHide + "," + bFadeOut + "," + bFadeIn + "," + bScale + "," + bScaleToWidth + "," + bScaleToHeight + ")";
		    window.setTimeout(sAFP,this.getStatus().animate_delay);
	    }
	    else{
		    this.getStatus().animate_count--;

		    if(bScale){
			    o.getContainer().style.width = bScaleToWidth + "px";
			    o.getContainer().style.height = bScaleToHeight + "px";
		    }

		    if(bHide)
		    {
			    o.getContainer().style.display = "none";
		    }
		    else if(bFadeIn){
			    o.getContainer().style.filter = "Alpha(Opacity=100)";
			    o.getContainer().style.opacity = 1; 
		    }

		    o.getContainer().style.top = iDY + "px";
		    o.getContainer().style.left = iDX + "px";
    		
		    if(typeof o._handle_animation_complete == "function") o._handle_animation_complete();
	    }
    }
    
		]]>
  </application-component>
  
</application-components>