/** 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.locationHTML= [];
    this.VERSION = '0.9.0';
    this.initialized = false;
    this.mapDocument = "";
    this.maxIconNumber = 25;
    this.baseIcon = new GIcon();
    this.markers = new Array();
    this.baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    this.baseIcon.iconSize = new GSize(20, 34);
    this.baseIcon.shadowSize = new GSize(37, 34);
    this.baseIcon.iconAnchor = new GPoint(9, 34);
    this.baseIcon.infoWindowAnchor = new GPoint(9, 2);
    this.baseIcon.infoShadowAnchor = new GPoint(18, 25);
    this.baseIcon2 = new GIcon();
    this.baseIcon2.shadow = "http://www.google.com/mapfiles/shadow50.png";
    this.baseIcon2.iconSize = new GSize(15, 24);
    this.baseIcon2.shadowSize = new GSize(28, 24);
    this.baseIcon2.iconAnchor = new GPoint(7, 17);
    this.baseIcon2.infoWindowAnchor = new GPoint(7, 5);
    this.baseIcon2.infoShadowAnchor = new GPoint(7, 25);
    this.iconUrl = "/maps/icons/";
    this.RED = "r";
    this.GREEN = "g";
    this.BLUE = "b";
    this.color = this.GREEN;
    this.maps = new Array();
  },
  // 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 {
      alert("Color not supported: " + c);
    }
  },
  setIconUrl: function(url) {
    this.iconUrl = url;
  },
  plot: function(mapId, labelsId, feedName) {
    if (!this.initialized) {
      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 GLargeMapControl());
          map.addControl(new GMapTypeControl());
          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 = "<table style=\"padding: 0px; margin: 0px;\" cellspacing=\"10\" cellpadding=\"0\" border=\"0\" width=\"775\"><tr>";
    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);
	var firstMarker;
	var tempHTML;


    var locations = xmlDoc.getElementsByTagName("location");
    for (var i=0;i<locations.length;i++) {
	
      var node = locations[i];
      var label = node.getAttribute("label");
      var latitude = node.getAttribute("latitude");
      var longitude = node.getAttribute("longitude");
      var type = this.getTagNodeValue(node, "type");
      var street = this.getTagNodeValue(node, "street");
      var suite = this.getTagNodeValue(node, "suite");
      var city = this.getTagNodeValue(node, "city");
      var state = this.getTagNodeValue(node, "state");
      var phone = this.getTagNodeValue(node, "phone");
      var zip = this.getTagNodeValue(node, "zip");
	var satellite = this.getTagNodeValue(node, "satellite");
	tempHTML= "";

      var number = i + 1

	if (((i % 4) == 0) && (i !== 0)) {
		innerHTML += "</tr><tr>";
	}



if (satellite == "no") {
	innerHTML = innerHTML + "<td valign=\"top\"><a href=\"javascript:plotter.markers["+i+"].openInfoWindowHtml(plotter.locationHTML["+i+"]);\"><img align='bottom' style='border: none; margin-right: 5px;' src='"+this.iconUrl + "icon" + this.color + number + ".png"+"'>";

tempHTML += number + ". " + label + "</a><br />";

	if (type != "") { tempHTML += "<strong>"+type+"</strong><br />"; }
      var address = "";
      if (street != "") { address += street; }
      if (suite) { address += "<br />" + suite; }
      if (city) { address += "<br />" + city; }
      if (state) { address += ", " + state; }
      if (zip) { address += " " + zip; }
      address += "<br />";
      if (phone) { address += phone + "<br />"; }
      //innerHTML += address + "<br />";

tempHTML += address;

	//this.locationHTML.push(tempHTML);

	innerHTML += tempHTML + "</td>";

	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(("<strong>" + number + ". " + label + "</strong>"), longitude, latitude, icon, address);
	map.addOverlay(marker);
} else {
	innerHTML = innerHTML + "<td valign=\"top\"><a href=\"javascript:plotter.markers["+i+"].openInfoWindowHtml(plotter.locationHTML["+i+"]);\"><img align='bottom' style='border: none; margin-right: 5px;' src='"+this.iconUrl + "icon" + this.GREEN + number + ".png"+"'>";
	tempHTML += number + ". " + label + "</a>";
      
	if (type != "") { tempHTML += "<strong>"+type+"</strong><br />"; }
	var address = "";
      if (street != "") { address += street; }
	if (satellite != "") { address += " " }
      address += "<br />";
      if (phone) { address += phone + "<br />"; }
	tempHTML += address;

	//this.locationHTML.push(tempHTML);

	innerHTML += tempHTML + "</td>";

	var icon = new GIcon(this.baseIcon2);
      if (number <= this.maxIconNumber) {
        icon.image = this.iconUrl + "icon" + this.GREEN + number + ".png";
      }
      else {
        icon.image = this.iconUrl + "icon" + this.GREEN + ".png";
      }
      var marker = this.createMarker09((number + ". " + label), longitude, latitude, icon, address);
      map.addOverlay(marker);
}
      
    }
	innerHTML += "</tr></table>";

    if (this.labels) {
      this.labels.innerHTML = innerHTML;
    }
  },
  createMarker09: function(label, longitude, latitude, icon, address)
  {
	var tab1, tab2;
	var infoTabs;
	var dMapDiv;
	var detailmap;	
	var CopyrightDiv;
	var CopyrightImg;
	var locationHTMLArray= this.locationHTML;

    var point = new GLatLng(latitude, longitude);
    var marker = new GMarker(point, icon);
    var text = label;
	var markersLength= this.markers.length;
    if (address != "") { text += "<br />" + address; }
    GEvent.addListener(marker, "click", function() {
	tab1 = new GInfoWindowTab("Info", "<div id='tab1' class='bubble'>"+text+"</div>");
	tab2 = new GInfoWindowTab("More", "<div class='zoomedMap' id='detailmap"+(markersLength+1)+"'></div>");
	infoTabs = [tab1,tab2];
	marker.openInfoWindowTabsHtml(infoTabs);
	dMapDiv = document.getElementById("detailmap"+(markersLength+1));
	detailmap = new GMap2(dMapDiv);
	detailmap.setCenter(point, 17, G_SATELLITE_TYPE);
	CopyrightDiv = dMapDiv.firstChild.nextSibling;
		CopyrightImg = dMapDiv.firstChild.nextSibling.nextSibling;
		CopyrightDiv.style.display = "none"; 
		CopyrightImg.style.display = "none";
    });
	locationHTMLArray.push(text);
    this.markers[this.markers.length] = marker;
    return marker;
  },

/*  OLD CODE */
/*
  createMarker09: function(label, longitude, latitude, icon, address)
  {
    var point = new GLatLng(latitude, longitude);
    var marker = new GMarker(point, icon);
    var text = label;
    if (address != "") { text += "<br />" + address; }

    GEvent.addListener(marker, "click", function() {
      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;
  },
  enableDebug: function() { alert('Function not supported: enableDebug'); }
});

