﻿var popupStatus = 0;
function checkModalResize() {
    if (popupStatus == 1) {
        centerPopup();
    }
}
//loading popup with jQuery magic!
function loadExternalPopup(path) {
  $('.modalWindow').load(path, function() {
    centerPopup();
    loadPopup();
  });
}
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $(".modalBackground").css({opacity: "0.4"});
        $(".modalBackground").fadeIn(200);
        $(".modalWindow").fadeIn(200);
        popupStatus = 1;

    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $(".modalBackground").fadeOut(200);
        $(".modalWindow").fadeOut(200);
        popupStatus = 0;
    }
}

//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(".modalWindow").height();
    var popupWidth = $(".modalWindow").width();

    //only need force for IE6

    if ($.browser.msie && $.browser.version == "6.0") {
        
        $(".modalWindow").css({
            "top": (document.documentElement.scrollTop + windowHeight / 2 - popupHeight / 2) + "px",
            "left": (windowWidth / 2 - popupWidth / 2) + "px"
        });
        $(".modalBackground").css({
            "position": "absolute",
            "top": (document.documentElement.scrollTop - windowHeight/2) + 'px',
            "left": "0px",
            "height": windowHeight*2,
            "width": windowWidth
        });  
    } else {
    $(".modalWindow").css({
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    $(".modalBackground").css({
        "height": windowHeight,
        "width": windowWidth
    });
    }

    

}



//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

    //LOADING POPUP
    //Click the button event!
    $(".modalbtn").click(function(event) {
        event.preventDefault();
        loadExternalPopup($(this).attr('href'));
    });

//    $(".modalbtn").click(function() {
//        //centering with css
//        centerPopup();
//        //load popup
//        loadPopup();
//    });

    //Click out event!
    $(".modalClose").click(function() {
        disablePopup();
    });
    $(".modalBackground").click(function() {
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

});
