//----------------------------------------------------------------------
// BELOW IS CUSTOM JAVASCRIPT NOT PERTAINING TO REGION SEARCH
$(document).ready(function() {
   $(".eitem").click(function () { 
     if( $(this).find(".fullsummary").css('display') == 'none' ) {
         $(this).css('height',200);
         $(this).find(".shortsummary").css('display','none');
         $(this).find(".fullsummary").css('display','inline');
     } else {
         $(this).find(".fullsummary").css('display','none');
         $(this).find(".shortsummary").css('display','inline');
         $(this).css('height',100);
     }
   });
});
// END OF CUSTOM JAVASCRIPT
//----------------------------------------------------------------------
// Array of booleans. Which regions are selected
var regions_selected = new Array( );  // an array of booleans

// Google Maps "options" set look & feel
var selected_options = {  // for current selection
    fillOpacity: 0.7,
    fillColor: "#FFFF00"
};
 
var unselected_options = { // for NOT current selection
  fillOpacity: 0.3
};
 
// An asociative array that takes a color, such
// as "red". Sets up Google Maps "options" look & feel
var poly_options = {
    "red" : {
        strokeColor: "#D32B56",
        fillColor: "#D32B56"
    },
 
    "brown" : {
        strokeColor: "#79002B",
        fillColor: "#79002B"
    },
 
    "blue" : {
        strokeColor: "#625A96",
        fillColor: "#625A96"
    }
};

// Array of the set up Google Maps Polygon objects.
// May be unnecessary if I can get 'this' to work
// for polygon eventListener
var polys = new Array( );

// It's a bit awkward, but keeping a global variable
// of color schemes for each region is useful when
// reverting from "selected" to "unselected" region
var poly_colors = new Array(
"blue", // CT Fairfield
"blue", // CT Litchfield Hills
"blue", // CT Mystic Country
"blue", // CT New Haven
"blue", // CT River Valley
"red", // ME Aroostook
"red", // ME Downeast & Arcadia
"red", // ME Kennebec & Moose River
"red", // ME Lakes & Mountains
"red", // ME Maine Beaches
"red", // ME Maine Highlands
"red", // ME Midcoast Maine
"red", // ME Portland
"brown", // RI Rhode Island
"brown", // VT Central VT
"brown", // VT Crossroads of VT
"brown", // VT Eastern VT
"brown", // VT Green Mountains
"brown", // VT Islands & Farms
"brown", // VT Lake Champlain
"brown", // VT Mid Vermont
"brown", // VT Northeast Kingdom
"brown", // VT Southern VT
"brown", // VT Stowe
"brown", // VT Southern Windsor
"blue", // NH Dartmouth-Lake Sunapee
"blue", // NH Great North Woods
"blue", // NH Lakes Region
"blue", // NH Merrimack Valley
"blue", // NH Monadnock
"blue", // NH Seacoast
"blue", // NH White Mountains
"red", // MA Berkshires
"red", // MA Boston Metro
"red", // MA Cape Cod
"red", // MA Lowell
"red", // MA Metro West
"red", // MA North Shore
"red", // MA Northern Mass
"red", // MA South Shore
"red", // MA Southeast Mass
"red", // MA Springfield
"red" // MA Worcester
  );

// And here's an array of region names to print
var region_names = new Array(
   "Fairfield, CT",
   "Litchfield Hills, CT",
   "Mystic Country, CT",
   "New Haven, CT",
   "River Valley, CT",
   "Aroostook, ME",
   "Downeast Maine & Arcadia",
   "Kennebec & Moose River, ME",
   "Maine Lakes & Mountains",
   "Maine Beaches",
   "Maine Highlands",
   "Midcoast Maine",
   "Portland",
   "Rhode Island",
   "Central Vermont",
   "Crossroads of Vermont",
   "Eastern Vermont",
   "Green Mountains, VT",
   "Islands & Farms, VT",
   "Lake Champlain, VT",
   "Mid Vermont",
   "Northeast Kingdom, VT",
   "Southern Vermont",
   "Stowe, VT",
   "Southern Windsor, VT",
   "Dartmouth-Lake Sunapee, NH",
   "Great North Woods, NH",
   "Lakes Region, NH",
   "Merrimack Valley, NH",
   "Monadnock, NH",
   "Seacoast, NH",
   "White Mountains, NH",
   "Berkshires, MA",
   "Boston Metro",
   "Cape Cod, MA",
   "Lowell, MA",
   "Boston Metro West",
   "MA North Shore",
   "Northwest Mass",
   "MA South Shore",
   "Southeast Mass",
   "Springfield, MA",
   "Worcester, MA"
  );

// And here's an array of region names to print
var region_classes = new Array(
"Fairfield",
"Litchfield_Hills",
"Mystic_Country",
"New_Haven",
"River_Valley",
"Aroostook",
"Downeast_Arcadia",
"Kennebec_Moose_River",
"ME_Lakes_Mountains",
"Maine_Beaches",
"Maine_Highlands",
"Midcoast_Maine",
"Portland",
"Rhode_Island",
"Central_VT",
"Crossroads_of_VT",
"Eastern_VT",
"Green_Mountains",
"Islands_Farms",
"Lake_Champlain",
"Mid_Vermont",
"Northeast_Kingdom",
"Southern_VT",
"Stowe",
"Southern_Windsor",
"Dartmouth_Lake_Sunapee",
"Great_North_Woods",
"Lakes_Region",
"Merrimack_Valley",
"Monadnock",
"Seacoast",
"White_Mountains",
"Berkshires",
"Boston_Metro",
"Cape_Cod",
"Lowell",
"Metro_West",
"North_Shore",
"Northwest_Mass",
"South_Shore",
"Southeast_Mass",
"Springfield",
"Worcester"
  );

// List the IDs of the neighboring regions for each ID
var region_neighbors = new Array (
  [1, 3],                            // Fairfield, CT
  [0, 3, 4, 32],                     // Litchfield Hills, CT
  [4, 41, 42, 13],                   // Mystic Country, CT
  [0, 1, 4],                         // New Haven, CT
  [3, 1, 32, 41, 2],                 // River Valley, CT
  [7, 10, 6],                        // Aroostook, ME
  [5, 10, 11],                       // Downeast Maine & Arcadia
  [8, 11, 10, 5],                    // Kennebec & Moose River, ME
  [26, 31, 9, 12, 11, 7],            // Maine Lakes & Mountains
  [30, 27, 8, 12],                   // Maine Beaches
  [5, 6, 11, 7],                     // Maine Highlands
  [12, 8, 7, 10, 6],                 // Midcoast Maine
  [9, 8, 7, 11],                     // Greater Portland
  [2, 42, 40],                       // Rhode Island
  [20, 19, 23, 21, 16],              // Central Vermont
  [20, 16, 24, 22, 17],              // Crossroads of Vermont
  [24, 15, 20, 14, 21, 31, 25],      // Eastern Vermont
  [32, 22, 15],                      // Green Mountains, VT
  [19, 23, 21],                      // Islands & Farms, VT
  [18, 23, 14, 20],                  // Lake Champlain, VT
  [15, 16, 14, 19],                  // Mid Vermont
  [14, 23, 18, 26, 16],              // Northeast Kingdom, VT
  [17, 15, 24, 29, 25, 38, 32],      // Southern Vermont
  [14, 19, 18, 21],                  // Stowe, VT
  [22, 15, 16, 25],                  // Southern Windsor, VT
  [29, 22, 24, 16, 31, 27, 28],      // Dartmouth-Lake Sunapee, NH
  [21, 31, 8],                       // Great North Woods, NH
  [28, 25, 31, 9, 30],               // Lakes Region, NH
  [29, 25, 27, 30, 35, 38],          // Merrimack Valley, NH
  [38, 22, 25, 28],                  // Monadnock, NH
  [37, 35, 28, 27, 9],               // Seacoast, NH
  [16, 26, 8, 27, 25],               // White Mountains, NH
  [17, 22, 41, 38, 4, 1],            // Berkshires, MA
  [37, 35, 36, 39],                  // Boston Metro
  [39, 40],                          // Cape Cod, MA
  [36, 38, 28, 30, 37, 33],          // Lowell, MA
  [40, 42, 38, 35, 37, 33, 39],      // Boston Metro West
  [33, 35, 30],                      // MA North Shore
  [32, 22, 29, 41, 42, 36, 35, 28],  // Northern Mass
  [34, 40, 36, 33],                  // MA South Shore
  [34, 39, 13, 36],                  // Southeast Mass
  [4, 32, 38, 42, 2],                // Springfield, MA
  [2, 41, 38, 36, 13]                // Worcester, MA
);

//----------------------------------------------------------------------
// Sets up all regions by defining polygons and their colors.
function initialize_region_search() {

  var myOptions = {
    zoom: 7,             // zoom in
    minZoom: 5,             // don't zoom too far out
    maxZoom: 11,             // don't zoom too close in
    center: new google.maps.LatLng(42.351,-71.496), // Center on Boston
    mapTypeControl: false,    // Don't allow change to satellite mode
    streetViewControl: false,  // Don't allow going into street mode
    mapTypeId: google.maps.MapTypeId.TERRAIN  // the least cluttered
  };

  var map_element = document.getElementById("region-search");
  var map = new google.maps.Map(map_element,
      myOptions);

  // The points of all regional polygons are named for nearby towns
  // Via experimentation I detemined that 3 decimal points is enough.
  var city_coordinates = new Array(
"CT Belle_Haven",40.967,-73.632,
"CT Bridgeport",41.145,-73.155,
"CT Colchester",41.568,-72.348,
"CT Georgetown",41.216,-73.488,
"CT Higganum",41.499,-72.563,
"CT Mansfield",41.837,-72.282,
"CT Meriden",41.551,-72.768,
"CT New_Haven",41.224,-72.938,
"CT New_London",41.270,-72.318,
"CT Ridgefield",41.290,-73.556,
"CT Stanwich",41.100,-73.729,
"CT Thompson",42.017,-71.800,
"CT Trumbull",41.233,-73.139,
"CT Trumbull2",41.313,-73.254,
"CT Waterbury",41.564,-73.015,
"CT Westbrook_Center",41.283,-72.437,
"MA Barre",42.466,-72.128,
"MA Brockton",42.057,-71.065,
"MA Burlington",42.507,-71.244,
"MA Canton",42.198,-71.086,
"MA Chatham",41.646,-69.916,
"MA Dedham",42.230,-71.207,
"MA Fall_River",41.652,-71.136,
"MA Haverhill",42.839,-71.080,
"MA Hudson",42.397,-71.618,
"MA Hull",42.317,-70.881,
"MA Littleton",42.549,-71.485,
"MA Marthas_Vinyard",41.330,-70.852,
"MA Marthas_Vinyard2",41.483,-70.589,
"MA Marthas_Vinyard3",41.332,-70.442,
"MA Methuen",42.739,-71.216,
"MA Mt_Washington",42.054,-73.492,
"MA Nantucket",41.287,-70.234,
"MA Nantucket2",41.357,-70.017,
"MA Nantucket3",41.239,-69.944,
"MA Newton",42.345,-71.275,
"MA North_Attleboro",42.017,-71.384,
"MA Orleans",41.795,-70.031,
"MA Pepperell",42.704,-71.624,
"MA Provincetown",42.027,-70.108,
"MA Provincetown2",42.013,-70.207,
"MA Provincetown3",42.073,-70.247,
"MA Provincetown4",42.026,-70.027,
"MA Quincy",42.261,-70.946,
"MA Revere",42.423,-70.981,
"MA Rockport",42.640,-70.549,
"MA Rowley",42.738,-70.782,
"MA Sagamore",41.808,-70.477,
"MA Salisbury",42.886,-70.816,
"MA Scituate",42.253,-70.741,
"MA Shelburne",42.736,-72.819,
"MA South_Deerfield",42.511,-72.713,
"MA Southbridge",42.033,-72.120,
"MA Stoneham",42.509,-71.093,
"MA Wareham",41.743,-70.711,
"MA Westfield",42.039,-72.875,
"MA Westhampton",42.304,-72.786,
"MA Williamstown",42.745,-73.265,
"MA Woods_Hole",41.495,-70.697,
"ME Amherst",44.795,-68.434,
"ME Bath",43.933,-69.861,
"ME Belfast",44.302,-68.835,
"ME Casco_Bay",43.696,-69.927,
"ME Clayton_Lake",46.578,-70.014,
"ME Clayton_Lake2",46.576,-69.706,
"ME Dexter",45.073,-69.424,
"ME East_Millinocket",45.603,-68.427,
"ME Eastport",44.745,-66.901,
"ME Edmunston",47.368,-68.286,
"ME Eustis",45.188,-70.431,
"ME Freeport",43.947,-70.068,
"ME Gorham",43.670,-70.435,
"ME Grand_Lake",45.677,-67.791,
"ME Grand_Lake2",45.671,-68.026,
"ME Greene",44.135,-70.041,
"ME Hampden",44.698,-68.835,
"ME Harmony",45.029,-69.678,
"ME Isle_Au_Haut",43.973,-68.843,
"ME Lac_Frontiere",46.697,-69.995,
"ME Livermore_Falls",44.479,-70.288,
"ME Lowelltown",45.649,-70.573,
"ME New_Vinyard",44.794,-70.093,
"ME NorthME",47.450,-69.219,
"ME NorthME2",47.057,-67.797,
"ME Old_Orchard_Beach",43.511,-70.306,
"ME Oxbow",46.392,-68.483,
"ME Oxbow2",46.566,-68.835,
"ME Pittsfield",44.752,-69.427,
"ME Vanceborg",45.595,-67.437,
"ME West_Grand_Lake",45.275,-67.953,
"NH Bellows_Falls",43.184,-72.450,
"NH Berwick",43.262,-70.863,
"NH Campton",43.822,-71.658,
"NH CardiganPark",43.564,-71.908,
"NH Chester",42.925,-71.265,
"NH Conway",43.871,-70.991,
"NH Dover",43.196,-70.922,
"NH Franklin",43.378,-71.649,
"NH Gorham",44.437,-71.020,
"NH Henniker",43.177,-71.823,
"NH Lancaster",44.493,-71.603,
"NH NorthWoods",45.296,-71.350,
"NH NorthWoods2",45.301,-71.086,
"NH Orford",43.922,-72.155,
"NH Orford",43.925,-72.154,
"NH Portsmouth",43.047,-70.703,
"NH Rochester",43.319,-70.961,
"NH Salem",42.793,-71.187,
"NY Whitehall",43.580,-73.431,
"RI East_Providence",41.818,-71.353,
"RI Newport",41.469,-71.120,
"RI Westerly",41.298,-71.814,
"VT Alburgh",44.998,-73.352,
"VT Barnet",44.309,-72.050,
"VT Brattleboro",42.730,-72.571,
"VT Bridgewater_Center",43.610,-72.662,
"VT Burlington",44.347,-73.328,
"VT Burlington2",44.365,-73.015,
"VT Chester_Chester",43.185,-72.679,
"VT Fair_Haven",43.615,-73.303,
"VT Granville",43.317,-73.257,
"VT GreenMtn",43.299,-72.839,
"VT GreenMtn2",43.202,-72.852,
"VT Greensboro",44.576,-72.296,
"VT Hereford",45.012,-71.500,
"VT Jeffersonville",44.766,-72.826,
"VT Ludlow",43.421,-72.713,
"VT Manchester_Center",43.197,-73.021,
"VT Marshfield",44.314,-72.265,
"VT Middlebury",43.989,-73.413,
"VT Middlebury2",44.008,-72.886,
"VT Middlebury3",44.008,-72.968,
"VT Milton",44.756,-73.350,
"VT Montgomery_Center",44.876,-72.610,
"VT Montpelier",44.200,-72.743,
"VT Newbury",44.079,-72.058,
"VT Newport",44.877,-72.477,
"VT North_Hartland",43.606,-72.352,
"VT Richford",45.000,-72.620,
"VT St_Johnsbury",44.403,-72.173,
"VT St_Johnsbury2",44.547,-72.268,
"VT Stamford",42.742,-73.062,
"VT Stowe",44.492,-72.848,
"VT Stowe2",44.536,-72.287
   );

  // Create temporary array of Google lat/long objects
  var r = new Array( );
  for(var i=0; i < city_coordinates.length; i += 3) {
      r[ city_coordinates[ i ] ]
          = new google.maps.LatLng( city_coordinates[ i + 1 ],
                                    city_coordinates[ i + 2 ] );
  }

  // Google polygon paths are arrays of Lat/Lng objects, except
  // Cape Cod which is a double array (i.e. multiple polys) 
  // This has been carefully ordered to match the IDs expected
  // on the server side through the Javascript search algorithm
  var poly_paths = new Array(
    [ r["CT Georgetown"], r["CT Trumbull2"], r["CT Trumbull"], r["CT Bridgeport"], r["CT Belle_Haven"], r["CT Stanwich"] ], // CT Fairfield
    [ r["MA Mt_Washington"], r["MA Westfield"], r["CT Waterbury"], r["CT Trumbull"], r["CT Trumbull2"], r["CT Georgetown"], r["CT Ridgefield"] ], // CT Litchfield Hills
    [ r["MA Southbridge"], r["CT Thompson"], r["RI Westerly"], r["CT New_London"], r["CT Colchester"], r["CT Mansfield"] ], // CT Mystic Country
    [ r["CT Bridgeport"], r["CT Trumbull"], r["CT Waterbury"], r["CT Meriden"], r["CT Higganum"], r["CT Westbrook_Center"], r["CT New_Haven"] ], // CT New Haven
    [ r["MA Westfield"], r["MA Southbridge"], r["CT Mansfield"], r["CT Colchester"], r["CT New_London"], r["CT Westbrook_Center"], r["CT Higganum"], r["CT Meriden"], r["CT Waterbury"] ], // CT River Valley
    [ r["ME Lac_Frontiere"], r["ME NorthME"], r["ME Edmunston"], r["ME NorthME2"], r["ME Grand_Lake"], r["ME Grand_Lake2"], r["ME East_Millinocket"], r["ME Oxbow"], r["ME Oxbow2"], r["ME Clayton_Lake2"], r["ME Clayton_Lake"] ], // ME Aroostook
    [ r["ME Grand_Lake2"], r["ME Grand_Lake"], r["ME Vanceborg"], r["ME Eastport"], r["ME Isle_Au_Haut"], r["ME Belfast"], r["ME Hampden"], r["ME Amherst"], r["ME West_Grand_Lake"] ], // ME Downeast & Arcadia
    [ r["ME Lowelltown"], r["ME Clayton_Lake"], r["ME Clayton_Lake2"], r["ME Harmony"], r["ME Dexter"], r["ME Pittsfield"], r["ME Greene"], r["ME Livermore_Falls"], r["ME New_Vinyard"], r["ME Eustis"] ], // ME Kennebec & Moose River
    [ r["NH NorthWoods2"], r["ME Lowelltown"], r["ME Eustis"], r["ME New_Vinyard"], r["ME Livermore_Falls"], r["ME Greene"], r["ME Freeport"], r["ME Gorham"], r["NH Conway"] ], // ME Lakes & Mountains
    [ r["NH Conway"], r["ME Gorham"], r["ME Old_Orchard_Beach"], r["NH Portsmouth"], r["NH Berwick"], r["NH Rochester"] ], // ME Maine Beaches
    [ r["ME Clayton_Lake2"], r["ME Oxbow2"], r["ME Oxbow"], r["ME East_Millinocket"], r["ME Grand_Lake2"], r["ME West_Grand_Lake"], r["ME Amherst"], r["ME Hampden"], r["ME Pittsfield"], r["ME Dexter"], r["ME Harmony"] ], // ME Maine Highlands
    [ r["ME Greene"], r["ME Pittsfield"], r["ME Hampden"], r["ME Belfast"], r["ME Isle_Au_Haut"], r["ME Casco_Bay"], r["ME Bath"], r["ME Freeport"] ], // ME Midcoast Maine
    [ r["ME Gorham"], r["ME Freeport"], r["ME Bath"], r["ME Casco_Bay"], r["ME Old_Orchard_Beach"] ], // ME Portland
    [ r["CT Thompson"], r["MA North_Attleboro"], r["RI East_Providence"], r["MA Fall_River"], r["RI Newport"], r["RI Westerly"] ], // RI Rhode Island
    [ r["VT Stowe"], r["VT Greensboro"], r["VT St_Johnsbury"], r["VT Marshfield"], r["VT Montpelier"], r["VT Burlington2"] ], // VT Central VT
    [ r["VT Middlebury"], r["VT Middlebury3"], r["VT Middlebury2"], r["VT Bridgewater_Center"], r["VT Ludlow"], r["VT GreenMtn"], r["VT Granville"], r["VT Fair_Haven"], r["NY Whitehall"] ], // VT Crossroads of VT
    [ r["VT Middlebury3"], r["VT Montpelier"], r["VT Marshfield"], r["VT Barnet"], r["VT Newbury"], r["VT North_Hartland"], r["VT Bridgewater_Center"], r["VT Middlebury2"] ], // VT Eastern VT
    [ r["VT Granville"], r["VT GreenMtn"], r["VT GreenMtn2"], r["VT Manchester_Center"], r["VT Stamford"], r["MA Williamstown"] ], // VT Green Mountains
    [ r["VT Alburgh"], r["VT Richford"], r["VT Montgomery_Center"], r["VT Jeffersonville"], r["VT Milton"] ], // VT Islands & Farms
    [ r["VT Milton"], r["VT Jeffersonville"], r["VT Stowe"],  r["VT Burlington2"], r["VT Burlington"] ], // VT Lake Champlain
    [ r["VT Burlington"], r["VT Burlington2"], r["VT Montpelier"], r["VT Middlebury3"], r["VT Middlebury"] ], // VT Mid Vermont
    [ r["VT Richford"], r["VT Hereford"], r["NH Lancaster"], r["VT Barnet"],  r["VT Marshfield"], r["VT St_Johnsbury"],  r["VT Greensboro"], r["VT Newport"], r["VT Montgomery_Center"] ], // VT Northeast Kingdom
    [ r["VT Manchester_Center"], r["VT GreenMtn2"], r["VT GreenMtn"],  r["VT Ludlow"], r["VT Chester_Chester"], r["NH Bellows_Falls"],  r["VT Brattleboro"], r["VT Stamford"] ], // VT Southern VT
    [ r["VT Jeffersonville"], r["VT Montgomery_Center"], r["VT Newport"], r["VT Greensboro"], r["VT Stowe"] ], // VT Stowe
    [ r["VT Bridgewater_Center"], r["VT North_Hartland"], r["NH Bellows_Falls"],  r["VT Chester_Chester"], r["VT Ludlow"] ], // VT Southern Windsor
    [ r["NH Orford"], r["NH Campton"], r["NH CardiganPark"], r["NH Franklin"], r["NH Henniker"], r["NH Bellows_Falls"], r["VT North_Hartland"] ], // NH Dartmouth-Lake Sunapee
    [ r["VT Hereford"], r["NH NorthWoods"], r["NH NorthWoods2"], r["NH Gorham"], r["VT Barnet"], r["NH Lancaster"] ], // NH Great North Woods
    [ r["NH Campton"], r["NH Conway"], r["NH Rochester"], r["NH Berwick"], r["NH Dover"], r["NH Franklin"], r["NH CardiganPark"] ], // NH Lakes Region
    [ r["NH Henniker"], r["NH Franklin"], r["NH Dover"], r["NH Chester"], r["NH Salem"], r["MA Methuen"], r["MA Pepperell"] ], // NH Merrimack Valley
    [ r["NH Bellows_Falls"], r["NH Henniker"], r["MA Pepperell"], r["VT Brattleboro"] ], // NH Monadnock
    [ r["NH Dover"], r["NH Berwick"], r["NH Portsmouth"], r["MA Salisbury"], r["MA Haverhill"], r["NH Salem"], r["NH Chester"] ], // NH Seacoast
    [ r["VT Barnet"], r["NH Gorham"], r["NH Conway"], r["NH Campton"], r["NH Orford"], r["VT Newbury"] ], // NH White Mountains
    [ r["MA Williamstown"], r["MA Shelburne"], r["MA South_Deerfield"], r["MA Westhampton"], r["MA Westfield"], r["MA Mt_Washington"] ], // MA Berkshires
    [ r["MA Revere"], r["MA Quincy"], r["MA Canton"], r["MA Dedham"], r["MA Newton"], r["MA Burlington"], r["MA Stoneham"] ], // MA Boston Metro
    [[ r["MA Sagamore"], r["MA Orleans"], r["MA Provincetown"], r["MA Provincetown2"], r["MA Provincetown3"], r["MA Provincetown4"], r["MA Chatham"], r["MA Woods_Hole"], r["MA Wareham"] ], [ r["MA Marthas_Vinyard"], r["MA Marthas_Vinyard2"], r["MA Marthas_Vinyard3"]], [r["MA Nantucket"], r["MA Nantucket2"], r["MA Nantucket3"]]], // MA Cape Cod
    [ r["MA Methuen"], r["NH Salem"], r["MA Haverhill"], r["MA Burlington"], r["MA Littleton"], r["MA Pepperell"] ], // MA Lowell
    [ r["MA Canton"], r["MA Brockton"], r["MA North_Attleboro"], r["MA Hudson"], r["MA Littleton"], r["MA Burlington"], r["MA Newton"], r["MA Dedham"] ], // MA Metro West
    [ r["MA Haverhill"], r["MA Salisbury"], r["MA Rowley"], r["MA Rockport"], r["MA Revere"], r["MA Stoneham"], r["MA Burlington"] ], // MA North Shore
    [ r["MA Shelburne"], r["MA Pepperell"], r["MA Littleton"], r["MA Hudson"], r["MA Barre"], r["MA South_Deerfield"] ], // MA Northern Mass
    [ r["MA Quincy"], r["MA Hull"], r["MA Scituate"], r["MA Sagamore"], r["MA Wareham"], r["MA Brockton"], r["MA Canton"] ], // MA South Shore
    [ r["MA Brockton"], r["MA Wareham"], r["RI Newport"], r["MA Fall_River"], r["RI East_Providence"], r["MA North_Attleboro"] ], // MA Southeast Mass
    [ r["MA South_Deerfield"], r["MA Barre"], r["MA Southbridge"], r["MA Westfield"], r["MA Westhampton"] ], // MA Springfield
    [ r["MA Barre"], r["MA Hudson"], r["MA North_Attleboro"], r["CT Thompson"], r["MA Southbridge"] ] // MA Worcester
  );

  // Go through all the polygon paths and actually make the polys
  for(var i=0; i < poly_paths.length; i++) {
       polys[i] = new google.maps.Polygon({
         paths: poly_paths[i],
         strokeOpacity: 0.8,
         strokeWeight: 1.5,
         fillOpacity: 0.3
      });

      // Select the 'red', 'blue', or 'brown' coloration
      polys[i].setOptions( poly_options[ poly_colors[i] ] );
      polys[i].setMap( map );  // plug it into the Map object
  }

  // Could not find an iterative way to do this. Use 'this' ?
  google.maps.event.addListener(polys[0], 'click',
       function() { click_poly( 0 ) } );
  google.maps.event.addListener(polys[1], 'click',
       function() { click_poly( 1 ) } );
  google.maps.event.addListener(polys[2], 'click',
       function() { click_poly( 2 ) } );
  google.maps.event.addListener(polys[3], 'click',
       function() { click_poly( 3 ) } );
  google.maps.event.addListener(polys[4], 'click',
       function() { click_poly( 4 ) } );
  google.maps.event.addListener(polys[5], 'click',
       function() { click_poly( 5 ) } );
  google.maps.event.addListener(polys[6], 'click',
       function() { click_poly( 6 ) } );
  google.maps.event.addListener(polys[7], 'click',
       function() { click_poly( 7 ) } );
  google.maps.event.addListener(polys[8], 'click',
       function() { click_poly( 8 ) } );
  google.maps.event.addListener(polys[9], 'click',
       function() { click_poly( 9 ) } );
  google.maps.event.addListener(polys[10], 'click',
       function() { click_poly( 10 ) } );
  google.maps.event.addListener(polys[11], 'click',
       function() { click_poly( 11 ) } );
  google.maps.event.addListener(polys[12], 'click',
       function() { click_poly( 12 ) } );
  google.maps.event.addListener(polys[13], 'click',
       function() { click_poly( 13 ) } );
  google.maps.event.addListener(polys[14], 'click',
       function() { click_poly( 14 ) } );
  google.maps.event.addListener(polys[15], 'click',
       function() { click_poly( 15 ) } );
  google.maps.event.addListener(polys[16], 'click',
       function() { click_poly( 16 ) } );
  google.maps.event.addListener(polys[17], 'click',
       function() { click_poly( 17 ) } );
  google.maps.event.addListener(polys[18], 'click',
       function() { click_poly( 18 ) } );
  google.maps.event.addListener(polys[19], 'click',
       function() { click_poly( 19 ) } );
  google.maps.event.addListener(polys[20], 'click',
       function() { click_poly( 20 ) } );
  google.maps.event.addListener(polys[21], 'click',
       function() { click_poly( 21 ) } );
  google.maps.event.addListener(polys[22], 'click',
       function() { click_poly( 22 ) } );
  google.maps.event.addListener(polys[23], 'click',
       function() { click_poly( 23 ) } );
  google.maps.event.addListener(polys[24], 'click',
       function() { click_poly( 24 ) } );
  google.maps.event.addListener(polys[25], 'click',
       function() { click_poly( 25 ) } );
  google.maps.event.addListener(polys[26], 'click',
       function() { click_poly( 26 ) } );
  google.maps.event.addListener(polys[27], 'click',
       function() { click_poly( 27 ) } );
  google.maps.event.addListener(polys[28], 'click',
       function() { click_poly( 28 ) } );
  google.maps.event.addListener(polys[29], 'click',
       function() { click_poly( 29 ) } );
  google.maps.event.addListener(polys[30], 'click',
       function() { click_poly( 30 ) } );
  google.maps.event.addListener(polys[31], 'click',
       function() { click_poly( 31 ) } );
  google.maps.event.addListener(polys[32], 'click',
       function() { click_poly( 32 ) } );
  google.maps.event.addListener(polys[33], 'click',
       function() { click_poly( 33 ) } );
  google.maps.event.addListener(polys[34], 'click',
       function() { click_poly( 34 ) } );
  google.maps.event.addListener(polys[35], 'click',
       function() { click_poly( 35 ) } );
  google.maps.event.addListener(polys[36], 'click',
       function() { click_poly( 36 ) } );
  google.maps.event.addListener(polys[37], 'click',
       function() { click_poly( 37 ) } );
  google.maps.event.addListener(polys[38], 'click',
       function() { click_poly( 38 ) } );
  google.maps.event.addListener(polys[39], 'click',
       function() { click_poly( 39 ) } );
  google.maps.event.addListener(polys[40], 'click',
       function() { click_poly( 40 ) } );
  google.maps.event.addListener(polys[41], 'click',
       function() { click_poly( 41 ) } );
  google.maps.event.addListener(polys[42], 'click',
       function() { click_poly( 42 ) } );

  //-----------------------------------------------------------------
  // Set up the default selected region. In the future, we should
  // get this using the Google Maps API built-in location checker
  // of the current user's IP address
  //polys[33].setOptions( selected_options );  // Boston Metro is 33
  //regions_selected[33] = 1;                  // booleans of what's highlighted
  //  search_param_regions = new Array( 33 );  // for event searching
}
 
//-------------------------------------------------------------------
// Someone clicked on a polygon! Update the currently selected region
function click_poly( id )
 {
    // Handle the actual selected region itself
     if( regions_selected[ id ] ) {  // Selected? Color unselected.
       polys[id].setOptions( unselected_options );
       polys[id].setOptions( poly_options[ poly_colors[ id ] ] );
       regions_selected[ id ] = 0;
     } else {                         // Unselected? Color selected.
       // If none of the neighbors of this region are selected,
       // zero out all selections. Thus, if you click Boston
       // and then Hartford, it zeros out Boston.
       var found_neighbors = 0;
       for( var i=0; i < region_neighbors[id].length; i++ ) {
           if( regions_selected[ region_neighbors[id][i] ] ) {
   	    found_neighbors = 1;
   	    break;
	   }
       }

       if( !found_neighbors ) {  // Zero out selections
	   for( var i=0; i < regions_selected.length; i++ ) {
	       if( regions_selected[ i ] ) {
		   polys[i].setOptions( unselected_options );
		   polys[i].setOptions( poly_options[ poly_colors[ i ] ] );
		   regions_selected[ i ] = 0;
	       }
	   }
       }

       // Now handle the actual selected region
       polys[id].setOptions( selected_options );
       regions_selected[ id ] = 1;
    }

    //---- get rid of all regions and then display the ones selected ----//
     var regions_showing_str = "Showing";
     for( var i=0; i < region_classes.length; i++ ) {
       if( regions_selected[ i ] ) {
	 if( regions_showing_str.length > 10 ) {
	   regions_showing_str += ",";
	 }
	 regions_showing_str += " " + region_names[ i ];

	 $(".eitem." + region_classes[ i ] ).css("display", "inline");
	 $(".holidayrow." + region_classes[ i ] ).css("display", "table-row");
       } else {
	 $(".eitem." + region_classes[ i ] ).css("display", "none");
	 $(".holidayrow." + region_classes[ i ] ).css("display", "none");
       }
     }

     $("#regioncaption").text( regions_showing_str );
}

