// popup initializing:
//   0: disabled
//   1: enabled
var popupStatus = 0;

function loadPopup(width, height)
{
    // if width and height are given set the values:
    if (width && height )
    {
        $jQ("#popup").css({"width": width + "px", "height": height + "px" });
        $jQ("#popupContent").css({"height": (height - 45) + "px" });
        setTimeout("CSBfleXcroll('popupContent');", 5);
    }

    // be sure the popup is centered before showing
    centerPopup();

    //loads popup only if it is disabled
    if (popupStatus == 0)
    {
        $jQ("#popupBackground").css({"opacity": "0.7"});
        $jQ("#popupBackground").fadeIn("slow");
        $jQ("#popup").fadeIn("slow");
        popupStatus = 1;
    }
}

function disablePopup()
{
    //disables popup only if it is enabled
    if(popupStatus==1)
    {
        $jQ("#popupBackground").fadeOut("slow");
        $jQ("#popup").fadeOut("slow");
        popupStatus = 0;
    }
}

function centerPopup()
{
    //request data for centering
    var windowWidth     = document.documentElement.clientWidth;
    var windowHeight    = document.documentElement.clientHeight;
    var popupHeight     = $jQ("#popup").height();
    var popupWidth      = $jQ("#popup").width();

    //centering
    $jQ("#popup").css({
        "position": "absolute",
        "top":      windowHeight/2-popupHeight/2,
        "left":     windowWidth/2-popupWidth/2
    });

    //only need force for IE6
    $jQ("#popupBackground").css({
        "height": windowHeight
    });
}

function addPopupCloseEvents()
{
    // Click the x event!
    $jQ("#popupClose").click(function(){ disablePopup(); });

    //Click out event!
    $jQ("#popupBackground").click(function(){ disablePopup(); });

    //Press Escape event!
    $jQ(document).keypress(function(e){
        if (e.keyCode == 27 && popupStatus == 1)
        {
            disablePopup();
        }
    });
}
