﻿//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	$("#backgroundPopup").fadeOut("fast");
	$(".popup").fadeOut("slow");
	$(".popup").remove();
}

//centering popup
function centerPopup(client) {
	//request data for centering
	var windowWidth = $(document).width();
	//document.documentElement.clientWidth;
	//var windowWidth = window.innerWidth; 
	var windowHeight = document.documentElement.clientHeight;
	//var windowHeight = window.innerHeight;
	var popupHeight = $(".popup").height();
	var popupWidth = $(".popup").width();

/*
	 $(windowWidth+"<br/>").appendTo($("#blaat"));
	 $("#blaat").append(windowWidth+"<br/>");
 	 $("#blaat").append(popupWidth+"<br/>");

*/
	$(".popup").css({
		"position": "absolute",
		"top": (windowHeight/2)-(popupHeight/2),
		"left": (windowWidth/2)-(popupWidth/2)
	});
}

function initPopup() {
	centerPopup('AtbRent');

	$("#backgroundPopup").css({"opacity": "0.7"});
	$("#backgroundPopup").fadeIn("slow");
	$(".popup").fadeIn("fast");
	
	$(".popupClose").click(function(){
		disablePopup();
	});
	
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	

	//LOADING POPUP
	//Click the button event! 
	$("#atb-rent").click(function() {
		$.get('/site_media/static/atb.html', function(data) {
			$("body").append(data);
			initPopup();
		});
	});

	$("#fietsvanjan").click(function() {
		$.get('/site_media/static/fietsvanjan.html', function(data) {
			$("body").append(data);
			initPopup();
		});
	});	

	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27){
			disablePopup();
		}
	});

});

