﻿var popup_area_id = "popup_area";
var overlay_id = "master_overlay";
var iframe_id = "iframe_hide";
var popup_id = "master_popup";


// 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);

//renders div and iframe elements necessary to show popups on page
function RenderPopupElements()
{
    document.writeln("<div id=\"" + popup_area_id + "\">");
    document.writeln("  <div id=\"" + overlay_id + "\"></div>");
    
    //write out iframe to hide select elements in IE 6
    if(jQuery.browser.msie && jQuery.browser.version < 7)
    {
       document.writeln('<iframe id="' + iframe_id + '" frameborder="0" scrolling="no" src="javascript;"></iframe>');
    }
    
    document.writeln("  <div id=\"" + popup_id + "\"></div>");
    document.writeln("</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 + 'px');
    $(OverlayID).css('height', oHeight + 'px');
    
    // IE 6 specific code
    if(ie6)
    {
        $(IframeID).css('width', oWidth + 'px');
        $(IframeID).css('height', oHeight + 'px');
        
        $(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
    
    $(PopupID).append($('#' + id).clone());
    
    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);
    
    $(PopupID).css('left', l + 'px');
    $(PopupID).css('top',  t + 'px');
   
    $(PopupAreaID).css('visibility', 'visible'); 
    
    $('div').ifixpng();  
}   

function ClosePopup() 
{
    // hide elements
    $(PopupAreaID).css('display', 'none');    
    $(OverlayID).css('display', 'none');
    
    if(ie6)
    {
        $(IframeID).css('display', 'none');
    }  
    
    $(PopupID).css('display', 'none');    
    
    $(PopupID).empty();
    
    $('#country_select').removeAttr('disabled');
    
} 