/*
var poiMarkers = Array();

function removeAllPOIMarkers(gmap) {
	$.each(poiMarkers, function() {
		gmap.removeOverlay(this);
	});
	poiMarkers = Array();
}

function updatePOIData(gmap) {
	removeAllPOIMarkers(gmap);
	
		
}


function addPOIToMap(gmap, catImageMap) {
	GEvent.addListener(gmap, "moveend", function() {
		updatePOIData(gmap);
	});
	
	$("input.search-checkbox").bind("click", function(e) {
		updatePOIData(gmap);
	});
	
	updatePOIData(gmap);
}

*/
var localSearches = [];

var catImageMap = {
	"coffee shop": "/images/maps/coffee_shops.gif",
	"cta": "/images/maps/cta.gif",
	"fire station": "/images/maps/fire_stations.gif",
	"grocery": "/images/maps/grocery.gif",
	"restaurant": "/images/maps/restaurant.gif",
	"health club": "/images/maps/health_clubs.gif",
	"hospital": "/images/maps/hospitals.gif",
	"library": "/images/maps/library.gif",
	"metra": "/images/maps/metra.gif",
	"movie theatre": "/images/maps/movie_theaters.gif",				
	"place of worship": "/images/maps/place_of_worship.gif",
	"post office": "/images/maps/post_office.gif",
	"police": "/images/maps/police.gif",				
	"school": "/images/maps/schools.gif"
};

// class for each local search
var LocalSearchCat = function(searchCheckbox, searchTerm) {
	// private variables
	var _searchCheckbox = searchCheckbox;
	var _searchTerm = searchTerm;				
	var _markers = [];
	
	// map icon
	var _smallIcon = new google.maps.Icon();
    _smallIcon.image = catImageMap[_searchTerm];
    _smallIcon.iconSize = new google.maps.Size(18, 18);
    _smallIcon.iconAnchor = new google.maps.Point(9, 9);
    _smallIcon.infoWindowAnchor = new google.maps.Point(9, 9);
    
    
	// local search object
	var _localSearch = new google.search.LocalSearch();
	_localSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	
    // the function that runs when the local search is finished
    _localSearch.setSearchCompleteCallback(null, function() {
		for (var i = 0; i < _localSearch.results.length; i++) {
			var currentResult = _localSearch.results[i];
			var latLng = new google.maps.LatLng(currentResult.lat, currentResult.lng);
			var marker = new google.maps.Marker(latLng, _smallIcon);
//				alert(currentResult.title);
//				for (var jj in _localSearch.results[i]) {
//					alert(jj);
//				}
			_addMarker(marker,currentResult);
			_markers.push(marker);
		}
	});
	
	// private functions
	var _addMarker = function(marker,result) {
		google.maps.Event.bind(marker, "click", null, function() {
			marker.openInfoWindow(result.html);
		});
		map.addOverlay(marker);
	};
	
	var _conductSearch = function (e) {
		for (var i = 0; i < _markers.length; i++) {
			map.removeOverlay(_markers[i]);
		}
		_markers = [];
		if (_searchCheckbox.attr("checked")) {
			_localSearch.setCenterPoint(map);
			_localSearch.execute(_searchTerm);					 
		}
	};
	_searchCheckbox.bind("click", _conductSearch);
	
	// return an object with conductSearch as the one public method
	return {
		conductSearch: _conductSearch
	};
};


// function to run onload and after the map is dragged and dropped
var updateCategories = function() {
	for (var i = 0; i < localSearches.length; i++) {
		localSearches[i].conductSearch();
	}
};


jQuery(document).ready(function() {

	google.maps.Event.bind(map, "dragend", null, updateCategories);
		
	if (typeof tabberOptions != "undefined") {
		tabberAutomatic(tabberOptions);
	}
	
	jQuery("input.search-checkbox").each(function(i) {
		localSearches[i] = new LocalSearchCat(jQuery(this), jQuery(this).val());
	});
	
    updateCategories();

});
