﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("MXA.FP.Control");

MXA.FP.Control.Menu = function (element) {
    MXA.FP.Control.Menu.initializeBase(this, [element]);
    this.addCssClass('Menu');
    this._expander = document.createElement('div');
    this._expander.className = 'mnuExpander';
    element.appendChild(this._expander);

    this._container = document.createElement('div');
    this._container.className = 'mnuContainer';
    element.appendChild(this._container);

    $(this._expander).css({ top: '-85px' });
    $(this._container).css({ top: '-85px' });
    var _this = this;
    this._mouseMoveDelegate = Function.createDelegate(_this, _this._onMouseMove);
    this._expanderHoverDelegate = Function.createDelegate(_this, _this._onExpandHover);

    var companyLogo = document.createElement('div');
    companyLogo.className = 'mnuLogo';
    this._container.appendChild(companyLogo);

    this._menuItemContainer = document.createElement('div');
    this._menuItemContainer.className = 'mnuItemContainer';
    this._container.appendChild(this._menuItemContainer);

    $addHandler(this._menuItemContainer, 'click', Function.createDelegate(_this, _this._onMenuItemClick));
}


MXA.FP.Control.Menu.prototype =
{
    initialize: function () {
        MXA.FP.Control.Menu.callBaseMethod(this, 'initialize');
    },

    dispose: function () {
        MXA.FP.Control.Menu.callBaseMethod(this, 'dispose');
    },

    _onMenuItemClick: function (evt) {
        var e = evt.target;


        while (e != null && e._commandArgument == null) {
            e = e.parentNode;
        }

        if (e != null) {
            var _this = this;
            var args = new Sys.EventArgs();
            args.commandArgument = e._commandArgument;
            _this.raiseEvent('itemClick', args);
        }
    },

    show: function (callback, autoclose) {
        $(this._expander).stop().animate({ top: '-85px' });
        $(this._container).stop().animate({ top: '0px' }, callback);
        if (autoclose == true) {
            $addHandler(window, 'mousemove', this._mouseMoveDelegate);
        }
        try {
            $removeHandler(this._expander, 'mouseover', this._expanderHoverDelegate);
        }
        catch (e) {

        }
    },

    hide: function (callback) {
        $(this._expander).stop().animate({ top: '0px' });
        $(this._container).stop().animate({ top: '-85px' }, callback);
        $addHandler(this._expander, 'mouseover', this._expanderHoverDelegate);
        try {
            $removeHandler(window, 'mousemove', this._mouseMoveDelegate);
        }
        catch (e) {

        }
    },

    _onExpandHover: function () {
        this.show(null, true);
    },

    _onMouseMove: function (evt) {
        if (evt.clientY > 85) {
            this.hide();
        }

    },

    addItem: function (label, argument) {
        var element = document.createElement('div');
        element.className = 'mnuItem';
        element.innerHTML = label;
        element._commandArgument = argument;
        this._menuItemContainer.appendChild(element);
        Cufon.replace(element);

    },

    raiseEvent: function (eventName, eventArgs) {

        var handler = this.get_events().getHandler(eventName);
        if (handler) {
            handler(this, eventArgs);
        }

    },

    add_itemClick: function (handler) {
        this.get_events().addHandler('itemClick', handler);
    },

    remove_itemClick: function (handler) {
        this.get_events().removeHandler('itemClick', handler);
    }
}

MXA.FP.Control.Menu.registerClass('MXA.FP.Control.Menu', Sys.UI.Control);
