var popupStatus = 0;
//this code will load popup with jQuery magic
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupOnce").fadeIn("slow");
        popupStatus = 1;
    }
}

//this code will disable popup when click on x
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $("#popupOnce").fadeOut("slow");
        popupStatus = 0;
    }
}

//this code will center popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupOnce").height();
    var popupWidth = $("#popupOnce").width();
    //centering
    $("#popupOnce").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6   
    $("#backgroundPopup").css({
        "height": windowHeight
    });
   
}
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
    if ($.cookie("20111029") != 1) {   
        //centering with css
        centerPopup();
        //load popup
        loadPopup();   
    }       
    //CLOSING POPUP
    //Click the x event
    $("#popupOnceClose").click(function(){
        disablePopup();
        $.cookie("20111029", "1", { expires: 60 });
    });
    //Click out event
    $("#backgroundPopup").click(function(){
        disablePopup();
        $.cookie("20111029", "1", { expires: 60 });
    });
    //Press Escape event
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
            $.cookie("20111029", "1", { expires: 60 });
        }
    });
});
