// 0 = disabled, 1 = enabled
var popupStatus = 0;

function setPopUpWidth(popUpWidth){
	$("#contentPopup").width(popUpWidth);
	$("#contentPopupText").width((popUpWidth - 20));
}

function loadPopup(enquete) {
	// load popup only if it is disabled
	if (popupStatus == 0) {
		$("#overlay").css( {
			"opacity" : "0.7"
		});
//		$("#contentPopup").corner("#555 10px hiddenParent");
		DD_roundies.addRule('#contentPopup', 10, true);
		$("#overlay").fadeIn("slow");
		$("#contentPopup").fadeIn("slow");
		popupStatus = 1;
	}

}

function disablePopup() {
	// disable popup only if it is enabled
	if (popupStatus == 1) {
		$("#overlay").fadeOut("slow");
		$("#contentPopup").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup() {
	// get data required for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#contentPopup").height();
	var popupWidth = $("#contentPopup").width();
	// center it
	$("#contentPopup").css( {
		"position" : "absolute",
		"top" : 50,
		"left" : windowWidth / 2 - popupWidth / 2,
		"z-index" : 10
	});
	//windowHeight / 2 - popupHeight / 2,
	
	// IE6 needs a bit of persuasion (of course)
	$("#overlay").css( {
		"height" : windowHeight
	});

}

function openEnquetePopup(resultId){
	return;
	// load the proper data into the div
	$("#contentPopupTitle").html('<img src="enquete_onderzoek/jouw_mening_telt.png">');
	// load data via AJAX
	$.ajax({
		type: 'GET',
		url: 'enquete_onderzoek/index.php?rid=' + resultId,
		success: function(data){
			$("#contentPopupText").html(data);
			$("#contentPopupText").css('overflow-y', 'auto');
		}
	});
	setPopUpWidth(980);
	// centering with css
	centerPopup();
	// load popup
	loadPopup();
	
//	var windowWidth = $(window).width();
//	var popupWidth = 940;
//	var popupHeight = 500;
//	
//	var popup = window.open('enquete_onderzoek/index.php', 'Soatest.nl | Hét online checkpunt | Enquête', 'status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=' + popupWidth + ',height=' + popupHeight);
//	popup.moveTo((windowWidth - popupWidth) / 2, 50);
}

$(document).ready(function()
{
	// open the privacystatement
	$("#testMailButton").click(function(){
		// load the proper data into the div
		$("#contentPopupTitle").html('<img src="css/img/send_anonymous.png">');
		// load data via AJAX
		$.ajax({
			type: 'GET',
			url: 'includes/mailflow.php',
			success: function(data){
			$("#contentPopupText").html(data);
		}
		});
		// centering with css
		centerPopup();
		// load popup
		loadPopup();
	});
	
	// open the privacystatement
	$("#privacyButton").click(function(){
		// load the proper data into the div
		$("#contentPopupTitle").html('<img src="css/img/privacy.png">');
		// load data via AJAX
		$.ajax({
			type: 'GET',
			url: 'includes/popupdata.php?nid=24',
			success: function(data){
			$("#contentPopupText").html(data);
			}
		});
		setPopUpWidth(500);
		// centering with css
		centerPopup();
		// load popup
		loadPopup();
	});
	
	// open the disclaimer
	$("#disclaimerButton").click(function(){
		// load the proper data into the div
		$("#contentPopupTitle").html('<img src="css/img/disclaimer.png">');
		$.ajax({
			type: 'GET',
			url: 'includes/popupdata.php?nid=25',
			success: function(data){
			$("#contentPopupText").html(data);
			}
		});
		setPopUpWidth(500);
		// centering with css
		centerPopup();
		// load popup
		loadPopup();
	});
	
	// open the promotions
	$("#promotieButton").click(function(){
		// load the proper data into the div
		$("#contentPopupTitle").html('<img src="css/img/promotion.png">');
		$("#contentPopupText").html(function(){
			$("#contentPopupText").load('promotion.html');
			$("#contentPopupText").css('overflow', 'auto');
		});
		setPopUpWidth(500);
		// centering with css
		centerPopup();
		// load popup
		loadPopup();
	});

	// Close the popup when de close button is clicked
	$("#contentPopupClose").click(function(){
		disablePopup();
	});
	// Close the popup when the user clicks outside the popup area
	$("#overlay").click(function(){
		disablePopup();
	});
	// Close the popup when the user presses the escape key
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
