﻿
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;


//loading popup with jQuery magic!
function loadPopup() {   
    //loads popup only if it is disabled
    if (popupStatus == 0) {   
        $("#popup_background").css({
            "opacity": "0.7"
        });
        $("#popup_background").fadeIn("slow");
        $("#popup_window").fadeIn("slow");
        popupStatus = 1;
    }
}


//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#popup_background").fadeOut("slow");
        $("#popup_window").fadeOut("slow");
        popupStatus = 0;
    }
}


//centering popup
function centerPopup() {

    var ScrollTop = document.body.scrollTop;
    var ScrollBottom = document.body.s
  
    if (ScrollTop == 0) {
        if (window.pageYOffset)
            ScrollTop = window.pageYOffset;
        else
            ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
  
    //request data for centering
    //var windowWidth = document.documentElement.clientWidth;
    //var windowHeight = document.documentElement.clientHeight;

    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight + ScrollTop;
    
    var popupHeight = $("#popup_window").height();
    var popupWidth = $("#popup_window").width();
    //centering
    $("#popup_window").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
   
    //only need force for IE6
    $("#popup_background").css({
        "height": document.body.clientHeight,
        "width": document.body.clientWidth
    });    
}


$(document).ready(function() {

//LOADING POPUP
//Click the button event!
$("#button").click(function() {    
    //centering with css
    centerPopup();
    //load popup
    loadPopup();
});


//CLOSING POPUP
//Click the x event!
$("#popup_window_close").click(function() {
    disablePopup();
});
//Click out event!
$("#popup_background").click(function() {
    disablePopup();
});
//Press Escape event!
$(document).keypress(function(e) {
    if (e.keyCode == 27 && popupStatus == 1) {
        disablePopup();
    }
});
});

