﻿function CityGMap(Element, Width, Height, Summary, ZoomLevel, ShowZoomControls, ShowMapTypeControls)
{
  this.Element = Element;
  this.Width = Width;
  this.Height=Height;
  this.Summary = Summary;
  this.ZoomLevel = ZoomLevel;
  this.ShowZoomControls = ShowZoomControls;
  this.ShowTypeControls = ShowMapTypeControls;
  var GMap, GGeoEncoder, GOptions;
  this.initGmaps();
}

CityGMap.prototype.initGmaps = function (){
  if (GBrowserIsCompatible()) {
    GMap = new GMap2(document.getElementById(this.Element),{size:new GSize(this.Width, this.Height)});
    GGeoEncoder = new GClientGeocoder();
    if (this.ShowZoomControls)GMap.addControl(new GSmallMapControl());
    if (this.ShowMapTypeControls)GMap.addControl(new GMapTypeControl());
  } 
}

CityGMap.prototype.DrawMapByPoints = function(Latitude, Longitude, Summary){
  var point = new GLatLng(Latitude, Longitude);
  GMap.setCenter(point, this.ZoomLevel);
//  var marker = new GMarker(point);
//  GMap.addOverlay(marker);
//  marker.openInfoWindowHtml(Summary);
//  GEvent.addListener(marker,"click", function() {marker.openInfoWindowHtml(Summary);});
	CityGMap.prototype.createMarker(point,Summary.toString().replace(new RegExp('--', 'gi')," "), Summary.toString().replace(new RegExp('--', 'gi'),"<br/>"));
}

CityGMap.prototype.DrawMapByAddress = function(Address){
  if (GGeoEncoder) {
    GGeoEncoder.getLatLng(Address,
      function(point) {
        if (point){
          GMap.setCenter(point, this.ZoomLevel);
          var marker = new GMarker(point);
          GMap.addOverlay(marker);
          marker.openInfoWindowHtml(Address);
          GEvent.addListener(marker,"click", function() {marker.openInfoWindowHtml(Address);});
        }
        else{
          // Couldn't find Lattitude and Longitued for Address, defaults to columbus city
          GMap.setCenter(new GLatLng(39.962208,-83.000676), 12);
        }
      }
    );
  }
}

// A function to create the marker and set up the event window
CityGMap.prototype.createMarker = function(point,name,html) {
  var marker = new GMarker(point);
	GMap.addOverlay(marker);
  // The info window version with the "to here" form open
  to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:CityGMap.prototype.fromhere(' + i + ')">From here</a>' +
     '<br>Start address:<form action="http://maps.google.com/" method="get" target="_blank">' +
     '<input type="text" SIZE=20 MAXLENGTH=60 name="saddr" id="saddr" value="" />' +
     '<INPUT value="Go" TYPE="SUBMIT">' +
     '<input type="hidden" name="daddr" value="' + name + '" /><br>'; 
  // The info window version with the "to here" form open
  from_htmls[i] = html + '<br>Directions: <a href="javascript:CityGMap.prototype.tohere(' + i + ')">To here</a> - <b>From here</b>' +
     '<br>End address:<form action="http://maps.google.com/" method="get"" target="_blank">' +
     '<input type="text" SIZE=20 MAXLENGTH=60 name="daddr" id="daddr" value="" />' +
     '<INPUT value="Go" TYPE="Button" onclick="javascript:CityGMap.prototype.getDirections();">' +
     '<input type="hidden" name="saddr" value="' + name + '" /><br>'; 
  // The inactive version of the direction info
  html = html + '<br>Directions: <a href="javascript:CityGMap.prototype.tohere('+i+')">To here</a> - <a href="javascript:CityGMap.prototype.fromhere('+i+')">From here</a>';

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  gmarkers[i] = marker;
  htmls[i] = html;
  i++;
  return marker;
}

// functions that open the directions forms
CityGMap.prototype.tohere = function(i){
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
CityGMap.prototype.fromhere = function(i){
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

CityGMap.prototype.getDirections = function(){  
	directionsPanel = document.getElementById("directions");  
	GMap.setCenter(new GLatLng(49.496675,-102.65625), 3);  
	directions = new GDirections(GMap, directionsPanel);  
	directions.load("from: 500 Memorial Drive, Cambridge, MA to: 4 Yawkey Way, Boston, MA 02215 (Fenway Park)");
	}

var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;
var directions;

