﻿var ActionsPopupAlign = { Left:"left", Right:"right", Center:"center" }

var ActionsPopup = function()
{
    this.parentId;
    this.parent;
    this.previousParent;
    this.popupContainerId;
    this.popupContainer = null;
    this.isEnabled = false;
    this.documentClicksCount = 0;
    // Aligns the popup window by the left or right border of its parent
    // Possible values: left, right, center
    this.align = ActionsPopupAlign.Right;
}

ActionsPopup.prototype.Show = function()
{
    this.popupContainerId = this.parentId + "_div";
    this.popupContainer = document.getElementById(this.popupContainerId);
    this.Register(this.popupContainer);
    this.popupContainer.style.zIndex = "1000";

    this.UpdatePosition();
    this.popupContainer.style.visibility = "visible"

    this.documentClicksCount = 1;

    this.AttachEvent(document, "click", this.OnDocumentClick);
    this.AttachEvent(document, "keydown", this.OnDocumentKeyDown);
    this.AttachEvent(this.parent, "click", this.OnParentContainerClick);
}

ActionsPopup.prototype.Hide = function(sender)
{
    if (this.popupContainer)
    {
        this.popupContainer.style.visibility = "hidden"
        this.DetachEvent(document, "click", this.OnDocumentClick);
        this.DetachEvent(document, "keydown", this.OnDocumentKeyDown);
        this.DetachEvent(this.parent, "click", this.OnParentContainerClick);
    }
}

ActionsPopup.prototype.Register = function(popupContainer)
{
    if (popupContainer.parentNode != document.body)
    {
        popupContainer.parentNode.removeChild(popupContainer);
        document.body.appendChild(popupContainer);

        if (BrowserIdentity.IsIE && (BrowserIdentity.Version >= 5 && BrowserIdentity.Version < 7))
        {
            this.FixContainerForIe6(popupContainer);
        }

        this.AttachEvent(window, "resize", UpdateActionPopupPosition);
    }
}

ActionsPopup.prototype.SetAlign = function(align)
{
    if (align == undefined)
    {
        return;
    }

    if (align.toLowerCase() != ActionsPopupAlign.Left ||
        align.toLowerCase() != ActionsPopupAlign.Right ||
        align.toLowerCase() != ActionsPopupAlign.Center)
    {
        this.align = align.toLowerCase();
    }
}

ActionsPopup.prototype.OnParentClick = function(parentId, align)
{
    var parent = document.getElementById(parentId);
    this.SetAlign(align);

    var showPanel = true;
    var previousParent = this.parent;

    if (this.IsVisible())
    {
        showPanel = (previousParent != parent)
        this.Hide();
    }
    else if (showPanel)
    {
        this.parent = parent;
        this.parentId = parentId;

        this.Show();
    }
}

ActionsPopup.prototype.GetElementPosition = function(element)
{
    result = { x: 0, y: 0 };
    while (element)
    {
        result.x += element.offsetLeft;
        result.y += element.offsetTop;

        if (element.tagName.toLowerCase() == "div")
        {
            result.x -= element.scrollLeft;
            result.y -= element.scrollTop;
        }

        element = element.offsetParent;
    }

    return result;
}

ActionsPopup.prototype.UpdatePosition = function()
{
    var pos = this.GetElementPosition(this.parent);
    
    pos.x += this.AdjustPopupXCoordinate();
    pos.y += this.parent.offsetHeight;

    this.popupContainer.style.left = ((pos.x > 0) ? pos.x : 0) + "px";
    this.popupContainer.style.top = ((pos.y > 0) ? pos.y : 0) + "px";
}

ActionsPopup.prototype.AdjustPopupXCoordinate = function()
{
    if (this.align == ActionsPopupAlign.Right)
        return this.parent.offsetWidth - this.popupContainer.offsetWidth;
    else if (this.align == ActionsPopupAlign.Left)
        return 0;
    else if (this.align == ActionsPopupAlign.Center)
        return (this.parent.offsetWidth - this.popupContainer.offsetWidth) / 2;
}

ActionsPopup.prototype.IsVisible = function()
{
    return (this.popupContainer != null) && (this.popupContainer.style.visibility == "visible");
}

ActionsPopup.prototype.OnDocumentClick = function(e)
{
    var actionsPopup = GetActionsPopup();
    actionsPopup.documentClicksCount = actionsPopup.documentClicksCount - 1; 
    
    if (actionsPopup.documentClicksCount == 0)
    {
        actionsPopup.Hide();
    }
}

ActionsPopup.prototype.OnDocumentKeyDown = function(e)
{    
    if (!e)
    {
        e = window.event;
    }

    if (e.keyCode == 27 || e.keyCode == 9)
    {
        GetActionsPopup().Hide();
    }
}

ActionsPopup.prototype.AttachEvent = function(element, event, delegate)
{
    if (window.addEventListener)
    {
        element.addEventListener(event, delegate, false);
    }
    else if (window.attachEvent)
    {
        element.attachEvent("on" + event, delegate);
    }
}

ActionsPopup.prototype.DetachEvent = function(element, event, delegate)
{
    if (window.addEventListener)
    {
        element.removeEventListener(event, delegate, false);
    }
    else if (window.attachEvent)
    {
        element.detachEvent("on" + event, delegate);
    }    
}

ActionsPopup.prototype.OnParentContainerClick = function()
{
    var actionsPopup = GetActionsPopup();
    actionsPopup.documentClicksCount = actionsPopup.documentClicksCount + 1;
    if (actionsPopup.IsVisible)
    {
        actionsPopup.Hide();
    }
}

ActionsPopup.prototype.FixContainerForIe6 = function(popupContainer)
{
    if (popupContainer != null)
    {
        var actions = popupContainer.getElementsByTagName("div");

        if (actions == undefined || actions.length == 0)
            return;

        var height = actions[0].offsetHeight;
        var width = actions[0].offsetWidth;
        for (var i = 0, len = actions.length; i < len; i++)
        {
            actions[i].childNodes[0].style.width = width + "px";
        }
    }
}

var UpdateActionPopupPosition = function()
{
    if (GetActionsPopup().IsVisible())
    {
        GetActionsPopup().UpdatePosition();
    }
}

var __actionsPopup;

function GetActionsPopup()
{
    if (!__actionsPopup)
    {
        __actionsPopup = new ActionsPopup;
    }
    return __actionsPopup;
}