
// desktop object
  Ext.Desktop = function(){

    var desktop   = Ext.get('x-desktop');
    var taskbar   = Ext.get('x-taskbar');
    var windows   = new Ext.WindowGroup();
    windows.zseed = 100;
    var activeWindow;
    
    function saveState(win){
      var size = win.getSize();
      Ext.state.Manager.set(win.id+'-x', win.x);
      Ext.state.Manager.set(win.id+'-y', win.y);
      Ext.state.Manager.set(win.id+'-w', size.width);
      Ext.state.Manager.set(win.id+'-h', size.height);
    }
    
    function restoreState(win){
      win.x       = (Ext.state.Manager.get(win.id+'-x')) ? Ext.state.Manager.get(win.id+'-x') : win.x;
      win.y       = (Ext.state.Manager.get(win.id+'-y')) ? Ext.state.Manager.get(win.id+'-y') : win.y;
      win.width   = (Ext.state.Manager.get(win.id+'-w')) ? Ext.state.Manager.get(win.id+'-w') : win.width;
      win.height  = (Ext.state.Manager.get(win.id+'-h')) ? Ext.state.Manager.get(win.id+'-h') : win.height;
    }
    
    function minimizeWin(win){
      win.minimized = true;
      win.hide();
    }
    
    function markActive(win){
      if(activeWindow && activeWindow != win){
        markInactive(activeWindow);
      }
      activeWindow = win;
      activeWindow.addClass('x-window-focus');
      win.minimized = false;
    }
    
    function markInactive(win){
      if(win == activeWindow){
        activeWindow.removeClass('x-window-focus');
        activeWindow = null;
      }
    }
    
    function removeWin(win){
      win.destroy();
      layout();
    }
    
    function layout(){
			//console.log(taskbar.getHeight());
      desktop.setHeight(Ext.lib.Dom.getDocumentHeight()-(taskbar ? taskbar.getHeight() : 0) - 1);
		}
    Ext.EventManager.onWindowResize(layout);
    
    this.layout = layout;
    
    this.createWindow = function(config, cls){
       if(config.w) {
         config.width    = config.w;
         config.minWidth = config.w;
       }
       if(config.h) {
         config.height    = config.h;
         config.minHeight = config.h;
       }
       var win = new (cls||Ext.Window)(
         Ext.applyIf(config||{}, {
           manager: windows,
           minimizable: false,
           maximizable: true
         })
       );
       
       /*
if(!config.restoreState == false) {
         //restoreState(win);
       }
*/
       win.render(desktop);
       //win.taskItem =  new Ext.Desktop.TaskBarItem(win);
      
       win.on('activate', markActive);
       win.on('beforeshow', markActive);
       win.on('deactivate', markInactive);
       win.on('minimize', minimizeWin);
       win.on('close', removeWin);
       //win.on('move', saveState);
       //win.on('resize', saveState);
       layout();
       return win;
    };

    this.getManager = function(){
      return windows;
    }
    
    this.getWindow = function(id){
      return windows.get(id);
    }
    
    this.getWinWidth = function(){
      var width = Ext.lib.Dom.getViewWidth();
      return width < 200 ? 200 : width;
    }
    
    this.getWinHeight = function(){
      var height = (Ext.lib.Dom.getViewHeight() - 302);
      return height < 100 ? 100 : height;
    }
    
    this.getWinX = function(width){
      return (Ext.lib.Dom.getViewWidth() - width) / 2
    }
    
    this.getWinY = function(height){
      return (Ext.lib.Dom.getViewHeight() - height) / 2;
    }
    
    this.getHeight = function(){
      return desktop.getHeight();
    }
    
    this.removeWin = removeWin;
    
    layout();
  };

