$(document).ready(function() {
   if (GBrowserIsCompatible()) {

      var dommap = $("#gmap");
      var directions = $("#directions");

      var map = new GMap2(dommap.get(0));
      var geocoder = new GClientGeocoder();


      var zoom = 15;

      // Information for finding TCI / Making a pretty bubble
      var TCIaddress = "240 E. Rosecrans Ave Gardena, CA 90248";
      var TCIlogo = '<img id="bubble" src="../images/logos/contact.gif" alt="TCI Precision Metals" />';
      var TCIphone = "Tel (800) 234-5613 <br />Fax (310) 323-1255";
      var TCIbubble = TCIlogo + "<br />" + "TCI Precision Metals <br />" + TCIaddress + "<br />" + TCIphone;

      var opts = { 'maxWidth' : 450 };

      // Add some basic controls
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());

      // Start the map at TCI
      geocoder.getLatLng(
         TCIaddress,
         function (point) {
            if (!point) {
               alert(TCIaddress + " not found");
            } else {
               map.setCenter(point, zoom);
               var marker = new GMarker(point);
               map.addOverlay(marker);
               marker.openInfoWindowHtml(TCIbubble, opts);
            }
         }
      );

      var findAction = function() {
         directions.html("");
         map.clearOverlays();

         var query = $("input#start_address").val();
         var gdir = new GDirections(map, directions.get(0));

         gdir.load("from: " + query + " to: " + TCIaddress);

      }

      // Use JQuery to stick a handler on the submit button and form
      $("input#submit_search").click(findAction);
      $("#find-us-form").submit(findAction);
   }
});

$(document).unload(function() {
   GUnload();
});
