﻿/// <reference path="../jQuery/jquery-1.6.1.js"/>

function ShowPopupOverlay(postFix)
{
    // Check if overlay exists, if not, create it.
    var popupUIOverlay = jQuery("#popupUIOverlay");
    if (!popupUIOverlay.length)
    {
        CreateOverlay();

        popupUIOverlay = jQuery("#popupUIOverlay");
    }

    // Add overlay click event to hide the popup.
    popupUIOverlay.unbind('click');
    popupUIOverlay.bind('click', function ()
    {
        HidePopupOverlay(postFix);
    });

    // Add cloce icon click event to hide the popup.
    var closeIcon = jQuery("#closeIcon" + postFix);
    closeIcon.unbind('click');
    closeIcon.bind('click', function ()
    {
        HidePopupOverlay(postFix);
    });

    // Set the width og height on the overlay to the documents width og height.
    SetOverlaySize(popupUIOverlay);

    popupUIOverlay.show();

    MakePopupDraggable(postFix);

    PositionPopup(postFix);

    SetBodyWidthAndHeight(postFix);

    var popupUI = jQuery("#popupUI" + postFix);

    popupUIOverlay.css("z-index", popupUI.css("z-index") - 1);
//    popupUI.css("z-index", popupUIOverlay.css("z-index") + 1);

    popupUI.fadeIn();

    jQuery(window).scroll([postFix], PositionPopupHandler);
    jQuery(window).resize([postFix], PositionPopupHandler_Resize);
}

function HidePopupOverlay(postFix)
{
    var popupUIOverlay = jQuery("#popupUIOverlay");
    var popupUI = jQuery("#popupUI" + postFix);

    popupUIOverlay.hide();
    popupUI.fadeOut();

    jQuery(window).unbind("scroll", PositionPopupHandler);
    jQuery(window).unbind("resize", PositionPopupHandler_Resize);
}

function SetBodyWidthAndHeight(postFix)
{
    var popupUI = jQuery("#popupUI" + postFix),
        body = jQuery("#body" + postFix);

    body.width(popupUI.width() - 20);
    body.height(popupUI.height() - 48);
}

function PositionPopupHandler(event)
{
    var postFix = event.data[0];

    PositionPopup(postFix);
}

function PositionPopupHandler_Resize(event)
{
    var postFix = event.data[0];

    PositionPopup(postFix);
    SetOverlaySize(jQuery("#popupUIOverlay"));
}

function PositionPopup(postFix)
{
    PositionPopupCenterLayer(postFix);
}

function PositionPopupCenterLayer(postFix)
{
    var popupUI = jQuery("#popupUI" + postFix);

    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
    });
}

function MakePopupDraggable(postFix)
{
    jQuery("#popupUI" + postFix).draggable({ handle: "#titlebar" + postFix });
    jQuery("#titlebar" + postFix).addClass("PopupDraggable");
}

function CreateOverlay()
{
    jQuery('<div id="popupUIOverlay" class="PopupUIOverlay"></div>').appendTo('body');
}

function SetOverlaySize(overlay)
{
//    DebugLog("Overlay: " + jQuery(overlay).width() + " document: " + jQuery(document).width() + " window: " + jQuery(window).width());

    jQuery(overlay).width(jQuery(document).width());
    jQuery(overlay).height(jQuery(document).height());
}

function CenterElement_WindowScrollEvent(event)
{
    var elementID = event.data[0];

    CenterPositionLayer(elementID);
}

function CenterElement_WindowResizeEvent(event)
{
    var elementID = event.data[0];
    var overlayID = event.data[1];

    CenterPositionLayer(elementID);
    SetOverlaySize(jQuery("#" + overlayID));
}

function RePositionPopupAfterWindowScrollEvent(event)
{
    var elementID = event.data[0];

    // Only fire event if element exists and is visible.
    if (jQuery("#" + elementID).length > 0 &&
        jQuery("#" + elementID).is(':visible'))
    {
        var positionFunction = event.data[1];

        eval(positionFunction + "('" + elementID + "');");
    }
}

function RePositionPopupAfterWindowResizeEvent(event)
{
    var elementID = event.data[0];

    // Only fire event if element exists and is visible.
    if (jQuery("#" + elementID).length > 0 &&
        jQuery("#" + elementID).is(':visible'))
    {
        var positionFunction = event.data[1];

        eval(positionFunction + "('" + elementID + "');");
    }
}

// layerObj.CenterLayer(elementID)...

function CenterPositionLayer(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;

    if (cssLeft < 0)
        cssLeft = 0;

    if (cssTop < 0)
        cssTop = 0;

    var cssLeftPx = cssLeft + "px",
        cssTopPx = cssTop + "px";

    popupUI.css({
        "left": cssLeftPx,
        "top": cssTopPx
    });
}

function BottomRightPositionLayer (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;

//    DebugLog('cssLeft: ' + cssLeft + ' cssTop: ' + cssTop + ' winScrollTop: ' + winScrollTop);

    if (cssLeft < 0)
        cssLeft = 0;

    if (cssTop < 0)
        cssTop = 0;

    var cssLeftPx = cssLeft + "px",
        cssTopPx = cssTop + "px";

    popupUI.css({
        "left": cssLeftPx,
        "top": cssTopPx
    });
}

function BottomLeftPositionLayer (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 - 26 < popupUI.width())
    {
        cssLeft = winScrollLeft + (winWidth - popupUI.width()) - 6;
    }

    if (winHeight < popupUI.height())
        cssTop = 0;

    if (cssLeft < 0)
        cssLeft = 0;

    if (cssTop < 0)
        cssTop = 0;

    var cssLeftPx = cssLeft + "px",
        cssTopPx = cssTop + "px";

    popupUI.css({
        "left": cssLeftPx,
        "top": cssTopPx
    });
}

function RightCenterPositionLayer (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;

    if (cssLeft < 0)
        cssLeft = 0;

    if (cssTop < 0)
        cssTop = 0;

    var cssLeftPx = cssLeft + "px",
        cssTopPx = cssTop + "px";

    popupUI.css({
        "left": cssLeftPx,
        "top": cssTopPx
    });
}

function TopCenterPositionLayer (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;

    if (cssLeft < 0)
        cssLeft = 0;

    if (cssTop < 0)
        cssTop = 0;

    var cssLeftPx = cssLeft + "px",
        cssTopPx = cssTop + "px";

    popupUI.css({
        "left": cssLeftPx,
        "top": cssTopPx
    });
}

function TopRightPositionLayer (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;

    if (cssLeft < 0)
        cssLeft = 0;

    if (cssTop < 0)
        cssTop = 0;

    var cssLeftPx = cssLeft + "px",
        cssTopPx = cssTop + "px";

    popupUI.css({
        "left": cssLeftPx,
        "top": cssTopPx
    });
}

function TopLeftPositionLayer (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;

    if (cssLeft < 0)
        cssLeft = 0;

    if (cssTop < 0)
        cssTop = 0;

    var cssLeftPx = cssLeft + "px",
        cssTopPx = cssTop + "px";

    popupUI.css({
        "left": cssLeftPx,
        "top": cssTopPx
    });
}
