///////////////////////////////////////////////////////////////
//
//  Script: Google Map
//  Version: 1.0
//  Author: Craig Nelson / Classic Labs Development
//
//  Notes: oo version
//

  var Ascribe = Ascribe || {};
  
  Ascribe.Map = function (config) {
    var that = this; // scope adjustment
    this.map = null;
    this.geocoder = null;
    this.toggleInProcess = false;
    this.mapped; // to prevent multiple map initializations
    this.address = config.address || "";
    
    // elements
    this.mapLink = config.mapLink || "map";
    this.mapDisplay = config.mapDisplay || "map-display";
    
    // methods
    this.initializeMap = function () {
      if (GBrowserIsCompatible()) {
        this.map = new GMap2($(this.mapDisplay));
        //map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        this.geocoder = new GClientGeocoder();
      }
    } // initializeMap()
    
    this.showAddress = function (address) {
      if (this.geocoder) {
        this.geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              that.map.setCenter(point, 13);
              var marker = new GMarker(point);
              that.map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);
              that.map.addControl(new GSmallMapControl());
              that.map.addControl(new GMapTypeControl());
            }
          }
        );
      }
    } // showAddress()
    
    if ($(this.mapLink)) {
      $(this.mapLink).onclick = function () {
        var el = this;
        el.blur();
        if ($(that.mapDisplay).visible()) {
          if (that.toggleInProcess == false) {
            Effect.BlindUp(that.mapDisplay, { // using blind up/down instead of slide up/down because slide is causing an error. don't know why.
              duration: .2,
              beforeStart: function () {
                that.toggleInProcess = true;
              },
              afterFinish: function () {
                that.toggleInProcess = false
                el.innerHTML = "View Map";
              }
            });
          }
        }
        else {
          if (that.toggleInProcess == false) {
            Effect.BlindDown(that.mapDisplay, { // using blind up/down instead of slide up/down because slide is causing an error. don't know why.
              duration: .2,
              beforeStart: function () {
                that.toggleInProcess = true;
              },
              afterFinish: function () {
                that.toggleInProcess = false;
                if (that.mapped != true) {
                  that.initializeMap();
                  that.showAddress(that.address);
                  that.mapped = true;
                }
                el.innerHTML = "Close Map";
              }
            });
          }
        }
        return false;
      } // $(this.mapLink).onclick
    } // if ($(this.mapLink))
  } // constructor
