/*jslint white: true, undef: true, nomen: true, eqeqeq: true */
/*global $, Class, Event, GBrowserIsCompatible, GEvent, GLatLng, GMap2, GMarker, GUnload, MapInfo, document, googleMap, setTimeout */
var GoogleMap = Class.create();
GoogleMap.prototype = {
  initialize: function (mapEl, mapInfo) {
    this.map = $(mapEl);
    this.mapInfo = mapInfo;
    
    this.addEvents();
    this.addMarker();
    this.marker.openInfoWindowHtml(this.html);
  },
  
  addEvents: function () {
    document.observe("unload", function () {
      var unload = new GUnload();
    });
  },
  
  addMarker: function () {
    this.map = new GMap2(this.map);
    // this.map.addControl(new GLargeMapControl());
    // this.map.addControl(new GMapTypeControl());
    this.point = new GLatLng(this.mapInfo.latitude, this.mapInfo.longitude);
    this.map.setCenter(this.point, 14);
    
    this.marker = this.createMarker(this.formatInfo());
    this.map.addOverlay(this.marker);
    
  },
  
  formatInfo: function () {
    return "<b class='event-name'>" + this.mapInfo.eventName + "</b><br/>" + this.mapInfo.locationDescription + "<br/>" + this.mapInfo.address + "<br/>" + this.mapInfo.city + ", " + this.mapInfo.state + " " + this.mapInfo.zip;
  },
  
  createMarker: function (html) {
    var marker = new GMarker(this.point);
    
    this.toHtml = html + '<br /><br /><b>Directions:</b> To here - <a href="javascript:googleMap.fromHere()">From here</a>' +
       '<br /><span class="address-label">Start address</span><form action="http://maps.google.com/maps" method="get" target="_blank">' +
       '<input type="text" name="saddr" id="saddr" value="" />' +
       '<input value="Go" type="submit" />' +
       '<input type="hidden" name="daddr" value="' + this.point.lat() + ',' + this.point.lng() + '"/>';
       
    this.fromHtml = html + '<br /><br /><b>Directions:</b> <a href="javascript:googleMap.toHere()">To here</a> - From here' +
       '<br /><span class="address-label">End address</span><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
       '<input type="text" name="daddr" id="daddr" value="" />' +
       '<input value="Go" type="submit" />' +
       '<input type="hidden" name="saddr" value="' + this.point.lat() + ',' + this.point.lng() + '"/>';
       
    // The inactive version of the direction info
    this.html = html + '<br /><br />Directions: <a href="javascript:googleMap.toHere()">To here</a> - <a href="javascript:googleMap.fromHere()">From here</a>';

    GEvent.addListener(marker, "click", function () {
      this.marker.openInfoWindowHtml(this.html);
    }.bind(this));
    
    return marker;
  },
  
  toHere: function () {
    this.marker.openInfoWindowHtml(this.toHtml);
    setTimeout(function () {
      $('saddr').focus();
    }, 100);
  },
  fromHere: function () {
    this.marker.openInfoWindowHtml(this.fromHtml);
    setTimeout(function () {
      $('daddr').focus();
    }, 100);
  }
};


Event.addBehavior({
  '#google-map-container' : function (e) {
    this.show();
  },
  '#google-map' : function (e) {
    if (new GBrowserIsCompatible() && MapInfo) {
      googleMap = new GoogleMap(this, MapInfo);
    }
  }
});