function getPageSize() {
		
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {    
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {    // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}    
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){    
		pageWidth = xScroll;        
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function activateMap() {
	var map = new google.maps.Map2(document.getElementById("mp_map"));
	var point = new GLatLng(52.45,5.5);
	map.setCenter(point, 7);

	gdir = new GDirections(map, document.getElementById("directions"));

	map.addControl(new google.maps.LargeMapControl());
	map.addControl(new google.maps.ScaleControl());
	map.addControl(new google.maps.MapTypeControl());
}

function setDirections(fromAddress) {
	var toAddress = $('#map_search_bureau').val();
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": 'nl_NL' });
	window.location = '#maps';
}


$(function() {
	$('#footer .cta').click(function() {
		if($(this).html() == 'Route naar Surhuisterveen') {
			$('#map_search_bureau').attr('selectedIndex', 0);
		} else {
			$('#map_search_bureau').attr('selectedIndex', 1)
		}
		
		var size = getPageSize();
		$('#overlay').css('width', size[0] + 'px');
		$('#overlay').css('height', size[1] + 'px');
		
		$('#mp').css('display', 'block');
		$('#overlay').css('display', 'block');
		activateMap();
		var fromAddress = $('#map_search_address').val();
//		$('#map_search_address').val('fromAddress');
		setDirections(fromAddress);
	});
		
	$("#mp .close").click(function() {
		$('#mp').css('display', 'none');
		$('#overlay').css('display', 'none');
		$('#directions').html('');
	});
		
	$('#map_search_submit').click(function() {
		$('#directions').html('');
		var fromAddress = $('#map_search_address').val();
		$('#route_naar').val(fromAddress);
		activateMap();
		setDirections(fromAddress);
	});
	
	$('#vrijblijvend').click(function() {
		var size = getPageSize();
		$('#overlay').css('width', size[0] + 'px');
		$('#overlay').css('height', size[1] + 'px');
		$('#overlay').css('display', 'block');
		$('#offerte').css('display', 'block');
	});
	
	$("#offerte .close").click(function() {
		$('#overlay').css('display', 'none');
		$('#offerte').css('display', 'none');
	});
	
	$('#offerte_verzenden').click(function() {
		$('#naam_error').css('display', 'none');
		$('#telefoon_nummer').css('display', 'none');
	
		var naam = $('#naam').val();
		var telefoon = $('#telefoon').val();
		var errors = 0;
		
		if(naam.length == 0) {
			$('#naam_error').css('display', 'block');
			errors++;
		}
		if(telefoon.length < 10) {
			$('#telefoon_nummer').css('display', 'block');
			errors++;
		}
		
		if(errors == 0) {
			$('#contact_opnemen').submit();
		}
	});
});
