//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var secs = 1;
function load_popup(){
        for(var i=secs;i>=0;i--)
        {
         window.setTimeout('update_popup(' + i + ')', (secs-i) * 1000);

        }
}

function update_popup(num)
{

    if(num == 0) { //popup time!
        if(($.cookie("ck_subsc_pop")!='1') && ($.cookie("ck_subsc_pop")!='2') ){

                centerPopup("#popupContact");
                loadPopup("#popupContact");
        }
    }
}



//loading popup with jQuery magic!
function loadPopup(id){
    
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.85"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(id).fadeIn("slow");
		popupStatus = 1;
	}
    
}


//disabling popup with jQuery magic!
function disablePopup(id){
    
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $(id).fadeOut("slow");
        popupStatus = 0;
        if(id=="#popupContact"){
            var email= '';
            email = document.getElementById('subscribe_email_popup').value;

            if(email=='Din e-mail'){
                    email='';
            }
            xajax_disable_popup(email);
        }
    }
}


//centering popup
function centerPopup(id){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(id).height();
	var popupWidth = $(id).width();
       
	//centering
	if(id=="#popupReferral"){
            $(id).css({
                    "position": "absolute",
                    "top": windowHeight/2-popupHeight/2,
                    "left": windowWidth/2-popupWidth/2
            });
        }else{
            $(id).css({
                    "position": "absolute",
                    "top": 25,//windowHeight/2-popupHeight/2,
                    "left": windowWidth/2-popupWidth/2
            });
        }
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});

}
$(document).ready(function(){
        
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup("#popupContact");
		//load popup
		loadPopup("#popupContact");
	});

	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup("#popupContact");
	});
        $("#popupReferralClose").click(function(){
		disablePopup("#popupReferral");
	});
	/*
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup("#popupReferral");
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup("#popupReferral");
		}
	});
	*/


});

