/* --- Functions for handling mouse scrolling --- */
function handle(delta) {
	if (delta < 0) {
		map.zoomOut();
	} else {
		map.zoomIn();
	}
}
function wheel(event){
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta) {
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta) {
		handle(delta);
		if (event.preventDefault) event.preventDefault();
		event.returnValue = false;
	}
}

/* --- Function to put the hotels on the map --- */
function HotelsnlCreateMap(map,hotelinfo, lang){
	var arrlength = hotelinfo.length;
	var bounds = map.getBounds();
 	var sw = bounds.getSouthWest();
 	var ne = bounds.getNorthEast();
 	var left = sw.lng();
 	var right = ne.lng();
 	var up = ne.lat();
	var down = sw.lat();
  	
	function createMarker(point, label, link) {
		var marker = new GMarker(point, icon);
		map.addOverlay(marker);
		GEvent.addListener(marker, "mouseover", function() {  marker.openInfoWindowHtml(label); });
		GEvent.addListener(marker, "click", function() { window.location=link; });
		return marker;
	}
  
	for(var i = 0; i <= arrlength-1; i++){
  		var icon = new GIcon();
  		icon.image = "http://www.hotels.nl/img/avmap/star"+hotelinfo[i][4]+".png";
  		icon.shadow = "http://www.hotels.nl/img/avmap/schaduw.png";
  		icon.iconSize = new GSize(22, 21);
  		icon.iconAnchor = new GPoint(6, 20);
  		icon.infoWindowAnchor = new GPoint(5, 1);
  		var point = new GLatLng(hotelinfo[i][2],hotelinfo[i][3]);
  		createMarker(point, ''+hotelinfo[i][0]+'<br />'+hotelinfo[i][1]+'', 'hotel.php?hotelid='+hotelinfo[i][5]+'&lang='+lang);
	}
	return(map);
}

/* --- This function calculates the average lon or lat to determine the center --- */
function average() {
	var items = average.arguments.length;
	var sum = 0;
	for (i = 0; i < items;i++) {
		sum += parseFloat(average.arguments[i]);
	}
	return (sum/items);
}

/* --- This function puts the map on the div with id="map" --- */
function load() {
	if (GBrowserIsCompatible()) {
		// Determine the center of the map
		var arrlength = hoteldata.length;
		var hoteldatalats = '';
		var hoteldatalons = '';
		for(var i = 0;i < arrlength;i++){
			hoteldatalats += hoteldata[i][2]+',';
			hoteldatalons += hoteldata[i][3]+',';
		}
		var hotellat2 = average(hoteldatalats);
		var hotellon2 = average(hoteldatalons);

		// Default zoom level
		var zoom = 13;

		// Create the map
		map = new GMap2(document.getElementById("map"));

		/* Put the zoom controls on the map.
		   GSmallMapControl is the small version. You can unescape it to use
		   it. You`ll have to escape the GLargeMapControl
		*/
		//map.addControl(new GSmallMapControl());
		map.addControl(new GLargeMapControl());

		map.addControl(new GMapTypeControl());		// The map types (map, satellite OR hybrid)(upper right corner)
		map.addControl(new GScaleControl());		// Displays the scale (lowerleft corner)
		map.addControl(new GOverviewMapControl());	// Display a small map in the lower right corner
		map.enableScrollWheelZoom();			// Zoom in and out with your mousewheel

		map.setCenter(new GLatLng(hotellat2, hotellon2), zoom);	// Sets the center of the map
		map = HotelsnlCreateMap(map, hoteldata, lang);		// Puts the hotels on the map
		 
		// This block is used for zooming in and out with the mousewheel.
	/*	if (window.addEventListener) {
			window.addEventListener('DOMMouseScroll', wheel, false);
		} else {
			window.onmousewheel = document.onmousewheel = wheel;
		}*/
	}
}


