var CARD_VENDOR = 1;
var PUBLIC_FACILITY = 2;
var PRIVATE_FACILITY = 3;
var COMING_SOON = 4;
var UNKNOWN = 5;
var map;
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.bikelink.org/images/noShadow.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

var letter_icons = new Array;
for (var i = 0; i < 26; ++i)
{
  letter_icons[i] = new GIcon(baseIcon);
  letter_icons[i].image = "/images/red_tear_" + String.fromCharCode(i + 97) + '.png';
}

var iconBred = new GIcon(baseIcon);
iconBred.image = "http://www.bikelink.org/images/markerBred.png";

var iconBblue = new GIcon(baseIcon);
iconBblue.image = "http://www.bikelink.org/images/markerBblue.png";

var iconByellow = new GIcon(baseIcon);
iconByellow.image = "http://www.bikelink.org/images/markerByellow.png";

var iconBorange = new GIcon(baseIcon);
iconBorange.image = "http://www.bikelink.org/images/markerBorange.png";

var iconVgreen = new GIcon(baseIcon);
iconVgreen.image = "http://www.bikelink.org/images/markerVgreen.png";

var iconDOTred = new GIcon(baseIcon);
iconDOTred.image = "http://www.bikelink.org/images/markerDOTred.png";

function marker_dropped(marker) 
{
  var latlng = marker.getLatLng();
  $('location_longitude').value = latlng.lng();
  $('location_latitude').value = latlng.lat();
}

var markers = new Hash();

function show_and_hide()
{
  markers.each(function(pair)
  {
    var display;
    var marker = pair.value;
    display = $F('display_' + marker.bikelink_map_region + '_' + marker.bikelink_type);
    if (display == 'on')
    {
      $('locations_' + marker.bikelink_map_region + '_' + marker.bikelink_type).show();
      marker.show();
    }
    else
    {
      $('locations_' + marker.bikelink_map_region + '_' + marker.bikelink_type).hide();
      marker.hide();
    }
  });
}

function create_marker(point, type, html, index, map_region)
{
  var marker;
  var options;

  options = {}

  if ($('location_longitude') == undefined)
  {
  }
  else
  {
    options['draggable'] = true;
  }

  options['icon'] = iconBred;
  if (type == COMING_SOON) options['icon'] = iconBblue;
  if (type == PRIVATE_FACILITY) options['icon'] = iconByellow;
  if (type == PUBLIC_FACILITY) options['icon'] = iconBorange;
  if (type == CARD_VENDOR) options['icon'] = iconVgreen;
  if (type == "DOTred") options['icon'] = iconDOTred;

  marker = new GMarker(point, options);
  marker.bikelink_type = type;
  marker.bikelink_map_region = map_region;

  markers.set(index, marker);

  GEvent.addListener(marker, "dragend", function() {
      marker_dropped(marker);
  });

  marker.html = html;
  return marker;
}

function create_marker_at(latitude, longitude, type, html, index, map_region)
{
  var point = new GLatLng(latitude, longitude);
  map.addOverlay(create_marker(point, type, html, index, map_region));
}

function load_map_with_single_point(latitude, longitude, type, html)
{
  var center_point = new GLatLng(latitude, longitude);
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(center_point, 9);
  map.addOverlay(create_marker(center_point, type, html));
}

var person_markers = [];
function create_person_marker_at(latitude, longitude)
{
  var options = {};
  var point = new GLatLng(latitude, longitude);
  options['icon'] = iconDOTred;
  var marker = new GMarker(point, options);
  person_markers.push(marker);
}

