﻿/// <reference path="jQuery/jquery-1.6.1.js"/>

// Global LayerOperations object used for positioning boxes and applying effects
var layerObj;

function LayerOperations()
{
    this.CenterLayer = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        var popupUI = jQuery("#" + elementID);

        var winWidth = jQuery(window).width(),
            winHeight = jQuery(window).height(),
            winScrollTop = jQuery(window).scrollTop(),
            winScrollLeft = jQuery(window).scrollLeft();

        var cssLeft = (winWidth / 2) - (popupUI.width() / 2) + winScrollLeft,
            cssTop = (winHeight / 2) - (popupUI.height() / 2) + winScrollTop;

        if (winWidth < popupUI.width())
            cssLeft = 0;

        if (winHeight < popupUI.height())
            cssTop = 0;

        var cssLeftPx = cssLeft + "px",
            cssTopPx = cssTop + "px";

        popupUI.css({
            "left": cssLeftPx,
            "top": cssTopPx
        });

        //        UnbindEventsOn(jQuery(window), elementID);

        //        jQuery(window).scroll([elementID, 'CenterPositionLayer'], RePositionPopupAfterWindowScrollEvent);
        //        jQuery(window).resize([elementID, 'CenterPositionLayer'], RePositionPopupAfterWindowResizeEvent);
    }

    this.BottomRightPositionLayer = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        var popupUI = jQuery("#" + elementID);

        var winWidth = jQuery(window).width(),
            winHeight = jQuery(window).height(),
            winScrollTop = jQuery(window).scrollTop(),
            winScrollLeft = jQuery(window).scrollLeft();

        var cssLeft = (winWidth - (popupUI.width() + 20)) + winScrollLeft,
            cssTop = (winHeight - (popupUI.height() + 20)) + winScrollTop;

        if (winWidth < popupUI.width())
            cssLeft = 0;

        if (winHeight < popupUI.height())
            cssTop = 0;

        var cssLeftPx = cssLeft + "px",
            cssTopPx = cssTop + "px";

        popupUI.css({
            "left": cssLeftPx,
            "top": cssTopPx
        });

        UnbindEventsOn(jQuery(window), elementID);

        jQuery(window).bind("scroll." + elementID, [elementID, 'BottomRightPositionLayer'], RePositionPopupAfterWindowScrollEvent);
        jQuery(window).bind("resize." + elementID, [elementID, 'BottomRightPositionLayer'], RePositionPopupAfterWindowResizeEvent);
    }

    this.BottomLeftPositionLayer = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        var popupUI = jQuery("#" + elementID);

        var winWidth = jQuery(window).width(),
            winHeight = jQuery(window).height(),
            winScrollTop = jQuery(window).scrollTop(),
            winScrollLeft = jQuery(window).scrollLeft();

        var cssLeft = 20 + winScrollLeft,
            cssTop = (winHeight - (popupUI.height() + 20)) + winScrollTop;

        if (winWidth < popupUI.width())
            cssLeft = 0;

        if (winHeight < popupUI.height())
            cssTop = 0;

        var cssLeftPx = cssLeft + "px",
            cssTopPx = cssTop + "px";

        popupUI.css({
            "left": cssLeftPx,
            "top": cssTopPx
        });

        UnbindEventsOn(jQuery(window), elementID);

        jQuery(window).bind("scroll." + elementID, [elementID, 'BottomLeftPositionLayer'], RePositionPopupAfterWindowScrollEvent);
        jQuery(window).bind("resize." + elementID, [elementID, 'BottomLeftPositionLayer'], RePositionPopupAfterWindowResizeEvent);
    }

    this.RightCenterPositionLayer = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        var popupUI = jQuery("#" + elementID);

        var winWidth = jQuery(window).width(),
            winHeight = jQuery(window).height(),
            winScrollTop = jQuery(window).scrollTop(),
            winScrollLeft = jQuery(window).scrollLeft();

        var cssLeft = (winWidth - (popupUI.width() + 20)) + winScrollLeft,
            cssTop = (winHeight / 2) - (popupUI.height() / 2) + winScrollTop;

        if (winWidth < popupUI.width())
            cssLeft = 0;

        if (winHeight < popupUI.height())
            cssTop = 0;

        var cssLeftPx = cssLeft + "px",
            cssTopPx = cssTop + "px";

        popupUI.css({
            "left": cssLeftPx,
            "top": cssTopPx
        });
    }

    this.TopCenterPositionLayer = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        var popupUI = jQuery("#" + elementID);

        var winWidth = jQuery(window).width(),
            winHeight = jQuery(window).height(),
            winScrollTop = jQuery(window).scrollTop(),
            winScrollLeft = jQuery(window).scrollLeft();

        var cssLeft = (winWidth / 2) - (popupUI.width() / 2) + winScrollLeft,
            cssTop = 20 + winScrollTop;

        if (winWidth < popupUI.width())
            cssLeft = 0;

        if (winHeight < popupUI.height())
            cssTop = 0;

        var cssLeftPx = cssLeft + "px",
            cssTopPx = cssTop + "px";

        popupUI.css({
            "left": cssLeftPx,
            "top": cssTopPx
        });
    }

    this.TopRightPositionLayer = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        var popupUI = jQuery("#" + elementID);

        var winWidth = jQuery(window).width(),
            winHeight = jQuery(window).height(),
            winScrollTop = jQuery(window).scrollTop(),
            winScrollLeft = jQuery(window).scrollLeft();

        var cssLeft = (winWidth - (popupUI.width() + 20)) + winScrollLeft,
            cssTop = 20 + winScrollTop;

        if (winWidth < popupUI.width())
            cssLeft = 0;

        if (winHeight < popupUI.height())
            cssTop = 0;

        var cssLeftPx = cssLeft + "px",
            cssTopPx = cssTop + "px";

        popupUI.css({
            "left": cssLeftPx,
            "top": cssTopPx
        });
    }

    this.TopLeftPositionLayer = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        var popupUI = jQuery("#" + elementID);

        var winWidth = jQuery(window).width(),
            winHeight = jQuery(window).height(),
            winScrollTop = jQuery(window).scrollTop(),
            winScrollLeft = jQuery(window).scrollLeft();

        var cssLeft = 20 + winScrollLeft,
            cssTop = 20 + winScrollTop;

        if (winWidth < popupUI.width())
            cssLeft = 0;

        if (winHeight < popupUI.height())
            cssTop = 0;

        var cssLeftPx = cssLeft + "px",
            cssTopPx = cssTop + "px";

        popupUI.css({
            "left": cssLeftPx,
            "top": cssTopPx
        });
    }

    this.AutoHideLayer = function (elementID, numOfMs)
    {
        // Do not use setTimeout
        //        setTimeout("jQuery('#" + elementID + "').fadeOut(5000)", numOfMs);
        //        jQuery('#' + elementID).delay(numOfMs).fadeOut(5000);

        jQuery('#' + elementID).fadeOut(5000 + numOfMs);

        // Add mousedown event
        if (!ElementEventExistsOnNameSpace(jQuery('#' + elementID), 'mouseower', elementID))
        {
            jQuery('#' + elementID).bind('mouseover.' + elementID, [elementID, numOfMs], StopFadeOut);
        }

        // Add mouseup event
        if (!ElementEventExistsOnNameSpace(jQuery('#' + elementID), 'mouseout', elementID))
        {
            jQuery('#' + elementID).bind('mouseout.' + elementID, [elementID, numOfMs], StartFadeOut);
        }
    }

    this.FadeOutLayer = function (elementID, durationInSeconds)
    {
        if (!jQuery(window).length)
            return;

        jQuery("#" + elementID).fadeOut(durationInSeconds * 1000);

        MakePopupAsHidden(elementID);
    }

    this.FadeInLayer = function (elementID, durationInSeconds)
    {
        if (!jQuery(window).length)
            return;

        jQuery("#" + elementID).fadeIn(durationInSeconds * 1000);
    }

    this.showBox = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        if (jQuery("#" + elementID).length)
        {
            var id = "#" + elementID;
            jQuery(id).show();
        }
    }

    this.hideBox = function (elementID)
    {
        if (!jQuery(window).length)
            return;

        if (jQuery("#" + elementID).length)
            jQuery("#" + elementID).hide();

        MakePopupAsHidden(elementID);
    }

    this.isVisible = function (elementID)
    {
        var elem = document.getElementById(elementID);

        if (elem)
            return elem.style.display != "none";
        else
            return false;
    }

    this.SlideDownLayer = function (elementID)
    {
        jQuery("#" + elementID).slideDown(300);
    }

    this.SlideUpLayer = function (elementID)
    {
        jQuery("#" + elementID).slideUp(300);
    }

    this.CreateDraggableLayer = function (elementID, dragHandleId)
    {
        jQuery("#" + elementID).draggable({ handle: "#" + dragHandleId });
    }

    this.DimBackGround = function (elementID)
    {
        var dimBackGroundElementID = "popupUIDimBackGround" + elementID;

        // Try to find the div
        if (!jQuery("#" + dimBackGroundElementID).length)
        {
            CreateDimBackGround(dimBackGroundElementID);
        }

        // Set the width og height on the overlay to the documents width og height.
        SetOverlaySize(jQuery("#" + dimBackGroundElementID));

        jQuery("#" + dimBackGroundElementID).css("z-index", jQuery("#" + elementID).css("z-index") - 1);

        jQuery("#" + dimBackGroundElementID).show();

        // If events not exists
        if (!ElementEventExistsOnNameSpace(jQuery(window), 'scroll', elementID))
        {
            jQuery(window).bind('scroll.' + elementID, [elementID], CenterElement_WindowScrollEvent);
            jQuery(window).bind('resize.' + elementID, [elementID, dimBackGroundElementID], CenterElement_WindowResizeEvent);

            // Add overlay click event to hide the popup.
            jQuery("#" + dimBackGroundElementID).bind('click.' + elementID, function ()
            {
                FadeOutPopup_DimmedBackGround_Click(elementID);
            });
        }

        //        ListAllEventsOn(jQuery(window));
    }

    this.UnDimBackGround = function (elementID)
    {
        var dimBackGroundElementID = "popupUIDimBackGround" + elementID;

        if (jQuery("#" + dimBackGroundElementID).length)
        {
            jQuery("#" + dimBackGroundElementID).unbind('.' + elementID);
            jQuery("#" + dimBackGroundElementID).hide();

            UnbindEventsOn(jQuery(window), elementID);
        }
    }
}

layerObj = new LayerOperations();

function MakePopupAsHidden(elementID)
{
    if (typeof (PopupIsOpen) != 'undefined')
        PopupIsOpen = false;

    // Try find StandardPopup StatusTexBox and set it to 'false'
    var statusBox = FindStandardPopupHiddenStatusBox(elementID);

    if (statusBox != null)
    {
//        DebugLog('MakePopupAsHidden(' + elementID + ')');
        statusBox.val('false');
    }
    else
    {
//        DebugLog('No StatusBox under elementID: ' + elementID);
    }

//    DebugLog(jQuery('#' + elementID).parent().find('[id$=_boxStatusTextBox').attr('id'));
//    DebugLog(jQuery('#' + elementID).parent().find('[id$=_boxStatusTextBox').val());
}

function FindParentStandardPopup(elementID)
{
    var parent = jQuery('#' + elementID).parent();
    var parentID = parent.attr('id');

    var run = 0;
    while (parent != null)
    {
        //        DebugLog('Parent LOG');

        //        DebugLog(jQuery('#' + parentID + '[class*=standardPopup]').length);
        //        DebugLog(jQuery('#' + parentID + '[id$=_standardPopup]').length);

        //        DebugLog('parent.id ' + parent.attr('id'));
        //        DebugLog('parent.class ' + parent.attr('class'));

        if (jQuery('#' + parentID + '[class*=standardPopup]').length > 0 &&
            jQuery('#' + parentID + '[id$=_standardPopup]').length > 0)
        {
//            DebugLog('r parent.id ' + parent.attr('id'));
//            DebugLog('r parent.class ' + parent.attr('class'));

            return parent;
        }

        parent = parent.parent();
        parentID = parent.attr('id');

        run++;

        if (run > 3)
            break;
    }

//    DebugLog('return null;');

    return null;
}

function FindStandardPopupHiddenStatusBox(elementID)
{
    var statusBoxID = elementID.replace('_standardPopup', '_boxStatusTextBox');

    if (jQuery('#' + statusBoxID).length > 0)
    {
        return jQuery('#' + statusBoxID);
    }

    return null;
}

function FindStandardPopupCloseButton(elementID)
{
    var statusBoxID = elementID.replace('_standardPopup', '_closeButton');

    if (jQuery('#' + statusBoxID).length > 0)
    {
        return jQuery('#' + statusBoxID);
    }

    return null;
}

function FINDdivOpenClosePopupHiddenStatusBox(divOpenClose)
{
    var popupControl = FindParentStandardPopup(jQuery(divOpenClose).attr('id'));

    if (popupControl == null)
        return null;

    var statusBox = FindStandardPopupHiddenStatusBox(popupControl.attr('id'));

    return statusBox;
}

function StopFadeOut(evt)
{
    var elementID = evt.data[0];
    var numsOfMs = evt.data[1];

    jQuery('#' + elementID).queue('fx', []);
    jQuery('#' + elementID).stop();
    jQuery('#' + elementID).css('opacity', 1);
}

function StartFadeOut(evt)
{
    var elementID = evt.data[0];
    var numsOfMs = evt.data[1];

    layerObj.AutoHideLayer(elementID, numsOfMs);
}

function CreateDimBackGround(dimBackGroundElementID)
{
    jQuery('<div id="' + dimBackGroundElementID + '" class="PopupUIOverlay"></div>').appendTo('body');
}

function FadeOutPopup_DimmedBackGround_Click(elementID)
{
    // If popup is runat=server find and click the close button.
    UnbindEventsOn(jQuery("#" + elementID), elementID);

    var closeButton = FindStandardPopupCloseButton(elementID);

    if (closeButton != null)
    {
        jQuery(closeButton).click();
    }
    else
    {
        layerObj.hideBox(elementID);
        layerObj.UnDimBackGround(elementID);
    }
}

function UnbindEventsOn(element, taggetID)
{
    jQuery(element).unbind('.' + taggetID);

    UnbindAllInvisibleElementEventsOn(element);
}

function UnbindAllInvisibleElementEventsOn(element)
{
    var eventList = jQuery(element).data('events');

    if (eventList != null)
    {
        jQuery.each(eventList, function (eName, eventData)
        {
            jQuery.each(eventData, function (i, e)
            {
                if (e == null)
                    return true; // continue

                if (e.data == null)
                    return true; // continue

                var elementID = e.data[0];

                if (jQuery("#" + elementID).length == 0)
                {
                    jQuery(element).unbind('.' + elementID);
                }
                else if (!jQuery("#" + elementID).is(":visible"))
                {
                    jQuery(element).unbind('.' + elementID);
                }
            });
        });
    }
}







function ListAllEventsOn(element)
{
    var eventList = jQuery(element).data('events');

    if (eventList != null)
    {
        DebugLog('', 'ListAllEventsOn Window');

        jQuery.each(eventList, function (eName, eventData)
        {
            DebugLog('EventName: ' + eName, 'Event Name:');

            jQuery.each(eventData, function (i, e)
            {
                var eID = e.data[0];
                var fn = e.data[1];
                var namespace = e.namespace;

                DebugLog('index: ' + i + ' y.ID: ' + eID);
                DebugLog('index: ' + i + ' y.FN: ' + fn);

                DebugLog('NameSpace: ' + namespace);

                if (jQuery("#" + eID).length == 0)
                {
                    DebugLog("jQuery(element).unbind('.' + " + eID + ") lenght == 0");
                }
                else if (!jQuery("#" + eID).is(":visible"))
                {
                    DebugLog("jQuery(element).unbind('.' + " + eID + ") invisible");
                }
            });
        });
    }
}

function ElementEventExistsOnNameSpace(element, evt, nameSpace)
{
    var returnValue = false;

    var eventList = jQuery(element).data('events');

    if (eventList != null)
    {
        jQuery.each(eventList, function (eventName, eventData)
        {
            if (evt == eventName)
            {
                jQuery.each(eventData, function (i, e)
                {
                    var eventNamespace = e.namespace;

                    if (nameSpace == eventNamespace)
                    {
                        returnValue = true;
                    }
                });
            }
        });
    }

    return returnValue;
}



// Opens or closes the given minimizer panel, depending on the panel status (open = true/false)
function ToggleMinimizePanel(targetPanelId, ImgId, minimizeImgUrl, maximizeImgUrl, panelStatusBoxId, appearEffect, disappearEffect)
{
    var maximizeImg = document.getElementById(ImgId);
    var targetPanel = document.getElementById(targetPanelId);
    var panelStatusBox = document.getElementById(panelStatusBoxId);

    if (targetPanel)
    {
        UnbindEventsOn(jQuery("#" + targetPanelId), targetPanelId);

        if (targetPanel.style.display == 'none')
        {
            if (appearEffect == 'Slide')
                layerObj.SlideDownLayer(targetPanelId);
            else if (appearEffect == 'Fade')
                layerObj.FadeInLayer(targetPanelId, 0.5);
            else
                layerObj.showBox(targetPanelId);

            maximizeImg.src = minimizeImgUrl;
            panelStatusBox.value = 'true';
        }
        else
        {
            if (disappearEffect == 'Slide')
                layerObj.SlideUpLayer(targetPanelId);
            else if (disappearEffect == 'Fade')
                layerObj.FadeOutLayer(targetPanelId, 0.5);
            else
                layerObj.hideBox(targetPanelId);

            maximizeImg.src = maximizeImgUrl;
            panelStatusBox.value = 'false';
        }
    }
}

// Opens or closes the given standard popup, depending on the panel status (open = true/false)
function ToggleStandardPopup(targetPanelId, panelStatusBoxId, appearEffect, disappearEffect)
{
    if (appearEffect == null)
        appearEffect = 'none';

    if (disappearEffect == null)
        disappearEffect = 'none';

    var targetPanel = $get(targetPanelId);
    var panelStatusBox = null;

    if (panelStatusBoxId != null)
        panelStatusBox = document.getElementById(panelStatusBoxId);

    if (targetPanel)
    {
        UnbindEventsOn(jQuery("#" + targetPanelId), targetPanelId);

        if (targetPanel.style.display == 'none')
        {
            if (appearEffect == 'Slide')
                layerObj.SlideDownLayer(targetPanelId);
            else if (appearEffect == 'Fade')
                layerObj.FadeInLayer(targetPanelId, 0.5);
            else
                layerObj.showBox(targetPanelId);

            if (panelStatusBox != null)
                panelStatusBox.value = 'true';

            layerObj.DimBackGround(targetPanelId, "dimmedBackground");
            layerObj.CenterLayer(targetPanelId);
        }
        else
        {
            if (disappearEffect == 'Slide')
                layerObj.SlideUpLayer(targetPanelId);
            else if (disappearEffect == 'Fade')
                layerObj.FadeOutLayer(targetPanelId, 0.5);
            else
                layerObj.hideBox(targetPanelId);

            if (panelStatusBox != null)
                panelStatusBox.value = 'false';

            layerObj.UnDimBackGround(targetPanelId);
        }
    }
    else
    {
        alert("Cant find Popup ID: " + targetPanelId);
    }
}



