﻿var msRefreshQuery;

function ShowItemDiscussion(type, id) 
{
    var url = "/Citizens/UserDiscussion.aspx?ID=" + id + "&ItemType=" + type;
    PopupWindowShow(url);
    return false;
}

function PopupWindowShow(url) {

    msRefreshQuery = escape(url);

    // if we're inside a framset we have to do things a little different
    var fInsideFrameset = false;
    try {
        fInsideFrameset = $(window.top.document.body).find("frameset").length > 0;
    }
    catch (ex) {
    }

    // darken the background
    if (!fInsideFrameset) {
        var blackBox = $("#divBlackBoxOverlay");
        if (blackBox.length == 0) {
            blackBox = $("<div id='divBlackBoxOverlay' class='BlackBoxOverlay'></div>");
            $("form").append(blackBox);
        }

        blackBox.css("width", "100%");
        blackBox.css("height", $(document).height() + "px");

        blackBox.css("opacity", 0.1);
        blackBox.fadeTo("400", 0.50);
        blackBox.css("display", "");
    }
    
    var popup = $("#divPopupBoxHolder");
    if (popup.length == 0) 
    {
        popup = $("<div id='divPopupBoxHolder' class='PopupBoxHolder ui-draggable' style='display:none'></div>");

        var sTop = "<div class='cornerImageHolder'></div><div class='closeButtonHolder'><input type='image' src='/Citizens/Images/close.png' onclick='javascript:return PopupWindowClose();'</div>";   
        var sInnerHtml = "<table class='roundedPopupBox' cellspacing='0' cellpadding='0'><tr><td class='topLeft'><div></div></td><td class='top'>" + sTop + "</td><td class='topRight'><div></div></tr><tr><td class='left'></td> <td id='tdContents'><div class='PopupTitleBar'></div></td><td class='right'></tr><tr><td class='bottomLeft'></td> <td class='bottom'><div></div></td><td class='bottomRight'></tr></table>";
        popup.append(sInnerHtml);
        
        $("form").append(popup);
    }
    
    var oFrame = popup.find("#frame");
    if (oFrame.length == 0)
    {
        oFrame = $("<iframe id='frame' frameborder='0' scrolling='no'></iframe>");
        popup.find("#tdContents").append(oFrame);
    }

    oFrame.unbind("load");
    oFrame.load(function() {

        var body = $(oFrame[0].contentWindow.document.body);
        if (body.length == 0)
            return;

        // get title text and corner image, if any
        var cornerImage = body.find("#popupCornerImage");
        if (cornerImage.length > 0)
            popup.find(".cornerImageHolder").html(cornerImage.html());

        var titleMessage = body.find("#popupTitleMessage");
        if (titleMessage.length > 0) {
            titleMessage.css("display", "none");
            popup.find(".PopupTitleBar").html(titleMessage.html());
        }

        body = body.find(".PopupBox");
        if (body.length == 0)
            return;

        var width = parseInt(body.css("width").replace("px", ""));
        var height = parseInt(body.css("height").replace("px", ""));

        popup.find("#tdTopLeft").css("width", (width) + "px");
        popup.find("#tdTopRight").css("width", "18px");

        popup.css("width", (width) + "px");
        popup.css("height", (height + 24) + "px");
        oFrame.css("width", (width) + "px");
        oFrame.css("height", height + "px");

        var iWindowScrollPos = $(window).scrollTop();

        var iMiddleX;
        if (fInsideFrameset)
            iMiddleX = 0;
        else
            iMiddleX = ($(window).width() - width) / 2;

        var iMiddleY = (($(window).height() - (height + 24)) / 2) + iWindowScrollPos;

        popup.css("left", iMiddleX + "px");
        popup.css("top", iMiddleY + "px");
        popup.css("display", "");
        body.css("display", "");
    });

    if (url.indexOf("PopupMode=Yes") > -1)
        oFrame.attr("src", url);    
    else if (url.indexOf("?") == -1)
        oFrame.attr("src", url + "?PopupMode=Yes");
    else
        oFrame.attr("src", url + "&PopupMode=Yes");

    return false;
}

function PopupWindowClose() 
{
    var popup = $("#divPopupBoxHolder");
    if (popup.length == 0) 
    {
        parent.PopupWindowClose();
        return false;
    }

    popup.css("display", "none");
    popup.find("#frame").attr("src", "");
    $("#divBlackBoxOverlay").css("display", "none");

    return false;
}

$(function() {

    try {
        parent.BackButtonFix();
    }
    catch (ex) {

    }

    if (window.location.href.indexOf("RefreshPage=Yes") > -1) {
        ShowPleaseWait();
        parent.RefreshPage(window.location.href);
        return;
    }

    var sRefreshQuery = $(".hfShowPopup").val();
    $(".hfShowPopup").val("");

    if (sRefreshQuery == undefined || sRefreshQuery.length == 0)
        return;

    PopupWindowShow(unescape(sRefreshQuery));

});

//this resolves an issue with the back button after closing a popup. it would wind up showing the popup window again but without the dark background
//this fix has been tested in firefox, chrome, and ie8
function BackButtonFix() 
{
    var blackBox = $("#divBlackBoxOverlay");
    if (blackBox.css("display") != "none")
        return;
       
    var popup = $("#divPopupBoxHolder");
    if(popup.length == 0)
        return;
        
    var oFrame = popup.find("#frame");
    if(oFrame.length == 0)
        return;
        
    oFrame.attr("src","");
    popup.css("display","none");
}

function RefreshPage(url) {
    
    // remove RefreshPage=Yes from the query
    url = url.replace("&RefreshPage=Yes", "");
    url = url.replace("RefreshPage=Yes", "");
    
    msRefreshQuery = url;
    
    if (msRefreshQuery == undefined)
        return;
        
    if (msRefreshQuery.indexOf("Login.aspx") > -1)
        msRefreshQuery = "";
        
    $(".hfShowPopup").val(msRefreshQuery);
    $(".btnRefreshPage")[0].click();

    //if this was called in a window, refresh the parent page
    try 
    {
        window.top.opener.RefreshPage("");
    }
    catch (ex) {

    }
}

function ChangeParentPage(url) {

    // if we're in the web outline, we want to only change the detail panel
    if (window.location.href.indexOf("WebOutline") > -1) {
        var detailFrame = $("#divRightPanel");

        //insert frame + css class parameteres if they're not already there
        if (url.indexOf("Frame=") == -1)
            url += "&Frame=Video";

        if (url.indexOf("CssClass=") == -1)
            url += "&CssClass=AgendaOutline";
            
        detailFrame.html("");
        detailFrame.attr("src", url);

        FrameInsert(detailFrame, function(e) { if (moRightPanelScroller != undefined) { moRightPanelScroller.scrollTo(0, 0); } });
        PopupWindowClose();
        return;
    }
    
    if (window.top.location.href.indexOf("WebOutline") > -1) {

        parent.ChangeParentPage(url);
        return;
    }

    // otherwise, try setting the outer window
    try 
    {
        window.top.location = url;
        return;
    }
    catch (e) { }

    // if there is no outer window, just set the current window
    window.location = url;
    
}

function ShowPleaseWait() 
{
    // replace entire body
    var oForm = $("form");
    oForm.html("<div class='LoginPleaseWait'>Logging in, please wait . . . </div>");

}


