/* map object */

function map(idMap){
	// attributs
	this.idMap = idMap;
	this.datas = false;
	this.requestDatas = false;
	this.isInit = false;
	this.hotelLink = $("#linkToOverview_" + idMap).val();

	// indexation de la map dans le tableau de map
	if(typeof tvTripMaps.tabMaps[tvTripMaps.mapExt+idMap] != "object"){
		tvTripMaps.tabMaps[tvTripMaps.mapExt+idMap]=this;
	}
	
	// la carte appelÃ© est la carte courante, la recentrer sur l'hotel, sinon lancer l'initialisation de la map.
	if(idMap == tvTripMaps.currentMap && $(".ie6, .ie5").length<1){
		tvTripMaps.tabMaps[tvTripMaps.mapExt+idMap]._SetCenter();
		tvTripMaps.tabMaps[tvTripMaps.mapExt+idMap]._SetMapLink();
	}else{
		$("#map-container").get(0).innerHTML="";
		tvTripMaps.tabMaps[tvTripMaps.mapExt+idMap]._GetMapObject(idMap);
	}
	tvTripMaps.currentMap=idMap;
}

var tvTripMaps={
	tabMaps:{}
	,mapExt:"m_"
	,currentMap:false
	,mapRequest:""
	,defMapLinkStamp:"XX_HOTEL_LINK_XX"
	,defHotelNameStamp:"XX_HOTEL_NAME_XX"
	,_Init:function()
	{
		if (document.getElementById('mapRequest'))
		{
			this.mapRequest = $("#mapRequest").get(0).value;
		}
	}
	,_CloseMap:function(){
		if(tvTripMaps.tabMaps[tvTripMaps.mapExt+this.currentMap].datas.hotelLink){ //listview context only
			var src = $("#hotelMapPopIn #embedInput").eq(0).val();
			src = src.replace(tvTripMaps.tabMaps[tvTripMaps.mapExt+this.currentMap].datas.hotelLink, this.defMapLinkStamp);
			src = src.replace($("a#"+this.currentMap).eq(0).parents(".hotel").eq(0).find("h2 .linkToOverview").eq(0).text(), tvTripMaps.defHotelNameStamp);
			$("#hotelMapPopIn #embedInput").eq(0).val(src);
		}
	}
};

// methods

map.prototype._InitMap=function(){
    if (GBrowserIsCompatible()) {
		// rÃ©cupÃ©ration de la liste des map keys
		this._GetMapKey();
        // INITIALIZE MAP
		this.GMap = new GMap2(document.getElementById("map-container"));
		this.GMap.disableScrollWheelZoom();
		this.GMap.addControl(new GLargeMapControl());
		this.GMap.addControl(new GMapTypeControl());
		this._GenerateMapIcons();
		this._SetCenter();
		this._SetMapLink();
	    this.isInit=true;
	}
}

map.prototype._SetMapLink=function(){
	if(this.datas.hotelLink){
		var src = $("#hotelMapPopIn #embedInput").val();
		if (src != null)
		{
			src = src.replace(tvTripMaps.defMapLinkStamp, this.datas.hotelLink);
			src = src.replace(tvTripMaps.defHotelNameStamp, $("a#"+this.idMap).eq(0).parents(".hotel").eq(0).find("h2 .linkToOverview").eq(0).text());
			$("#hotelMapPopIn #embedInput").val(src);
		}
	}
}
map.prototype._GetMapKey=function(){
	if(typeof(this.mapkeys) != "string"){
		this.mapkeys='<ul>';
		if (this.requestDatas.mapkeys != null)
		{
			for(var i = 0; i < this.requestDatas.mapkeys.length; i++)
			{
				this.mapkeys += '<li class="picto ' + this.requestDatas.mapkeys[i].klass + '">' + this.requestDatas.mapkeys[i].name + '</li>';
			}
		}
		this.mapkeys+='</ul>';
	}
	$("#legende").get(0).innerHTML=this.mapkeys;
}
map.prototype._SetCenter=function(){
	this.GMap.thisHotelPoint = new GLatLng(this.requestDatas.latitude, this.requestDatas.longitude);
	this.GMap.setCenter(this.GMap.thisHotelPoint, this.requestDatas.zoom);
}
map.prototype._GenerateMapIcons=function(){
	for(var funcType in this.requestDatas.functions){
		for(var i=0; i<this.requestDatas.functions[funcType].length; i++){
			this[funcType](this.requestDatas.functions[funcType][i]);
		}
	}
}
map.prototype._CreateHotelIcon=function(_480, _481) {
	var _482 = new GIcon();
	var _483 = "/img/mapPictos/hotelWith";
	if (!_481) {
	    _483 = _483 + "out";
	}
	_483 = _483 + "Videos";
	if (_480) {
	    _483 = _483 + "Selected";
	    _482.iconSize = new GSize(33, 57);
	    _482.iconAnchor = new GPoint(16, 48);
	    _482.infoWindowAnchor = new GPoint(16, 48);
	} else {
	    _482.iconSize = new GSize(23, 44);
	    _482.iconAnchor = new GPoint(11, 41);
	    _482.infoWindowAnchor = new GPoint(11, 41);
	}
	_483 = _483 + ".png";
	_482.image = _483;
	return _482;
}
map.prototype._CreateHotelMarker=function(_484, _485, _486, _487, _488) {
	var _489 = new GLatLng(_486, _487);
	var _48a = new GMarker(_489, this._CreateHotelIcon(_484, _485));
	if (_488 != 0) {
	    GEvent.addListener(_48a, "click",
	    function() {
	        $.ajax({
	            type: "GET",
	            url: "/infowindow/small/hotel/" + _488,
	            success: function(_48b) {
	                _48a.openInfoWindowHtml(_48b);
	            }
	        });
	    });
	}
	return _48a;
}
map.prototype._CreatePoiMarker=function(lat, long, category, url, name, klass, sentence) {
	var point = new GLatLng(lat, long);
	var marker = new GMarker(point, this._CreatePoiIcon(klass));
	GEvent.addListener(marker, "click",function(){
			var html='<content><div class="smallInfoWindow siwInterest">';
					html+='<div class="poiInfos "'+klass+'>';
						html+='<span class="poiName legende_localattractions">';
							html+=name;
						html+='</span>';
						html+='<span class="poiCategory legende_localattractions">&nbsp;-&nbsp;'+category+'</span>';
					html+='</div>';
					html+='<div class="link">';
						html+='<a href="'+url+'">'+sentence+'</a>';
					html+='</div>';
				html+='</div>';
			html+='</content>';
    	marker.openInfoWindowHtml(html);
    	return false;
	});
	return marker;
}
map.prototype._AddHotelMarkerToMap=function() {
	//_48c, _48d, _48e, _48f, _490
	var _492 = this._CreateHotelMarker(arguments[0][0], arguments[0][1], arguments[0][2], arguments[0][3], arguments[0][4]);
	this.GMap.addOverlay(_492);
}
map.prototype._CreatePoiIcon=function(_493) {
	var _494 = new GIcon();
	_494.image = _493;
	if (_493 == "district") {
	    _494.iconSize = new GSize(15, 15);
	    _494.iconAnchor = new GPoint(11, 16);
	    _494.infoWindowAnchor = new GPoint(11, 16);
	} else {
	    _494.iconSize = new GSize(23, 35);
	    _494.iconAnchor = new GPoint(10, 38);
	    _494.infoWindowAnchor = new GPoint(10, 38);
	}
	return _494;
}

map.prototype._AddPoiMarkerToMap=function() {
	//_49c, _49d, _49e, _49f
	var _4a1 = this._CreatePoiMarker(arguments[0][0], arguments[0][1], arguments[0][2], arguments[0][3], arguments[0][4], arguments[0][5], arguments[0][6]);
	this.GMap.addOverlay(_4a1);
}
map.prototype._GetMapObject = function(idMap){
	//récupération des coordonnées
	if(this.datas == false){
		if (document.getElementById('coords')) // overview context
			this.datas=$.parseJSON($("#coords").get(0).value);
		else // listview context
			this.datas=$.parseJSON($("#coords_" + idMap).get(0).value);
		
		this.datas.idMap=idMap;
		$.ajax({
            type: "POST"
            ,url: tvTripMaps.mapRequest
			,data: tvTripMaps.tabMaps[tvTripMaps.mapExt+idMap].datas
            ,success: function(result) {
				tvTripMaps.tabMaps[tvTripMaps.mapExt + idMap].requestDatas = $.parseJSON(result);
				tvTripMaps.tabMaps[tvTripMaps.mapExt + idMap]._InitMap();
            }
        });
	}else{
		this._InitMap();
	}
}

tvTripMaps._Init();

