﻿function LocalSearcher(query, iconPrimaryColor, iconStrokeColor) {
    var me = this;
    this.searcher = null;
    this.query = query;
    //this.markerHash = new Hash();
    this.resultsHash = new Hash();
    this.markerIcon = lmap.createCustomIcon(iconPrimaryColor, iconStrokeColor);

    this.init = function() {
        this.searcher = new google.search.LocalSearch();
        this.searcher.setNoHtmlGeneration();
        this.searcher.setResultSetSize(google.search.Search.LARGE_RESULTSET);
        this.searcher.setSearchCompleteCallback(this.searcher, function() {
            //var markerIcon = (me.searcher.Pf == "public school") ? lmap.createCustomIcon("#FF0000", "#000000") : lmap.createCustomIcon("#009900", "#000000");

            var results = me.searcher.results;
            for (var i = 0; i < results.length; i++) {
                var result = results[i];
                var lat = parseFloat(result.lat);
                var lng = parseFloat(result.lng);
                var key = lat + "|" + lng;

                // ensure there is not a marker for this location
                if (me.resultsHash.hasItem(key)) {
                    // marker exists
                    continue;
                }
                else {
                    // marker does not exist - add it
                    var searchResult = new LocalSearchMarker();
                    searchResult.lat = lat;
                    searchResult.lng = lng;
                    searchResult.glocalresult = result;

                    var marker = me.createLocalSearchMarker(searchResult);
                    searchResult.gmarker = marker;
                    me.resultsHash.setItem(key, marker);
                    lmap.map.addOverlay(marker);                    
                }
            }

            // if you want to remove markers that aren't within map bounds, do it here
        });
    }

    this.createLocalSearchMarker = function(searchResult) {
        var point = new GLatLng(searchResult.lat, searchResult.lng);
        var marker = new GMarker(point, { icon: me.markerIcon });

        GEvent.addListener(marker, 'click', function() {
            var infoHtml = searchResult.getLocalSearchInfoWindowHtml();
            lmap.map.openInfoWindowHtml(point, infoHtml);
        });
        GEvent.addListener(marker, "mouseover", function() {
            marker.tooltip = searchResult.getLocalSearchMouseOverHtml();
            lmap.showTooltip(marker);
        });
        GEvent.addListener(marker, "mouseout", function() {
            lmap.tooltip.style.visibility = "hidden"
        });
        return marker;
    }
    
    this.execLocalSearch = function(setTimer) {
        $("#timer").stopTime(this.query);
        var time = (setTimer) ? 400 : 0;
        $("#timer").oneTime(time, this.query, function() {
            me.searcher.setCenterPoint(lmap.map);
            me.searcher.execute(me.query);
        });
    }

    this.removeLocalSearch = function() {
        // remove existing markers
        for (var i in this.resultsHash.items) {
            var marker = this.resultsHash.getItem(i);
            lmap.map.removeOverlay(marker);
        }
        this.resultsHash.clear();
    }      
}
