﻿function PopupMenuSkin()
{
    extend(this, Skin);
};

PopupMenuSkin.prototype = new Skin();

PopupMenuSkin.prototype.setBorder = function(border)
{
    this.border = border;
};

PopupMenuSkin.prototype.setDecorator = function(decorator)
{
    this.decorator = decorator;
};

PopupMenuSkin.prototype.install = function(widget)
{
    if(this.border)
    {
        this.border.install(widget);
    }
    
    if(this.decorator)
    {
        this.decorator.install(widget);
    }

    widget.setPadding({left:4, top:4, right:4, bottom:4});
    
    widget.domContainer.onresize = getMethodPointer(this, function()
    {
        if(this.border)
        {
            this.border.update(widget);
        }
        
        if(this.decorator)
        {
            this.decorator.update(widget);
        }
    });
    
    widget.getCell = function(menu, object, index, selected, editing, update)
    {
        var cell = menu[index];
        
        if(!cell)
        {
            cell = document.createElement("div");
            cell.nMenuIndex = index;
            menu[index] = cell;
         
            cell.onselectstart = function() { return false; };
            
            cell.label = document.createElement("span");
            cell.label.innerHTML = "<center><label><nobr>" + object.toString() + "</nobr></label></center>";
            cell.appendChild(cell.label);
               
            cell.onmouseenter = function()
            {
                this.style.background = "#DDDDDD";
            };
            
            cell.onmouseleave = function()
            {
                this.style.background = "none";
            };
            
            cell.onmouseup = function()
            {
                menu.click(this.nMenuIndex);
            };
            
            Toolkit.getImage(index % 2 != 4 ? "theme/nuera/popupMenu/evenbg.png" : "theme/nuera/popupMenu/oddbg.png", function(path, image, async) {
                cell.image = image;
                cell.appendChild(image);
                
                cell.onresize = function()
                {
                    try
                    {
                        this.image.style.width  = parseInt(this.style.width);
                        this.image.style.height = parseInt(this.style.height);
                    } catch (e)
                    {
                        this.image.style.width  = this.clientWidth;
                        this.image.style.height = this.clientHeight;
                    }
                };
            });
        }
        
        if(!update)
        {
            return cell;
        }
        
        return cell;
    };
};

PopupMenuSkin.prototype.uninstall = function(widget)
{
    if(this.border)
    {
        this.border.uninstall(widget);
    }
    
    if(this.decorator)
    {
        this.decorator.uninstall(widget);
    }
};
