
Taskbar=function()
{
    this.Groups=new Hashtable();
    this.Element;
    this._locked=false;
    //var show=this.Groups.count();
    var mywins;
    var tmpwidth=0;
    
    this.initialize=function()
    {
        this.Element=$("taskbar");
        
        this.GroupsElement=document.createElement("ul");
        this.Element.appendChild(this.GroupsElement);

        var resizeHandler=Function.createDelegate(this,this._resize);
        window.attachEvent("onresize",resizeHandler);
    }
    this._resize=function()
   {       
        var width= document.body.offsetWidth-280;
        //window.status=width+"::"+tmpwidth+"::"+mywins.length;
        if(tmpwidth!=0 && Math.abs(tmpwidth-width)>=70)
        {
            //tmpwidth=width;  
            this.load(mywins);
        }
        $("taskbar").fireEvent("onresize");
   }  
    this.addWindow=function(win)
    {
        this.addGroup(win.Group);
        if(this.Groups.contains("More") && !this.Groups.contains(win.Group))
        {
            var itemElement=document.createElement("li");
            itemElement.setAttribute("id","taskbar_item_"+win.ID);
            itemElement.onclick=Function("javascript:taskbar._clickButton(this);");
            itemElement.innerHTML="<a href=\"javascript:;\" title=\"\">"+win.Title+"</a>";
            
            $("taskbar_items_More").appendChild(itemElement);
        }
        else
        {
            var itemElement=document.createElement("li");
            itemElement.setAttribute("id","taskbar_item_"+win.ID);
            itemElement.onclick=Function("javascript:taskbar._clickButton(this);");
            itemElement.innerHTML="<a href=\"javascript:;\" title=\"\">"+win.Title+"</a>";
            
            $("taskbar_items_"+win.Group).appendChild(itemElement);
        }
//        var element=document.createElement("<div id=taskbarbtn_"+win.ID+" class='obx_taskbarbtn' onclick='javascript:taskbar._clickButton(this);'>");
//        
//        var html="<a href=\"javascript:;\"'><span class='obx_tb_1'></span>\
//                <span class='obx_tb_2'>"+win.Title+"</span><span class='obx_tb_3'></span></a>"
//                
//        element.innerHTML=html;
//        
//        this.Element.appendChild(element);
    }
    this._clickButton=function(e)
    {
        if(this._locked)return false;
        var winid=e.getAttribute("id").slice(13);
        WindowList.item(winid).show();
        uimanager.CurrentDesktop.activeWindow(winid);
    }
    this.removeWindow=function(win)
    {
        var element=$("taskbar_item_"+win.ID);
        if(element)
        {
            if(this.Groups.contains(win.Group))
           { 
                $("taskbar_items_"+win.Group).removeChild(element);
           } 
           else if(!this.Groups.contains(win.Group) && this.Groups.contains("More")) 
          {
                $("taskbar_items_More").removeChild(element);
          } 
        } 
        this.removeGroup(win.Group);
    }
    this.load=function(wins)
    {
        this.clear();
        if(!wins)return;
        mywins=wins;
        tmpwidth=document.body.offsetWidth-280;
        for(var i=0;i<wins.length;i++)
        {
            this.addWindow(WindowList.item(wins[i]).Attribute);
        }
    }
    this.clear=function()
    {
        this.Element.innerHTML="";
        this.Groups.clear();
        this.initialize();
    }
    this.lock=function()
    {
        this._locked=true;
    }
    this.unlock=function()
    {
        this._locked=false;
    }
    this.addGroup=function(group)
    {
        if(this.Groups.contains(group))
        {
            var tmp=parseInt(this.Groups.item(group))+1
            this.Groups.edit(group,tmp);
        }
        else
        {
            if((document.body.offsetWidth-280)<100*(this.Groups.count()+2) && !this.Groups.contains("More"))
            {
                this.Groups.add("More",1);
                var groupElement=document.createElement("li");
                groupElement.setAttribute("id","taskbar_group_"+"More");
                
                var html="<a class=\"hide\" href=\"javascript:;\">"+"More"+"</a>";
                
                if(com.ie6)html+="<a href=\"javascript:;\">"+"More"+"<table><tr><td>";
                
                html+="<ul id=\"taskbar_items_"+"More"+"\"></ul>";
                
                if(com.ie6)html+="</td></tr></table></a>"; 
                groupElement.innerHTML=html;
                this.GroupsElement.appendChild(groupElement);
            } 
           else if(!this.Groups.contains("More"))
           {  
                this.Groups.add(group,1);
            
                var groupElement=document.createElement("li");
                groupElement.setAttribute("id","taskbar_group_"+group);
                
                var html="<a class=\"hide\" href=\"javascript:;\">"+group+"</a>";
                
                if(com.ie6)html+="<a href=\"javascript:;\">"+group+"<table><tr><td>";
                
                html+="<ul id=\"taskbar_items_"+group+"\"></ul>";
                
                if(com.ie6)html+="</td></tr></table></a>";
                groupElement.innerHTML=html;
                this.GroupsElement.appendChild(groupElement);
            }
        }
    }
    this.removeGroup=function(group)
    {
        var i=0;
        if(this.Groups.contains(group))
        {
            var tmp=parseInt(this.Groups.item(group))-1
            this.Groups.edit(group,tmp);
            if(parseInt(this.Groups.item(group))<1)
            {
                this.Groups.remove(group);
                this.GroupsElement.removeChild($("taskbar_group_"+group));
            }
        }
        else if(!this.Groups.contains(group) && this.Groups.contains("More"))
        {
            var tmp=parseInt(this.Groups.item("More"))-1
            this.Groups.edit("More",tmp);
            if(parseInt(this.Groups.item("More"))<1)
            {
                this.Groups.remove("More");
                this.GroupsElement.removeChild($("taskbar_group_"+"More"));
            }
        }
    }
    this.initialize();
}
