/* Google maps */

var map = null;
var geocoder = null;


$(document).ready(function(){
	initMap("kaart");
});


function createMarker(point,html,image, setcenter){

	var icon = new GIcon();
	icon.image = image;
	icon.iconSize = new GSize(30, 30);
	icon.iconAnchor = new GPoint(0, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);


	var marker = new GMarker(point,icon);
		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	if (setcenter)
		map.setCenter(point);
	map.addOverlay(marker);
	return;
}

function initMap(mapId){
	var mapbox = document.getElementById(mapId);
	if (mapbox != null)
	{
		map = new GMap2(mapbox);
		map.setMapType(G_NORMAL_MAP); 
		map.setCenter(new GLatLng(47.189712,7.778320), 8);
		
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GSmallZoomControl ());
		
		getAccomodations(acco_land_id, acco_id );
		
		/* Place the active accomodation on the map */
		getAddress(acco_straat +" "+ acco_huisnr +", "+ acco_plaats +", "+ acco_land, acco_title, "/Content/Images/hotel-logo-active.png", true);
	}
}

function getAddress(address, title, image, setcenter){
	geocoder = new GClientGeocoder();
	
	geocoder.getLatLng(
		address,
		function(point){
			if (point)
			{
				createMarker(point,title,image, setcenter);
			}
		});
}

function getAccomodations(country, active_acco){
	//$.getJSON("/includes/mapResults.php?land=0&active="+active_acco, function(data){

	$.getJSON("/includes/mapResults.php?land="+country+"&active="+active_acco, function(data){
		$.each(data.results, function(i,item){
			var hotel_id	 		= item.id;
			var hotel_title 		= item.title;
			var hotel_guid 			= item.guid;
			var hotel_land			= item.land;
			var plaats 				= item.city;
			var straat				= item.straat;
			var huisnr				= item.husnr;
	
			var html = "<a href=\"/wintersport/"+hotel_guid+"\">Meer informatie over "+ hotel_title +"</a>\n";
			
			if (!straat) 		straat = "";
			if (!huisnr)		huisnr = "";
			if (!plaats)		plaats = "";
			if (!hotel_land)	hotel_land = "";
			
			getAddress(straat +" "+ huisnr +", "+ plaats +", "+ hotel_land, html, "/Content/Images/hotel-logo.png", false);	
		});
	});
}



