/* A class used to geolocate the bike shops */

var map;
var geocoder;

// All shops in JSON notation
// Use geocoder.us to get the lat/long from the address.
var allShops = { "shops" : [			    
  { "id": "abs",
    "name": "Another Bike Shop", 
    "address": "2361 Mission Street Santa Cruz, CA", 
    "url": "http://www.anotherbikeshop.com/",
    "coord": "36.961199,-122.04756" },

  { "id": "fcc",
    "name": "Family Cycling Center", 
    "address": "914 41st Avenue, Santa Cruz, CA", 
    "url": "http://www.familycycling.com/",
    "coord": "36.964698,-121.964868" },

  { "id": "svcs",
    "name": "Scotts Valley Cycle Sport", 
    "address": "245-J Mt. Hermon Road, Scotts Valley, CA", 
    "url": "http://www.svcyclesport.com/",
    "coord": "37.046067,-122.028544" },

  { "id": "spokes",
    "name": "The Spokesman Bicycles", 
    "address": "231 Cathcart Street, Santa Cruz, CA", 
    "url": "http://www.spokesmanbicycles.com/",
    "coord": "36.971623,-122.025872" },

  { "id": "epicenter",
    "name": "Epicenter Cycling", 
    "address": "8035 Soquel Drive, Aptos, CA 95003", 
    "url": "http://epicentercycling.com",
    "coord": "36.976872,-121.901901" },

  { "id": "trip",
    "name": "The Bicycle Trip", 
    "address": "1001 Soquel Avenue, Santa Cruz, CA", 
    "url": "http://www.bicycletrip.com/",
    "coord": "36.978550,-122.013073" }
  ]
}

// An array of all the markers.
var allmarkers = [];

// Same method as in maps.js
function initMap() {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
}

function addHandler(id, index) {
  var element = document.getElementById(id);
  element.onclick = function() { showLocation(index); };
}

function createHtml(index) {
  var name = allShops.shops[index].name;
  var url = allShops.shops[index].url;
  var address = allShops.shops[index].address;

  var html = '<a href="' + url + '" target="_blank">' + '<strong>' + name + '</strong></a><br />' + address + '<br />';
  
  return html;
}

// Creates a marker with the info window
function createMarker(point, index) {
    var marker = new GMarker(point);

    //    var element = document.getElementById("desc" + allShops.shops[index].id);
    //    marker.bindInfoWindow(element);

    var html = createHtml(index);
   
    GEvent.addListener(marker, "click", function() {
			 map.panTo(marker.getPoint(), map.getZoom());
			 marker.openInfoWindowHtml(html);
		       });
    return marker;
}


// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
// XXX - currently deprecated
/*
function addAddressToMap(response) {
  //  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");

  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
    var marker = createMarker(point, currentIndex);
    map.addOverlay(marker);

    //marker.openInfoWindowHtml(html);
  }
}
*/

// showLocation() will present a popup marker and info window at the geo locations
// represented by the address.
function showLocation(index) {
  //  geocoder.getLocations(allShops.shops[index].address, addAddressToMap);
  var marker = allmarkers[index];

  map.panTo(marker.getPoint(), map.getZoom());

  // XXX don't really want to regen the html. Is there any way that we can just show it?
  marker.openInfoWindowHtml(createHtml(index));
}

// Entry point for when the page loads
// This will contain data specific code
window.onload = function() {
  if (GBrowserIsCompatible()) {
    initMap();

    map.setCenter(new GLatLng(36.974, -122.01), 13);
    geocoder = new GClientGeocoder();


    allmarkers.length = 0;
    // show all the shops on the map

    for (var i = 0; i < allShops.shops.length; i++) {
      // Break the point into lat and long
      var latlong = allShops.shops[i].coord.split(",");
      var marker = createMarker(new GLatLng(latlong[0], latlong[1]), i);

      map.addOverlay(marker);

      allmarkers.push(marker);

      addHandler(allShops.shops[i].id, i);
    }

    /*
    addHandler("abs", abs);
    addHandler("fcc", fcc);
    addHandler("svcs", svcs);
    addHandler("spokes", spokes);
    addHandler("trip", trip);
    addHandler("trip", trip); */

    //showLocation(abs);
  }
}

// Clean up
  window.onunload = function() {
    GUnload();
  }

