

	/*
	*	Global settings and vars
	*/
	var map;
	var side_bar_html 	= "";
	var gmarkers 		= [];
	var i 				= 0;
	var groupList 		= [];
	var routepoints 	= [];
	
	saveClickMarker		= null;
	colorIdLink			= null;
	var poly;
	var dirMarker;


	/*
	*	Geocoder toevoegen om van adres naar LatLng te berekenen
	*/
	var geocoder = new GClientGeocoder();	
	/*
	*	functie om de map te laden via de body onload
	*/
    function loadHandler() 
	{ 
    	if (GBrowserIsCompatible()) 
		{
			// Functie die de map + settings bevat
			loadMap();
	    }
    	else
    	{
			// Google maps werkt niet
		    alert("Browser is niet compatible of uw sleutel is niet juist");
	    }
    }
    
	function print_route()
	{
		var url = '/cmsfiles/components/dealers/print.php';	
		window.open(url,'print','width=500,height=600,menubar=yes,status=yes,resize=yes,location=no,toolbar=no,scrollbars=yes');
	}
	
	/*
	* Functie om de cache/data te legen als de pagina verlaten wordt
	*/
    function unloadHandler()
    {
    	GUnload();
    }
	

	/*
	*	Als er op een link geklikt word dan gaat hij naar de juiste marker
	*/
	var bodyHeight;
	function myclick(i) 
	{
		GEvent.trigger(gmarkers[i], "click");
		
		var timeout = function()
		{
			var $route = $('route_container');
			if($route.style.display != 'none')
			{
				$route.style.display = 'none';
			}
			
			var $body		= ('body');
			var $content	= $('content');
			var $colLeft	= $('col_left');
			
			if(!bodyHeight) $content.style.height = 'auto';
			var $height = adjustHeight("body", ["col_left", "content"]);
			
			$content.style.height = ($height == bodyHeight)
				? '100%'
				: 'auto';
			
			bodyHeight = $height;
		};
		
		setTimeout(timeout, 150);
		$("openingstijden_container").style.display = "block";
	}	

	/*
	*	Het maken van een formulier voor de infowindows (Route)
	*/
	function createForm()
	{
		var form = 		'<div style="color:#000;">'
				   	+	'<form>'
					+	'<table border="0" cellpadding="1" cellspacing="3" class="form_directions">'
					+	'<tr>'
					+	'<td>Straatnaam: </td><td><input type="text" name="street" id="street" /></td>'
					+	'</tr><tr>'
					+	'<td>Postcode: </td><td><input type="text" name="postcode" id="postcode" /></td>'
					+	'</tr><tr>'
					+	'<td>Plaats:</td><td><input type="text" name="city" id="city"/></td>'
					+	'</tr><tr>'
					+	'<td colspan="2">&nbsp;</td>'
					+	'</tr><tr>'					
					+	'<td colspan="2" align="right"><input type="button" style="width:75px;" value="Plannen" onclick="showAddress();" name="submit"></td>'
					+	'</tr>'
					+	'</table>'
					+	'</form>'
					+	'</div>';
		
		return form;
	}

	/*
	*	Maak de markers aan die uit de database komen.
	*/
	function createMarker(point,name,html, newicon, times, address) 
	{
		var marker = new GMarker(point, {title: name, icon:newicon});
		
		if(html == "")
		{
			html = "Geen informatie gevonden voor deze vestiging";
		}

		address = "<div class='adres_tooltip'>"+address+"</div>";
		html = "<div class='html_tooltip'>"+html+"</div>";
		
		GEvent.addListener(marker, "click", function() 
		//GEvent.addListener(marker, "mouseover", function() 
		{
			if(times)
			{
				$("openingstijden").innerHTML = "<div class='openingstijden_titel'>Openingstijden</div><div class='openingstijden_content'>"+times+"</div>";
			}
			else
			{
				$("openingstijden").innerHTML = "";
			}
			saveClickMarker = marker;
			marker.openInfoWindowTabsHtml([new GInfoWindowTab("Adres",address), new GInfoWindowTab("Info",html), new GInfoWindowTab('Route',createForm())]);
		});

		gmarkers[i] = marker;
		side_bar_html += '<div class="sidebar_content_link" id="sidebar_content_'+i+'"><a href="javascript:myclick(' + i + ')">' + name + '</a></div>';
		i++;
		
		return marker;
	}

	/*
	*	Maak de marker aan voor de route beschrijving (Punt A)
	*/	
	function showAddress(address) 
	{
		var postcode 	= $("postcode").value;
		var plaats 		= $("city").value;
		var straat 		= $("street").value;
			
		if(!address)
		{
			if(postcode && plaats && straat)
			{
				address = straat+", "+postcode+", "+plaats
			}
			else
			{
				alert("Geen adres opgegeven");
				return false;
			}
			
		}
	
		geocoder.getLatLng
		(
			address,
			function(point) 
			{
				if (!point) 
				{
					alert(address + " niet gevonden");
					return false;
				} 
				else 
				{
					if(dirMarker) { map.removeOverlay(dirMarker) };
					
					var dirIcon = new GIcon();
					dirIcon.image = "http://maps.google.com/intl/nl_ALL/mapfiles/icon_greenA.png";
					dirIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
					dirIcon.iconSize = new GSize(15, 23);
					dirIcon.shadowSize = new GSize(22, 23);
					dirIcon.iconAnchor = new GPoint(6, 23);
					dirIcon.infoWindowAnchor = new GPoint(5, 1);      					
					
					dirMarker = new GMarker(point, {title: 'Vertrekpunt', icon:dirIcon});
					//map.addOverlay(dirMarker);
					
					homeLatLng 		= dirMarker.getPoint();
					destLatLng		= saveClickMarker.getPoint();
					
					saveClickMarker.closeInfoWindow() 
					//map.setCenter(new GLatLng(destLatLng.lat(),destLatLng.lng()),10);


					/*
					*	Laad de route in de google Maps
					*/	
					routepoints[0] = address+"@"+homeLatLng.lat()+", "+homeLatLng.lng();
					routepoints[1] = saveClickMarker.getTitle()+" @"+destLatLng.lat()+", "+destLatLng.lng();


					gdir.load("from: "+routepoints[0]+" to:"+routepoints[1], { "locale": "nl","getPolyline": true, "getSteps": true });
					
					$('content').style.height = 'auto';
					$("print_route").style.display = "block";
					$("route_container").style.display = "block";
					
					var timeout = function()
					{
						var $height = adjustHeight("body", ["col_left", "content"]);
						if($height <= bodyHeight)
						{
							$('content').style.height = '100%';
						}
						bodyHeight = $height;
					};
					
					setTimeout(timeout, 700);
				}
			}
		);
	}	

    
	/*
	*	Handel alle errors netjes af
	*/
	function handleErrors()
	{
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		{
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
		}
		else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		{
			alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
		}
		else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		{
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
		}
		else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		{
			alert("De sleutel voor de Dealer locator is niet juist. \n Error code: " + gdir.getStatus().code);
		}
		else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		{
			alert("De route beschrijving kon niet juist afgehandeld worden.\n Error code: " + gdir.getStatus().code);
		}
		else 
		{
			alert("Er is een onbekende fout opgetreden.");
		}
	}
