﻿var popupCommercialsStatus = 0;

function loadCommercialsPopup() {
    if (popupCommercialsStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupCommercials").fadeIn("slow");
        popupCommercialsStatus = 1;
    }
}

function disableCommercialsPopup() {
    if (popupCommercialsStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#popupCommercials").fadeOut("slow");
        popupCommercialsStatus = 0;
    }
}

function centerCommercialsPopup() {
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupCommercials").height();
    var popupWidth = $("#popupCommercials").width();
    $("#popupCommercials").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() {

    //LOADING POPUP
    //Click the button event!
    $("#divCommercials").click(function() {
        centerCommercialsPopup();
        loadCommercialsPopup();
    });

    $("#popupCommercialsClose").click(function() {
        disableCommercialsPopup();
    });

    $("#popupCommercialsClose2").click(function() {
        disableCommercialsPopup();
    });

    $("#backgroundPopup").click(function() {
        disableCommercialsPopup();
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupCommercialsStatus == 1) {
            disableCommercialsPopup();
        }
    });
});