// vim: sw=4:ts=4:nu:nospell

/**
 * HyperCities search Object
 *
 * @author    Chen-Kuei Lee
 * @copyright (c) 2008, by HyperCities Tech Team
 * @date      2009.05.06
 * @version   $Id$
 *
 */
 
HyperCities.search = function() {
	// do NOT access javascript generated DOM from here; elements don't exist yet
    
	// Private variable goes here
	var _id      = "[HyperCities.search] ";
	var _address = null;
	var _marker  = null;
	var _keyword = null;

        var _updateSearchResults = function ($data) {
            // pass to collectionList
            
        };

	var _setCity = function ($cityId) {
		return false;
	};
        
    
	return {

		// reset the search keyword and address
		reset: function() {
			_address = null;
            _keyword = null;
			_marker  = null;
			$("#addressSearch #address").val("");
			$("#keywordSearch #keyword").val("");
			return false;
		},
        
		// reset the search keyword
		resetKeyword: function() {
			$(this).blur();
			_keyword = null;
			$("#keywordSearch #keyword").val("");
			return false;
		},
		// search the address
		searchAddress: function() {
			$(this).blur();
			_address = $("#addressSearch #address").val();
//			HyperCities.debug(_id + "Goto " + _address);
			HyperCities.mainMap.searchCenter(_address);
			return false;
		},

		// search the keyword
		searchKeyword: function() {
			$(this).blur();
                        // Post to search.php
                        var params = {
                            keywords: $("#keywordSearch #keyword").val(),
                            where: $("#keywordSearch input:radio[name=where]:checked").val()
                        };
                        if (params.where == 'CurrentView') {
                            var currentBounds = HyperCities.mainMap.getBounds();
                            params.swLat = currentBounds.getSouthWest().lat();
                            params.swLon = currentBounds.getSouthWest().lng();
                            params.neLat = currentBounds.getNorthEast().lat();
                            params.neLon = currentBounds.getNorthEast().lng();
                            var currentTime = HyperCities.mainMap.getTimespan();
                            // BC date handling happens on server
                            params.start = currentTime.min.toString("yyyy-MM-dd HH:mm:ss");
                            params.end = currentTime.max.toString("yyyy-MM-dd HH:mm:ss");
                        }
                        HyperCities.debug(params);
                        $.post("./search.php", params, HyperCities.collectionList.parseSearchResult, "xml");
			//alert("This feature is under construction.");
			return false;
		},

		removeAddressMarker: function() {
			if (_marker !== null) {
				HyperCities.mainMap.removeOverlay(_marker);
			}
		},

		addAddressMarker: function($response) {
			var place, point;

			if (!$response || $response.Status.code != 200) {
				alert("Sorry, we were unable to geocode this location");
			} 
			else {
//				HyperCities.debug($response);
				place = $response.Placemark[0];
				point = new GLatLng(place.Point.coordinates[1],
									place.Point.coordinates[0]);
				// Remove old marker
				if (_marker !== null) {
					HyperCities.mainMap.removeOverlay(_marker);
				}
				// Add new marker
				_marker = new GMarker(point);
				HyperCities.mainMap.setCenter(point);
				HyperCities.mainMap.addOverlay(_marker);
				_marker.openInfoWindowHtml(place.address);

				GEvent.addListener(_marker, "click", function() {
					var tmpListener = GEvent.addListener(_marker, "infowindowbeforeclose", function() {
							var latlng = HyperCities.mainMap.getCenter();
                  
							if (HyperCities.mainMap.getCurrentMapType() !== G_SATELLITE_3D_MAP)
								HyperCities.mainMap.getMapInstance().panTo(latlng);

							GEvent.removeListener(tmpListener);
						});

					_marker.openInfoWindowHtml(place.address);
				});
			}			
		}
	};
}(); // end of Object

// end of file
