/* /static/js/hotels_list.js */ /* UTF8 COOKIE éà */


function	hotelsListClass(pageHotels, allHotels, gBoundLat, gBoundLng, gBoundZoom)
{
	this.pageHotels = pageHotels;
	this.allHotels = allHotels;
	this.gBoundLat = gBoundLat;
	this.gBoundLng = gBoundLng;
	this.gBoundZoom = gBoundZoom;

	this.compSelected = [];

	this.popupGmap = null;
	this.popupGmapPin = null;
	this.popupGmapIcon = null;

	this.listGmap = null;
	this.focusedListMarker = null;
	this.unfocusMarkerTimeout = null;
}


hotelsListClass.prototype.onCompClick = function(obj, idx)
{
	for(var i = 0; i < this.compSelected.length; i++)
	{
		if(this.compSelected[i] == idx)
		{
			for(var j = i; j < this.compSelected.length-1; j++)
				this.compSelected[j] = this.compSelected[j+1];

			this.compSelected.length -= 1;

			obj.className = 'shift_comparer';
			return false;
		}
	}

	if(this.compSelected.length >= 3)
	{
		alert(
			'Vous ne pouvez sélectionner plus de trois hôtels.\n'+
			'Veuillez désélectionner un autre hôtel pour ajouter celui-ci à votre sélection.'
		);
		return false;
	}

	this.compSelected[this.compSelected.length] = idx;

	obj.className = 'shift_comparer_selected';
	
	return false;
}


hotelsListClass.prototype.onCompDoClick = function()
{
	if(!this.compSelected.length)
	{
		alert(
			'Vous n\'avez pas sélectionné d\'hôtels pour la comparaison.\n'+
			'Veuillez sélectionner deux ou trois hôtels en cliquant sur le bouton "Comparer" qui apparaît dans la description de chaque hôtel.'
		);
		return false;
	}

	if(this.compSelected.length < 2)
	{
		alert('Vous devez sélectionner au moins deux hôtels pour effectuer la comparaison.');
		return false;
	}
	

	// this.compSelected = this.compSelected.sort(function(a, b) { return hotelsList.pageHotels[a].HOTEL_ID - hotelsList.pageHotels[b].HOTEL_ID; } );

	var	i, url = window.location.href;

	if(url.substr(url.length-1, 1) == '#')
		url = url.substr(0, url.length-1);

	url += '?';

	for(i = 0; i < this.compSelected.length; i++)
		url += (i ? '&' : '')+'compare[]='+this.pageHotels[this.compSelected[i]].HOTEL_ID;
	
	window.location = url;

	return false;
}


hotelsListClass.prototype.onGmapOpenClick = function(idx)
{
	var	el = vkDom.el('GMAPS_POPUP');

	if(el && GBrowserIsCompatible() && idx >= 0 && idx <= this.pageHotels.length)
	{
		var	hotel;
		var	minTop = 440;	// Avoid displaying the map over the flash player

		hotel = this.pageHotels[idx];

		vkDom.setText('GMAPS_TITLE', hotel.HOTEL_NAME);

		if(this.popupGmap == null)
		{
			this.popupGmap = new GMap2(vkDom.el('GMAPS_MAP'), { mapTypes: [ G_PHYSICAL_MAP, G_NORMAL_MAP, G_SATELLITE_MAP ] });
			this.popupGmap.addControl(new GLargeMapControl());
			this.popupGmap.addControl(new GMapTypeControl());
			this.popupGmap.enableDoubleClickZoom();
			this.popupGmap.enableContinuousZoom();

			//this.popupGmap.enableScrollWheelZoom();
		}

		this.popupGmap.setCenter(new GLatLng(hotel.HOTEL_GMAP_BOUND_LAT, hotel.HOTEL_GMAP_BOUND_LNG), parseInt(hotel.HOTEL_GMAP_BOUND_ZOOM));

		if(this.popupGmapPin != null)
			this.popupGmap.removeOverlay(this.popupGmapPin);

		if(this.popupGmapIcon == null)
		{
			this.popupGmapIcon = new GIcon();
			this.popupGmapIcon.image = '/static/img/gpins/point.gif';
			this.popupGmapIcon.iconSize = new GSize(22, 23);
			this.popupGmapIcon.iconAnchor = new GPoint(5, 23);
		}


		this.popupGmap.addOverlay(
			this.popupGmapPin = new GMarker(
				new GLatLng(hotel.HOTEL_GMAP_LAT, hotel.HOTEL_GMAP_LNG),
				{ 'icon' : this.popupGmapIcon, 'title': hotel.HOTEL_NAME, 'draggable' : false }
			)
		);

		el.style.left = (Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + ((document.documentElement.clientWidth-el.clientWidth)>>1))+'px';
		el.style.top = Math.max(minTop, (Math.max(document.documentElement.scrollTop, document.body.scrollTop) + ((document.documentElement.clientHeight-el.clientHeight)>>1)))+'px';

		el.style.visibility = 'visible';
	}

	return false;
}

hotelsListClass.prototype.onGmapCloseClick = function()
{
	var	el = vkDom.el('GMAPS_POPUP');

	if(el)
	{
		el.style.visibility = 'hidden';
		//el.style.display = 'none';
	}

	return false;
}




hotelsListClass.prototype.setupListGmaps = function()
{
	if(this.allHotels == null)
		return;

	this.listGmap = new GMap2(vkDom.el('GMAPS_LIST'), { mapTypes: [ G_PHYSICAL_MAP, G_NORMAL_MAP, G_SATELLITE_MAP ] });
	this.listGmap.enableDoubleClickZoom();
	this.listGmap.enableContinuousZoom();
	this.listGmap.addControl(new GLargeMapControl());
	this.listGmap.addControl(new GMapTypeControl());

	this.listGmap.setCenter(new GLatLng(this.gBoundLat, this.gBoundLng), parseInt(this.gBoundZoom));

	for(var i = 0; i < this.allHotels.length; i++)
	{
		this.allHotels[i].gIcon = new GIcon();
		this.allHotels[i].gIcon.image = '/static/img/gpins/'+(i+1)+'.gif';
		this.allHotels[i].gIcon.iconSize = new GSize(22, 23);
		this.allHotels[i].gIcon.iconAnchor = new GPoint(5, 23);
		this.allHotels[i].gMarker = new GMarker(
			new GLatLng(this.allHotels[i].HOTEL_GMAP_LAT, this.allHotels[i].HOTEL_GMAP_LNG),
			{ 'icon' : this.allHotels[i].gIcon, 'title': this.allHotels[i].HOTEL_NAME + ' ['+(i+1)+']', 'draggable' : false, 'zIndexProcess' : function() { return 1;} }
		);

/*
			vkDebug.enable();
			vkDebug.text('I='+i);
			vkDebug.text('NAME: '+this.allHotels[i].HOTEL_NAME);
			vkDebug.text('LAT:' +this.allHotels[i].HOTEL_GMAP_LAT);
			vkDebug.text('LAT:' +this.allHotels[i].HOTEL_GMAP_LNG);
			vkDebug.enable(false);
*/

		this.listGmap.addOverlay(this.allHotels[i].gMarker);

		this.attachEvents(i);
	}
}

hotelsListClass.prototype.attachEvents = function(i)
{
	var	self = this;

	// Attaches focus events on hotels list on the left

	if(window.addEventListener)
	{
		vkDom.el('GMAPS_LIST_ITEM_'+i).addEventListener('mouseover', function() { self.focusMarker(i); }, false);
		vkDom.el('GMAPS_LIST_ITEM_'+i).addEventListener('mouseout', function() { self.unfocusMarker(); }, false);
	}
	else if(window.attachEvent)
	{
		vkDom.el('GMAPS_LIST_ITEM_'+i).attachEvent('onmouseover', function() { self.focusMarker(i); });
		vkDom.el('GMAPS_LIST_ITEM_'+i).attachEvent('onmouseout', function() { self.unfocusMarker(); });
	}

	var	url = this.allHotels[i].URL;

	// Attaches click event on the markets
	GEvent.addListener(this.allHotels[i].gMarker, 'click', function() { window.location = url; } );
}

hotelsListClass.prototype.unfocusMarker = function()
{
	var	self = this;

	if(this.unfocusMarkerTimeout)
		clearTimeout(this.unfocusMarkerTimeout);

	this.focusedListMarker = null;

	this.unfocusMarkerTimeout = setTimeout(function() { self._unfocusMarker() }, 500);
}



hotelsListClass.prototype._unfocusMarker = function()
{
	if(this.listGmap && this.focusedListMarker == null)
	{
		for(var i = 0; i < this.allHotels.length; i++)
			this.allHotels[i].gMarker.show();
	}

	this.unfocusMarkerTimeout = null;
}


hotelsListClass.prototype.focusMarker = function(idx)
{
	var	prevFocus, newFocus, zoom;

	if(!this.listGmap)
		return;

	this.focusedListMarker = idx;

/*	

	prevFocus = this.focusedListMarker;
	newFocus = this.focusedListMarker = idx;

	if(prevFocus != null)
	{
		// Remove it now, since we override the object
		this.listGmap.removeOverlay(this.allHotels[prevFocus].gMarker);

		// Reset the previously focused marker
	}

	// Set the new one
	// Remove it now since we override the obkje
	this.listGmap.removeOverlay(this.allHotels[newFocus].gMarker);
*/
	// And reset everything

	for(var i = 0; i < this.allHotels.length; i++)
	{
		/*
		this.listGmap.removeOverlay(this.allHotels[i].gMarker);

		if(prevFocus != null && i == prevFocus)
		{
			this.allHotels[prevFocus].gMarker = new GMarker(
				new GLatLng(this.allHotels[prevFocus].HOTEL_GMAP_LAT, this.allHotels[prevFocus].HOTEL_GMAP_LNG),
				{ 'icon' : this.allHotels[prevFocus].gIcon, 'title': this.allHotels[prevFocus].HOTEL_NAME, 'draggable' : false, 'zIndexProcess' : function() { return 1;} }
			);
		}
		
		if(newFocus != null && newFocus == i)
		{
			var icon = new GIcon();
			icon.image = '/static/img/gpins/'+(i+1)+'_on.gif';
			icon.iconSize = new GSize(22, 23);
			icon.iconAnchor = new GPoint(5, 23);

			this.allHotels[newFocus].gMarker = new GMarker(
				new GLatLng(this.allHotels[newFocus].HOTEL_GMAP_LAT, this.allHotels[newFocus].HOTEL_GMAP_LNG),
				{ 'icon' : icon, 'title': this.allHotels[newFocus].HOTEL_NAME, 'draggable' : false, 'zIndexProcess' : function() { return 2;} }
			);
		}

		this.listGmap.addOverlay(this.allHotels[i].gMarker);
		*/

		if(this.focusedListMarker != null && this.focusedListMarker == i)
			this.allHotels[i].gMarker.show();
		else
			this.allHotels[i].gMarker.hide();
	}

	return;
}


vkDom.onLoad(
	function()
	{
		if(hotelsList)
			hotelsList.setupListGmaps();
	}
);


