$(function() {

function countySelectCallback(county_url) {
  return function() {
   countySelect(county_url);
  };
};

function countySelect(county_url) {
  window.location = county_url;
}

if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("quilt_trail_map"));
      map.setCenter(new GLatLng(37.499920931273685, -84.78973388671875), 9);
      map.addControl(new GSmallMapControl());

      var counties = [];      

      // iterate through each county
      for (var i = 0; i <= county_data.length - 1; i++)
      {

       counties.push(county_data[i].polylines.info.countyname.toLowerCase());
      
        // iterate through each polyline
        var count = county_data[i].polylines.info.count;
        for (var j = 1; j <= count; j++)
        {
          var key = "polyline" + j;
          var polylines = county_data[i].polylines[key];
          var points = [];

          // iterate through each polyline's point
          for (var p = 0; p < polylines.length; p++)
          {
            points.push( new GLatLng(parseFloat(polylines[p].lat), parseFloat(polylines[p].lng)) );
          }

          // add the points to the map
          var county_polygon = new GPolygon(points, '#ff0000', 2, 1, '#33FF00', .2);
          GEvent.addListener(county_polygon, 'click', countySelectCallback("/trail/" + counties.pop()));
          map.addOverlay(county_polygon); 

        }// end county polyline loop

      }// end county loop

    }// end GBrowserIsCompatible
}); // end jQuery DOM ready.

