google.load('maps','2');
google.load('search', '1')
// initialiseer variablesvar map;var geocoder;var searcher;
var hotelData = [];
var searchResults = [];
var searchForm;
var markerCluster;
var markers = [];
var latlngs = [];
var icon;
var bounds;
var cityArray = [];
var provinceArray = [];
var dataProcessed = 0;

var gSmallIcon = null;
function initialize() {
	

	gSmallIcon = new google.maps.Icon(G_DEFAULT_ICON);	map = new google.maps.Map2(document.getElementById("map_canvas"));	map.setCenter(new google.maps.LatLng(localLat, localLng), 3);//	map.setUIToDefault();
	map.addControl(new google.maps.LargeMapControl3D());
	
	map.disableScrollWheelZoom();		icon = new google.maps.Icon(G_DEFAULT_ICON);	icon.image = "http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png";		// initialiseer geocoder	geocoder = new google.maps.ClientGeocoder();		
	// initialiseer zoekveld//	searchForm = new google.search.SearchForm(false, document.getElementById("maps_search_input"));	searcher = new google.search.LocalSearch();
    searcher.setCenterPoint(map);	    searcher.setAddressLookupMode(google.search.LocalSearch.ADDRESS_LOOKUP_DISABLED);
	searcher.setSearchCompleteCallback(null, OnLocalSearch);	}

google.setOnLoadCallback(initialize);


// Called when Local Search results are returned, we clear the old
// results and load the new ones.
function OnLocalSearch() {
		
	dataProcessed++;
		
	if (!searcher.results) return;
//	var searcherResultContainer = document.getElementById("maps_search_results");

	// Clear the map and the old search well
//	searcherResultContainer.innerHTML = "";
	for (var i = 0; i < searchResults.length; i++) {
  		map.removeOverlay(searchResults[i].marker());
	}

	searchResults = [];
	if (searcher.results.length > 0) {
		for (var i = 0; i < searcher.results.length; i++) {
			var result = searcher.results[i];
//			searchResults.push(new LocalResult(result));

		}
		
		// move the map to the first result
		var first = searcher.results[0];
		
		var latlng = new google.maps.LatLng(parseFloat(first.lat), parseFloat(first.lng));
		var marker = new google.maps.Marker(latlng,gSmallIcon);
		marker.id = markers.length;
		latlngs.push(latlng);
		markers.push(marker);
				
		map.addOverlay(marker);
				
		GEvent.addListener(marker, "click", function() {
//			alert(hotelData[this.id].name);
			var html = "<p style='line-height: 120%'><b>" +hotelData[this.id].name + "</b><br />";
			if (hotelData[this.id].address != '') html += hotelData[this.id].address + "<br />";
			if (hotelData[this.id].zipcode != '') html += hotelData[this.id].zipcode + "<br />";
			if (hotelData[this.id].city != '') html += hotelData[this.id].city + "<br />";
			if (hotelData[this.id].province != '') html += hotelData[this.id].province + "<br />";
			html += hotelData[this.id].country + "<br />";
			if (hotelData[this.id].phone != '') html += "<span style='font-size: 10px; line-height: 100%'>Telefoon: " + hotelData[this.id].prefix + " " + hotelData[this.id].phone + "<br />";
			if (hotelData[this.id].fax != '') html += "Fax: " + hotelData[this.id].prefix + " " + hotelData[this.id].fax + "<br />";
			if (hotelData[this.id].email != '') html += "Email: " + hotelData[this.id].email + "<br />";
			if (hotelData[this.id].website != '') html += 'Website: <a href="' + hotelData[this.id].website + '" target="_new>' + hotelData[this.id].website + '</a><br />';
			if (hotelData[this.id].desc != '') html += "Omschrijving: <br />" + hotelData[this.id].desc + "<br /></span>";
			html += "</p>";
		
			this.openInfoWindow(html);
		});
		
		zoomfit();
				
	} else {
		$.log('niet gevonden');
/*	
		$("#maps_search_results").html("<p style='font-size:11px; width: 180px'>De door U opgegeven lokatie kan helaas niet gevonden worden. <br /><br />Om gerichter te zoeken, typ de plaatsnaam achter uw zoekopdracht.<br /><br />Bijvoorbeeld:<br />'Stationsplein, Hilversum'<br/>'Paradiso, Amsterdam'.</p>");
*/
		
	}
	
	if (dataProcessed == hotelData.length) {
		enableComboboxes();	
	}
	
}



function fillCountriesCombobox() {
	
	$.get("/ontvangst/hotels/xml_hotels_countries/",{}, function(data) {

		$(data).find("item").each(function(){

			var country = $(this).text();
			
			$('#hotels_countries_combobox').append('<option value=' + country + '>' + country + '</option>')
			
		});
		
	}, "xml");	
	
}

function setComboboxes() {
	
	$('#hotels_cities_combobox').html('<option value="-1" selected>-- Selecteer een stad --</option>');
	$('#hotels_provincies_combobox').html('<option value="-1" selected>-- Selecteer een streek/provincie --</option>');
	
	var country = $('#hotels_countries_combobox :selected').text();
	loadHotelData(country);
}

function setCity() {
	var city = $('#hotels_cities_combobox :selected').text();
	if (city.substring(0,2) != -1) {
		narrowHotels(city, 'city');
	}
}

function setProvince() {
	var province = $('#hotels_provincies_combobox :selected').text();
	if (province.substring(0,2) != -1) {
		narrowHotels(province, 'province');	
	}
}


 // Cancel the form submission, executing an AJAX Search API search.
function doLocalSearch(index, name, inputLocation, city, country) {
	
	if (inputLocation == '') {
		var centerString = city + ", " + country;	
	} else {
		var centerString = inputLocation + ", " + city + ", " + country;	
	}
	var searchString = name + ", " + centerString;
	
	searcher.setCenterPoint(centerString);	
	searcher.execute(searchString);
		
	return false;
}

function searchHotels(value) {
	
	// clean out the search results list
	$('#maps_search_results_list').html('');
	
//	map.setCenter(value);
	var temp = value;
	getData(temp);
}

function zoomfit() {
	
	bounds = null;
	bounds = new GLatLngBounds();	
		
	for ( var i = 0; i < latlngs.length; i++ ) {
    	bounds.extend(latlngs[i]);
	}
	
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
}

function enableComboboxes() {
	$('#hotels_cities_combobox').attr('disabled',false);
	$('#hotels_countries_combobox').attr('disabled',false);
	$('#hotels_provincies_combobox').attr('disabled',false);			
}

function loadHotelData(param) {
	
	markers = [];	

	map.clearOverlays();
	bounds = null;
	
	dataProcessed = 0;
	
//	$('#maps_search_results').css({ 'opacity':'0.9'});
	
	$('#maps_search_results_list').html('');
	
	$('#hotels_cities_combobox').attr('disabled',true);
	$('#hotels_countries_combobox').attr('disabled',true);
	$('#hotels_provincies_combobox').attr('disabled',true);		
	
			$.get("/ontvangst/hotels/xml_hotels_maps_search/" + param,{}, function(data) {

		hotelId = 0;
		hotelData = [];
		cityArray = [];
		provinceArray = [];
		latlngs = [];

		$(data).find("item").each(function(index){
			
			var hotelObj = new Object();
			
			hotelObj.id = $(this).attr("id");
			hotelObj.name = $(this).find('name').text();
			hotelObj.corp = $(this).find('corp').text();
			hotelObj.address = $(this).find('address').text();
			hotelObj.zipcode = $(this).find('zipcode').text();
			hotelObj.city = $(this).find('city').text();
			hotelObj.province = $(this).find('province').text();
			hotelObj.country = $(this).find('country').text();
			hotelObj.prefix = $(this).find('prefix').text();
			hotelObj.phone = $(this).find('phone').text();
			hotelObj.fax = $(this).find('fax').text();
			hotelObj.email = $(this).find('email').text();
			hotelObj.website = $(this).find('website').text();
			hotelObj.rooms = $(this).find('rooms').text();
			hotelObj.stars = $(this).find('stars').text();
			hotelObj.desc = $(this).find('desc').text();
			
			hotelObj.lat = $(this).attr("lat");
			hotelObj.lng = $(this).attr("lng");
						
			if ($.inArray(hotelObj.city, cityArray) == -1) {
				if (hotelObj.city != '') {
					cityArray.push(hotelObj.city);	
				}
			}

			if ($.inArray(hotelObj.province, provinceArray) == -1) {
				if (hotelObj.province != '') {
					provinceArray.push(hotelObj.province);	
				}
			}
			
			//	container.appendChild(this.resultObj.html.cloneNode(true));
			$('#maps_search_results_list').append('<li id="' + index + '"><a class="maps_search_result_link" href="javascript:dummy()" onclick="selectLocation(' + index + ')"><span class="maps_search_result_title">' + hotelObj.name + '</span><br /><span class="maps_search_result_city">' + hotelObj.city + '</span></a></li>');
	
			
//	        var latlng = new google.maps.LatLng(lat, lng);//	        var marker = new google.maps.Marker(latlng, {icon: icon});//	        markers.push(marker);			
			hotelData.push(hotelObj);	
						
			// add localization if coordinates are empty
			if (hotelObj.lat == '0' && hotelObj.lng == '0') {
				if (hotelObj.address == '') {
					doLocalSearch(index, hotelObj.name, '', hotelObj.city, hotelObj.country);				
				} else {
					doLocalSearch(index, hotelObj.name, hotelObj.address, hotelObj.city, hotelObj.country);									
				}
			} else {
				
				var latlng = (hotelObj.lat, hotelObj.lng);
				latlngs.push(latlng);		
				
				var marker = new google.maps.Marker(latlng,gSmallIcon);
				marker.id = markers.length;
				markers.push(marker);
				
			}		
			
		});
				
		
		$.each( cityArray, function( index, objValue) {
			$('#hotels_cities_combobox').append('<option value=' + objValue + '>' + objValue + '</option>');	
		});
		
		$.each( provinceArray, function( index, objValue) {
			$('#hotels_provincies_combobox').append('<option value=' + objValue + '>' + objValue + '</option>');	
		});
		
		$('#maps_search_results_list').jScrollPane();

//		$('#hotels_cities_combobox').attr('disabled',false);
		
			
//		    var markerCluster = new MarkerClusterer(map, markers);    					
/*
	        for (var i = 0; i < data.length; ++i) {	                var latlng = new google.maps.LatLng(data[i].lat, data[i].lng);	                var marker = new google.maps.Marker(latlng, {icon: icon});	                markers.push(marker);	        }	        var markerCluster = new MarkerClusterer(map, markers);
*/	        
	        	}, "xml");
		}

function narrowHotels(value, source) {

	$('#maps_search_results_list').html('');
	
	map.clearOverlays();	
		
	$.each(hotelData, function( index, hotelObj) {	
						
		if (eval("hotelObj." + source) == value) {
				
		//	container.appendChild(this.resultObj.html.cloneNode(true));
			$('#maps_search_results_list').append('<li><a class="maps_search_result_link" href="javascript:dummy()" onclick="selectLocation(' + hotelObj.lat + ',' + hotelObj.lng + ')"><span class="maps_search_result_title">' + hotelObj.name + '</span><br /><span class="maps_search_result_city">' + hotelObj.city + '</span></a></li>');
			
			// add localization if coordinates are empty
			if (hotelObj.lat == '0' && hotelObj.lng == '0') {
				if (hotelObj.address == '') {
					doLocalSearch(hotelObj.name, '', hotelObj.city, hotelObj.country);				
				} else {
					doLocalSearch(hotelObj.name, hotelObj.address, hotelObj.city, hotelObj.country);									
				}
			} else {
				
				var latlng = (hotelObj.lat, hotelObj.lng);
				latlngs.push(latlng);		
				
				var marker = new google.maps.Marker(latlng,gSmallIcon);
				marker.id = i;
				markers.push(marker);
				
			}					
	
		}
		$('#maps_search_results_list').jScrollPane();
		
		zoomfit();
		
	});

	
}



//	map.addOverlay(this.marker(gSmallIcon));

function selectLocation(id) {
	
	GEvent.trigger(markers[id], "click");	
}

$(document).ready(function(){
		
	fillCountriesCombobox();
    initialize();
    
    $(document).bind("keypress", function(e) {
    	if (e.keyCode == 13) {
    		return false;
    	}
    });
    
//	$('#maps_search_results_list').hide();

		        });