/** GPlotter - Plotter interface for use with Google Maps - offwhite@gmail.com - 2005-07-24        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/

// Usage:
// var plotter = new GPlotter();
// plotter.setColor(plotter.BLUE);
// plotter.setIconUrl("http://gplotter.offwhite.net/maps/icons/");
// plotter.plot("map", "labels", "milwaukee.xml");

var GPlotter = Class.create();

Object.extend(GPlotter.prototype, {
  initialize: function() {
    // Variables
    this.VERSION = '0.9.0';
    this.initialized = false;
    this.mapDocument = "";
    this.maxIconNumber = 30;
    this.baseIcon = new GIcon();
    this.markers = new Array();
    this.baseIcon.shadow = "../Images/Maps/GoogleMapIcons/shadow.png";
    this.baseIcon.iconSize = new GSize(24, 24);
    //this.baseIcon.shadowSize = new GSize(37, 34);
    this.baseIcon.iconAnchor = new GPoint(12, 24);
    this.baseIcon.infoWindowAnchor = new GPoint(9, 2);
    //this.baseIcon.infoShadowAnchor = new GPoint(18, 34);
    this.dcIcon = new GIcon();
    this.dcIcon.shadow = "../Images/Maps/GoogleMapIcons/icon10s.png";
    this.dcIcon.iconSize = new GSize(32, 32);
    this.dcIcon.iconAnchor = new GPoint(16, 32);
    this.dcIcon.infoWindowAnchor = new GPoint(16, 8);
    this.iconUrl = "/maps/icons/";
    this.RED = "r";
    this.GREEN = "g";
    this.BLUE = "b";
    this.WEST = "w";
    this.color = this.WEST;
    this.maps = new Array();
    this.divId = "";
  },
  // Functions
  setColor: function(c) {
    if (c == this.RED) {
      this.color = this.RED;
    }
    else if (c == this.GREEN) {
      this.color = this.GREEN;
    }
    else if (c == this.BLUE) {
      this.color = this.BLUE;
    }
    else if (c == this.WEST) {
      this.color = this.WEST;
    }
    else {
      alert("Color not supported: " + c);
    }
  },
  setIconUrl: function(url) {
    this.iconUrl = url;
  },
  plot: function(mapId, labelsId, feedName, divisionId) {
    if (!this.initialized) {
      this.divId = divisionId
      var plotter = this;
      this.labels = $(labelsId);
      var mapElem = $(mapId);
      if (mapElem) {
        var map;

        if (this.maps[mapId] && this.maps[mapId].setCenter) {
          map = this.maps[mapId];
        }
        else {
          map = new GMap2(mapElem);
          map.addControl(new GSmallMapControl());
          map.addControl(new GMapTypeControl());
          //Add physical terrain map
          map.addMapType(G_PHYSICAL_MAP); 
          map.enableDoubleClickZoom();
          map.enableScrollWheelZoom();
          this.maps[mapId] = map;
        }

        var params = {
          map : map,
          mapID : mapId,
          labelsID : labelsId
        }

        this.successHandler = this.handleResponse.bind(this, params);

        var opt = {
          asynchronous:true,
          // Use POST
          method: 'get',
          // Handle successful response
          onSuccess: this.successHandler,
          // Handle 500
          on500: function(t) {
              alert('Error 500: location "' + t.statusText + '" was not found.');
          },
          // Handle 404
          on404: function(t) {
              alert('Error 404: location "' + t.statusText + '" was not found.');
          },
          // Handle other errors
          onFailure: function(t) {
              alert('Error ' + t.status + ' -- ' + t.statusText);
          }
        }

        new Ajax.Request(feedName, opt);

        this.initialized = true;
      }
    }
  },
  handleResponse: function(p, t) {
    this.runPlotter(p.map, t.responseXML);
  },
  runPlotter: function(map, xmlDoc)
  {
    this.mapDocument = xmlDoc;
    var root = xmlDoc.getElementsByTagName("locations")[0];
    var version = root.getAttribute("version");
    if (version == "0.9") {
      this.runPlotter09(map, xmlDoc);
    }
    else {
      alert("Locations version not supported");
    }
  },
  runPlotter09: function(map, xmlDoc)
  {
    var innerHTML = "";
    var cLat, cLong, zoomLevel;
    var root = xmlDoc.getElementsByTagName("locations")[0];
    cLat = root.getAttribute("latitude");
    cLong = root.getAttribute("longitude");
    zoomLevel = parseInt(root.getAttribute("zoomlevel"));
    var centerPoint = new GLatLng(cLat, cLong);
    map.setCenter(centerPoint, zoomLevel);
    //map.setCenter(centerPoint, zoomLevel, G_PHYSICAL_MAP);

    var locations = xmlDoc.getElementsByTagName("location");
    for (var i=0;i<locations.length;i++) {
      var node = locations[i];
      var cid = node.getAttribute("communityid");
      var label = node.getAttribute("label");
      var latitude = node.getAttribute("latitude");
      var longitude = node.getAttribute("longitude");
      var street = this.getTagNodeValue(node, "street");
      var suite = this.getTagNodeValue(node, "suite");
      var city = this.getTagNodeValue(node, "city");
      var county = this.getTagNodeValue(node, "county");
      var state = this.getTagNodeValue(node, "state");
      var phone = this.getTagNodeValue(node, "phone");
      var zip = this.getTagNodeValue(node, "zip");
      var school = this.getTagNodeValue(node, "schooldistrict");
      var drivingdirections = this.getTagNodeValue(node, "drivingdirections");

      var number = i + 1
      innerHTML = innerHTML + number + ". " + label;
      var address = "";
      var directions = "";
      
      if (drivingdirections) { directions = drivingdirections }
      
      
      //address+="<a href='../community/?cid=" + cid + "'>" + label + "</a>";
      //if (street != "") { address += street; }
      //if (suite) { address += "<br />" + suite; }
      //if (cid) { address += "<br />" + cid;}
      if (city) { address += "<b>City:</b> " + city; }
      if (state) { address += ", " + state; }
      //if (county) { address +="<br /><b>County:</b> " + county; }
      if (school) { address +="<br /><b>School District:</b> " + school; }
      //if (zip) { address += " " + zip; }
      address += "<br />";
      //if (phone) { address += phone + "<br />"; }
      
      
      innerHTML += address + "<br />";

      var icon = new GIcon(this.baseIcon);
      if (number <= this.maxIconNumber) {
        icon.image = this.iconUrl + "icon" + this.color + number + ".png";
      }
      else {
        icon.image = this.iconUrl + "icon" + this.color + ".png";
      }
      var marker = this.createMarker09(label, cid, longitude, latitude, icon, address, directions, 'community', '');
      map.addOverlay(marker);
    }

    if (this.labels) {
      this.labels.innerHTML = innerHTML;
    }
    
    
    //EXTRAS
    if (this.divId == "4"){
        var icon = new GIcon(this.dcIcon);
        icon.image = this.iconUrl + "casetta_base.png";
      
        var marker = this.createMarker09('Home Center', cid, '-82.95047879219055', '40.13990496089712', icon, 'Greg Szott<br />Office: 614-891-8545<br />Cell Phone: 614-208-8287', 'The Westport Home Center is located at our corporate office in Westerville, just south of Polaris Parkway off of Cleveland Ave.', 'standard', '../Columbus-Home-Center/');
        map.addOverlay(marker);
    }
    
    
  },
  

  
  
  createMarker09: function(label, cid, longitude, latitude, icon, address, directions, linkType, link)
  {
    var point = new GLatLng(latitude, longitude);
    var marker = new GMarker(point, icon);
    //var text = "<b><a style=\"text-decoration:none;font-size:14px;color:#850707;\" href=\"../community/?cid=" + cid + "\">" + label + " <img src=\"../images/Maps/arrow.gif\" /></a></b>";
    var text = "";
    if (linkType == 'community'){
        text = "<div style=\"width:250px;\"><b><a style=\"text-decoration:none;font-size:14px;color:#850707;\" href=\"../community/?cid=" + cid + "\">" + label + " <img src=\"../images/Maps/arrow_right.png\" /></a></b></div>";
    } else if (linkType == 'standard') {
        text = "<div style=\"width:250px;\"><b><a style=\"text-decoration:none;font-size:14px;color:#850707;\" href=\"" + link + "\">" + label + " <img src=\"../images/Maps/arrow_right.png\" /></a></b></div>";
    }
    
    var dirtext ="<div style=\"width:250px;\">" + directions + "<br/ ><br />"
    dirtext+= "</div>"
    
    if (address != "") { text += "<br />" + address; }
    
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowTabsHtml([new GInfoWindowTab("Info",text), new GInfoWindowTab("Directions",dirtext)]);
      //marker.openInfoWindowHtml(text);
    });
    this.markers[this.markers.length] = marker;
    return marker;
  },
  getTagNodeValue: function(node, tagName)
  {
      var value = "";
      var tag = node.getElementsByTagName(tagName)[0];
      if (tag && tag.firstChild) {
        value = tag.firstChild.nodeValue;
      }
      return value;
  },
  resetmap: function(map, xmlDoc)
  {
    var innerHTML = "";
    var cLat, cLong, zoomLevel;
    cLat = root.getAttribute("latitude");
    cLong = root.getAttribute("longitude");
    zoomLevel = parseInt(root.getAttribute("zoomlevel"));
    var centerPoint = new GLatLng(cLat, cLong);
    map.setCenter(centerPoint, zoomLevel);
    },
  enableDebug: function() { alert('Function not supported: enableDebug'); }
});

