var mapDiv=document.getElementById('sup_big_map');
var panelHotels=document.getElementById('panel_hotel');
var hotels;
var map;
var clustererHotel;


var hotelIcon=new GIcon();
hotelIcon.image = "http://fr.federal-hotel.com/images/puce-hotel.png" ;
hotelIcon.shadow = "http://fr.federal-hotel.com/images/ombre.png";
hotelIcon.iconSize = new GSize(33, 42);
hotelIcon.shadowSize = new GSize(52, 42);
hotelIcon.iconAnchor = new GPoint(0, 42);
hotelIcon.infoWindowAnchor = new GPoint(0, 42);
hotelIcon.infoShadowAnchor = new GPoint(2, 42);

var residenceIcon=new GIcon();
residenceIcon.image = "http://fr.federal-hotel.com/images/puce-resid.png" ;
residenceIcon.shadow = "http://fr.federal-hotel.com/images/ombre.png";
residenceIcon.iconSize = new GSize(33, 42);
residenceIcon.shadowSize = new GSize(52, 42);
residenceIcon.iconAnchor = new GPoint(0, 42);
residenceIcon.infoWindowAnchor = new GPoint(0, 42);
residenceIcon.infoShadowAnchor = new GPoint(2, 42);

var hotelClusterIcon=new GIcon();
hotelClusterIcon.image = "http://fr.federal-hotel.com/images/groupe-hotels.png" ;
hotelClusterIcon.iconSize = new GSize( 40, 50 );
hotelClusterIcon.shadowSize = new GSize( 56, 51 );
hotelClusterIcon.iconAnchor = new GPoint( 20, 25 );
hotelClusterIcon.infoWindowAnchor = new GPoint( 13, 3 );
hotelClusterIcon.infoShadowAnchor = new GPoint( 27, 37 );

window.onload = Setup;

function Setup(){
	try{
		if(!GBrowserIsCompatible()){
			mapDiv.innerHTML='Sorry, your browser is not compatible with Google Maps.';
			return;
		}
		
		map=new GMap2(mapDiv,{
			draggableCursor:'default'
		});
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());

		clustererHotel=new Clusterer(map);
		clustererHotel.SetMaxVisibleMarkers(10);
		clustererHotel.SetMinMarkersPerCluster(6);
		clustererHotel.gridSize=5;
		clustererHotel.maxLinesPerInfoBox=10;
		clustererHotel.SetIcon(hotelClusterIcon);
		clustererHotel.name='hotel';
		clustererHotel.autoCenter=true;

		map.setCenter(new GLatLng(46.980252,3.47168),5);
		map.setMapType(G_NORMAL_MAP);
		HttpGet('hotel-discounts.xml.php',RequestChecker);
	}catch(e){
		alert('Erreur sur la carte '+e);
	}
}

function RequestChecker(request){
	try{
		var xmlDoc=request.responseXML.documentElement;
		var i;
		
		//parsing of hotels elements
		var hotelElements=xmlDoc.getElementsByTagName('hotel');
		hotels=[];
		for(var s=0;s<hotelElements.length;++s){
			var hotel=new Object();
			hotel.lat=parseFloat(hotelElements[s].getAttribute('lat'));
			hotel.lng=parseFloat(hotelElements[s].getAttribute('lng'));
			hotel.cat=parseFloat(hotelElements[s].getAttribute('cat'));
			hotel.id=hotelElements[s].getAttribute('id');
			hotel.etoiles=hotelElements[s].getAttribute('etoiles');
			hotel.lien=hotelElements[s].getAttribute('lien');
			hotel.location=new GLatLng(hotel.lat,hotel.lng);
			hotel.name=hotelElements[s].getAttribute('name');
			hotel.town=hotelElements[s].getAttribute('town');
			hotel.prix=hotelElements[s].getAttribute('prix');
			hotel.promo=hotelElements[s].getAttribute('promo');
			hotel.img=hotelElements[s].getAttribute('img');
			hotels.push(hotel);
		}
		hotels.sort(CompareNames);
		for(var s=0;s<hotels.length;++s){
			var hotel=hotels[s];
			if(hotel.cat==1){
				var marker=new GMarker(hotel.location,{
					icon:hotelIcon,title:hotel.name
				});
			}else if(hotel.cat==3){
				var marker=new GMarker(hotel.location,{
					icon:residenceIcon,title:hotel.name
				});
				
			}
			hotel.marker=marker;
			GEvent.addListener(marker,'click',MakeCaller(PopUpHotel,s));
			clustererHotel.AddMarker(marker,hotel.name,s);
		}
	}catch(e){
		alert('Erreur lors du chargement des données');
	}
}

function createHtmlList(type,elementNumber){
	if(type=='hotel'){
		var elementToDisplay=hotels[elementNumber];
		var link='<a href="javascript:PopUpHotel( '+elementNumber+' )">';
		var html='<li>'+link+elementToDisplay.name+'</a></li>';
	}
	return html;
}

function CompareNames(a,b){
	if(a.name<b.name)
		return-1;
	else if(a.name>b.name)
		return 1;
	else return 0;
}

function PopUpHotelId(id){
	try{
		var toShow = -1;
		for(var s=0;s<hotels.length;s++){
			if(hotels[s].id==id){
				toShow = s;
				s = hotels.length+1;
			}
		}
		if(toShow>-1){
			var lat = hotels[toShow].lat;
			var lng = hotels[toShow].lng;		
			var zoomLevel = map.getZoom();
			map.setCenter(new GLatLng(lat,lng),zoomLevel);
			var markerToShow = clustererHotel.markers[toShow];	
			while(!(markerToShow.onMap && ! markerToShow.inCluster)){
				zoomLevel++;
				map.setCenter(new GLatLng(lat,lng),zoomLevel);
			}
			PopUpHotel(toShow);
		}else{
			alert('Cet hôtel n\'est pas affiché sur la carte');
		}
	}catch(e){
		
	}
}

function PopUpHotel(s){
	try{
		var hotel=hotels[s];
		var html='<table class="infoWinStyle"><tr><td colspan="2"><b>'+hotel.name+' '+hotel.etoiles+'</b></td></tr><tr><td><img class="infoWinStyle_img" src="'+hotel.img+'" width="103" heigh="77"/></td><td>Ville : <b>'+hotel.town+'</b><br>';
		/*
		if(hotel.prix>0){
			html+='Prix à partir de : '+hotel.prix+' euros<br>';
		}
		*/
		if(hotel.promo>0){
			html+='<b style="color:#c00;">PROMO : -'+hotel.promo+' % </b><br>';
		}
		html += '<div class="action-link">';
		html += '<a href="'+hotel.lien+'" style="text-decoration:none;" target="_blank"><img src="http://www.federal-hotel-luxe.fr/images/ico_savoir.gif" border="0" />&nbsp;&nbsp;<span style="text-decoration:underline;">En savoir plus</span></a><br>';
		html += '<a href="#" onclick="selectionCaddy(\'add\',\''+hotel.id+'\'); alert(\'Hotel ajouté dans votre sélection.\');" style="text-decoration:none;"><span style="text-decoration:none;"><img src="http://www.federal-hotel-luxe.fr/images/accept.gif" border="0" />&nbsp;&nbsp;<span style="text-decoration:underline;">Ajouter à ma sélection</span></a>';
		html +='</div></td></tr></table>';
		hotel.marker.openInfoWindowHtml(html,{
			maxWidth:400
		});
	}catch(e){

	}
}