//***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!                  
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(x){
    //loads popup only if it is disabled
    if(popupStatus==0){
if(x==0){
if (document.documentElement && document.documentElement.clientWidth){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");	   
	   }  
        $("#popupContact").fadeIn("slow");
}
if(x==1){
if (document.documentElement && document.documentElement.clientWidth){
        $("#backgroundPopup1").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup1").fadeIn("slow");
	   }
        $("#popupContact1").fadeIn("slow");
}
if(x==2){
if (document.documentElement && document.documentElement.clientWidth){
        $("#backgroundPopup2").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup2").fadeIn("slow");
	   }
        $("#popupContact2").fadeIn("slow");
}
if(x==3){
if (document.documentElement && document.documentElement.clientWidth){
        $("#backgroundPopup3").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup3").fadeIn("slow");
	   }
        $("#popupContact3").fadeIn("slow");
}
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(x){
    //disables popup only if it is enabled
    if(popupStatus==1){
if(x==0){
        $("#popupContact").fadeOut("slow");
        $("#backgroundPopup").fadeOut("slow");
}
if(x==1){
        $("#popupContact1").fadeOut("slow");
        $("#backgroundPopup1").fadeOut("slow");
}
if(x==2){
        $("#popupContact2").fadeOut("slow");
        $("#backgroundPopup2").fadeOut("slow");
}
if(x==3){
        $("#popupContact3").fadeOut("slow");
        $("#backgroundPopup3").fadeOut("slow");
}
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(x){
    //request data for centering
if (self.innerWidth) 
   var windowWidth = self.innerWidth; 
else if (document.documentElement && document.documentElement.clientWidth) 
    var windowWidth = document.documentElement.clientWidth; 
else if (document.body) 
    var windowWidth = document.body.clientWidth; 
//      
 if (self.innerHeight) 
    var windowHeight = self.innerHeight; 
else if (document.documentElement && document.documentElement.clientHeight) 
    var windowHeight = document.documentElement.clientHeight; 
else if (document.body) 
    var windowHeight = document.body.clientHeight;      
    
//    var windowWidth = document.documentElement.clientWidth;
//    var windowHeight = document.documentElement.clientHeight;
if(x==0){
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
}
if(x==1){
    var popupHeight = $("#popupContact1").height();
    var popupWidth = $("#popupContact1").width();
}    
if(x==2){
    var popupHeight = $("#popupContact2").height();
    var popupWidth = $("#popupContact2").width();
}    
if(x==3){
    var popupHeight = $("#popupContact3").height();
    var popupWidth = $("#popupContact3").width();
}    
	
//	alert("w="+windowWidth+" h="+windowHeight+" ph="+popupHeight+"
// pw="+popupWidth);
 var pH = windowHeight/2-popupHeight/2;
 var pW = windowWidth/2-popupWidth/2;
 if(popupHeight==663){
  pH=36;
  pW=178;
 }
 if(x==0){
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": pH,
        "left": pW
    });
}    
 if(x==1){
    //centering
    $("#popupContact1").css({
        "position": "absolute",
        "top": pH,
        "left": pW
    });
}    
 if(x==2){
    //centering
    $("#popupContact2").css({
        "position": "absolute",
        "top": pH,
        "left": pW
    });
}    
 if(x==3){
    //centering
    $("#popupContact3").css({
        "position": "absolute",
        "top": pH,
        "left": pW
    });
}    
/*      "top":  windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2     */


    //only need force for IE6
if(x==0){    
    $("#backgroundPopup").css({
        "height": windowHeight
    });
}    
if(x==1){    
    $("#backgroundPopup1").css({
        "height": windowHeight
    });
}    
if(x==2){    
    $("#backgroundPopup2").css({
        "height": windowHeight
    });
}    
if(x==3){    
    $("#backgroundPopup3").css({
        "height": windowHeight
    });
}    
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

    //LOADING POPUP
    //Click the button event!
    $("#button").click(function(){
        //centering with css
        centerPopup('0');
        //load popup
        loadPopup('0');
    });
    
    $("#button1").click(function(){
        //centering with css
        centerPopup('1');
        //load popup
        loadPopup('1');
    });
    $("#button2").click(function(){
        //centering with css
        centerPopup('2');
        //load popup
        loadPopup('2');
    });
    $("#button3").click(function(){
        //centering with css
        centerPopup('3');
        //load popup
        loadPopup('3');
    });
                
    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function(){
        disablePopup('0');
    });
    $("#popupContactClose1").click(function(){
        disablePopup('1');
    });
    $("#popupContactClose2").click(function(){
        disablePopup('2');
    });
    $("#popupContactClose3").click(function(){
        disablePopup('3');
    });
    //Click out event!
    $("#backgroundPopup").click(function(){
        disablePopup('0');
    });
    $("#backgroundPopup1").click(function(){
        disablePopup('1');
    });
    $("#backgroundPopup2").click(function(){
        disablePopup('2');
    });
    $("#backgroundPopup3").click(function(){
        disablePopup('3');
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });

});
