    var menuItems = new Array("villages", "schools", "parks", "shopping", "golf", "transportation", "employment", "selectall", "clearall");
  
    // preload interactive map menu images
    for (i = 0; i < menuItems.length; i++) {
        eval("var " + menuItems[i] + "_i=new Image();");
        eval(menuItems[i] + "_i.src='/assets/images/mapimages/Default/imap_" + menuItems[i] + "_i.jpg';");
        eval("var " + menuItems[i] + "_a=new Image();");
        eval(menuItems[i] + "_a.src='/assets/images/mapimages/Default/imap_" + menuItems[i] + "_a.jpg';");
    }
    
    
    
    var map;
    var baseIcon;
    var village_icon;
    var overlays = [];
    var location_overlays = {};
    var location_infotabs = {};
    var overlay_layers = [];
    var gdir;
    var gdirlink;

    function setup_interactive_map(section) {
        setup_map(false);
        toggle_button_location(section);
    }


    function setup_planner_map() {
        setup_map(false);
    }


    function setup_village_map() {
        setup_map(true);
        add_all_locations();
        open_first_location_point("villages");
    }


    function setup_map(blocalsearch) {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GLargeMapControl);
            map.addControl(new GMapTypeControl);
            map.addControl(new GScaleControl);
            map.addControl(new GOverviewMapControl);
            if (blocalsearch) {
                var options = {searchFormHint:"Search Village Area", resultList:document.getElementById("results")};
                map.addControl(new google.maps.LocalSearch(options));
            }
            
            // === create a GDirections Object ===
            gdir = new GDirections(map, document.getElementById("directions"));
            
            // === Array for decoding the failure codes ===
            var reasons = [];
            reasons[G_GEO_SUCCESS] = "Success";
            reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value.";
            reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
            reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
            reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
            reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
            reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed.";
            reasons[G_GEO_BAD_REQUEST] = "A directions request could not be successfully parsed.";
            reasons[G_GEO_MISSING_QUERY] = "No query was specified in the input.";
            reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
            
            // === catch Directions errors ===
            GEvent.addListener(gdir, "error", function () {var code = gdir.getStatus().code;var reason = "Code " + code;if (reasons[code]) {reason = reasons[code];}alert("Failed to obtain directions, " + reason);});
            
            
            baseIcon = new GIcon;
            baseIcon.iconSize = new GSize(32, 32);
            baseIcon.shadowSize = new GSize(59, 32);
            baseIcon.iconAnchor = new GPoint(16, 32);
            baseIcon.infoWindowAnchor = new GPoint(16, 0);
            
            baseIcon.shadow = "/Assets/IMAGES/GMapIcons/homegardenbusiness_002.png";
            village_icon = new GIcon(baseIcon, "/Assets/IMAGES/GMapIcons/homegardenbusiness.png", null, null);
            
            baseIcon.shadow = "/Assets/IMAGES/GMapIcons/rangerstation_002.png";
            school_icon = new GIcon(baseIcon, "/Assets/IMAGES/GMapIcons/rangerstation.png", null, null);
            
            baseIcon.shadow = "/Assets/IMAGES/GMapIcons/tree_002.png";
            park_icon = new GIcon(baseIcon, "/Assets/IMAGES/GMapIcons/tree.png", null, null);
            
            baseIcon.shadow = "/Assets/IMAGES/GMapIcons/shopping_002.png";
            shopping_icon = new GIcon(baseIcon, "/Assets/IMAGES/GMapIcons/shopping.png", null, null);
            
            baseIcon.shadow = "/Assets/IMAGES/GMapIcons/golfer_002.png";
            golf_icon = new GIcon(baseIcon, "/Assets/IMAGES/GMapIcons/golfer.png", null, null);
            
            baseIcon.shadow = "/Assets/IMAGES/GMapIcons/cabs.png";
            transportation_icon = new GIcon(baseIcon, "/Assets/IMAGES/GMapIcons/cabs_002.png", null, null);
            
            baseIcon.shadow = "/Assets/IMAGES/GMapIcons/pushpin_shadow.png";
            employment_icon = new GIcon(baseIcon, "/Assets/IMAGES/GMapIcons/blue-pushpin.png", null, null);
            
            zoom_default();
        }
    }


    function create_marker(latitude, longitude, details_body, icon, directions_body) {
        var lat_long = new GLatLng(latitude, longitude);
        var marker = new GMarker(lat_long, icon);
        if (details_body) {
            var infoTabs = [new GInfoWindowTab("Details", details_body), new GInfoWindowTab("Directions", directions_body)];
            GEvent.addListener(marker, "click", function () {marker.openInfoWindowTabsHtml(infoTabs);});
        }
        return marker;
    }


    function zoom_default() {
        var lat_lng = new GLatLng(33.69963622071172, -117.73876190185547);
        map.setCenter(lat_lng, 14);
    }


    function zoom_latlng(lat,lng) {
        var lat_lng = new GLatLng(lat, lng);
        map.setCenter(lat_lng, 14);
    }

    function zoom_to_location(location) {
        var checkbox = document.getElementById("location_" + location + "_checkbox");
        if (!checkbox.checked) {
            add_location_points(location);
            checkbox.checked = true;
        }
        var bounds = new GLatLngBounds;
        for (var i = 0, point; point = locations[location][i]; i++) {
            bounds.extend(new GLatLng(point[3], point[2]));
        }
        var lngCenter = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
        var latCenter = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
        var center = new GLatLng(latCenter, lngCenter);
        map.setCenter(center, map.getBoundsZoomLevel(bounds));
    }


    function add_all_locations() {
        remove_all_locations();
        for (var location in locations) {
            try {
                add_location_points(location);
                var checkbox = document.getElementById("location_" + location + "_checkbox");
                checkbox.checked = true;
                eval("$(\"img_" + location + "\").src = " + location + "_a.src;");
            } catch (err) {
            }
        }
    }


    function remove_all_locations() {
        for (var location in locations) {
            try {
                remove_location_points(location);
                var checkbox = document.getElementById("location_" + location + "_checkbox");
                checkbox.checked = false;
                eval("$(\"img_" + location + "\").src = " + location + "_i.src;");
            } catch (err) {
                txt = "There was an error on this page.\n\n";
                txt += "Error description: " + err.description + "\n\n";
                txt += "Click OK to continue.\n\n";
            }
        }
    }

    function add_location_point(location,id) {
        for (var i = 0, info; info = locations[location][i]; i++) {
            if (info[0] == id) {
                latitude = info[3];
                longitude = info[4];    
            }
        }

        switch (location) {
          case "villages":
            icon = village_icon;
            break;
          case "schools":
            icon = school_icon;
            break;
          case "parks":
            icon = park_icon;
            break;
          case "shopping":
            icon = shopping_icon;
            break;
          case "golf":
            icon = golf_icon;
            break;
          case "transportation":
            icon = transportation_icon;
            break;
          case "employment":
            icon = employment_icon;
            break;
          default:;
        }    
        var marker = create_marker(latitude, longitude, '', icon, '');
        map.addOverlay(marker);                
        return marker;
    }
    
    function add_location_points(location) {
        try {
            location_overlays[location] = [];
            location_infotabs[location] = [];
            for (var i = 0, info; info = locations[location][i]; i++) {
                address = info[2];
                place = info[1];
                latitude = info[3];
                longitude = info[4];
                notes = info[5];
                photo = info[6];
                e = "";
                e += "<div id=\"gmdetails\">";
                if (photo != "") {
                    e += "<img src=\"" + photo + "\" /><br />";
                }
                e += "<b class=\"Location\">" + place + "</b><br /><span class=\"address\">" + address + "</span><br /><br />";
                if (notes) {
                    e += "<div class=\"detail\">" + notes + "</div>";
                }
                e += "</div>";
                directions_html = "<div id=\"gmdirections\">" + "<b class=\"Location\">" + place + "</b><br /><span class=\"address\">" + address + "</span><br /><br /><div class=\"detail\">" + "Enter your starting address here for directions to this location.<br /><br />" + "<span>Start address:</span><br />" + "<input type=\"text\" size=20 maxlength=40 name=\"saddr\" id=\"saddr\" value=\"\" /><br>" + "<input value=\"Get Directions\" type=\"button\" onclick=\"getDirections();\">" + "<input type=\"hidden\" id=\"daddr\" value=\"" + place + "@" + latitude + "," + longitude + "\"/>" + "</div></div>";
                var icon;
                switch (location) {
                  case "villages":
                    icon = village_icon;
                    break;
                  case "schools":
                    icon = school_icon;
                    break;
                  case "parks":
                    icon = park_icon;
                    break;
                  case "shopping":
                    icon = shopping_icon;
                    break;
                  case "golf":
                    icon = golf_icon;
                    break;
                  case "transportation":
                    icon = transportation_icon;
                    break;
                  case "employment":
                    icon = employment_icon;
                    break;
                  default:;
                }
                var marker = create_marker(latitude, longitude, e, icon, directions_html);
                var infoTabs = [new GInfoWindowTab("Details", e), new GInfoWindowTab("Directions", directions_html)];
                location_overlays[location].push(marker);
                location_infotabs[location].push(infoTabs);
                map.addOverlay(marker);
            }
        } catch (err) {
        }
    }


    function open_first_location_point(location) {
        try {
            marker = location_overlays[location][0];
            infotab = location_infotabs[location][0];
            marker.openInfoWindowTabsHtml(infotab);
        } catch (err) {
        }
    }


    function remove_location_point(location,id) {
        try {
            marker = location_overlays[id][0];
            map.removeOverlay(marker);
        } catch (err) {
            //alert(err);
        }        
    }
    
    function remove_location_points(location) {
        for (var i = 0, marker; marker = location_overlays[location][i]; i++) {
            map.removeOverlay(marker);
        }
    }


    function toggle_location(location) {
        var checkbox = document.getElementById("location_" + location + "_checkbox");
        if (checkbox.checked) {
            add_location_points(location);
        } else {
            remove_location_points(location);
        }
    }


    function toggle_locations() {
        var checkbox = document.getElementById("hide_locations_checkbox");
        if (checkbox.checked) {
            add_all_locations();
        } else {
            remove_all_locations();
        }
    }


    function toggle_button_location(location) {
        var checkbox = document.getElementById("location_" + location + "_checkbox");
        if (checkbox.checked) {
            checkbox.checked = false;
            eval("$(\"img_" + location + "\").src = " + location + "_i.src;");
            remove_location_points(location);
        } else {
            checkbox.checked = true;
            eval("$(\"img_" + location + "\").src = " + location + "_a.src;");
            add_location_points(location);
        }
    }

    // ===== request the directions =====
    function popTo(radioValue) {
        for (var i = 0, info; info = locations.villages[i]; i++) {
            if (info[0] == radioValue) {
                $("daddr").value = info[1] + "@" + info[3] + "," + info[4];
            }
        }
    }


    function popFrom(radioValue) {
        if (radioValue != 0) {
            for (var i = 0, info; info = locations.villages[i]; i++) {
                if (info[0] == radioValue) {
                    $("saddr").value = info[1] + "@" + info[3] + "," + info[4];
                }
            }
        }
        return true;
    }

    function addIcon(location,id,direction) {
        for (var i = 0, info; info = locations.villages[i]; i++) {
            var markerid = direction+info[0];
            if (info[0] == id) {
                marker = add_location_point(location,info[0]);
                
                
                location_overlays[markerid] = [];
                location_overlays[markerid].push(marker);
                
                
            } else {
                remove_location_point(location,markerid)
            }
        }
    }


    function getPlannerDirections() {
        if (getRadioValue("villageFrom") == 0) {
            if ($("usaddr").value != "") {
                $("saddr").value = $("usaddr").value;
            } else {
                alert("Enter a Starting Address");
                return false;
            }
        }
        getDirections();
    }


    function getDirections() {
        var saddr = document.getElementById("saddr").value;
        var daddr = document.getElementById("daddr").value;
        gdirlink = printMapLink(saddr,daddr);
        gdir.load("from: " + saddr + " to: " + daddr);
        Element.show("directionsContainer");
        $("directionsContainer").scrollTo();
    }


    function getRadioValue(idOrName) {
        var value = null;
        var element = document.getElementById(idOrName);
        var radioGroupName = null;
        // if null, then the id must be the radio group name
        if (element == null) {
            radioGroupName = idOrName;
        } else {
            radioGroupName = element.name;
        }
        if (radioGroupName == null) {
            return null;
        }
        var radios = document.getElementsByTagName("input");
        for (var i = 0; i < radios.length; i++) {
            var input = radios[i];
            if (input.type == "radio" &&
                input.name == radioGroupName && input.checked) {
                value = input.value;
                break;
            }
        }
        return value;
    }
    
    function printMapLink(start,destination) {
        link = 'http://maps.google.com/maps?f=d&hl=en&saddr=' + start + '&daddr=' + destination + '&mra=pe&mrcr=0&sll=33.851331,-117.948896&sspn=0.796067,1.2854&ie=UTF8&spn=0.796099,1.2854&t=h&z=10&layer=tc&pw=2';
        return link;
    }