/*
 * Class name: Room
 *
 * Parameters:
 *   NAME            DESCRIPTION
 *   roomId        - 
 *   numberOfRooms -
 *   roomName      -
 *
 * Methods:
 *   initialize
 *   setupFromXML
 *
 */
GoogleMapManager = Class.create();

GoogleMapManager.prototype = {
    initialize: function(map, hotelManager) {
	this.map          = map;
	this.hotelManager = hotelManager;
	this.markerArray  = {};

	var tooltip = document.createElement("div");
	tooltip.style.backgroundColor="#ffffff";
	tooltip.style.border="thin solid #4394CE";
	Element.hide(tooltip);

	this.mouseovertooltip = tooltip;

	this.map.getContainer().appendChild(this.mouseovertooltip);

	this.toolTipTemplate   = $('toolTipMarker');
	this.mouseOverTemplate = $('mouseOverToolTip');

    },

    mapIsLoaded: function()
    {
	return (this.map != null && this.map.isLoaded());
    },

    addMarkersToMap: function ()
    {
	if(!window.GMap || !this.mapIsLoaded() || !this.markerArray)
	    return;
	this.map.clearOverlays();
	for (var i in this.markerArray)
	{
	    this.map.addOverlay(this.markerArray[i]);
	}
    },

    showTooltip: function(event)
    {
	return this._showHideTooltip(event, true);
    },

    hideTooltip: function(event)
    {
	return this._showHideTooltip(event, false);
    },

    _showHideTooltip: function(event, show)
    {
	hotelId = this.hotelManager.getHotelIdFromEventElement(event);
	if(this.map && this.map.isLoaded() && this.markerArray[hotelId])
        {
          var marker = this.markerArray[hotelId];
          if(show)
	     marker.openInfoWindow(marker.tooltip); 
          else
	     this.map.closeInfoWindow(); 
	}
	return false;
    },

    setElementContent: function(cacheIds, id, content, attribute)
    {
	var o = cacheIds && cacheIds.get ? cacheIds.get(id) : null;
	if(o != null)
	    {	
		// Find what attribute to set
		var setAttribute = 'innerHTML';
		if(attribute && o[attribute])
		    setAttribute = attribute;
		else if(o.tagName == 'IMG')
		    setAttribute = 'src';
		else if(o.tagName == 'INPUT')
		    setAttribute = 'value';
		
		o.id = '';
		o[setAttribute] = content;
		
		return o;
	    }
	return;
    },

    createIcon: function(categoryId, site, partner)
    {
	if(!window.GIcon)
	    return;

	if (!site)
	    site    = config.site;
	if (partner == null)
	    partner = config.partner;
	
	var icon = new GIcon();
	if (categoryId)
	    icon.image = "http://static.resfeber.se/media/images/"+site+"/"+partner+"/hotel/markers/marker_"+categoryId+".png";
	else
	    icon.image = "http://static.resfeber.se/media/images/"+site+"/"+partner+"/hotel/markers/marker_20.png";
	icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	icon.transparent= "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
	icon.iconSize = new GSize(21, 35);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(10, 35);
	icon.infoWindowAnchor = new GPoint(10, 1);
	return icon;
    },

    createMarker: function(GeoPoint, icon, hotelName, ratingUrl, hotelId, price, categoryData, priceFormatting,onehotel)
    {
	if(!GMarker)
	    return;

	var marker = new GMarker(GeoPoint,icon);
	if(onehotel)
	    {
		return marker;
	   }
	else
	    {
		GEvent.addListener(marker, "click", function() {
			$('hotel_'+hotelId).scrollTo();
			//hotelManager.selectOneHotel(hotelId);
		     });

		if (this.toolTipTemplate)
		    {
			var newMarker = this.toolTipTemplate.cloneNode(true);
			var markerIds = new IdCache(newMarker);
			this.setElementContent(markerIds, 'tooltipHotelName', hotelName);
			this.setElementContent(markerIds, 'tooltipRatingUrl', ratingUrl);
			if (categoryData != null && categoryData.name != null && categoryData.id != 20){
			    this.setElementContent(markerIds, 'tooltipCategoryName', categoryData.name);
			    
			    var categoryId = markerIds.get('tooltipCategoryId');
			    if(categoryId != null)
				Element.addClassName(categoryId, 'preferred_'+categoryData.id);
			}
			if(price && price != '')
			    {
				var pF = this.setElementContent(markerIds, 'tooltipPriceFrom', TNText.replaceHash(priceFormatting, { price: price } ) || price );
				var priceRow = markerIds.get('tooltipPriceFromText');
				if(!pF && priceRow != null)
				    priceRow.parentNode.removeChild(priceRow);
			    }
			if(markerIds.get('tooltipReadMoreLink'))
			    {
				this.setElementContent(markerIds, 'tooltipReadMoreLink', "javascript:hotelList.selectOneHotel('"+hotelId+"');",'href');
			    }
			
			newMarker.style.display = 'block';
			newMarker.id = 'tooltip_'+hotelId;
			marker.infoWinShown = false;
			marker.tooltip = newMarker;
			if (this.mouseOverTemplate)
			    {
				var newMouseOverTooltip = this.mouseOverTemplate.cloneNode(true);
				var mouseOverIds = new IdCache(newMouseOverTooltip);
				this.setElementContent(mouseOverIds, 'mouseOverTooltipHotelName', hotelName);
				if(mouseOverIds.get('mouseOverTooltipRatingUrl'))
				    {
					this.setElementContent(mouseOverIds, 'mouseOverTooltipRatingUrl', ratingUrl);
				    }
				if(price && price != '')
				    {
					var pF = this.setElementContent(mouseOverIds, 'mouseOverTooltipPriceFrom', 
									TNText.replaceHash(priceFormatting, { price: price } ) || price );
					var priceRow = mouseOverIds.get('mouseOverTooltipPriceFromText');
					if(!pF && priceRow != null)
					    priceRow.parentNode.removeChild(priceRow);
				    }
				Element.show(newMouseOverTooltip);
				marker.mouseOverTooltip = newMouseOverTooltip;
				
				GEvent.addListener(marker, 'infowindowopen', function() {
					marker.infoWinShown = true;
					mapManager.mouseovertooltip.hide();
				    });
				
				GEvent.addListener(marker, 'infowindowclose', function() {
					marker.infoWinShown = false;
				    });

				GEvent.addListener(marker,"mouseover", function() {
					if (!marker.infoWinShown) {
					    mapManager.mouseOverTooltip(marker);
					}
				    });
				
				GEvent.addListener(marker,"mouseout", function() {
					mapManager.mouseovertooltip.hide();
				    }); 
			    }
			
			//	GEvent.addListener(marker,"click", function() {
			//		marker.openInfoWindow(marker.tooltip);
			//	    });
			
			return marker;
		    }
	    }
    },

    addCustomMarker: function(latitude, longitude,  hotelName, rating, categoryData, hotelId, price, site, ratingPath, markerArray,icon, priceFormatting,onehotel, partner)
    {
	// Placed here because it also needs price
	if(latitude != '' && latitude != 0 && longitude != '' && longitude != 0)
	    {
		if (!icon)
		    icon = this.createIcon(categoryData.id, site, partner);
		var ratingUrl = ratingPath + rating + "_star_ff.gif";
		var geoPoint = new GLatLng(latitude, longitude);
		var marker   = this.createMarker(geoPoint, icon, hotelName, ratingUrl, hotelId, price, categoryData, priceFormatting,onehotel);
		if(marker != null && this.markerArray)
		    {
			// Add to marker array if we have one (for remove or init later).
			this.markerArray[hotelId] = marker;
		    }
		return marker;
	    }
    },

    mouseOverTooltip: function(marker)
    {
	this.mouseovertooltip.innerHTML = marker.mouseOverTooltip.innerHTML;
	var point=this.map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=this.map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,
								       - offset.y + point.y + anchor.y));
	pos.apply(this.mouseovertooltip);
	Element.show(this.mouseovertooltip);
    },

    clearMap: function(marker)
    {
	if (this.map)
	    {
		this.map.closeInfoWindow();
		this.map.clearOverlays();
	    }
	this.markerArray  = {};
    }
};

