/*
 Copyright (c) 2009 Perception Web Solutions
 All rights reserved. 
 The contents of this file are subject to the
 Perception Web Solutions Open Source License Version 1.0. 
 View license at http://perceptionweb.net/licenses/PWS-OSL-1.0.html
*/


//---------------------------------------------------------
/* Nav menu class

Constructor array
widget_list_item = (required) CSS selector to find nav menu widget list item
current_page_class = (required) CSS class indicating current page
page_item_class = CSS class indicating list item is part of nav menu
item_parent_class = CSS class name to assign to menu items with children
    default = "nav_menu_parent"
current_page_top_ancestor_class = CSS class name to assign to highest ancestor
    of current page, default = "current_page_top_ancestor"

*/
function PWS_Nav_Menu(param)
{
    // Defaults
    // this.widget_list_item - required
    // this.current_page_class - required
    this.page_item_class = "";
    this.item_parent_class = "nav_menu_parent";
    this.current_page_top_ancestor_class = "current_page_top_ancestor";
    
    // Assign class properties
    //   Required properties
    this.widget_list_item = param["widget_list_item"];
    this.current_page_class = param["current_page_class"];
    
    //   Optional properties
    for (var key in param)
    {
        switch (key)
        {
            case "page_item_class":
                this.page_item_class = param[key];
                break;
            
            case "item_parent_class":
                this.item_parent_class = param[key];
                break;
            
            case "current_page_top_ancestor_class":
                this.current_page_top_ancestor_class = param[key];
                break;
            
        }
    }
    
    
    // Get list of all menu li elements and top level li elements
    this.menu_list = jQuery(this.widget_list_item + " ul li"+this.selForm(this.page_item_class));
    this.top_menu_list = jQuery(this.widget_list_item + ">ul>li"+this.selForm(this.page_item_class));
    
}


// Format class name as selector
PWS_Nav_Menu.prototype.selForm = function(clsName)
{
    if (clsName)
    {
        return "." + clsName;
    }
    else
    {
        return "";
    }
}


// Remove title attribute from menus items
PWS_Nav_Menu.prototype.removeTitleAtt = function(selector)
{
    jQuery(selector).removeAttr("title");
}


// Find whether each menu item has children and add nav_menu_parent class
PWS_Nav_Menu.prototype.labelParents = function()
{
    for (var i = 0; i < this.menu_list.length; i++)
    {
        var li = jQuery(this.menu_list[i]);
        if (li.find(">ul>li"+this.selForm(this.page_item_class)).is("li"))
        {
            li.addClass(this.item_parent_class);
        }
    }
}

// Find current_page_item class and add current_page_parent_top class
//   to top level ancestor
PWS_Nav_Menu.prototype.labelCurrentPageTopMenuItem = function()
{
    // Loop through each top level item
    for (var i = 0; i < this.top_menu_list.length; i++)
    {
        var li = jQuery(this.top_menu_list[i]);
        if (li.hasClass(this.current_page_class))
        {
            li.addClass(this.current_page_top_ancestor_class);
        }
        else if (li.find("."+this.current_page_class).hasClass(this.current_page_class))
        {
            li.addClass(this.current_page_top_ancestor_class);
        }
    }
}

// Copy menu item title attribute to alt and remove titles
PWS_Nav_Menu.prototype.title2Alt = function()
{
    for (var i = 0; i < this.menu_list.length; i++)
    {
        var li = jQuery(this.menu_list[i]).find(">a:first-child");
        var titleVal = li.attr("title");
        if (titleVal)
        {
            li.attr("alt", titleVal);
            li.removeAttr("title");
        }
    }
}

