/* $Id: script.js 3034 2006-10-05 17:17:09Z wheirman $ */

/* Resize one item */
function resizeItem(item) {
  if (!item)
    return;

  /* Find browser window height */
  if (window.innerHeight)
    h = window.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)
    h = document.documentElement.clientHeight;
  else if( document.body && document.body.clientHeight)
    h = document.body.clientHeight;
  else
    h = 800;

  /* Find item top */
  t = 0;
  o = item;
  while(o) {
    t += o.offsetTop;
    if (!o) break; /* This shouldn't be necessary, but IE barfs less now... Duhh!! */
    o = o.offsetParent;
  }

  /* Item height = window height - top - bottom margin */
  item.style.height = (h - t - 30) + 'px';
}

function autoSizeItem(item) {
  if (item)
    item.style.height = 'auto';
}

/* Resize all items that need resizing, and notify the Google map it has been resized */
function resize() {
  resizeItem(document.getElementById("map"));

  if (map)
    map.checkResize();
}

function getMarker(point) {
  var pt = new GLatLng(parseFloat(point[0]), parseFloat(point[1]));
  var marker = new GMarker(pt, icons[point[3]]);
  var html = point[2];
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html)
  });
  return marker;
}


/* On page load */
var map = false;
var baseIcon = false;
var icons = [];
function setupMap() {
  /* Create the Google Map */
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(40, -80), 12);

  /* Create the icon */
  baseIcon = new GIcon();
  baseIcon.image = "images/mm_20_red.png";
  baseIcon.shadow = "images/mm_20_shadow.png";
  baseIcon.iconSize = new GSize(12, 20);
  baseIcon.shadowSize = new GSize(22, 20);
  baseIcon.iconAnchor = new GPoint(6, 20);
  baseIcon.infoWindowAnchor = new GPoint(5, 1);

  icons[0] = baseIcon;

  icons[1] = new GIcon(baseIcon);
  icons[1].image = "images/mm_20_yellow.png";

  icons[2] = new GIcon(baseIcon);
  icons[2].image = "images/mm_20_green.png";
}

function plotMarkers(points)
{
  var markers = [];
  for(var i = 0; i < points.length; ++i) {
    var marker = getMarker(points[i]);
    map.addOverlay(marker);
    markers.push(marker);
  }

  zoomToBounds(map, markers);
}

function arrayBounds(ary)
{
    // compute the bounds for an array of markers
    var bounds = new GLatLngBounds();
    if (ary && ary.length > 0)
        {
        for (var i = 0; i < ary.length; i++)
                {
            var marker = ary[i];
            if (marker)
                        {
                bounds.extend(marker.getPoint());
            }
        }
    }
    return bounds;
}

function zoomToBounds(map, ary)
{
  var bounds = arrayBounds(ary);
  var center_lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
  var center_lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
  var center = new GLatLng(center_lat, center_lng)
  var zoom = map.getBoundsZoomLevel(bounds);
  map.setCenter(center, zoom);
}

