﻿var popup_area_id = "popup_area";
var overlay_id = "master_overlay";
var iframe_id = "iframe_hide";
var popup_id = "master_popup";

var popup_parent_id = "";

// composite ids, for jQuery calls
var PopupAreaID = "#" + popup_area_id;
var OverlayID = "#" + overlay_id;
var IframeID = "#" + iframe_id;
var PopupID = "#" + popup_id;

var ie6 = (jQuery.browser.msie && jQuery.browser.version < 7);

//appends div and iframe elements necessary to show popups on page to DOM
function AppendPopupElements() {
    $('body').append('<div id="' + popup_area_id + '"></div>');

    $(PopupAreaID).append('<div id="' + overlay_id + '"></div>');

    //append iframe to hide select elements in IE 6
    if (jQuery.browser.msie && jQuery.browser.version < 7) {
        $(PopupAreaID).append('<iframe id="' + iframe_id + '" frameborder="0" scrolling="no" src="javascript;"></iframe>');
    }

    $(PopupAreaID).append('<div id="' + popup_id + '"></div>');

}

function ShowPopup(id) {
    // bind keypress event to handle ESC key    
    document.onkeydown = function(e) {
        var keycode;

        if (e == null) // ie
        {
            keycode = event.keyCode;
        }
        else // mozilla
        {
            keycode = e.which;
        }

        if (keycode == 27) {
            ClosePopup();
        }
    };

    var oWidth;
    var oHeight;

    oWidth = $(window).width();

    if ($(window).height() >= $(document).height()) {
        oHeight = $(window).height();
    }
    else {
        oHeight = $(document).height();
    }

    $(OverlayID).css('width', oWidth);
    $(OverlayID).css('height', oHeight);

    // IE 6 specific code
    if (ie6) {
        $(IframeID).css('width', oWidth);
        $(IframeID).css('height', oHeight);

        $(OverlayID).css('position', 'absolute');
        $(PopupID).css('position', 'absolute');
    }

    // show elements, and set master area invisible, to calculate dimensions

    $(PopupAreaID).css('visibility', 'hidden');

    $(PopupAreaID).css('display', 'block');
    $(OverlayID).css('display', 'block');

    if (ie6) {
        $(IframeID).css('display', 'block');
    }

    $(PopupID).css('display', 'block');

    //prepare popup contents

    popup_parent_id = '#' + $('#' + id).parent().attr('id');

    $(PopupID).append($('#' + id));

    var h = $(PopupID).height();
    var w = $(PopupID).width();

    $(PopupID).css('height', h + 'px');
    $(PopupID).css('width', w + 'px');

    var l = ($(window).width() / 2) - ($(PopupID).width() / 2);
    var t = ($(window).height() / 2) - ($(PopupID).height() / 2);

    var scroll;

    if (ie6) {
        scroll = $(window).scrollTop();
    }
    else {
        scroll = 0;
    }

    $(PopupID).css('left', l + 'px');
    $(PopupID).css('top', scroll + t + 'px');

    $(PopupAreaID).css('visibility', 'visible');

    //made this specific to popup area.  applying to all divs is causing issues on tabbar on IE6 when modal is closed.
    $(PopupAreaID + " div").ifixpng();
}

function ClosePopup() {
    // hide elements
    $(PopupAreaID).css('display', 'none');
    $(OverlayID).css('display', 'none');

    if (ie6) {
        $(IframeID).css('display', 'none');
    }

    $(PopupID).css('display', 'none');

    $(popup_parent_id).append($(PopupID + "> div"));

}
