function addMarker(latitude, longitude, description) {

  var marker = new GMarker(new GLatLng(latitude, longitude));
  
  /*GEvent.addListener(marker, 'click',
    function() {
        marker.openInfoWindowHtml(description);
    }
  );*/

  GEvent.addListener(marker, 'mouseover',
    function() {
        marker.openInfoWindowHtml(description);
    }
  );

  GEvent.addListener(marker, 'mouseout',
    function() {
        marker.closeInfoWindow();
    }
  );
  
  map.addOverlay(marker);
}

function init() {

  if (GBrowserIsCompatible()) {	
  
    var startZoom =       readCookie(cookieZoom)      ?   parseInt(readCookie(cookieZoom))      : defaultZoom;
    var centerLatitude =  readCookie(cookieCenterLat) ? parseFloat(readCookie(cookieCenterLat)) : 33.456636;
    var centerLongitude = readCookie(cookieCenterLon) ? parseFloat(readCookie(cookieCenterLon)) : -95.361328;
    
    if( typeof( resizeOffset ) != 'undefined' ) {
      handleResize();
    }

    map = new GMap2(document.getElementById('map'));

    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    
    if( ( typeof( enableScrollWheelZoom ) != 'undefined' ) && ( enableScrollWheelZoom == true ) ) {
      map.enableScrollWheelZoom();
    }

    GEvent.addListener(map, "moveend", map_movedOrZoomed);
    GEvent.addListener(map, "zoomend", map_movedOrZoomed);
    
    for(id in markers) {
      addMarker(markers[id].latitude, markers[id].longitude, "<b>" + markers[id].name + "</b><br/><br/>" + markers[id].location);
    }
  }
  else {
    document.getElementById('map').style.visibility = 'hidden';
    unhide( 'notsupported' );
  }
}

function is_submittable() {
  return ( document.getElementById('query').value != '' && document.getElementById('query').value != defaultQueryText );
}

function myKeyPressed(e) {
  e = e || window.event;

  var unicode = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;

  if (unicode == 13) {
    return is_submitable();
  }
  else {
    return true;
  }
}

function windowHeight() {
  // Standard browsers (Mozilla, Safari, etc.)
  if (self.innerHeight)
    return self.innerHeight;

  // IE 6
  if (document.documentElement && document.documentElement.clientHeight)
    return document.documentElement.clientHeight;

  // IE 5
  if (document.body)
    return document.body.clientHeight;

  // Just in case.
  return 0;
}

function handleResize() {
  var topHeight = document.getElementById('top').offsetHeight;
  var height = windowHeight() - topHeight - resizeOffset;

  document.getElementById('map').style.height = height + 'px';
}

function textbox_focus() {
  var contents = document.getElementById('query').value;
  
  if( contents == defaultQueryText ) {
    q = document.getElementById('query');
    
    q.value = '';
    q.style.color = '#000';
  }
}

function textbox_blur() {
  var contents = document.getElementById('query').value;
  
  if( contents == '' ) {
    q = document.getElementById('query');
    
    q.style.color = '#ccc';
    q.value = defaultQueryText;
  }
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');

  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  
  return null;
}

function map_movedOrZoomed() {
  document.cookie = cookieCenterLon + '=' + map.getCenter().lng() + '; expires=Tue, 19 Jan 2038 03:14:07 UTC; path=/';
  document.cookie = cookieCenterLat + '=' + map.getCenter().lat() + '; expires=Tue, 19 Jan 2038 03:14:07 UTC; path=/';
  document.cookie = cookieZoom      + '=' + map.getZoom()         + '; expires=Tue, 19 Jan 2038 03:14:07 UTC; path=/';
}

