/*
 * jQuery geo_autocomplete plugin 1.0
 *
 * Copyright (c) 2009 Bob Hitching
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Requires jQuery Autocomplete plugin by Jörn Zaefferer - see http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/
 * jquery.autocomplete.js requires a minor modification for geo_autocomplete to work, as shown in /lib/jquery.autocomplete_geomod.js
 * 
 */
;(function($) {

$.fn.extend({
	geo_autocomplete: function(_geocoder, _options) {
		options = $.extend({}, $.Autocompleter.defaults, {
			geocoder: _geocoder,
			mapwidth: 88,
			mapheight: 88,
			maptype: 'terrain',
			mapkey: 'ABQIAAAAbnvDoAoYOSW2iqoXiGTpYBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQNumU68AwGqjbSNF9YO8NokKst8w', // localhost
			mapsensor: false,
			mapSearchSufix: '',
			mapSkipBlanks: false,
			parse: function(_results, _status, _query) {
				var _parsed = [];
				if (_results && _status && _status == 'OK') {
					$.each(_results, function(_key, _result) {
					    if (!options.mapSkipBlanks || (_result.types && _result.types[0] in { route:1 , street_address:1}) )
					    {
						    if (_result.geometry && _result.geometry.viewport) {
							    // place is first matching segment, or first segment
							    var _place_parts = _result.formatted_address.split(',');
							    var _place = _place_parts[0];
							    $.each(_place_parts, function(_key, _part) {
								    if (_part.toLowerCase().indexOf(_query.toLowerCase()) != -1) {
									    _place = $.trim(_part);
									    return false; // break
								    }
							    });
							    _parsed.push({
								    data: _result,
								    value: _place,
								    result: _place
							    });
						    }
						}
					});
				}
				return _parsed;
			},
			formatItem: function(_data, _i, _n, _value) {
                var _place = [];
                var _place2 = [];
                $.each(_data.address_components, function(_key, _addr) {
				    if (_addr.long_name) {
				    if (_addr.types && _addr.types in { route:1 , street_address:1, street_number:1})
				    {
				        _place.push($.trim(_addr.long_name));
				    }else{
				        _place2.push($.trim(_addr.long_name));
				    }
				    }
				});

				return (_place.length > 0) ?(_place.join(', ') + ",<br />" + _place2.join(', ')) : _place2.join(', ');
			}
		}, _options);
		
		// if highlight is set to false, replace it with a do-nothing function
		options.highlight = options.highlight || function(value) { return value; };
		
		// if the formatMatch option is not specified, then use formatItem for backwards compatibility
		options.formatMatch = options.formatMatch || options.formatItem;

		return this.each(function() {
			new $.Autocompleter(this, options);
		});
	}
});

})(jQuery);
