google.load('maps','2');
google.load('search','1');
google.load('jquery', '1.3.1');

// initialiseer variablesvar map;
var geocoder;
var searcher;
var searchResults = [];
var hotels = [];
var markers = [];
// vars for zoomfit
var bounds;
var latlngs = [];
// vars for icon
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(37.4419, -122.1419), 3);//	map.setUIToDefault();
	map.addControl(new google.maps.LargeMapControl3D());
	
	geocoder = new google.maps.ClientGeocoder();
	
	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";
	
    searcher = new google.search.LocalSearch();
//	searcher.setNoHtmlGeneration();
    searcher.setSearchCompleteCallback(null, OnLocalSearch);	
    			
}

function findHotel() {

	var searchInput = $('#searchMapsBox').val();
	var searchLocation = $('#searchMapsLocation').val();
    searcher.setCenterPoint(searchLocation);
   	searcher.execute(searchInput); 
   	
}


// Called when Local Search results are returned, we clear the old
// results and load the new ones.
function OnLocalSearch() {


	// Clear map from markers
	map.clearOverlays();

	// Clear results list
	$('#maps_search_results_list').html('');
	
	// clear arrays	
	hotels = [];
	latlngs = [];
	markers = [];
	searchResults = [];
	
	
	if (!searcher.results){
				return;
	}

	
	if (searcher.results.length > 0) {
		
		
		
		for (var i = 0; i < searcher.results.length; i++) {

			resultObj = searcher.results[i];
			
			// place result in resultsarray for later reference
			searchResults.push(resultObj);
						
			var lat = resultObj.lat;
			var lng = resultObj.lng;
					
			var latlng = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
			var marker = new google.maps.Marker(latlng,gSmallIcon);
			marker.id = i;
			
			// place in latlngs array for zoomfit		
			latlngs.push(latlng);	
			// place in markers array for GEvent
			markers.push(marker);	
			// place in hotels array for on-click action
			map.addOverlay(marker);			
			
			GEvent.addListener(marker, "click", function() {
				var html = "<p style='line-height: 100%; font-size: 10px'><b>" + searchResults[this.id].titleNoFormatting + "</b><br />";
				if (searchResults[this.id].streetAddress != '') html += searchResults[this.id].streetAddress + "<br />";
				if (searchResults[this.id].city != '') html += searchResults[this.id].city + "<br />";
				if (searchResults[this.id].region != '') html += searchResults[this.id].region + "<br />";
				if (searchResults[this.id].country != '') html += searchResults[this.id].country + "<br />";
//				if (searchResults[this.id].phoneNumbers[0].number != '') html += "Telefoon: " + searchResults[this.id].phoneNumbers[0].number + "<br />";
				if (searchResults[this.id].url != '') html += '<a href="' + searchResults[this.id].url + '" target="_new">More info</a><br /><br />';
	
				html += '<a href="javascript:dummy()" onclick="selectHotel(' + this.id + ')">ADD HOTEL</a><br />';
	
				html += "</p>";
			
				this.openInfoWindow(html);
			});			
			
			$('#maps_search_results_list').append('<li><a href="javascript:dummy()" onclick="viewHotel(' +  i + ')"><span class="map_result_title">' + searchResults[i].titleNoFormatting + '</span><br /><span class="map_result_city">' + searchResults[i].city + '</span></a></li>');			
						
		}
				
	} else {
		
		alert('No results found');	
	
	
	}
	

	
	zoomfit();
		
//	map.setZoom(13);
//	searcher.setCenterPoint(map);	
}

function placeHotels(hotelData, limit) {
	
	// clear map markers on new search
	map.clearOverlays();
	
	hotels = [];
	latlngs = [];
	markers = [];
	
	i = 0;
	
	$('#maps_search_results_list').html("");
		
	// place markers		
	hotelData.each(function() {	
	
		hotels[this.id] = this;

		if (this.lat != '0' && this.lng != '0') {
			
			var latlng = new google.maps.LatLng(parseFloat(this.lat), parseFloat(this.lng));
			var marker = new google.maps.Marker(latlng,gSmallIcon);
			marker.id = this.id;
		
			
		
			// place in latlngs array for zoomfit		
			latlngs.push(latlng);	
			// place in markers array for GEvent
			markers.push(marker);	
			// place in hotels array for on-click action
			map.addOverlay(marker);			
	
			GEvent.addListener(marker, "click", function() {
				
				
				var html = "<p style='line-height: 100%; font-size: 10px'><b>" + hotels[this.id].hotelname + "</b><br />";
				if (hotels[this.id].address_post != '') html += hotels[this.id].address_post + " " + hotels[this.id].address_post_nr + "<br />";
				if (hotels[this.id].city != '') html += hotels[this.id].city + "<br />";
				if (hotels[this.id].province != '') html += hotels[this.id].province + "<br />";
				if (hotels[this.id].country != '') html += hotels[this.id].country + "<br />";
				if (hotels[this.id].phone != '') html += "Telefoon: " + hotels[this.id].phone + "<br />";
				if (hotels[this.id].googleurl != ""){
					 html += '<br /><a href="' + hotels[this.id].googleurl + '" target="_new">More info</a><br />';
				}
				else if (hotels[this.id].web != ""){
					 
					
					 html += '<br /><a href="' + hotels[this.id].web + '" target="_new">More info</a><br />';
				}

				html += '<a href="javascript:dummy()" onclick="nietOK(' + hotels[this.id].id + ')">Not OK</a><br />';
	
				html += "</p>";
			
				this.openInfoWindow(html);
			});
			
			$('#maps_search_results_list').append('<li><a href="javascript:dummy()" onclick="selectLocation(' +  i + ')"><span class="map_result_title">' + hotels[this.id].hotelname + '</span><br /><span class="map_result_city">' + hotels[this.id].city + '</span></a></li>');			
						
		} else {

		}
		
		i++;
		
	});
		
	if(hotelData.length == 0){
		
		alert('No results found');		
	}



	if ( i > 48) {
		$('.next50').show();	
	} else {
		$('.next50').hide();			
	}
	
	if (limit > 0) {
		$('.previous50').show();			
	} else {
		$('.previous50').hide();					
	}
	
	zoomfit();
	
	
}

function showHotelbyID(hotelData) {

	$('#maps_database_results_list').html("");
		
	map.clearOverlays();
		
	var latlng = new google.maps.LatLng(parseFloat(hotelData[0].lat), parseFloat(hotelData[0].lng));
	var marker = new google.maps.Marker(latlng,gSmallIcon);
	
	// place in latlngs array for zoomfit		
	latlngs.push(latlng);		

	map.addOverlay(marker);					

		GEvent.addListener(marker, "click", function() {
			
			var html = "<p style='line-height: 100%; font-size: 10px'><b>" + hotelData[0].hotelname + "</b><br />";
			if (hotelData[0].address_post != '') html += hotelData[0].address_post + " " + hotelData[0].address_post_nr + "<br />";
			if (hotelData[0].city != '') html += hotelData[0].city + "<br />";
			if (hotelData[0].province != '') html += hotelData[0].province + "<br />";
			if (hotelData[0].country != '') html += hotelData[0].country + "<br />";
			if (hotelData[0].phone != '') html += "Telefoon: " + hotelData[0].phone + "<br />";

			html += "</p>";
		
			this.openInfoWindow(html);
		});
				
	$('#maps_database_results_list').append('<li><b><span class="map_result_title">' + hotelData[0].hotelname + '</span><br /><span class="map_result_city">' + hotelData[0].city + '</span></b></li>');			
	
	zoomfit();
	
}


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));
}

google.setOnLoadCallback(initialize);

