/// class PD.Web.UI.Controls.ContextMenuSegment

PD.Web.UI.Controls.ContextMenuSegment =
Class.extend({

    //===================================
    // FIELDS
    //===================================

    _originalContext: null,
    _context: null,
    _items: null,
    _eventArgs: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (context) {
        this._originalContext = context;
        this._context = this._transformContext(context);
        this._items = new PD.BCL.List();
    },

    //===================================
    // PROPERTIES
    //===================================

    get_context: function () {
        return this._context;
    },

    get_items: function () {
        return this._items;
    },

    //===================================
    // EVENT HANDLERS
    //===================================

    onRegisterItems: function (e) {
        // overridable
    },

    //===================================
    // PUBLIC METHODS
    //===================================

    meetsLoadConditions: function (e) {
        return true;
    },

    open: function (menu, target) {

        var that = this;

        this._eventArgs = this._getEventArgs(menu, target);

        this.onOpen(this._eventArgs);

        this._items.each(function (index, item) {
            item.open(that._eventArgs);
        });

        this._hidePairedSeparators(menu.element);

    },

    click: function (menu, target, item) {

        this.onClick(this._eventArgs);

        item.onClick(this._eventArgs);

    },

    onOpen: function (args) {
        //do nothing on purpose
    },

    onClick: function (args) {
        //do nothing on purpose
    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _getEventArgs: function (menu, target) {

        this._context = this._transformContext(this._originalContext, target);

        return {
            menu: menu,
            target: target,
            context: this._context
        };

    },

    _transformContext: function (context, target) {

        var result = null;

        switch (true) {
            case PD.Type.isFunction(context):
                result = context(target);
                break;
            case PD.Type.isObject(context):
                result = context;
                break;
            default:
                result = {};
                break;
        }

        return result;

    },

    _hidePairedSeparators: function ($menu) {

        var $items = $menu.find("LI");

        $items.each(function (index, item) {

            var $item = $(item);
            var isCurrSeparator = $item.hasClass("separator");
            
            if (isCurrSeparator) {
            
                var $prev = $item.prev("LI").not(".hidden").first(),
                    isPrevSeparator = $prev.hasClass("separator"),
                    hasNoVisibleSibling = $prev.length === 0;

                if (isPrevSeparator || hasNoVisibleSibling) {
                    $item.addClass("hidden");
                }
                else {
                    $item.removeClass("hidden");
                }

            }

        });

        var $last = $items.not(".hidden").last();

        if ($last.hasClass("separator")) {
            $last.addClass("hidden");
        }

    }

});