var ICON_PATH = "/assets/images/ameneties/";
var PRIMARY_LOCATION_ICON = ICON_PATH + "community.png";
var AIRPORT_ICON = ICON_PATH + "airports_marker.png";
var VILLAGES_ICON = ICON_PATH + "villages_marker.png";
var TRAILS_ICON = ICON_PATH + "trails_marker.png";
var TRAINS_ICON = ICON_PATH + "trains_marker.png";
var RETAIL_ICON = ICON_PATH + "retail_marker.png";
var POLICE_ICON = ICON_PATH + "police_marker.png";
var PARKS_ICON = ICON_PATH + "parks_marker.png";
var OPEN_SPACE_ICON = ICON_PATH + "open_space_marker.png";
var K_12_ICON = ICON_PATH + "k_12_marker.png";
var FIRE_ICON = ICON_PATH + "fire_marker.png";
var EMPLOYMENT_ICON = ICON_PATH + "employment_marker.png";
var COLLEGES_ICON = ICON_PATH + "colleges_marker.png";
var BUSES_ICON = ICON_PATH + "buses_marker.png";

var gmap;
var gmarkers = [];
var overlays = [];
var trails = [];
var encodedPolyline;
var currentTrailId;

function setCustomUI(map) {
    customUI = map.getDefaultUI();
    customUI.maptypes.satellite = false;
    customUI.maptypes.hybrid = false;
    customUI.maptypes.physical = false;
    customUI.controls.largemapcontrol3d = true;
    map.setUI(customUI);
}

function SetupMap() {
    if (GBrowserIsCompatible()) {
        gmap = new GMap2(document.getElementById("map_canvas"));

        //remove the default map controls
        setCustomUI(gmap);

        plotRanch();
        getCommunityIconOverlay(gmap, 33.69619907340723, -117.78648376464844, PRIMARY_LOCATION_ICON);

    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Plot Ranch outline
function plotRanch() {
    AmenetiesMapService.GetBoundaryPlots(SucceededRanchCallback);
}
function SucceededRanchCallback(result) {
    if (result.length > 0) {
        for (i = 0; i < result.length; i++) {
            if (result[i].MapPlotTypeId == 1) {
                plotRanchOutline(result[i].Id, result[i].EncodedPolyline, result[i].EncodedLevels, result[i].Location);
            }
        }
    }

}

function plotRanchOutline(id, lines, levels, location) {
    var encodedPoints;
    var encodedLevels;

    encodedPoints = lines;
    encodedLevels = levels;
    encodedPolyline = new GPolyline.fromEncoded({
        color: "#E11921",
        weight: 2,
        points: encodedPoints,
        levels: encodedLevels,
        zoomFactor: 32,
        numLevels: 4
    });

    gmap.addOverlay(encodedPolyline);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function plotVillages(what) {
    //getCommunityIconOverlay(gmap, 33.668668, -117.827383, PRIMARY_LOCATION_ICON);

    ///GetBoundaryPlots
    if (what.checked) {
        AmenetiesMapService.GetBoundaryPlots(SucceededPlotCallback);
    }
    if (!what.checked) {
        if (gmarkers.length > 0) {
            for (i = 0; i < gmarkers.length; i++) {
                if (gmarkers[i].name == what.name) {
                    gmap.removeOverlay(gmarkers[i]);
                }
            }
        }
    }

}
function SucceededPlotCallback(result) {
    if (result.length > 0) {
        for (i = 0; i < result.length; i++) {
            if (result[i].MapPlotTypeId == 2) {
                plotInfoMarker(gmap, result[i].CenterPointLat, result[i].CenterPointLon, result[i].Id, VILLAGES_ICON, result[i].EncodedPolyline, result[i].EncodedLevels, result[i].Location, result[i].MapPlotTypeId);
            }
        }
    }

}

function plotTrails(what) {

    if (what.checked) {
        currentTrailId = what.name;
        AmenetiesMapService.GetBoundaryPlots(SucceededTrailCallback);
    }
    if (!what.checked) {
        currentTrailId = 0;
        if (trails.length > 0) {
            for (i = 0; i < trails.length; i++) {
                if (trails[i].id == what.name) {
                    gmap.removeOverlay(trails[i]);
                }
            }
        }
    }

}
function SucceededTrailCallback(result) {
    if (result.length > 0) {
        for (i = 0; i < result.length; i++) {
            if (result[i].MapPlotTypeId == 3) {
                plotTrail(result[i].Id, result[i].EncodedPolyline, result[i].EncodedLevels, result[i].Location);
            }
        }
    }

}

function plotTrail(id, lines, levels, location) {
    if (currentTrailId == id) {
        var encodedPoints;
        var encodedLevels;
        //trails.clear();

        encodedPoints = lines;
        encodedLevels = levels;
        encodedPolyline = new GPolyline.fromEncoded({
            //color: "#4c8617",
            color: "#003B15",
            weight: 2,
            points: encodedPoints,
            levels: encodedLevels,
            zoomFactor: 32,
            numLevels: 4
        });

        gmap.addOverlay(encodedPolyline);
        trails.push(encodedPolyline);
        trails[trails.length - 1].id = id;
    }
}

function displayBorder(id, lines, levels, location, typeid) {
    //Encoder Utility - http://code.google.com/apis/maps/documentation/polylineutility.html
    //http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/encodeForm.html
    var encodedPoints;
    var encodedLevels;
    overlays.clear();

    switch (typeid) {
        case 2: //Village
            encodedPolyline = new GPolygon.fromEncoded({
                polylines: [
            { points: lines,
                levels: "PHJGIHP",
                color: "#fef37d",
                opacity: 0.7,
                weight: 3,
                numLevels: 18,
                zoomFactor: 2}],
                fill: true,
                color: "#fef37d",
                opacity: 0.4,
                outline: true
            });
            break;
    }

    gmap.addOverlay(encodedPolyline);
    overlays.push(encodedPolyline);
}
function hideBorder() {
    gmap.removeOverlay(overlays[0]);
}

function plotInfoMarker(map, lat, lon, id, icon, lines, levels, location, typeid) {
    point = new GLatLng(lat, lon);

    //setup our icon
    primIcon = new GIcon();
    primIcon.image = icon;
    //primIcon.iconSize = new GSize(21, 24);
    primIcon.iconSize = new GSize(33, 38);
    primIcon.iconAnchor = new GPoint(1, 1);
    primIcon.infoWindowAnchor = new GPoint(1, 5);
    markerOptions = { icon: primIcon };
    var imarker = new GMarker(point, markerOptions);
    imarker.id = id;
    imarker.name = "28";

    gmarkers.push(imarker);

    var htmlInfo = getHTMLForPlots(location);
    var infoTabs = [new GInfoWindowTab("Details", htmlInfo)];

    //GEvent.addListener(imarker, "click", function() { imarker.openInfoWindowTabsHtml(infoTabs); })
    GEvent.addListener(imarker, "mouseover", function() { displayBorder(id, lines, levels, location, typeid); imarker.openInfoWindowTabsHtml(infoTabs); })
    GEvent.addListener(imarker, "mouseout", function() { hideBorder(); })

    map.addOverlay(imarker);
}

function getCommunityIconOverlay(map, lat, lon, icon) {
    point = new GLatLng(lat, lon);
    map.setCenter(point, 11);

    //	//setup our icon
    //	primIcon = new GIcon();
    //	primIcon.image = icon;
    //	primIcon.iconSize = new GSize(30, 41);
    //	primIcon.iconAnchor = new GPoint(1, 1);
    //	primIcon.infoWindowAnchor = new GPoint(1, 5);
    //	markerOptions = { icon:primIcon };

    //	//marker = new GMarker(point, markerOptions);
    //	//marker = new GMarker(point);
    //	
    //	map.addOverlay(marker);

}

function getAmenityIconOverlay(map, amenity, icon) {
    point = new GLatLng(amenity.Latitude, amenity.Longitude);

    //setup our icon
    primIcon = new GIcon();
    primIcon.image = icon;
    //primIcon.iconSize = new GSize(21, 24);
    primIcon.iconSize = new GSize(33, 38);
    primIcon.iconAnchor = new GPoint(1, 1);
    primIcon.infoWindowAnchor = new GPoint(1, 5);
    markerOptions = { icon: primIcon };

    //lets get out location information
    var htmlInfo = getHTMLForBaloon(amenity);
    var directions = getHTMLForDirectionsTab(amenity);

    var marker = new GMarker(point, markerOptions);
    marker.id = amenity.SubCategoryId;
    gmarkers.push(marker);

    var infoTabs = [new GInfoWindowTab("Details", htmlInfo), new GInfoWindowTab("Directions", directions)];

    GEvent.addListener(marker, "mouseover", function() { marker.openInfoWindowTabsHtml(infoTabs); })

    map.addOverlay(marker);
}
function getIndividualAmenityIconOverlay(map, amenity, icon) {
    point = new GLatLng(amenity.Latitude, amenity.Longitude);

    //setup our icon
    primIcon = new GIcon();
    primIcon.image = icon;
    //primIcon.iconSize = new GSize(21, 24);
    primIcon.iconSize = new GSize(33, 38);
    primIcon.iconAnchor = new GPoint(1, 1);
    primIcon.infoWindowAnchor = new GPoint(1, 5);
    markerOptions = { icon: primIcon };

    //lets get out location information
    var htmlInfo = getHTMLForBaloon(amenity);
    var directions = getHTMLForDirectionsTab(amenity);

    var marker = new GMarker(point, markerOptions);
    marker.id = amenity.AmenityId;
    gmarkers.push(marker);

    var infoTabs = [new GInfoWindowTab("Details", htmlInfo), new GInfoWindowTab("Directions", directions)];

    GEvent.addListener(marker, "mouseover", function() { marker.openInfoWindowTabsHtml(infoTabs); })

    map.addOverlay(marker);
}

function getPointsForSubCategory(map, subCat, lat, lon) {
    if (subCat.checked) {
        AmenetiesMapService.GetMapPointsForSubCategory(parseInt(subCat.name), lat, lon, 100, SucceededCallback);
    }

    if (!subCat.checked) {
        if (gmarkers.length > 0) {
            for (i = 0; i < gmarkers.length; i++) {
                if (gmarkers[i].id == subCat.name) {
                    map.removeOverlay(gmarkers[i]);
                }
            }
        }
    }
}

function getPointsForShoppingCenters(map, subCat, lat, lon) {
    if (subCat.checked) {
        AmenetiesMapService.GetMapPointsForSubCategory(3, lat, lon, 100, SucceededCallbackIndividualAmenity);
        AmenetiesMapService.GetMapPointsForSubCategory(4, lat, lon, 100, SucceededCallbackIndividualAmenity);
        AmenetiesMapService.GetMapPointsForSubCategory(5, lat, lon, 100, SucceededCallbackIndividualAmenity);

        jqCheckAll('retail');
    }
    else {
        jqUnCheckAll('retail');
    }
}

function SucceededCallback(result) {
    if (result.length > 0) {
        for (i = 0; i < result.length; i++) {
            getAmenityIconOverlay(gmap, result[i], getIconForCat(result[i].SubCategoryId));
        }
    }
}

function getPointsForAmenity(map, amenity) {
    if (amenity.checked) {
        AmenetiesMapService.GetMapPointsForAmenity(parseInt(amenity.name), SucceededCallbackIndividualAmenity);
    }

    if (!amenity.checked) {
        if (gmarkers.length > 0) {
            for (i = 0; i < gmarkers.length; i++) {
                if (gmarkers[i].id == amenity.name) {
                    map.removeOverlay(gmarkers[i]);
                }
            }
        }
    }
}

function SucceededCallbackIndividualAmenity(result) {
    if (result.length > 0) {
        for (i = 0; i < result.length; i++) {
            getIndividualAmenityIconOverlay(gmap, result[i], getIconForCat(result[i].SubCategoryId));
        }
    }
}

function jqUnCheckAll(group) {
    if (group == 'retail') {
        jQuery("#uxFashionIsland").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxIrvineSpectrumCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheMarketPlace").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxAltonSquare").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxAltonTechnologyCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxBaysideCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCampusPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCoronadelMarPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCrystalCovePromenade").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCulverPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxEastbluffVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxExecutivePark").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxHarborViewCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxHarvardPlace").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxHarvardPlaceAutoPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxIrvineHomeCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNewportCoastShoppingCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNewportHillsShoppingCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNewportNorthCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNorthparkPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxOakCreekVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxOrchardHillsVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxParkviewCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxQuailHillVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxSandCanyonPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheBluffs").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheCommons").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheCrossroads").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheSquare").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTrabucoGrove").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxUniversityCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxUniversityParkCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWalnutVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWestParkPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWestcliffPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWoodbridgeVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWoodburyTownCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));


        getPointsForAmenity(gmap, jQuery("#uxFashionIsland")[0])
        getPointsForAmenity(gmap, jQuery("#uxIrvineSpectrumCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxTheMarketPlace")[0])
        getPointsForAmenity(gmap, jQuery("#uxAltonSquare")[0])
        getPointsForAmenity(gmap, jQuery("#uxAltonTechnologyCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxBaysideCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxCampusPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxCoronadelMarPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxCrystalCovePromenade")[0])
        getPointsForAmenity(gmap, jQuery("#uxCulverPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxEastbluffVillageCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxExecutivePark")[0])
        getPointsForAmenity(gmap, jQuery("#uxHarborViewCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxHarvardPlace")[0])
        getPointsForAmenity(gmap, jQuery("#uxHarvardPlaceAutoPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxIrvineHomeCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxNewportCoastShoppingCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxNewportHillsShoppingCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxNewportNorthCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxNorthparkPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxOakCreekVillageCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxOrchardHillsVillageCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxParkviewCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxQuailHillVillageCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxSandCanyonPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxTheBluffs")[0])
        getPointsForAmenity(gmap, jQuery("#uxTheCommons")[0])
        getPointsForAmenity(gmap, jQuery("#uxTheCrossroads")[0])
        getPointsForAmenity(gmap, jQuery("#uxTheSquare")[0])
        getPointsForAmenity(gmap, jQuery("#uxTrabucoGrove")[0])
        getPointsForAmenity(gmap, jQuery("#uxUniversityCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxUniversityParkCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxWalnutVillageCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxWestParkPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxWestcliffPlaza")[0])
        getPointsForAmenity(gmap, jQuery("#uxWoodbridgeVillageCenter")[0])
        getPointsForAmenity(gmap, jQuery("#uxWoodburyTownCenter")[0])

    }
}

function jqCheckAll(group) {
    if (group == 'retail') {
        jQuery("#uxFashionIsland").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxIrvineSpectrumCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheMarketPlace").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxAltonSquare").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxAltonTechnologyCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxBaysideCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCampusPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCoronadelMarPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCrystalCovePromenade").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxCulverPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxEastbluffVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxExecutivePark").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxHarborViewCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxHarvardPlace").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxHarvardPlaceAutoPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxIrvineHomeCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNewportCoastShoppingCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNewportHillsShoppingCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNewportNorthCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxNorthparkPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxOakCreekVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxOrchardHillsVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxParkviewCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxQuailHillVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxSandCanyonPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheBluffs").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheCommons").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheCrossroads").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTheSquare").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxTrabucoGrove").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxUniversityCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxUniversityParkCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWalnutVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWestParkPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWestcliffPlaza").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWoodbridgeVillageCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));
        jQuery("#uxWoodburyTownCenter").attr('checked', jQuery('#uxAllRetail').is(':checked'));

    }
    if (group == 'employment') {
        jQuery("#uxNewportCenter").attr('checked', jQuery('#uxAllEmployment').is(':checked'));
        jQuery("#uxIrvineBusinessComplex").attr('checked', jQuery('#uxAllEmployment').is(':checked'));
        jQuery("#uxUniversityResearchPark").attr('checked', jQuery('#uxAllEmployment').is(':checked'));
        jQuery("#uxIrvineSpectrum").attr('checked', jQuery('#uxAllEmployment').is(':checked'));
        jQuery("#uxIrvineTechnologyCenter").attr('checked', jQuery('#uxAllEmployment').is(':checked'));

        getPointsForSubCategory(gmap, jQuery("#uxNewportCenter")[0], amenityLat, amenityLon)
        getPointsForSubCategory(gmap, jQuery("#uxIrvineBusinessComplex")[0], amenityLat, amenityLon)
        getPointsForSubCategory(gmap, jQuery("#uxUniversityResearchPark")[0], amenityLat, amenityLon)
        getPointsForSubCategory(gmap, jQuery("#uxIrvineSpectrum")[0], amenityLat, amenityLon)
        getPointsForSubCategory(gmap, jQuery("#uxIrvineTechnologyCenter")[0], amenityLat, amenityLon)
    }
}

function resetCheckBoxes() {
    jQuery("form#form1 INPUT[type='checkbox']").attr('checked', false);
    jQuery("#firstpane div.accordian_menu_head").siblings("div.menu_body").slideUp("slow")
    jQuery("#firstpane div.accordian_menu_head").removeClass("accordian_menu_head_active");
}

function getIconForCat(subcatId) {
    switch (subcatId) {
        case 1:
            return K_12_ICON;
            break;

        case 2:
            return COLLEGES_ICON;
            break;

        case 3:
        case 4:
        case 5:
            return RETAIL_ICON;
            break;

        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
            return EMPLOYMENT_ICON;
            break;

        case 11:
        case 12:
        case 13:
            return PARKS_ICON;
            break;

        case 14:
        case 15:
        case 16:
        case 17:
            return TRAILS_ICON;
            break;

        case 45:
            return OPEN_SPACE_ICON;
            break;

        case 21:
            return FIRE_ICON;
            break;

        case 22:
            return POLICE_ICON;
            break;

        case 24:
            return AIRPORT_ICON;
            break;

        case 25:
            return BUSES_ICON;
            break;

        case 26:
        case 27:
            return TRAINS_ICON;
            break;

        default:
            return GENERAL_ICON;
            break;

    }
}

function getHTMLForBaloon(amenity) {
    output = "<div id='gmdetails'>";
    if (amenity.ImageURL) { output += "<img src='/assets/images/category/" + amenity.ImageURL + "'/><br/>"; }
    if (amenity.Title) { output += "<b class='Location'>" + amenity.Title + "</b><br />"; }
    if (amenity.AmenityDesc) { output += "<b class='Location'>" + amenity.AmenityDesc + "</b><br />"; }
    output += "<span class='address'>";
    if (amenity.Address) { output += " " + amenity.Address; }
    if (amenity.City) { output += " " + amenity.City + ""; }
    if (amenity.State) { output += ", " + amenity.State + " "; }
    if (amenity.Phone) { output += " " + amenity.Phone + " "; }
    output += "</span>";

    return output;
}

function getHTMLForDirectionsTab(amenity) {
    output = "<div id='gmdetails'>";
    if (amenity.AmenityDesc) { output = "<div style='width:190px;height:95px;color:#000;'><span style='font-size:14px;font-weight:bold;'>" + amenity.AmenityDesc + "</span>"; }
    if (amenity.Title) { output = "<div style='width:150px;height:95px;color:#000;'><span style='font-size:14px;font-weight:bold;'>" + amenity.Title + "</span>"; }
    output += "<br /><br /><span style='font-size:9px;'>start address:</span><br /><input type='text' id='uxStartAddress' name='uxStartAddress' style='width:200px' onload='this.focus()'/>";
    output += "<br /><input type='button' id='uxDirections' value='Get Directions' onclick='showDirections();'/>";
    output += "<input type='hidden' id='uxDestinationAddress' name='uxDestinationAddress' value='" + amenity.Address + ", " + amenity.City + ", " + amenity.State + "' />";
    if (amenity.AmenityDesc) { output += "<input type='hidden' id='uxDestName' name='uxDestName' value='" + amenity.AmenityDesc + "' />" };
    if (amenity.Title) { output += "<input type='hidden' id='uxDestName' name='uxDestName' value='" + amenity.Title + "' />" };

    return output;
}

function getHTMLForPlots(location) {
    output = "<div id='villagedetails'>";
    output += "<b class='Location'>" + location + "</b>";

    return output;
}

function showDirections() {
    getDirections();
    jQuery("#directionsPanel").css({ display: "block" });
    jQuery("#directionsPanel").css({ zindex: "998" });
    jQuery("#menu").css({ display: "none" });
}

function hideDirections() {
    jQuery("#directionsPanel").css({ display: "none" });
    jQuery("#directionsPanel").css({ zindex: "996" });
    jQuery("#menu").css({ display: "block" });
}

function getDirections() {
    var directions = null;
    var fmaddr = jQuery("#uxStartAddress").val();
    var toaddr = jQuery("#uxDestinationAddress").val();
    var amenityName = jQuery("#uxDestName").val();

    if (!directions) {
        directions = new GDirections(gmap);
    }

    GEvent.addListener(directions, "addoverlay", function() {

        var route = directions.getRoute(0);
        var bounds = new GLatLngBounds();
        var disp = "";
        disp = disp + " <h2 style='margin:0'>Directions</h2><h4 style='padding:5px;'>" + fmaddr + "</h4>" + directions.getDistance().html + " about " + directions.getDuration().html + "<br />";
        disp = disp + "<table width='100%' style='padding-right:5px;padding-left:5px;' cellspacing='0'>";

        // Start looping through the turn by turn elements.
        for (i = 0; i < route.getNumSteps(); i++) {
            var stp = route.getStep(i);
            bounds.extend(stp.getLatLng());
            var clr = (i % 2) ? "#fff" : "#fff";

            disp = disp + "<tr valign='top'><td style='background:" + clr + ";'>" + stp.getDescriptionHtml() + "</td>";
            disp = disp + "<td style='background:" + clr + ";'>" + stp.getDistance().html + "</td></tr>";
        }

        disp = disp + "<tr><td colspan='2'><h4 style='padding:5px;'>" + amenityName + "</h4><a href='#' onclick='javascript:printPage();'><img src='/assets/images/ameneties/print.gif'/></a><a href='#' id='uxCloseDirections' onclick='hideDirections();' style='margin-bottom:10px;margin-left:200px;margin-top:-23px;float:right;'><img src='/assets/images/ameneties/close.gif' /></a></td></tr></table>";
        jQuery("#directionsPanel").html(disp);

        gmap.setZoom(gmap.getBoundsZoomLevel(bounds));
        gmap.setCenter(bounds.getCenter());
    });

    GEvent.addListener(directions, "error", function() {
        alert("Could not generate a route for the current start and end addresses");
        hideDirections();
    });

    directions.load("from: " + fmaddr + " to: " + toaddr, { preserveViewport: true, getSteps: true });
}

function removeSpaces(string) {
    return string.split(' ').join('');
}
