
function hideOverlay() {
	var countdown = 15;
	var height;
	if(window.innerHeight && window.scrollMaxY) {
		height = window.innerHeight + window.scrollMaxY;
	}else if(document.body.scrollHeight > document.body.offsetHeight) {
		height = document.body.scrollHeight;
	}else{
		height = document.body.offsetHeight + document.body.offsetTop;
	}
	document.getElementById('overlay').style.height = height + 'px';
	var reference = window.setInterval(function () {
		document.getElementById('countdown').innerHTML = countdown;
		countdown--;
		if(countdown < 0) {
			window.clearInterval(reference);
			document.getElementById('overlay').style.display = 'none';
			document.getElementById('notice').style.display = 'none';
		}
	}, 1000);
}

var maps = [];

function calculateRouting(button) {
	var hidden = button.previousSibling;
	var loc = button.previousSibling.previousSibling;
	var map = maps[hidden.value];
	if('marker' in map && map.marker !== null) {
		map.gmap.removeOverlay(map.marker);
	}
	
	var str = loc.value;
	var string = str.replace(/ /g, '+');
	
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(string, function(point) {
		if(!point) {
			window.alert('Adres niet gevonden');
		}else{
			/*			
			var icon = new GIcon();
		    icon.image = './images/bookmark.gif';
		    icon.iconSize = new GSize(31, 48);
		    icon.iconAnchor = new GPoint(5, 49);
		    icon.infoWindowAnchor = new GPoint(5, 49);
			*/
			
			map.marker = new GMarker(point,
			{
				title: 'Vertrekpunt',
				// icon: icon,
				clickable: true,
				draggable: false
			});
			
			map.gmap.addOverlay(map.marker);

			if(map.directions == 'init') {
				map.directions = new GDirections(map.gmap, map.routing.childNodes[4]);
			}
			var request = 'from: ' + point.lat() + ',' + point.lng() +  ' to: ' + map.firstMarker.latitude + ',' + map.firstMarker.longitude;
			map.directions.load(request,
				{
					"locale": "nl_NL"
				}
			);
			map.routing.childNodes[5].href =	'http://maps.google.com/maps?f=d&hl=nl&geocode=' + 
												'&saddr=' + string +
												'&daddr=test&ie=UTF8&z=13&pw=2';
			//map.routing.childNodes[5].style.display = 'block';
		}
	});
}

function toggleRouting(anchor) {
	var div = anchor.parentNode.parentNode;
	div.childNodes[0].style.display = 'none';
	div.childNodes[1].style.display = 'block';
}

function appendRouting(id) {
	var map = maps[id];
	var baseRouting = document.getElementById('smi_googlemap_routing');
	var content = map.div.parentNode;
	var routing = baseRouting.cloneNode(true);
	map.container = routing;
	map.routing = routing.childNodes[1];
	map.routing.childNodes[2].value = id;
	map.container.removeAttribute('id');
	map.container.style.width = map.div.style.width;
	content.insertBefore(routing, map.div.nextSibling);
}

function onLoadGooglemap(widget)
{
	widget.gmap.addControl(new GLargeMapControl());
	if(widget.item.name && 0 in widget.item.markers) {
		maps.push({
			widget: widget,
			div: widget.divObject,
			firstMarker: widget.item.markers[0],
			gmap: widget.gmap,
			directions: 'init',
			marker: null
			});
		appendRouting(maps.length - 1);
	}
}

function onLoadForm(widget)
{
	var form = widget.divObject;
	form.style.height = 'auto';
	form.style.width = 'auto';
	
	widget.addEventListener("submitted", function()
	{
		widget.showForm();
		if (!widget.form.confirmResults)
		{
			alert("Uw bericht is succesvol verstuurd.");
		}
		else
		{
			alert("Uw bericht is succesvol verstuurd.");
			//alert("Uw bericht is succesvol geplaatst, het kan echter even duren voordat deze zichtbaar is");
		}
	});
	
	widget.addEventListener("beforeSubmit", function()
	{
		// console.log(widget);
	});
}

function onLoadMovie(widget)
{
	widget.play();
}

addOnload(function()
{
	hideOverlay();
	/*
	addClipboardSetter(document.getElementById("test"), function(fn)
	{
		fn("testing");
	});
	*/
});		
