(function(n,t,i,r){function e(t,i){this.options=n.extend(!0,{},u,i);this.input=t;this.$input=n(t);this._defaults=u;this._name="geocomplete";this.init()}var u={bounds:!0,country:null,map:!1,details:!1,detailsAttribute:"name",autoselect:!0,location:!1,mapOptions:{zoom:14,scrollwheel:!1,mapTypeId:"roadmap"},markerOptions:{draggable:!1},maxZoom:16,types:["geocode"],blur:!1},o="street_address route intersection political country administrative_area_level_1 administrative_area_level_2 administrative_area_level_3 colloquial_area locality sublocality neighborhood premise subpremise postal_code natural_feature airport park point_of_interest post_box street_number floor room lat lng viewport location formatted_address location_type bounds".split(" "),f="id place_id url website vicinity reference name rating international_phone_number icon formatted_phone_number".split(" ");n.extend(e.prototype,{init:function(){this.initMap();this.initMarker();this.initGeocoder();this.initDetails();this.initLocation()},initMap:function(){if(this.options.map){if(typeof this.options.map.setCenter=="function"){this.map=this.options.map;return}this.map=new google.maps.Map(n(this.options.map)[0],this.options.mapOptions);google.maps.event.addListener(this.map,"click",n.proxy(this.mapClicked,this));google.maps.event.addListener(this.map,"zoom_changed",n.proxy(this.mapZoomed,this))}},initMarker:function(){if(this.map){var t=n.extend(this.options.markerOptions,{map:this.map});t.disabled||(this.marker=new google.maps.Marker(t),google.maps.event.addListener(this.marker,"dragend",n.proxy(this.markerDragged,this)))}},initGeocoder:function(){var t={types:this.options.types,bounds:this.options.bounds===!0?null:this.options.bounds,componentRestrictions:this.options.componentRestrictions};this.options.country&&(t.componentRestrictions={country:this.options.country});this.autocomplete=new google.maps.places.Autocomplete(this.input,t);this.geocoder=new google.maps.Geocoder;this.map&&this.options.bounds===!0&&this.autocomplete.bindTo("bounds",this.map);google.maps.event.addListener(this.autocomplete,"place_changed",n.proxy(this.placeChanged,this));this.$input.keypress(function(n){if(n.keyCode===13)return!1});this.$input.bind("geocode",n.proxy(function(){this.find()},this));this.options.blur===!0&&this.$input.blur(n.proxy(function(){this.find()},this))},initDetails:function(){function t(n){r[n]=i.find("["+u+"="+n+"]")}if(this.options.details){var i=n(this.options.details),u=this.options.detailsAttribute,r={};n.each(o,function(n,i){t(i);t(i+"_short")});n.each(f,function(n,i){t(i)});this.$details=i;this.details=r}},initLocation:function(){var n=this.options.location,t;if(n){if(typeof n=="string"){this.find(n);return}n instanceof Array&&(t=new google.maps.LatLng(n[0],n[1]));n instanceof google.maps.LatLng&&(t=n);t&&(this.map&&this.map.setCenter(t),this.marker&&this.marker.setPosition(t))}},find:function(n){this.geocode({address:n||this.$input.val()})},geocode:function(t){this.options.bounds&&!t.bounds&&(t.bounds=this.options.bounds===!0?this.map&&this.map.getBounds():this.options.bounds);this.options.country&&(t.region=this.options.country);this.geocoder.geocode(t,n.proxy(this.handleGeocode,this))},selectFirstResult:function(){var t="";n(".pac-item-selected")[0]&&(t="-selected");var u=n(".pac-container .pac-item"+t+":first span:nth-child(2)").text(),r=n(".pac-container .pac-item"+t+":first span:nth-child(3)").text(),i=u;return r&&(i+=" - "+r),this.$input.val(i),i},handleGeocode:function(n,t){if(t===google.maps.GeocoderStatus.OK){var i=n[0];this.$input.val(i.formatted_address);this.update(i);n.length>1&&this.trigger("geocode:multiple",n)}else this.trigger("geocode:error",t)},trigger:function(n,t){this.$input.trigger(n,[t])},center:function(n){n.viewport?(this.map.fitBounds(n.viewport),this.map.getZoom()>this.options.maxZoom&&this.map.setZoom(this.options.maxZoom)):(this.map.setZoom(this.options.maxZoom),this.map.setCenter(n.location));this.marker&&(this.marker.setPosition(n.location),this.marker.setAnimation(this.options.markerOptions.animation))},update:function(n){this.map&&this.center(n.geometry);this.$details&&this.fillDetails(n);this.trigger("geocode:result",n)},fillDetails:function(t){var i={},r=t.geometry,u=r.viewport,e=r.bounds;n.each(t.address_components,function(t,r){var u=r.types[0];n.each(r.types,function(n,t){i[t]=r.long_name;i[t+"_short"]=r.short_name})});n.each(f,function(n,r){i[r]=t[r]});n.extend(i,{formatted_address:t.formatted_address,location_type:r.location_type||"PLACES",viewport:u,bounds:e,location:r.location,lat:r.location.lat(),lng:r.location.lng()});n.each(this.details,n.proxy(function(n,t){var r=i[n];this.setDetail(t,r)},this));this.data=i},setDetail:function(n,t){t===r?t="":typeof t.toUrlValue=="function"&&(t=t.toUrlValue());n.is(":input")?n.val(t):n.text(t)},markerDragged:function(n){this.trigger("geocode:dragged",n.latLng)},mapClicked:function(n){this.trigger("geocode:click",n.latLng)},mapZoomed:function(){this.trigger("geocode:zoom",this.map.getZoom())},resetMarker:function(){this.marker.setPosition(this.data.location);this.setDetail(this.details.lat,this.data.location.lat());this.setDetail(this.details.lng,this.data.location.lng())},placeChanged:function(){var n=this.autocomplete.getPlace(),t;n&&n.geometry?this.update(n):this.options.autoselect&&(t=this.selectFirstResult(),this.find(t))}});n.fn.geocomplete=function(t){var r="plugin_geocomplete",u,i;return typeof t=="string"?(u=n(this).data(r)||n(this).geocomplete().data(r),i=u[t],typeof i=="function"?(i.apply(u,Array.prototype.slice.call(arguments,1)),n(this)):(arguments.length==2&&(i=arguments[1]),i)):this.each(function(){var i=n.data(this,r);i||(i=new e(this,t),n.data(this,r,i))})}})(jQuery,window,document)