(function() {
	google.load("maps", "2");
	google.load("search", "1");
	
	var map = {};
	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.get("checked")) {
				_localSearch.setCenterPoint(map);
				_localSearch.execute(_searchTerm);					 
			}
		};
		_searchCheckbox.on("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();
		}
	};
	
	google.setOnLoadCallback(function() {
      	if (google.maps.BrowserIsCompatible()) {
      		
			window.onunload = google.maps.Unload;			    	
          // Create and Center a Map
			map = new google.maps.Map2(document.getElementById("map"));
			var centerPoint = new google.maps.LatLng(pruPrefLat, pruPrefLon);		
			map.setCenter(centerPoint, 14);					
			map.addControl(new google.maps.LargeMapControl());
			map.addControl(new google.maps.MapTypeControl());
			var houseIcon = new google.maps.Icon();
	    	houseIcon.image = "/images/maps/house-for-map.png";
	    	houseIcon.iconSize = new google.maps.Size(18, 18);
	    	houseIcon.iconAnchor = new google.maps.Point(9, 9);
			map.addOverlay(new google.maps.Marker(centerPoint,houseIcon));
      		
			google.maps.Event.bind(map, "dragend", null, updateCategories);
			
			if (typeof tabberOptions != "undefined") {
				tabberAutomatic(tabberOptions);
			}
			
			var checkboxArray = YAHOO.util.Dom.getElementsByClassName("search-checkbox","input","localSearchForm");
			for (var i = 0; i < checkboxArray.length; i++) {
				var checkboxEl = new YAHOO.util.Element(checkboxArray[i]);
				var searchTerm = checkboxEl.get("value");
				localSearches[i] = new LocalSearchCat(checkboxEl,searchTerm);
			}
        }
        updateCategories();
  	});
 })();
