﻿var Global =
{
    Browser:
    {
        IsFirefox: function()
        {
            return (navigator.userAgent.toLowerCase().indexOf("firefox") >= 0);
        },
        IsInternetExplorer: function()
        {
            return (navigator.userAgent.toLowerCase().indexOf("msie") >= 0);
        }
    },
    CookieManager:
    {
        C_BROWSER_WIDTH: "BrowserWidth",
        Exists: function(name)
        {
            return (GetCookie(name) != null);
        },
        SetCookie: function(name, value, expirySeconds)
        {
            var expires = null;

            if (expirySeconds != null)
            {
                var date = new Date();

                date.setTime(date.getTime() + (expirySeconds * 1000));
                expires = "expires=" + date.toGMTString();
            }
            else
                expires = "";

            document.cookie = name + "=" + value + "; " + expires + "; path=/";
        },
        GetCookie: function(name)
        {
            var cookies = document.cookie.split(';');

            for (var i = 0; i < cookies.length; i++)
            {
                var current = cookies[i];
                var pair = current.split("=");

                if (pair[0] == name)
                {
                    if (pair.length == 2)
                        pair[1];
                    else if (pair.length > 2)
                    {
                        var value = pair[1];

                        for (var v = 2; v < pair.length; v++)
                            value += "=" + pair[v];

                        return value;
                    }
                }
            }

            return null;
        },
        DeleteCookie: function(name)
        {
            this.setCookie(name, "", null);
        }
    },
    EventManager:
    {
        AddEvent: function(element, type, func)
        {
            if (element.attachEvent)
            {
                element["e" + type + func] = func;
                element[type + func] = function()
                {
                    element["e" + type + func](window.event);
                };
                element.attachEvent("on" + type, element[type + func]);
            }
            else
                element.addEventListener(type, func, false);
        },
        RemoveEvent: function(element, type, func)
        {
            if (element.detachEvent)
            {
                if (element[type + func] == null)
                    return;

                element.detachEvent("on" + type, element[type + func]);
                element[type + func] = null;
            }
            else
                element.removeEventListener(type, func, false);
        }
    },
    PopupManager:
    {
        Popups: new Array(),
        Create: function(parentLink, popupElement, horizontalAlignment, verticalAlignment, usePositioning, onShow, onHide)
        {
            var popup = new Popup(parentLink, popupElement, horizontalAlignment, verticalAlignment, usePositioning, onShow, onHide);

            this.Popups[this.Popups.length] = popup;

            return popup;
        },
        GetByParentLink: function(parentLink)
        {
            for (var i = 0; i < this.Popups.length; i++)
            {
                if (this.Popups[i].ParentLink == parentLink)
                {
                    return this.Popups[i];
                }
            }

            return null;
        },
        HideAll: function()
        {
            for (var i = 0; i < this.Popups.length; i++)
            {
                this.Popups[i].Hide();
            }
        }
    },
    GetLeftOffset: function(element, usePositioning)
    {
        return $(element).offset().left;
    },
    GetTopOffset: function(element, usePositioning)
    {
        return $(element).offset().top;
    },
    GetVerticalScroll: function()
    {
        var scroll = 0;

        if (window.pageYOffset != null)
            scroll = window.pageYOffset;
        else if (document.documentElement != null && document.documentElement.scrollTop != null)
            scroll = document.documentElement.scrollTop;
        else if (document.body != null && document.body.scrollTop != null)
            scroll = document.body.scrollTop;

        return scroll;
    },
    GetHorizontalScroll: function()
    {
        var scroll = 0;

        if (window.pageXOffset != null)
            scroll = window.pageXOffset;
        else if (document.documentElement != null && document.documentElement.scrollLeft != null)
            scroll = document.documentElement.scrollLeft;
        else if (document.body != null && document.body.scrollLeft != null)
            scroll = document.body.scrollLeft;

        return scroll;
    }
};

function Popup(parentLink, popupElement, horizontalAlignment, verticalAlignment, usePositioning, onShow, onHide)
{
    this.ParentLink = parentLink;
    this.PopupElement = popupElement;
    this.HorizontalAlignment = horizontalAlignment;
    this.VerticalAlignment = verticalAlignment;
    this.UsePositioning = usePositioning;
    this.AutoHideTimeout = 500;
    this.AutoHideTimer = null;
    this.OnShow = onShow;
    this.OnHide = onHide;

    this.Show = function()
    {
        //Default to "left-bottom" alignment.
        var left = 0;
        var top = 0;

        this.CancelAutoHide();
        Global.PopupManager.HideAll();

        //Show the element first otherwise offsetWidth is 0.
        this.PopupElement.style.display = "";

        switch (this.HorizontalAlignment)
        {
            case "Right":
                {
                    left = (Global.GetLeftOffset(this.ParentLink, usePositioning) - this.PopupElement.offsetWidth + this.ParentLink.offsetWidth);
                    break;
                }
            default:
                left = Global.GetLeftOffset(this.ParentLink, usePositioning);
        }

        switch (this.VerticalAlignment)
        {
            case "Top":
                {
                    top = (Global.GetTopOffset(this.ParentLink, usePositioning) - $(this.PopupElement).height());
                    break;
                }
            default:
                top = (Global.GetTopOffset(this.ParentLink, usePositioning) + $(this.ParentLink).height());
        }

        $(this.PopupElement).offset({ "left": left, "top": top });
//        this.PopupElement.style.left = (left + "px");
//        this.PopupElement.style.top = (top + "px");

        if (this.OnShow != null)
            this.OnShow();
    };
    this.Hide = function()
    {
        this.CancelAutoHide();
        this.PopupElement.style.display = "none";

        if (this.OnHide != null)
            this.OnHide();
    };
    this.Toggle = function()
    {
        if (this.PopupElement.style.display == "none")
            this.Show();
        else
            this.Hide();
    };
    this.AutoHide = function()
    {
        this.AutoHideTimer = setTimeout("document.getElementById(\"" + this.PopupElement.id + "\").Popup.Hide();", this.AutoHideTimeout);
    };
    this.CancelAutoHide = function()
    {
        if (this.AutoHideTimer != null)
            clearTimeout(this.AutoHideTimer);
    };
    this.Initialize = function()
    {
        var linkMouseoverFunction = function(e) { this.Popup.Show(); };
        var linkMouseoutFunction = function(e) { this.Popup.AutoHide(); };
        var popupMouseoverFunction = function(e) { this.Popup.CancelAutoHide(); };
        var popupMouseoutFunction = linkMouseoutFunction;

        //Initialize the sub menu.
        this.ParentLink.Popup = this;
        this.PopupElement.Popup = this;

        //Remove events first.
        Global.EventManager.RemoveEvent(this.ParentLink, "mouseover", linkMouseoverFunction);
        Global.EventManager.RemoveEvent(this.ParentLink, "mouseout", linkMouseoutFunction);
        Global.EventManager.RemoveEvent(this.PopupElement, "mouseover", popupMouseoverFunction);
        Global.EventManager.RemoveEvent(this.PopupElement, "mouseout", popupMouseoutFunction);

        //Add events.
        Global.EventManager.AddEvent(this.ParentLink, "mouseover", linkMouseoverFunction);
        Global.EventManager.AddEvent(this.ParentLink, "mouseout", linkMouseoutFunction);
        Global.EventManager.AddEvent(this.PopupElement, "mouseover", popupMouseoverFunction);
        Global.EventManager.AddEvent(this.PopupElement, "mouseout", popupMouseoutFunction);
    };
    
    this.Initialize();

    return true;
}
