var CurrentPopupDesc = "";
var CurrentPopupButtonOffsetX = 16, CurrentPopupButtonOffsetY = 16;

function EnsurePopup()
{
    if ($("#PopupBackground").length == 0)
    {
        var PopupHTML = "";
        PopupHTML += "<div id=\"PopupBackground\" class=\"PopupContainer\"></div>";
        PopupHTML += "<div id=\"PopupForegroundBlur\" class=\"PopupContainer\"></div>";
        PopupHTML += "<div id=\"PopupForeground\" class=\"PopupContainer\">";
        PopupHTML += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"width:100%; height:100%\">";
        PopupHTML += "<tr><td><iframe id=\"PopupFrame\" src=\"\" frameborder=\"0\" scrolling=\"no\"></iframe>";
        PopupHTML += "</td></tr></table></div>";
        PopupHTML += "<img id=\"PopupCloseButton\" src=\"/Images/CloseButton.png\" alt=\"Close Popup\" title=\"Close Popup\" onclick=\"ClosePopup()\" />";
        $(document.body).append(PopupHTML);
    }
}

function ResizePopup()
{
    if ($("#PopupBackground").css("visibility") == "visible") {
        var rView = getViewportRect();

        var fWidth = parseDecimal($("#PopupForeground").css("width"));
        var fHeight = parseDecimal($("#PopupForeground").css("height"));

        var sT = $(window).scrollTop();
        var sL = $(window).scrollLeft();

        $("#PopupBackground").css({
            left: sL,
            top: sT,
            height: rView.height,
            width: rView.width
        });

        $("#PopupForeground").css({
            left: sL+((rView.width-fWidth)/2),
            top: sT+((rView.height-fHeight)/2)
        });

        $("#PopupCloseButton").css({
            left: sL+((rView.width-fWidth)/2)+fWidth-CurrentPopupButtonOffsetX, 
            top: sT+((rView.height-fHeight)/2)-CurrentPopupButtonOffsetY
        });
    
        $("#PopupForegroundBlur").css({
            left: sL+parseDecimal($("#PopupForeground").css("left"))-30,
            top: sT+parseDecimal($("#PopupForeground").css("top"))-30
        });
    }
}

function PopupIEBlur()
{
    if (browserInfo.ie)
    {
        $("#PopupForegroundBlur").css({
            width: parseDecimal($("#PopupForeground").css("width")),
            height: parseDecimal($("#PopupForeground").css("height")),
            left: parseDecimal($("#PopupForeground").css("left"))-30,
            top: parseDecimal($("#PopupForeground").css("top"))-30
        });

        $("#PopupForegroundBlur").css({opacity: 1});
        $("#PopupForegroundBlur").css({"visibility":"visible","display":"block","filter":
            "progid:DXImageTransform.Microsoft.Alpha(opacity=65,style=0) " +
            "progid:DXImageTransform.Microsoft.Blur(pixelradius=30)"});
    }
}

function Popup(fWidth, fHeight, fURL)
{
    OpenPopup(fWidth, fHeight, 16, 16, fURL);
}

function ImagePopup() { PopupImage.apply(this, arguments); }
function PopupImage()
{
    var url = "";
    CurrentPopupDesc = ((arguments.length > 1) && (typeof(arguments[0]) == "string")) ? arguments[1] : "";
    
    if (arguments.length > 0)
    {
        url = arguments[0];
    }

    if (url.length == 0)
    {
        return;
    }
    
    OpenPopup(640, (CurrentPopupDesc.length > 0) ? 512 : 480, 16, 16, url + ",AsHtml");
}

function MapPopup(fWidth, fHeight, fURL)
{
    OpenPopup(fWidth, fHeight, 29, -3, fURL);
}

function OpenPopup(fWidth, fHeight, buttonOffsetX, buttonOffsetY, fUrl)
{
    EnsurePopup();
    
    var viewportRect = getViewportRect();
    var wScrollTop = $(window).scrollTop();
    var wScrollLeft = $(window).scrollLeft();

    CurrentPopupButtonOffsetX = buttonOffsetX;
    CurrentPopupButtonOffsetY = buttonOffsetY;

    $("#PopupFrame").css({width:fWidth, height:fHeight});
    $("#PopupFrame").attr("src", fUrl);
    
    $("#PopupCloseButton").css({
        left: (wScrollLeft+((viewportRect.width-fWidth)/2)+fWidth-CurrentPopupButtonOffsetX), 
        top: (wScrollTop+((viewportRect.height-fHeight)/2)-CurrentPopupButtonOffsetY),
        "visibility":"visible", 
        "display":"block", 
        opacity: 0
    });
    
    //IE seems to not register left/top until you ask for it
    $("#PopupCloseButton").position(); //do nothing with the position to fix IE

    $("#PopupBackground").css({left:wScrollLeft, top:wScrollTop, opacity: 0, width: viewportRect.width, height: viewportRect.height, "visibility":"visible", "display":"block"});
    //$("#PopupBackground").animate({opacity: .75}, 1000);
    
    $("#PopupForeground").css({height:5, width:5, opacity: 0, left: wScrollLeft+((viewportRect.width-2)/2), top: wScrollTop+((viewportRect.height-2)/2), "visibility":"visible", "display":"block"});
    
    $("#PopupForeground").animate({opacity: .5, width:fWidth, left: wScrollLeft+((viewportRect.width-fWidth) / 2)}, 250, function() {
        $("#PopupForeground").animate({opacity: 1, height:fHeight, top: wScrollTop+((viewportRect.height-fHeight) / 2)}, 250, function() {
            $("#PopupCloseButton").animate({opacity: 1}, 250, PopupIEBlur);
        });
    });
}

function ClosePopup()
{
    if ($("#PopupBackground").css("visibility") == "visible") {
        var rView = getViewportRect();
        var sT = $(window).scrollTop();
        var sL = $(window).scrollLeft();

        $("#PopupCloseButton").css({"visibility":"hidden","display":"none","position": "absolute",opacity: 0});
        $("#PopupForegroundBlur").css({"visibility":"hidden","display":"none","position": "absolute",opacity: 0});

        $("#PopupForeground").animate({opacity: .5, height: 5, top: sT+(rView.height / 2)}, 250, function() {
            $("#PopupForeground").animate({opacity: 0, width: 5, left: sL+(rView.width / 2)}, 250, function() {
                $("#PopupForeground").css({"visibility":"hidden","display":"none","position": "absolute"});
                //$("#PopupBackground").animate({opacity: 0}, 500, function() {
                    $("#PopupBackground").css({"visibility":"hidden","display":"none","position": "absolute"});
                //});
            });
        });
    }
}

$(function() { $(window).resize(function(ev) { ResizePopup(); }); });
