﻿Type.registerNamespace("Pathways");

Pathways.Timer = function() {
    Pathways.Timer.initializeBase(this);
    this._timerID = null;
    this._tickHandler = null;
    this._timeout = null;
    this._callback = null;
    this._callbackData = null;
}

Pathways.Timer.prototype =
{
    initialize: function() {
        Pathways.Timer.callBaseMethod(this, 'initialize');
        this._tickHandler = Function.createDelegate(this, this.onTick);
    },

    dispose: function() {
        this._tickHandler = null;
        this._callback = null;
        window.clearTimeout(this._timerID);
        Pathways.Timer.callBaseMethod(this, 'dispose');
    },

    start: function() {
        this._timerID = window.setTimeout(this._tickHandler, this._timeout);
    },

    get_data: function() {
        return this._callbackData;
    },

    set_data: function(data) {
        this._callbackData = data;
    },

    set_interval: function(interval) {
        this._timeout = interval;
    },

    get_interval: function() {
        return this._timeout;
    },

    set_callback: function(callback) {
        this._callback = callback;
    },

    onTick: function() {
        this._callback(this);
    }

}

Pathways.Timer.descriptor =
{
    properties: [{ name: 'interval', type: Number },
                    { name: 'data', type: String}]
}

Pathways.Timer.registerClass('Pathways.Timer', Sys.Component);

Pathways.Menu = function(element) {
    Pathways.Menu.initializeBase(this, [element]);
    this._menuIDs = [];
    this._previousMenu = "none";
    this._currentMenu = "none";
    this._fadeTime = 4000; //how long (in milliseconds) will the menu remain displayed after the mouse leaves
    this._selectedItemClassName = "menuLinkSelected";
    this._unselectedItemClassName = "menuLink";
    this._subMenuSuffix = "SubMenu";
    this._mouseOverHandler = null;
    this._mouseOutHandler = null;
    this._hideHandler = null;
}

Pathways.Menu.prototype =
{
    initialize: function() {
        Pathways.Menu.callBaseMethod(this, 'initialize');
        this._registerEvents();
    },

    dispose: function() {
        this._unregisterEvents();
        Pathways.Menu.callBaseMethod(this, 'dispose');
    },

    onMenuMouseOver: function(element) {
        var elementId = element.target.id;
        var menuName = this._getHighLevelMenuName(elementId);
        this._menuMouseOver(menuName);
    },

    onMenuMouseOut: function(element) {
        var elementId = element.target.id;
        var menuName = this._getHighLevelMenuName(elementId);
        this._menuMouseOut(menuName);
    },

    addMenuItem: function(menuName, elementID) {
        this._menuIDs[menuName] = elementID;
    },

    set_menuItems: function(menuArray) {
        this._menuIDs = menuArray;
    },

    get_menuItems: function() {
        return this._menuIDs;
    },

    _onLoad: function(sender, args) {
        this._mouseOverHandler = Function.createDelegate(this, this.onMenuMouseOver);
        this._mouseOutHandler = Function.createDelegate(this, this.onMenuMouseOut);
        for (var key in this._menuIDs) {
            var elementName = this._menuIDs[key];

            var element = $get(elementName);
            $addHandler(element, 'mouseover', this._mouseOverHandler);
            $addHandler(element, 'mouseout', this._mouseOutHandler);
            var name = this._getHighLevelMenuName(element.id);
            var subMenu = $get(name + "SubMenu");
            if (subMenu != null) {
                var children = subMenu.childNodes;

                for (var i = 0; i < children.length; i++) {
                    var curElement = children[i].firstChild;
                    if (curElement != null && curElement.nodeName == 'A') {
                        $addHandler(curElement, 'mouseover', this._mouseOverHandler);
                        $addHandler(curElement, 'mouseout', this._mouseOutHandler);
                    }
                }
            }
        }
    },

    _unregisterEvents: function() {
        for (var key in this._menuIDs) {
            var elementName = this._menuIDs[key];

            var element = $get(elementName);
            if (this._mouseOverHandler != null) {
                $removeHandler(element, 'mouseover', this._mouseOverHandler);
            }

            if (this._mouseOutHandler != null) {
                $removeHandler(element, 'mouseout', this._mouseOutHandler);
            }

            var name = this._getHighLevelMenuName(element.id);
            var subMenu = $get(name + "SubMenu");
            if (subMenu != null) {
                var children = subMenu.childNodes;

                for (var i = 0; i < children.length; i++) {
                    var curElement = children[i].firstChild;
                    if (curElement != null && curElement.nodeName == 'A') {
                        if (this._mouseOverHandler != null) {
                            $removeHandler(curElement, 'mouseover', this._mouseOverHandler);
                        }

                        if (this._mouseOutHandler != null) {
                            $removeHandler(curElement, 'mouseout', this._mouseOutHandler);
                        }
                    }
                }
            }
        }

        this._mouseOverHandler = null;
        this._mouseOutHandler = null;

        this._hideHandler = null;

    },

    _registerEvents: function() {
        this._showHandler = Function.createDelegate(this, this._showSubMenu);
        this._hideHandler = Function.createDelegate(this, this._timerHideSubMenu);
        this._onLoad();
    },

    _getHighLevelMenuName: function(elementId) {
        var start = this.get_id().length + 1;
        return elementId.substring(start, elementId.length - 4);
    },

    _showSubMenu: function(menuName) {
        this._currentMenu = menuName;
        if (this._previousMenu != "none") {
            this._hideSubMenu(this._previousMenu);
        }
        this._previousMenu = menuName;

        var submenu = $get(menuName + this._subMenuSuffix);
        if (submenu != null) {
            submenu.style.display = 'block';
        }
        document.getElementById(this._menuIDs[menuName]).className = this._selectedItemClassName;
    },

    _menuMouseOut: function(menuName) {
        this._currentMenu = "none";
        var timer = $create(Pathways.Timer, { interval: this._fadeTime, data: menuName }, null, null, null);
        timer.set_callback(this._hideHandler);
        timer.start();
    },

    _menuMouseOver: function(menuName) {
        this._showSubMenu(menuName);
    },

    _timerHideSubMenu: function(timer) {
        var menuName = timer.get_data();
        timer.dispose();
        this._hideSubMenu(menuName);
    },

    _hideSubMenu: function(menuName) {
        if (this._currentMenu != menuName) {
            var submenu = $get(menuName + this._subMenuSuffix);
            if (submenu != null) {
                submenu.style.display = 'none';
            }
            document.getElementById(this._menuIDs[menuName]).className = this._unselectedItemClassName;
        }
    }
}

Pathways.Menu.descriptor =
{
    properties: [{ name: 'menuItems', type: Array}]
}

Pathways.Menu.registerClass('Pathways.Menu', Sys.UI.Control);

// Notify ScriptManager that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
