var defaultMapLevel = 13; //not really used for anything
var innCloseupMapLevel = 14;

var mapbaseUrl = "http://www.bbonline.com/locatormaps/";

function getInnIcon(num) {
  var icon = new GIcon();
  icon.image = mapbaseUrl + "images/h" + num + ".gif"; 
  icon.iconSize = new GSize(18, 22);
  icon.iconAnchor = new GPoint(9, 14);
  icon.infoWindowAnchor = new GPoint(9, 8);
  return icon;
}

function getCityIcon() {
  var icon = new GIcon();
  icon.image = mapbaseUrl + "images/cityicon.png"; 
  icon.iconSize = new GSize(43, 30);
  icon.iconAnchor = new GPoint(22, 15);
  icon.clickable = false;
  return icon;
}

function getStandardMap(name, options) {
  var map = new GMap2(document.getElementById(name), options);
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  return map;
} 

function setError(name, message) {
   var element = document.getElementById(name);
   element.innerHTML = message;
   element.style.display="block";
}

function clearError(name) {
   var element = document.getElementById(name);
   element.innerHTML = "";
   element.style.display="none";
}

function zoomOnMarkers(map, markers) {
  var bounds = getMarkerBounds(markers);
  zoomOnBounds(map, bounds);
}

function zoomOnBounds(map, bounds) {
  var zoom = map.getBoundsZoomLevel(bounds);
  map.setZoom(zoom);

  //unfortunatly, the boundsZoomLevel doesn't take into account the map center
  //(it implicitly assumes that the zoomed map will be centered on the bounds 
  //rectangle).  However what we want is to have the bounds on the map while it
  //is centered on an arbitrary point which isn't quite the same thing.  

  //The above is the minimum that the zoom level could be and still show all of the
  //markers, so we just need to zoom out until the map bounds contains the marker bounds
  //Since the marker bounds and the map bounds should be pretty close for usual (sane)
  //cases, we will step up instead of attemping a binary search (slower overall but
  //faster for our expected cases)

//var mapbounds = map.getBounds();
//var xcontains = mapbounds.contains(bounds);

  while(!map.getBounds().containsBounds(bounds)) {
    map.zoomOut();
  }
}

function getMarkerBounds(markers) {
  var bounds = new GLatLngBounds;//(markers[0].getPoint(), markers[0].getPoint());
  for (var key in markers) {
    bounds.extend(markers[key].getPoint()); 
  }

  return bounds;
}


/*
  Stuff to handle info windows for the inns
*/

//directionsLink is optional, if not specified it pops open a innMapWindow
function toggleInnWindow(map, marker, inn, directionsLink) {
  var infoWindow = map.getInfoWindow();
  var windowAnchor = infoWindow.getPoint();
  if (!infoWindow.isHidden() && windowAnchor && windowAnchor.equals(marker.getPoint())) {
    map.closeInfoWindow();
  }
  else {
    var text = getInnWindowHtml(inn, directionsLink);
    marker.openInfoWindowHtml(text);
  }
}

function getInnWindowHtml(inn, directionsLink) {
  if (!directionsLink) {
    directionsLink = "javascript:openMapPopup(\"innMap.php?inn=" + inn["MEMBERID"] + "&driving=1\")";
  }
  var text = "<b>" + htmlEncode(inn.name) + "</b><br/>";
  text += makeAddress(inn) + "<br/>";
  text += makePhones(inn) + "<br/>";
/*
  //the website link isn't working quite right and is a bit superfluous.  Yank at least for now.
  if(window.opener) {
    text += "<a href='javascript:void(0);' onclick='window.opener.location=\"" + 
      htmlEncode(makeLink(inn)) + "\"'>website</a>&nbsp;";
  }
  else {
    text += "<a href='" + htmlEncode(makeLink(inn)) + "' target='_blank'>website</a>&nbsp;";
  }
*/
  text += "<a href='" + htmlEncode(directionsLink) + "'>directions</a>";
  
  return text;
}

function makePhones(inn) {
  var phones = "";

  if(inn.phone) {
    phones = inn.phone;
    if(inn.tollFreePhone) {
      phones += " - ";
    }
  }

  if(inn.tollFreePhone) {
    phones += inn.tollFreePhone;
  }
  return phones;
}

function makeAddress(inn) {
  var address = htmlEncode(inn["ADDRESS"]);
  if(inn["ADDRESS2"] != "") {
    address += "<br/>" + htmlEncode(inn["ADDRESS2"]);
  }

  address += "<br/>" + htmlEncode(inn["CITY"] + ", " + inn["STATE"] + " " + inn["ZIPCODE"]); 
  return address;
}

function makeLink(inn) {
  if(inn["URL"]) {
    return inn["URL"];
  }
  else {
    return "http://www.bboline.com/" + inn["STATE"].toLowerCase() + "/" + 
      inn["SHORTNAME"] + "/";
  }
}
