$(function()
{
 $('.pop-up-link, .popupContactClose').click(function(ev)
    {
        ev.preventDefault();
        $('.pop-up .pop-box').hide();
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });    
 
 // first part is the class on the link that pops a box
    $('.why-plan-today-link').click(function(event){
        // second part is the class of the div to popup
        $('.pop-why-plan-today').show();
    });

   
   
    $(".popupContactClose").click(function(){
        disablePopup();
        $('.pop-up').css({'display':'none'});
    });
    $(".backgroundPopup").click(function(){
        disablePopup();
        $('.pop-up').css({'display':'none'});
    });
    $(document).keypress(function(e){
        if(e.keyCode==27){
            disablePopup();
        }
    });
    
    //SETTING UP OUR POPUP
    //0 means disabled; 1 means enabled;
    var popupStatus = 0;
    
    //loading popup with jQuery magic!
    function loadPopup(){
        //loads popup only if it is disabled
        if(popupStatus==0){
            $(".backgroundPopup").css({
                "opacity": "0.5"
            });
            $(".backgroundPopup").show();
            $(".pop-up").show();
            popupStatus = 1;
        }
    }
    
    //disabling popup with jQuery magic!
    function disablePopup(){
        //disables popup only if it is enabled
        if(popupStatus==1){
            $(".backgroundPopup").hide();
            $(".pop-up").hide();
            popupStatus = 0;
        }
    }
    
	var fromTop = 100;
    //centering popup
    function centerPopup(){
        //request data for centering
        var windowWidth = $(document).width();
        var windowHeight = $(document).height();
        var popupHeight = $(".pop-up").height();
        var popupWidth = $(".pop-up").width();
        //centering
        $(".pop-up").css({
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2 +1,
            //"top": 600,
            "left": windowWidth/2-popupWidth/2
        });
        //only need force for IE6
        
        $(".backgroundPopup").css({
            "height": windowHeight + 'px',
            "width": windowWidth + 'px'
        });
		$('.pop-up').css({'top':$(document).scrollTop() + fromTop + 'px'}).css({'left':'50%'});
    }
	
	$(document).scroll(function(ev) {
		$('.pop-up').css({'top':$(document).scrollTop() + fromTop + 'px'}).css({'left':'50%'});
	});
});

