
var gmarkers = [];
var htmls = [];
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];

var gdir = "";
var map = "";
var marker = "";

function initMap(latittude, longitude, info, switchaddress)
{
	var iconSwitch = new GIcon();
	//alert(switchaddress);
	
	iconSwitch.image = 'http://www.switch.be/images/marker.png';
	iconSwitch.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	iconSwitch.iconSize = new GSize(43, 55);
	iconSwitch.iconAnchor = new GPoint(6, 50);
	iconSwitch.infoWindowAnchor = new GPoint(5, 1);
	
	map = new GMap2(document.getElementById("googlemaps"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	//map.setCenter(new GLatLng(latittude, longitude + 0.006), 13);
	map.setCenter(new GLatLng(latittude + 0.006, longitude + 0.006), 13);
	
	
	
	var center = new GLatLng(latittude, longitude);
	marker = new GMarker(center);
	addMarker(latittude, longitude, iconSwitch, info, switchaddress);
			
	function addMarker(lat, lng, icon, info, switchaddress)
	{
		var center = new GLatLng(lat,lng);
		//var point = new GLatLng(center.lat(),center.lng());
		var marker = new GMarker(center, icon);
				
		var i = gmarkers.length;
			    
		var name = "test";
		// The info window version with the "to here" form open
        to_htmls[i] = info + '<br>Routebeschrijving: <b>Hier naartoe<\/b> - <a href="javascript:fromhere(' + i + ')">Hier vandaan<\/a>' +
           '<br>Vertrekpunt:<form action="http://maps.google.com/maps">' +
           '<input type="text" SIZE=30 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           //'<input type="hidden" id="daddr" name="daddr" value="' + info + lat + ',' + lng + '"/>' +
           '<input type="hidden" id="daddr" name="daddr" value="' + switchaddress + '"/>' +
           '<INPUT value="Uitvoeren" TYPE="SUBMIT"><br>' +
           'Te voet <input type="checkbox" name="walk" id="walk" /> &nbsp; Vermijd snelweg <input type="checkbox" name="highways" id="highways" />';
			//'<input type="hidden" id="daddr" name="daddr" value="Vaubanstraat 39, ieper"/>';
        // The info window version with the "from here" form open
        from_htmls[i] = info + '<br>Routebeschrijving: <a href="javascript:tohere(' + i + ')">Hier naartoe<\/a> - <b>Hier vandaan<\/b>' +
           '<br>Bestemming:<form action="http://maps.google.com/maps">' +
           '<input type="text" SIZE=30 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Uitvoeren" TYPE="SUBMIT"><br>' +
           'Te voet <input type="checkbox" name="walk" id="walk" /> &nbsp; Vermijd snelweg <input type="checkbox" name="highways" id="highways" />' +
           //'<input type="hidden" id="saddr" name="saddr" value="'+name+"@"+ lat + ',' + lng + '"/>';
           '<input type="hidden" id="saddr" name="saddr" value="' + switchaddress + '"/>';
        // The inactive version of the direction info
		info = info + '<br>Routebeschrijving: <a href="javascript:tohere('+i+')">Hier naartoe<\/a> - <a href="javascript:fromhere('+i+')">Hier vandaan<\/a>';

		GEvent.addListener(marker, 'mouseover', function(){ 
		    marker.openExtInfoWindow(
		      map,
		      "simple_example_window",
		      info,
		      {beakOffset: 3}
		    ); 
		});
		gmarkers.push(marker);

	
		map.addOverlay(marker);
		
		gdir = new GDirections(map, document.getElementById("directions"));
	
		/*GEvent.addListener(marker,"mouseover",function(){
			marker.openInfoWindowHtml(info, {maxWidth:100,maxHeight:50});
		});*/
	}	
	
}

// ===== request the directions =====
function getDirections() {
	// ==== Set up the walk and avoid highways options ====
	var opts = {};
	if (document.getElementById("walk").checked) {
		opts.travelMode = G_TRAVEL_MODE_WALKING;
	}
	if (document.getElementById("highways").checked) {
		opts.avoidHighways = true;
	}
	// ==== set the start and end locations ====
	var saddr = document.getElementById("saddr").value
	var daddr = document.getElementById("daddr").value
	gdir.load("from: "+saddr+" to: "+daddr, opts);
}

// functions that open the directions forms
function tohere(i) {
	//gmarkers[i].openInfoWindowHtml(to_htmls[i]);
	marker.openExtInfoWindow(
		map,
		"simple_example_window2",
		to_htmls[i],
		{beakOffset: 3}
	);
}

function fromhere(i) {
	    
	marker.openExtInfoWindow(
		map,
		"simple_example_window2",
		from_htmls[i],
		{beakOffset: 3}
	); 
	
}

