/* Google maps */

var map = null;
var geocoder = null;


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);
		if (type_map == "country")
		{
			geocoder1 = new GClientGeocoder();
			var location = geocoder1.getLatLng(acco_land, function(point){
				map.setCenter(point, 5);
			});
		}
		else
			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 */
		if (type_map != 'country')
		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_desc			= item.description;
			var hotel_guid 			= item.guid;
			var hotel_land			= item.land;
			var plaats 				= item.city;
			var image				= item.thumbnail;
			var straat				= item.straat;
			var huisnr				= item.husnr;
	
			var html = '<div class="map-result"><a href="/wintersport/'+hotel_guid+'/" class="map-result-left"><img width="100" height="75" src="'+image+'" alt="" /></a><div class="map-result-right"><p>'+hotel_desc+'</p><a href="/wintersport/'+hotel_guid+'/">Meer informatie over '+ hotel_title +'</a></div></div>\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);	
		});
	});
}


