//
// JavaScript functions to be used with the GMaps Servlet
//
//	(C) Copyright 2006: TripAdvisor LLC, All Rights Reserved
//
//	@author:	mhoule@tripadvisor.com
//	@since:   8/19/06
//

/* ============================================================= */
/*	Map Object */
/* ============================================================= */

function Map_savePosition()
{
  this.map.savePosition();
}

function Map_setPosition(lng, lat, zoom) 
{
  try
  {
    var curCenter = this.center;

    this.zoom = zoom;
    this.center = new GLatLng(lat, lng);
    this.map.setCenter(this.center, zoom);
    this.map.setZoom(zoom);

    this.savePosition();

    if (curCenter != null) 
      delete curCenter;
  }
  catch(e)
  {
  }
}

function Map_showRefresh()
{
	//	Don't show refresh if we are in an RPC or HAC series of calls...
	if (this.inRPC || this.inHAC)
	  return;

	if (UpdateMapUI)	UpdateMapUI(true);
}

//	Show Marker
function Map_showMarker(loc)
{
  if (loc != null)
  {
  	var marker = loc.getMarker();
  	if (marker == null)
  	{
      var lngLat = loc.getCenter();
      var gIcon = loc.getIcon();
      var toolTip = '';

      if (gIcon != null)
        marker = new GMarker(lngLat, { icon: gIcon, title: toolTip } );
      else
        marker = new GMarker(lngLat, { title: toolTip } );
      marker.title = toolTip;

      loc.setMarker(marker);
      GEvent.bind(marker, "click", loc, loc.onclick);
      GEvent.bind(marker, "mouseover", loc, loc.onmouseover);
      GEvent.bind(marker, "mouseout", loc, loc.onmouseout);
      GEvent.bind(marker, "infowindowopen", loc, function () { loc.infoOpened = true; });
      var closeFunc = function () { 
        loc.infoOpened = false; 
        document.loadingLocation = null; 
        if (typeof(hideSavesWidget) != 'undefined') { 
            hideSavesWidget(); 
        }
      };
      GEvent.bind(marker, "infowindowclose", loc, closeFunc);

      this.map.addOverlay(marker);
    }
  }
}

//  Hide Marker
function Map_hideMarker(loc)
{
  if (loc == null)
    return;

  var marker = loc.getMarker();
  if (marker != null)
  {
    this.map.removeOverlay(marker);
    loc.setMarker(null);
    delete marker;
  }
}


function Map_create(lat, lng, zoom, elt)
{
  var map = new GMap2(elt);
  if (map != null)
  {
    this.map = map;

    map.addControl(new GScaleControl());
    var overControl = new GOverviewMapControl();
    map.addControl(overControl);
    //map.addControl(new GMapTypeControl());
    //map.addControl(new GSmallMapControl());

    //this.overMap = overControl.getOverviewMap();

    // Set event handlers
    GEvent.bind(map, "dragend", this, Map_showRefresh);
    GEvent.bind(map, "zoomend", this, function(lOld, lNew) { this.showRefresh(); });
    //GEvent.bind(this.overMap, "dragend", this.overMap, function() { document.map.showRefresh(); });
    //GEvent.addListener(this.overMap, "dragend", function() { document.map.showRefresh(); })

    // Set map center position and zoom level
    this.setPosition(lng, lat, zoom);
  }
}

function Map_destroy()
{
	GUnload();

	this.center = null;
	this.zoom = null;
	this.map = null;
}


function Map_RPCStart()
{
  if (!this.inRPC)
  {
    this.inRPC = true;
    this.inHAC = (document.hacQuery != null) && !this.cancelled;
    this.cancelled = false;

    //	Display Update Control if not yet visible
    if (this.updateVisible == false)
    {
      var image = document.getElementById('cancelHACImage');
      if (image != null)
        image.style.display = (this.inHAC)? 'block' : 'none';

      this.map.addControl(this.updateControl);
      this.updateVisible = true;        
    }
  }
}

function Map_RPCEnd(doneHAC)
{
  if (this.inRPC)
  {
    this.inRPC = false;

    if (doneHAC)
      this.inHAC = false;

    if ((this.updateVisible == true) && (this.inHAC == false))
    {	//	We have a control and we are no LONGER in hac,  Remove control
      this.map.removeControl(this.updateControl);
      this.updateVisible = false;
    }
  }
}

function MAP_CancelHAC()
{
  this.cancelled = true;
  this.inHAC = false;

  var image = document.getElementById('cancelHACImage');
  if (image)
    image.style.display = 'none';

  if (document.timeoutId != -1)
  {
    window.clearTimeout(document.timeoutId);
    document.timeoutId = -1;
    this.updateContents();
  }
}


/*	Constructor */
function Map( fnUpdate )
{
  this.map = null;
  this.center = null;
  this.zoom = null;
  this.inRPC = false;
  this.inHAC = false;
  this.cancelled = false;

  this.refreshControl = null;
  this.refreshVisible = false;
  this.updateControl = null;
  this.updateVisible = false;

  this.create = Map_create;
  this.destroy = Map_destroy;
  this.showRefresh = Map_showRefresh;
  this.showMarker = Map_showMarker;
  this.hideMarker = Map_hideMarker;
  this.savePosition = Map_savePosition;
  this.setPosition = Map_setPosition;
  this.isCancelled = function() { return this.cancelled; }
  this.getCenter = function() { return this.center; }
  this.getZoom = function() { return this.zoom; }

  this.updateContents = fnUpdate;

  this.RPCStart = Map_RPCStart;
  this.RPCEnd = Map_RPCEnd;
  this.CancelHAC = MAP_CancelHAC;
}


/* ============================================================= */
/*	Location Object information */
/* ============================================================= */

function Location_getCenter() { return this.center; }
function Location_getId() { return this.id; }
function Location_getMarker() { return this.marker; }
function Location_getName() { return this.name; }
function Location_getIcon() { return this.icon; }
function Location_getContents() { return this.contents; }

function Location_isInfoOpened() { return this.infoOpened; }

function Location_setMarker(marker) { this.marker = marker; }
function Location_setContents(contents) { this.contents = contents; }


function Location_onclick()
{
  OpenMarkerInfo(this);
  if(this.tContext && this.tAction)
  {
    actionRecord(this.tContext, this.tAction);
  }
}

function Location_onmouseover()
{
  ShowLocationName(this); 
}

function Location_onmouseout() 
{
  HideLocationName(this); 
}

//	Constructor
function Location(lat,lng,id,icon,name,contents, tContext, tAction)
{
  this.center = new GLatLng(lat, lng);
  this.id = id;
  this.marker = null;
  this.name = name;
  this.icon = icon;
  this.contents = contents;
  this.infoOpened = false;
  this.tContext = tContext;
  this.tAction = tAction;
  
	this.getCenter = Location_getCenter;
	this.getId = Location_getId;
	this.getMarker = Location_getMarker;
	this.getName = Location_getName;
	this.getIcon = Location_getIcon;
	this.getContents = Location_getContents;
	this.isInfoOpened = Location_isInfoOpened;

	this.onclick = Location_onclick;
	this.onmouseover = Location_onmouseover;
	this.onmouseout = Location_onmouseout;

	this.setMarker = Location_setMarker;
	this.setContents = Location_setContents;
}


/* ============================================================= */
/*	Icon Constructors */
/* ============================================================= */
var hotelIcons = new Array();
function HotelIcon(domain, idx)
{
  if ((idx >= 0) && (hotelIcons[idx] != null))
  {
    return hotelIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/hotels/hotel";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(16, 29);
    icon.shadowSize = new GSize(38, 29);
    icon.iconAnchor = new GPoint(9, 27);
    icon.infoWindowAnchor = new GPoint(10, 0);
    icon.infoShadowAnchor = new GPoint(28, 8);
    icon.imageMap = [1,6,9,1,16,7,16,18,9,18,8,26,7,18,1,18,1,6];
    
    if (idx >= 0)
      hotelIcons[idx] = icon;
  }

  return icon;
}

var bbIcons = new Array();
function BBIcon(domain, idx)
{
  if ((idx >= 0) && (bbIcons[idx] != null))
  {
    return bbIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/bbs/bbs";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(16, 29);
    icon.shadowSize = new GSize(38, 29);
    icon.iconAnchor = new GPoint(9, 27);
    icon.infoWindowAnchor = new GPoint(10, 0);
    icon.infoShadowAnchor = new GPoint(28, 8);
    icon.imageMap = [1,6,9,1,16,7,16,18,9,18,8,26,7,18,1,18,1,6];
    
    if (idx >= 0)
      bbIcons[idx] = icon;
  }

  return icon;
}

var otherIcons = new Array();
function OtherIcon(domain, idx)
{
  if ((idx >= 0) && (otherIcons[idx] != null))
  {
    return otherIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/others/other";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(16, 29);
    icon.shadowSize = new GSize(38, 29);
    icon.iconAnchor = new GPoint(9, 27);
    icon.infoWindowAnchor = new GPoint(10, 0);
    icon.infoShadowAnchor = new GPoint(28, 8);
    icon.imageMap = [1,6,9,1,16,7,16,18,9,18,8,26,7,18,1,18,1,6];
    
    if (idx >= 0)
      otherIcons[idx] = icon;
  }

  return icon;
}

var cityIcons = new Array();
function CityCenterIcon(domain, idx)
{
  if ((idx >= 0) && (cityIcons[idx] != null))
  {
    return cityIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/citycenter";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(33, 46);
    icon.shadowSize = new GSize(51, 46);
    icon.iconAnchor = new GPoint(21, 44);
    icon.infoWindowAnchor = new GPoint(21, 2);
    icon.infoShadowAnchor = new GPoint(21, 44);
    icon.imageMap = [21,44, 0,20, 0,14, 14,0, 20,0, 32,14, 32,20, 21,32, 21,44];
    
    if (idx >= 0)
      cityIcons[idx] = icon;
  }

  return icon;
}

var attractIcons = new Array();
function AttractionIcon(domain, idx)
{
  if ((idx >= 0) && (attractIcons[idx] != null))
  {
    return attractIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/attraction";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(33, 46);
    icon.shadowSize = new GSize(51, 46);
    icon.iconAnchor = new GPoint(21, 44);
    icon.infoWindowAnchor = new GPoint(21, 2);
    icon.infoShadowAnchor = new GPoint(21, 44);
    icon.imageMap = [21,44, 0,20, 0,14, 14,0, 20,0, 32,14, 32,20, 21,32, 21,44];

		if (idx >= 0)
		  attractIcons[idx] = icon;
  }

  return icon;
}

var eateryIcons = new Array();
function EateryIcon(domain, idx)
{
  if ((idx >= 0) && (eateryIcons[idx] != null))
  {
    return eateryIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/dining";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(33, 46);
    icon.shadowSize = new GSize(51, 46);
    icon.iconAnchor = new GPoint(21, 44);
    icon.infoWindowAnchor = new GPoint(21, 2);
    icon.infoShadowAnchor = new GPoint(21, 44);
    icon.imageMap = [21,44, 0,20, 0,14, 14,0, 20,0, 32,14, 32,20, 21,32, 21,44];

		if (idx >= 0)
		  eateryIcons[idx] = icon;
  }

  return icon;
}


var bigHotelIcons = new Array();
function BigHotelIcon(domain, idx)
{
  if ((idx >= 0) && (bigHotelIcons[idx] != null))
  {
    return bigHotelIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/hotel";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(33, 46);
    icon.shadowSize = new GSize(51, 46);
    icon.iconAnchor = new GPoint(21, 44);
    icon.infoWindowAnchor = new GPoint(21, 2);
    icon.infoShadowAnchor = new GPoint(21, 44);
    icon.imageMap = [21,44, 0,20, 0,14, 14,0, 20,0, 32,14, 32,20, 21,32, 21,44];

    if (idx >= 0)
        bigHotelIcons[idx] = icon;
    }

  return icon;
}

var searchIcons = new Array();
function SearchIcon(domain, idx)
{
  if ((idx >= 0) && (searchIcons[idx] != null))
  {
    return searchIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/search";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(33, 46);
    icon.shadowSize = new GSize(51, 46);
    icon.iconAnchor = new GPoint(21, 44);
    icon.infoWindowAnchor = new GPoint(21, 2);
    icon.infoShadowAnchor = new GPoint(21, 44);
    icon.imageMap = [21,44, 0,20, 0,14, 14,0, 20,0, 32,14, 32,20, 21,32, 21,44];

		if (idx >= 0)
		  searchIcons[idx] = icon;
  }

  return icon;
}

var miniCityIcons = new Array();
function MiniCityCenterIcon(domain, idx)
{
  if ((idx >= 0) && (miniCityIcons[idx] != null))
  {
    return miniCityIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/mini_geo";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".png";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(19, 19);
    icon.shadowSize = new GSize(23, 19);
    icon.iconAnchor = new GPoint(0, 19);
    icon.infoWindowAnchor = new GPoint(10, 0);
    icon.infoShadowAnchor = new GPoint(10, 19);
    icon.imageMap = [0,18, 3,15, 0,12, 0,4, 4,0, 12,0, 19,4, 19,12, 12,15, 5,15, 2,19, 0,18];
    
    if (idx >= 0)
      miniCityIcons[idx] = icon;
  }

  return icon;
}

var miniAttractIcons = new Array();
function MiniAttractionIcon(domain, idx)
{
  if ((idx >= 0) && (miniAttractIcons[idx] != null))
  {
    return miniAttractIcons[idx];
  }

  var icon = new GIcon();

  if (domain != null)
  {
    var root = "img/gmaps/mini_attraction";
    var image = domain + root;
    if (idx > 0)
      image = image + "_" + idx + ".gif";
    else
      image = image + ".png";

    icon.image = image;
    icon.transparent = image;
    icon.shadow = domain + root + "_shadow.png";
    icon.iconSize = new GSize(26, 20);
    icon.shadowSize = new GSize(36, 20);
    icon.iconAnchor = new GPoint(0, 20);
    icon.infoWindowAnchor = new GPoint(14, 0);
    icon.infoShadowAnchor = new GPoint(14, 20);
    icon.imageMap = [0,18, 4,14, 4,0, 26,0, 26,16, 6,16, 2,20, 0,20, 0,18];

		if (idx >= 0)
		  miniAttractIcons[idx] = icon;
  }

  return icon;
}

var sponsorAIcons = new Array();
function SponsorAIcon(domain, idx, markerType)
{ 
  var icon = new GIcon();
  
  if (domain != null)
  {
    var root = "img/gmaps/spr/pins/"; 
    var image = domain + root + document.sponsorAPushPin;

    icon.image = image;
    icon.transparent = image;
//    icon.shadow = domain + root + "_shadow.png";
//    icon.shadowSize = new GSize(36, 20);
   switch(markerType)
   {
     case "bw":  // bw (tall)
        icon.iconSize = new GSize(32, 35);
        icon.iconAnchor = new GPoint(1, 36);
        break;
     default:  // ihg
        icon.iconSize = new GSize(36, 28);  
        icon.iconAnchor = new GPoint(8, 29);
     }

    icon.infoWindowAnchor = new GPoint(32, 8);
    icon.infoShadowAnchor = new GPoint(20, 0);
    //icon.imageMap = [ 9,0, 26,0, 26,15, 9,15, 1,17, 0,17, 0,16, 9,15, 9,0 ];
    
    if (idx >= 0)
		  sponsorAIcons[idx] = icon;
  }

  return icon;
}

function actionRecord(context, action)
{
  var errFunc = function(req) {}
  var responseFunc = function(req) {}
  params = new UrlParams();
  if (context && action) {
    params.add('context', context);
    params.add('action', action);
  }
  var req = new HttpRpc('/ActionRecord?' + params.toString(), errFunc, responseFunc, null, 'GET');
  req.sendRequest();
  return false;
}
