var icons = {};
var map;
var geocoder;
var markerOptions;

var debug;
var bedIcon;

var hotelsnotfound = 0;
var hotelsfound = 0;

function addDebugLine(line) {
	var eDebug = document.getElementById("debug");
	if (eDebug) {
		eDebug.innerHTML += line + "<br>";
	}
}

function getIcon() {
	/*
	var icon = null;
	
	icon = new GIcon();
	icon.image = "img/bed.gif";
	var size = { width: 20, height: 20 };
	icon.iconSize = new GSize(size.width, size.height);
	
	icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
	icon.shadow = "images/bed_schaduw.png";
	size = { width: 20, height: 20 };
	icon.shadowSize = new GSize(size.width, size.height);
	
	return icon;
	*/
	
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon();
//	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
//	baseIcon.shadowSize = new GSize(37, 34);
//	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	baseIcon.iconSize = new GSize(30, 30);
	
	if (site_versie == 3) {
		baseIcon.iconAnchor = new GPoint(15, 26);
		baseIcon.infoWindowAnchor = new GPoint(15, 3);
		baseIcon.image = "images_freebookings/logo-maps-30.png";
	}
	else
	{
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.image = "img/bed2.gif";
	}
	return baseIcon;
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
//	map.clearOverlays();
	
	if (!response) {
		hotelsnotfound++;
	}
	else {
		var titel = "";
		var i = 0;
		for (i = 0; i < hotels.length; i++) {
			if (response.name == hotels[i]) {
				titel = hotelnamen[i] + ", " + hotels[i];
				break;
			}
		}
		
		if (response.Status.code != 200) {
			hotelsnotfound++;
			
			// addDebugLine(response.name + ": ?:?");
		}
		else {
			// response.[name, Status, Placemark]
			
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			
			markerOptions = { icon:bedIcon, title:titel };
			marker = new GMarker(point, markerOptions);
			map.addOverlay(marker);
			
			hotelsfound++;
			
			addDebugLine(response.name + ": " + place.Point.coordinates[1] + ":" + place.Point.coordinates[0]);
			
		//	marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
		}
	}
}

function addHotelToMap(naam, adres, googlemapsAdres, hotelurl, hotelimg) {
	// addDebugLine("addHotelToMap: " + naam + ", " + adres + ", " + googlemapsAdres);
	
	var punt = googlemapsAdres.split(":");
	markerOptions = { icon:bedIcon, title:naam + ", " + adres, clickable:true };
	point = new GLatLng(punt[0], punt[1]);
	marker = new GMarker(point, markerOptions);
	map.addOverlay(marker);
	
	if (document.getElementById("map_canvas").className == 'zoekenresultaten') {
		GEvent.addListener(marker, 'click', function() {
			top.location.href = hotelurl;
		});
		return;
	}
	
	adres = adres.split(", ");
	if (adres.length == 1) {
		adres[1] = '';
		adres[2] = '';
	}
	
	var target = '';
	if (p == "google_maps") {
		target = ' target="_blank"';
	}
	
	if (hotelurl != "") {
		if (hotelimg != "") {
			hotelimg = '<a href="' + hotelurl + '"' + target + ' title="' + txtMeerInformatie + '">' + hotelimg + '</a>';
		}
	}
	var html = '<table><tr valign="top"><td>' + hotelimg + '</td>';
	if (hotelurl != "") {
		naam = '<a href="' + hotelurl + '"' + target + ' title="' + txtMeerInformatie + '">' + naam + '</a>';
	}
	html += '<td><b>' + naam + '</b><br>' + adres[0] + '<br>' + adres[1] + ' ' + adres[2] + '<br><br><br>';
	if (hotelurl != "") {
		// html += '<a href="' + hotelurl + '">' + txtMeerInformatie + '</a>';
	}
	html += '</td></tr></table>';
	marker.bindInfoWindowHtml(html);
}

function setMapCenter(response) {
	if (!response) {
		// alert("!response");
	}
	else if (response.Status.code != 200) {
		
		if (response.name) {
			addDebugLine("Sorry, we were unable to geocode: " + response.name);
		}
		/*
		else {
			alert("Sorry, we were unable to geocode that address");
		}
		*/
		// map.setCenter(new GLatLng(52, 5), 9);
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		map.setCenter(point, 9);
	}
}

function initializeRouteTabblad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		geocoder = new GClientGeocoder();
		
		if (google_maps_adres != "") {
			var ll = google_maps_adres.split(":");
			zoom = 11;
			map.setCenter(new GLatLng(ll[0], ll[1]), zoom);
			
			bedIcon = getIcon();
			
			if (typeof(adresPostcodePlaatsTbvMap)=='undefined' || adresPostcodePlaatsTbvMap===null) {
				adresPostcodePlaatsTbvMap = adresPostcodePlaats;
			}
			
			addHotelToMap(hotelnaam, adresPostcodePlaatsTbvMap, google_maps_adres, hotelurl, hotelimg);
		}
	}
}

var computeroute_dir;
function computeroute() {
	var from = document.getElementById('routefrom').value;
	if (from != "") {
		var md = getE('map_directions');
		md.style.display = 'block';
		md.innerHTML = '';
		
		if (!computeroute_dir) {
			computeroute_dir = new GDirections(map, md);
		}
		else {
			computeroute_dir.clear;
		}
		
		if (taal == "du") {
			localeTaal = "de";
		}
		else {
			localeTaal = taal;
		}
		
		GEvent.addListener(computeroute_dir, "error", checkRouteStatus);
		GEvent.addListener(computeroute_dir, "load", checkRouteStatus);
		
		computeroute_dir.load('from: ' + from + ' to: ' + adresPostcodePlaats, {locale: localeTaal});
		
		createCookie('routeTabbladFrom', from, 14);
		
	//	setTimeout('checkRouteStatus()', 2000);
	}
}

function checkRouteStatus() {
	var md = getE('map_directions');
	var gs = computeroute_dir.getStatus();
	if (gs["code"] == G_GEO_SUCCESS) {
		// prima
	}
	else {
		if (md.innerHTML == '') {
			md.style.padding = '4px';
			md.innerHTML = routeErrorMelding + '<br>Error nr: ' + gs["code"];
			
			eraseCookie('routeTabbladFrom');
		}
	}
}

function checkRouteFrom(e) {
	if (!e) return;
	if (e.value == adresPlaatsTxt) {
		e.value = '';
		e.style.color = '#000000';
	}
	else if (e.value == '') {
		var from = readCookie('routeTabbladFrom');
		if (from) {
			e.value = from;
		}
		else {
			e.value = adresPlaatsTxt;
			e.style.color = '#666666';
		}
	}
}

function initializeHotelsFreebookings() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		
		var eDiv = document.getElementById("map_canvas");
		if (eDiv.className == 'zoekenresultaten') {
		}
		else {
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
		}
		
		geocoder = new GClientGeocoder();
		
		// markermanager = new GMarkerManager();
		
		// geocoder.getLocations("Hearedyk 12, Itens, Nederland", addAddressToMap);
		
		if ( (hotels_centrum_lat != "") && (hotels_centrum_lng != "") ) {
			var zoom = 9;
			if ( (eDiv.className == 'zoekenresultaten') && (hotels.length < 10) ) {
				zoom = 11;
			}
			else if (plaats != "") {
				zoom = 11;
			}
			else if (provincie == "") {
				zoom = 7;
			}
			map.setCenter(new GLatLng(hotels_centrum_lat, hotels_centrum_lng), zoom);
		}
		else {
			// utrecht
			map.setCenter(new GLatLng(52.1, 5.1), 7);
			
			if (provincie != "") {
				// gekozen provincie
				geocoder.getLocations(provincie + ', Nederland', setMapCenter);
			}
		}
		
		bedIcon = getIcon();
		
		setTimeout("plaatsHotels()", 10);
	}
}

function plaatsHotels() {
	addDebugLine("plaatsHotels, aantal hotels = " + hotels.length);
	var i = 0;
	for (var key = 0; key < hotels.length; key++) {
		// addDebugLine(hotels[key]);
		
		if (googlemaps[key] == "") {
			i++;
			if ( (atze == "true") && (i >= 0) ) {
				geocoder.getLocations(hotels[key], addAddressToMap);
			}
		}
		else {
			addHotelToMap(hotelnamen[key], hotels[key], googlemaps[key], hotelurls[key], hotelimgs[key])
		}
		
		// geocoder.getLatLng(key + "@@" + hotels[key], addPointToMap);
	}
	
	// setTimeout("debugAlert()", 5000);
}

function debugAlert() {
	alert("hotels = " + hotels.length + ", hotelsnotfound = " + hotelsnotfound + ", hotelsfound = " + hotelsfound);
}
