var ButtonMenu = new Object();


// =========================================================
// Initialization
// =========================================================
/*
    Initializes the tree menu
*/
ButtonMenu.Init = function(htmlObject)
{
    Menu.Inherit(this, "Button", htmlObject);
}

ButtonMenu.HasRelevant = function(menuitems)
{
    for(var i in menuitems){
        var menuitem = menuitems[i];
        
        // check each item for buttons
        for (var j in menuitem.Children){
            if (menuitem.Children[j].Type == "Button") return true;
        }
        
        // check the children of each item for buttons
        if (menuitem.IsOwner != true){
            if (this.HasRelevant(menuitem.Children)) return true;
        }
    }
}

ButtonMenu.GetButtons = function(menuitems)
{
    var result = new Array();
    
    if (menuitems == null) return result;
    
    for(var i in menuitems)
    {
        var menuitem = menuitems[i];
        
        if (menuitem.Type == "Button")
        {
            result.push(menuitem);
        }
    }
    
    return result;
}

ButtonMenu.Show = function(buttons)
{
    var html = "";
    
    for (var i in buttons){
        var button = buttons[i];
        
        html += '<a href="#" onClick="Navigator.OpenId(\'' + button.Id + '\');" ' + ' id="' + this.ButtonId(button) + '" class="' + this.GetButtonClasses(button) + '">'; 
        html += button.Title;
        html += '</a>';
    }
    
    this.HtmlObject.innerHTML = html;
}

ButtonMenu.ButtonId = function(menuitem)
{
    return (this.Id + '_' + menuitem.Id);
}

ButtonMenu.GetButtonClasses = function(menuitem)
{
    var classes = this.Style;
    if (menuitem.Filtered == true) classes += ' ' + this.Style + '_' + Navigator.GetFilterType();

    return classes;
}

ButtonMenu.UpdateItem = function(menuitem, opened)
{
    // a new menuitem has been loaded, get the buttons for this menuitem
    if (opened == true){
        this.Buttons = this.GetButtons(menuitem.Children);
        this.Show(this.Buttons);
        Navigator.ResizeButtonMenu();
    }
}

ButtonMenu.UpdateAllItems = function()
{
    // update classes for all buttons
    for (var i in this.Buttons)
    {
        var button = this.Buttons[i];
        
        var object = document.getElementById(this.ButtonId(button));
        object.className = this.GetButtonClasses(button);
    }
}