(function(e,t){var s=function(t,s){this.options=s,this.$element=e(t),this._create()};s.prototype={constructor:s,marker:function(){return this.gmarker},map:function(){return this.gmap},updatePosition:function(){this.gmarker.setVisible(!0),this.reloadPosition()},reloadPosition:function(){this.gmap.setCenter(this.gmarker.getPosition()),this.gmarker.setVisible(!0),t.maps.event.trigger(this.gmap,"resize")},selected:function(){return this.selectedResult},_create:function(){this.geocoder=new t.maps.Geocoder;var s=this;this.$element.typeahead({minLength:3,delay:this.options.typeaheaddelay,source:this._geocode.bind(this),textproperty:function(e){return e.formatted_address},updater:function(e){return s.$element.trigger("addressChanged",e),this.textproperty(e)},matcher:function(){return!0}}),this.options.map&&(this.$mapElement=e(this.options.map)[0],this._initMap())},_initMap:function(){this.gmap=new t.maps.Map(this.$mapElement,this.options.mapOptions),this.gmarker=new t.maps.Marker({position:this.options.mapOptions.center,map:this.gmap,draggable:this.options.draggableMarker}),t.maps.event.addListener(this.gmarker,"dragend",e.proxy(this._markerMoved,this)),this.gmarker.setVisible(!1),this.$element.on("addressChanged",this._focusAddress.bind(this))},_getAddressByMarkerPosition:function(e,s){var i=this;this.geocoder.geocode({latLng:e},function(e,r){if(r==t.maps.GeocoderStatus.OK){var n=e[0];i.$element.trigger("addressChanged",n),s(n)}})},_markerMoved:function(){var e=this.gmarker.getPosition();e.getAddress=this._getAddressByMarkerPosition.bind(this,e),this.$element.trigger("positionChanged",e)},_geocode:function(e,s){this.geocoder.geocode({address:e+" ",region:this.options.regionBias},function(e,i){if(i==t.maps.GeocoderStatus.OK)for(var r=[],n=0;e.length>n;n++)r.push(e[n]);s(r)})},_findInfo:function(e,t){for(var s=0;e.address_components.length>s;s++){var i=e.address_components[s];if(-1!=i.types.indexOf(t))return i.long_name}return!1},_focusAddress:function(e,t){t&&this.gmarker&&(this.gmarker.setPosition(t.geometry.location),this.gmarker.setVisible(!0),this.gmap.fitBounds(t.geometry.viewport))},_addressSelected:function(e,t){console.dir(t),this.selectedResult=t},_locationSelected:function(){}},e.fn.addresspicker=function(t,i){return this.each(function(){var r=e(this),n=r.data("addresspicker"),o=e.extend(!0,{},e.fn.addresspicker.defaults,r.data(),"object"==typeof t&&t);n||r.data("addresspicker",n=new s(this,o)),"string"==typeof t&&n[t](i)})},e.fn.addresspicker.defaults={draggableMarker:!0,regionBias:null,map:!1,typeaheaddelay:300,mapOptions:{zoom:5,center:new t.maps.LatLng(52.5122,13.4194),scrollwheel:!1,mapTypeId:t.maps.MapTypeId.ROADMAP}},e.fn.addresspicker.Constructor=s,e.fn.addresspicker.position=function(){return this.selectedResult},Array.indexOf||(Array.prototype.indexOf=function(e){for(var t=0;this.length>t;t++)if(this[t]==e)return t;return-1})})(window.jQuery,window.google);