Uwd.System.Desktop = function(){
	var desktop = Ext.get('x-desktop');
    
	var wbar = Ext.get('x-windows');
	if (window.system.isFisheye == true) {
		Ext.get('x-windows').setHeight(3);
		var taskbar = Ext.get('dock-container2');
	} else {
		var taskbar = Ext.get('x-taskbar');
	}
	var deskButtons = [];
	
	var contextMenu = new Uwd.System.Desktop.ContextMenu();
	this.onContext = function(e) {
		e.preventDefault();
		contextMenu.show(e);
	};
	desktop.on('contextmenu', this.onContext, this);
    
	var windows = new Ext.WindowGroup({
		zseed: 5000
	});
	
	
	windows.zseed = 5000;
	
	Ext.WindowMgr.zseed = 9000;
	
    var activeWindow;

    function minimizeWin(win){
        win.minimized = true;
        win.hide();
    }

    function markActive(win){
        if(activeWindow && activeWindow != win){
            markInactive(activeWindow);
        }
        activeWindow = win;
        Ext.fly(win.taskItem.el).addClass('active-win');
        win.minimized = false;
    }

    function markInactive(win){
        if(win == activeWindow){
            activeWindow = null;
            Ext.fly(win.taskItem.el).removeClass('active-win');
        }
    }
	
    this.layout = function() {
		if (Ext. isIE6) {
			desktop.setHeight(Ext.lib.Dom.getViewHeight()-(taskbar ? taskbar.getHeight() : 0)-wbar.getHeight() - 17);
		} else {
			desktop.setHeight(Ext.lib.Dom.getViewHeight()-(taskbar ? taskbar.getHeight() : 0)-wbar.getHeight() + 3);
		}
		desktop.setWidth(Ext.lib.Dom.getViewWidth());
    };
	
	
	mainObject = this;
    function removeWin(win){
        win.taskItem.destroy();
        mainObject.layout();
    }
	
    
	Ext.EventManager.onWindowResize(this.layout);

    //this.layout = layout;

    this.createWindow = function(config, cls){
    	var win = new (cls||Ext.Window)(
            Ext.applyIf(config||{}, {
                manager: windows,
                minimizable: true,
                maximizable: true
            })
        );
        win.render(desktop);
		
		win.taskItem =  new Uwd.System.Desktop.TaskBarItem(win, this);
       
	    win.cmenu = new Ext.menu.Menu({
            items: [

            ]
        });

        win.animateTarget = win.taskItem.el;
        
        win.on({
        	'activate': {
        		fn: markActive
        	},
        	'beforeshow': {
        		fn: markActive
        	},
        	'deactivate': {
        		fn: markInactive
        	},
        	'minimize': {
        		fn: minimizeWin
        	},
        	'close': {
        		fn: removeWin
        	}
        });
        
        this.layout();
        return win;
    };


    this.getManager = function(){
        return windows;
    };

    this.getWindow = function(id){
        return windows.get(id);
    };
	
	this.setUpDeskButtons = function(ids) {
		// Remove old buttons
		Ext.each(deskButtons, function(b) {
			b.destroy();
		});
		// clear button array
		deskButtons = [];
		// set up new buttons
		Ext.each(ids, function(id) {
			var prog = Uwd.System.ProgramStack.getProgramById(id);
			if (prog !== null) {
				var button = new Uwd.System.Desktop.Button({
					text : prog.getName(),
					iconCls : prog.getIconClass() + '-desktop',
					handler : prog.openWindow.createDelegate(prog)

				});
				button.show();
				deskButtons.push(button);
			}
		});
	};
	
    this.layout();
};

Uwd.System.Desktop.TaskBarItem = function(win, desktop){
    this.win = win;
    this.desktop = desktop;
    Uwd.System.Desktop.TaskBarItem.superclass.constructor.call(this, {
        iconCls: win.iconCls,
        text: win.title,
        renderTo: 'x-winlist',
        handler : function(){
            if(win.minimized || win.hidden){
                win.show();
            }else if(win == win.manager.getActive()){
                if (win.minimizable) {
					win.minimize();
				}
            }else{
                win.toFront();
            }
        },
        clickEvent:'mousedown',
        template: new Ext.Template(
			'<table cellspacing="0" class="x-btn {3}"><tbody><tr>',
			'<td class="ux-taskbutton-left"><i>&#160;</i></td>',
            '<td class="ux-taskbutton-center"><em class="{5} unselectable="on">',
                '<button class="x-btn-text {2}" type="{1}" style="height:28px;">{0}</button>',
            '</em></td>',
            '<td class="ux-taskbutton-right"><i>&#160;</i></td>',
			"</tr></tbody></table>")   
    });
};

Ext.extend(Uwd.System.Desktop.TaskBarItem, Ext.Button, {
    onRender : function(){
        Uwd.System.Desktop.TaskBarItem.superclass.onRender.apply(this, arguments);

        this.cmenu = new Ext.menu.Menu({
            items: [{
				icon: './resources/default/images/desktop/restore.gif',
                text: 'restore',
                handler: function(){
                    if(!this.win.isVisible()){
                        this.win.show();
                    }else{
						this.win.restore();
                    }
                },
                scope: this
            },{
				icon: './resources/default/images/desktop/minimize.gif',
                text: 'minimize',
                handler: this.win.minimizable == true ? this.win.minimize : Ext.emptyFn,
				scope: this.win
            },{
				icon: './resources/default/images/desktop/maximize.gif',
                text: 'maximize',
                handler: this.win.maximizable == true ? this.win.maximize : Ext.emptyFn,
                scope: this.win
            }, '-', {
				icon: './resources/default/images/desktop/close.gif',
                text: 'close',
                handler: function(){
                    if(!this.win.isVisible()){
                        this.win.show();
                    }
                    this.win.close();
                    
                },
                scope: this
            }]
        });

        this.cmenu.on('beforeshow', function(){
            var items = this.cmenu.items.items;
            var w = this.win;
            items[0].setDisabled(w.maximized !== true && w.hidden !== true);
            items[1].setDisabled(w.minimized === true || w.minimizable != true);
            items[2].setDisabled(w.maximized === true || w.hidden === true || w.maximizable != true);
        }, this);

        this.el.on('contextmenu', function(e){
            e.stopEvent();
            if(!this.cmenu.el){
                this.cmenu.render();
            }
            var xy = e.getXY();
            xy[1] -= this.cmenu.el.getHeight();
            this.cmenu.showAt(xy);
        }, this);
    }
});




