﻿var popupStatus = 0;
function checkModalResize() {
    if (popupStatus == 1) {
        centerPopup();
    }
}
//loading popup with jQuery magic!
function loadExternalPopup(path) {
    $('.modalWindow').load(path, function() {
        loadPopup();
        centerPopup();
    });
}

function loadPopup(byId) {
    centerPopup();
    
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $(".modalBackground").css({
            "opacity": "0.3"
        });
        $(".modalBackground").fadeIn("slow");

        var element;

        if (byId) {
            element = $("#" + byId);
        }
        else {
            element = $(".modalWindow");
        }
        
        element.fadeIn("slow", function() { sIFR.replace(sifrRhode, { selector: 'h2', css: ['.sIFR-root { color: #FFFFFF;}'], wmode: 'transparent' }); });
        
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $(".modalBackground").fadeOut("slow");
        $(".modalWindow").fadeOut("slow");
        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!
    $(".guidemodalbtn").click(function(event) {
        event.preventDefault();
        loadExternalPopup($(this).attr('href'));
    });

    $(".modalbtnex").click(function(event) {
        event.preventDefault();
        loadExternalPopup($(this).attr('href'));
    });

    $(".modalbtn").click(function() {
        loadPopup();
    });

    $(".modalbtn1").click(function() {
        loadPopup("modalWindow1");
    });

    $(".modalbtn2").click(function() {
        loadPopup("modalWindow2");
    });

    $(".modalbtn3").click(function() {
        loadPopup("modalWindow3");    
    });

    //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();
        }
    });

});
