
Desktop=function(arr)
{
    this.Attribute=arr;
    this.Element;
    
    var _status;
    var _type="User";
    var _name="";
    
    this.initialize=function()
    {
        this.create();
        this.restore();
    }
    this.create=function()
    {
        this.Element=document.createElement("div");
        this.Element.className="desktop";
        //this.Element.innerHTML=this.Attribute.BoName;
        
        $("container").appendChild(this.Element);
    }
    this.show=function()
    {
        this.Element.style.visibility="visible";
        this.Element.style.display="block";
    }
    this.hide=function()
    {
        this.Element.style.visibility="hidden";
        this.Element.style.display="none";
    }
    this.restore=function()
    {
        if(this.Attribute.ID<0)return;
        var body="action=restore&objectid="+this.Attribute.ID;
        context.request(DesktopHandler_URL,body,this);
    }
    this.refresh=function(windows)
    {
        if(this.Attribute.ID<0)return;
        if(windows)
        {
            for(var i=0;i<windows.length;i++)
            {
                var body="action=reload&id="+windows[i];
                context.request(WindowHandler_URL,body,this);
            }
        }
        else
        {
            for(var i=0;i<this.Attribute.Windows.length;i++)
            {
                context.request(WindowHandler_URL,"action=reload&id="+this.Attribute.Windows[i],this);
            }
            for(var i=0;i<this.Attribute.Gadgets.length;i++)
            {
                context.request(WindowHandler_URL,"action=reload&id="+this.Attribute.Gadgets[i],this);
            }
        }
    }
    this.tile=function()
    {
       var ieWidth=parseInt(document.documentElement.clientWidth);
       var rowWidth=50;
       var rowHeight=50;
       
       var tempHeight=0;
       
        for (var i = 0; i < this.Attribute.Windows.length; i++) 
        {
            
            var winWidth=WindowList.item(this.Attribute.Windows[i]).Attribute.Width;
            var winHeight=WindowList.item(this.Attribute.Windows[i]).Attribute.Height;
            if((rowWidth+winWidth)<ieWidth){
                WindowList.item(this.Attribute.Windows[i]).move(rowWidth,rowHeight)
                rowWidth+=(winWidth+25);
                tempHeight=(tempHeight<winHeight)?winHeight:tempHeight;   
            }
            else{
                rowHeight+=(tempHeight+25);
                WindowList.item(this.Attribute.Windows[i]).move(50,rowHeight)
                rowWidth=winWidth+50+25;
            }
        }
    }
    this.cascade=function()
    {
       var top=100;
       var left=100;
       //_maxZindex=200;
        for (var i = (this.Attribute.Windows.length-1); i >= 0 ; i--) 
        {
            WindowList.item(this.Attribute.Windows[i]).move(left,top);
            top+=20;
            left+=20;
        }
    }
    this.edit=function(type,name)
    {
        if(this.Attribute.ID<0)return;
        if(!type)
        {
            _type="User";
        }
        else
        {
            _type=type;
        }
        if(!name)
        {
            _name="";
        }
        else
        {
            _name=name;
        }
        for(var i=0;i<this.Attribute.Gadgets.length;i++)
        {
            WindowList.item(this.Attribute.Gadgets[i]).edit();
        }
        _status="edit";
        
        window.lock();
        this.minWindows();
    }
    this.destoryGadgets=function()
    {
        if(this.Attribute.ID<0)return;
        for(var i=0;i<this.Attribute.Gadgets.length;i++)
        {
            WindowList.item(this.Attribute.Gadgets[i]).destroy();
        }
        this.Attribute.Gadgets=new Array();
    }
    this.cancelEdit=function()
    {
        if(this.Attribute.ID<0)return;
        for(var i=0;i<this.Attribute.Gadgets.length;i++)
        {
//            if(WindowList.contains(this.Attribute.Gadgets[i])
//            {
                WindowList.item(this.Attribute.Gadgets[i]).cancelEdit();
//            }
//            else
//            {
//                WindowList.item(this.Att
//            }
        }
        _status="";
        
        window.unlock();
    }
    this.save=function()
    {
       var xml=this.Attribute.saveXML(_type,_name);
   
  
       var body="action=save&objectid="+this.Attribute.ID+"&windowsXml="+uriEncode(xml);
   
       context.request(DesktopHandler_URL,body,this);
    }
    // window method
    this.saveDesktopShow=function(boname)
    {
      var xml=this.Attribute.saveXML(_type,_name);

    // alert(this.Attribute.ID); 
      //var body="&params="+xml;
    xml="<param id='desktop'>"+xml+"</param>";
         //alert(xml); 
     // alert(boname,xml);  
        
     _ShowWindow(boname,xml) 

     // context.request(WindowHandler_URL,body,this);
    }
    
    this.addWindow=function(id)
    {
        if(this.Attribute.Windows.length>0&&WindowList.contains(this.Attribute.Windows[0]))
        {
            WindowList.item(this.Attribute.Windows[0]).inactive();
        }
        this.Attribute.Windows.unshift(id);
        WindowList.item(id).active();
        
        // event handler
        var activeHandler=Function.createDelegate(this,this.activeWindow);
        WindowList.item(id).onactive=activeHandler;
        
        var minHandler=Function.createDelegate(this,this.minWindow);
        WindowList.item(id).onminimize=minHandler;
    }
    this.addGadget=function(id)
    {
        this.Attribute.Gadgets.push(id);
        if(_status=="edit")
        {
            WindowList.item(id).edit();
         }
         else
         {
            WindowList.item(id).cancelEdit();
         }
         
         // event handler
        var activeHandler=Function.createDelegate(this,this.activeWindow);
        WindowList.item(id).onactive=activeHandler;
    }
    this.activeWindow=function(id)
    {
        if(WindowList.item(id).Attribute.Type=="Gadget")
        {
            WindowList.item(id).active();
            return true;
        }
        if(this.Attribute.Windows[0]==id)return true;
        if(WindowList.contains(this.Attribute.Windows[0]))
        {
            WindowList.item(this.Attribute.Windows[0]).inactive();
        } 
        for(var i=0;i<this.Attribute.Windows.length;i++)
        {
            if(this.Attribute.Windows[i]==id)
            {
                this.Attribute.Windows.splice(i,1);
                this.Attribute.Windows.unshift(id);
                WindowList.item(id).active();
                return true;
            }
        }
    }
    this.minWindow=function(id)
    {
        for(var i=0;i<this.Attribute.Windows.length;i++)
        {
            if(this.Attribute.Windows[i]==id)
            {
                var tmp=this.Attribute.Windows.shift();
                this.Attribute.Windows.push(tmp);
                
                if(this.Attribute.Windows[0])WindowList.item(this.Attribute.Windows[0]).active();
                return true;
            }
        }
    }
    this.delWindow=function(id)
    {
        for(var i=0;i<this.Attribute.Windows.length;i++)
        {
            if(this.Attribute.Windows[i]==id)
            {
                this.Attribute.Windows.splice(i,1);
                if(this.Attribute.Windows[0])WindowList.item(this.Attribute.Windows[0]).active();
                return true;
            }
        }
        for(var i=0;i<this.Attribute.Gadgets.length;i++)
        {
            if(this.Attribute.Gadgets[i]==id)
            {
                this.Attribute.Gadgets.splice(i,1);
                return true;
            }
        }
    }
    //------------------------------------------ Desktop Window -----------------------------------------
    this.showWindows=function()
    {
        for(var i=0;i<this.Attribute.Windows.length;i++)
        {
            WindowList.item(this.Attribute.Windows[i]).show();
        }
    }
    this.closeWindows=function()
    {
        for(var i=0;i<this.Attribute.Windows.length;i++)
        {
            WindowList.item(this.Attribute.Windows[i]).close();
        }
    }
    this.minWindows=function()
    {
        for(var i=0;i<this.Attribute.Windows.length;i++)
        {
            WindowList.item(this.Attribute.Windows[i]).hideonly();
        }
    }
    this.startProcess=function()
    {
        var process=$(this.Attribute.BoName+"_processing");
        if(!process)
        {
            process=document.createElement("div");
            process.setAttribute("id",this.Attribute.BoName+"_processing");
            process.className="processing";
            this.Element.appendChild(process);
        }
        process.style.visibility="visible";
        process.style.display="block";
    }
    this.endProcess=function()
    {
        var process=$(this.Attribute.BoName+"_processing");
        if(process)
        {
            process.style.visibility="hidden";
            process.style.display="none";
        }
    }
    this.onLoad=function()
    {
    }
    this.initialize();
}
