$.namespace("ProspectDirector");

/**
@class Menu
@namespace ProspectDirector.Types.Core
**/
ProspectDirector.Types.Core.Menu = Class.extend({

    //---------------------------
    //--- CONSTRUCTOR
    //---------------------------

    init: function (options) {

        this._plugins = [];

        this.options = $.extend({
            selector: "",
            select: null
        }, options);

        this._init_menu();

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    /**
    Gets the jQuery wrapped HTML element that represents the menu.
    @property get_menu
    @type Object
    **/
    get_menu: function () {
        return $(this.options.selector)
    },

    /**
    Gets the Kendo Menu object.
    @property get_menuObject
    @type Object
    **/
    get_menuObject: function () {
        return $(this.options.selector).data("kendoMenu")
    },

    //---------------------------
    //--- EVENT HANDLERS
    //---------------------------

    /**
    Event handler for the menu "select" event.
    @private
    @method _menu_onSelect
    **/
    _menu_onSelect: function (e) {

        var i = 0,
            extension = null,
            role = this.getRootRole(e.item),
            command = this.getItemCommand(e.item);

        e = $.extend({
            command: command,
            role: role
        }, e);

        if (role !== undefined && role !== null) {

            for (i = 0; i < this._plugins.length; i++) {
                this._plugins[i].executeCommand(e);
            }

            e.preventDefault();

        }

    },

    //---------------------------
    //--- PUBLIC METHODS
    //---------------------------

    /**
    Returns the root menu item wrapped HTML element that represents the menu.
    @method getRootItem
    @param item {Object} The HTML element from which to start upward DOM traversal to find the root menu item.
    @return A jQuery wrapped HTML element.
    **/
    getRootItem: function (item) {
        return $(item).closest("ul.menu.k-widget > li[data-role]");
    },

    /**
    Returns role as specified by the data-role attribute of the root menu item.
    @method getRootRole
    @param item {Object} The HTML element from which to start upward DOM traversal to find the root menu item and retrieve its role.
    @return {String} The role id.
    **/
    getRootRole: function (item) {
        var data = this.getRootItem(item).data();
        return (data ? data.role : null);
    },

    /**
    Gets the command name as specified by the data-command attribute of the menu item.
    @method getItemCommand
    @param item {Object} The HTML element representing the menu item from which to extract the command.
    @return {String} The command name.
    **/
    getItemCommand: function (item) {
        return $(item).data().command;
    },

    addPlugIn: function (type, args) {

        args = $.extend({
            parent: this
        }, args);

        this._plugins[this._plugins.length] = new type(args);

    },

    //---------------------------
    //--- PRIVATE METHODS
    //---------------------------

    /**
    Menu intialization function.
    @private
    @method _init_menu
    **/
    _init_menu: function () {

        menu = this.get_menu().kendoMenu({
            closeOnClick: true
        }).data("kendoMenu");

        menu.bind("select", $.proxy(this._menu_onSelect, this));

        if ($.isFunction(this.options.select)) {
            menu.bind("select", $.proxy(this.options.select, this));
        }

    }

});