var popupStatus = 0;
var popupvis = "";

function loadPopup(popname) {
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(popname).fadeIn("slow");
		popupStatus = 1;
		popupvis = popname;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		if (popupvis != "")
			$(popupvis).fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(popname){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupWidth = $(popname).width();
	var popupHeight = $(popname).height();
	var popTop = $(window).scrollTop();
	var PopY = windowHeight/2 - popupHeight/2;
	//centering
	$(popname).css({
	    "position": "absolute",
	    "top": PopY + popTop - 5,
		"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!
    $("#button").click(function () {
        //centering with css
        centerPopup("#popupmain");

        //load popup
        loadPopup("#popupmain");
    });

    $("#shot1").click(function (event) {
        //centering with css
        centerPopup("#screenPopup1");

        //load popup
        loadPopup("#screenPopup1");
        event.preventDefault();
    });

    $("#shot2").click(function (event) {
        //centering with css
        centerPopup("#screenPopup2");

        //load popup
        loadPopup("#screenPopup2");
        event.preventDefault();
    });

    $("#shot3").click(function (event) {
        //centering with css
        centerPopup("#screenPopup3");

        //load popup
        loadPopup("#screenPopup3");
        event.preventDefault();
    });

    $("#shot4").click(function (event) {
        centerPopup("#screenPopup4");

        loadPopup("#screenPopup4");
        event.preventDefault();
    });

    $("#shot5").click(function (event) {
        centerPopup("#screenPopup5");

        loadPopup("#screenPopup5");
        event.preventDefault();
    });

    $("#shot6").click(function (event) {
        centerPopup("#screenPopup6");

        loadPopup("#screenPopup6");
        event.preventDefault();
    });

    $("#shot7").click(function (event) {
        centerPopup("#screenPopup7");

        loadPopup("#screenPopup7");
        event.preventDefault();
    });

    $("#shot8").click(function (event) {
        centerPopup("#screenPopup8");

        loadPopup("#screenPopup8");
        event.preventDefault();
    });

    $("#shot9").click(function (event) {
        centerPopup("#screenPopup9");

        loadPopup("#screenPopup9");
        event.preventDefault();
    });

    $("#shot10").click(function (event) {
        centerPopup("#screenPopup10");

        loadPopup("#screenPopup10");
        event.preventDefault();
    });

    $("#shot11").click(function (event) {
        centerPopup("#screenPopup11");

        loadPopup("#screenPopup11");
        event.preventDefault();
    });

    //Click out event!
    $("#backgroundPopup").click(function () {
        disablePopup();
    });

    //Click out event!
    $("#screenPopup_close_button").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    //Click out event!
    $("#screenPopup_close_button1").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    //Click out event!
    $("#screenPopup_close_button2").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    //Click out event!
    $("#screenPopup_close_button3").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    //Click out event!
    $("#screenPopup_close_button4").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    $("#screenPopup_close_button5").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    $("#screenPopup_close_button6").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    $("#screenPopup_close_button7").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    $("#screenPopup_close_button8").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    $("#screenPopup_close_button9").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    $("#screenPopup_close_button10").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    $("#screenPopup_close_button11").click(function (event) {
        disablePopup();
        event.preventDefault();
    });

    //Click out event!
    $("#screenPopup").click(function () {
        disablePopup();
    });

    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

});
