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

// vkDebug.enable();

function	stayClass(hotels, gBoundLat, gBoundLng, gBoundZoom)
{
	this.hotels = hotels;
	this.current = 0;
	this.preloader = new vkImagePreload(1);

	this.gBoundLat = gBoundLat;
	this.gBoundLng = gBoundLng;
	this.gBoundZoom = gBoundZoom;


	// Skip the first one
	//for(var i = 1; i < hotels.length; i++)
	// Oops, dont skip the first one - ffox doesn't seem to cache it!
	for(var i = 0; i < hotels.length; i++)
		this.preloader.add(hotels[i].HOTEL_IMAGE);


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

stayClass.prototype.initHotelImageRollover = function()
{
	var	el;

	for(var i = 0; i < this.hotels.length; i++)
	{
		el = vkDom.el('HOTEL_IMG_'+i);

		if(el)
		{
			this.setupHotelImageRollover(el, i);

			if(!i)
			{
				el.className = 'on';
				this.current = 0;
			}
		}		
	}
}

stayClass.prototype.setupHotelImageRollover = function(el, idx)
{
	var	self = this;

	if(window.addEventListener)
		el.addEventListener('mouseover', function() { self._mouseOver(idx); }, false);
	else
		el.attachEvent('onmouseover', function() { self._mouseOver(idx); });
}




stayClass.prototype._mouseOver = function(idx)
{
	var	el;

	if(idx != this.current)
	{
		vkDom.el('HOTEL_IMG_'+this.current).className = 'off';

		vkDom.el('HOTEL_IMG_'+idx).className = 'on';
		vkDom.el('CURRENT_HOTEL_PHOTO').src = this.hotels[idx].HOTEL_IMAGE;
		vkDom.el('CURRENT_HOTEL_PHOTO').alt = this.hotels[idx].HOTEL_NAME;

		this.current = idx;
	}
}




stayClass.prototype.setupListGmaps = function()
{
	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.hotels.length; i++)
	{
		this.hotels[i].gIcon = new GIcon();
		this.hotels[i].gIcon.image = '/static/img/gpins/'+(i+1)+'.gif';
		this.hotels[i].gIcon.iconSize = new GSize(22, 23);
		this.hotels[i].gIcon.iconAnchor = new GPoint(5, 23);
		this.hotels[i].gMarker = new GMarker(
			new GLatLng(this.hotels[i].HOTEL_GMAP_LAT, this.hotels[i].HOTEL_GMAP_LNG),
			{ 'icon' : this.hotels[i].gIcon, 'title': this.hotels[i].HOTEL_NAME + ' ['+(i+1)+']', 'draggable' : false, 'zIndexProcess' : function() { return 1;} }
		);

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

		this.attachEvents(i);
	}
*/

	// 27/10/2008 - Multiple instances of the same hotel are now allowed - but the hotel number must not appear more than once...

	var	markerHash = {};
	var	nextPinId = 0;
	var	pinId;

	for(var i = 0; i < this.hotels.length; i++)
	{
		// Check if hotel already exists
		if(typeof(markerHash[this.hotels[i].HOTEL_UNIQUE_ID]) == 'number')
			pinId = markerHash[this.hotels[i].HOTEL_UNIQUE_ID];
		else
		{
			pinId = ++nextPinId;
			markerHash[this.hotels[i].HOTEL_UNIQUE_ID] = pinId;
		}

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

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

		this.attachEvents(i);
	}

}

stayClass.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(); });
	}
}

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

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

	this.focusedListMarker = null;

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



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

	this.unfocusMarkerTimeout = null;
}


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

	if(!this.listGmap)
		return;

	this.focusedListMarker = idx;

	for(var i = 0; i < this.hotels.length; i++)
	{
		if(this.focusedListMarker != null && this.focusedListMarker == i)
			this.hotels[i].gMarker.show();
		else
			this.hotels[i].gMarker.hide();
	}

	return;
}



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


