define(["Ti/_/Evented","Ti/_/lang","Ti/Network"],function(e,o,n){function i(e){var o=m(window,"deviceorientation",function(n){o(),e(n)})}function t(e){return function(o){s={heading:{accuracy:o.webkitCompassAccuracy,magneticHeading:o.webkitCompassHeading},success:!0,timestamp:Date.now()},r.fireEvent("heading",s),e&&e(s)}}function a(e){return function(o){var n="coords"in o;l={success:n},n?l.coords=o.coords:l.code=o.code,r.fireEvent("location",l),e&&e(l)}}function c(){return{enableHighAccuracy:r.accuracy===r.ACCURACY_HIGH,timeout:r.MobileWeb.locationTimeout,maximumAge:r.MobileWeb.maximumLocationAge}}var r,s,u,d,l,m=require.on,g=!1,f=0,v=0,p=o.isDef;return i(function(e){p(e.webkitCompassHeading)&&(g=!0)}),r=o.setObject("Ti.Geolocation",e,{getCurrentPosition:function(e){r.locationServicesEnabled&&navigator.geolocation.getCurrentPosition(a(e),a(e),c())},getCurrentHeading:function(e){g&&(s&&Date.now()-s.timestamp<r.maximumHeadingAge?e(s):i(t(e)))},forwardGeocoder:function(e,o){if(require.is(e,"String")){var i=n.createHTTPClient({onload:function(){var e=this.responseText.split(",");o({success:!0,code:0,latitude:parseFloat(e[2]),longitude:parseFloat(e[3])})},onerror:function(e){o({success:!1,code:-1,error:e+""})},timeout:r.MobileWeb.forwardGeocoderTimeout});i.open("GET","http://api.appcelerator.net/p/v1/geo?d=f&q="+escape(e)),i.send()}},reverseGeocoder:function(e,o,i){if(p(e)&&p(o)){var t=n.createHTTPClient({onload:function(){i(JSON.parse(this.responseText))},onerror:function(){i({success:!1})},timeout:r.MobileWeb.forwardGeocoderTimeout});t.open("GET","http://api.appcelerator.net/p/v1/geo?d=r&q="+e+","+o),t.send()}},addEventListener:function(o,n){switch(o){case"heading":g&&(f++,1===f&&(u=m(window,"deviceorientation",t())));break;case"location":r.locationServicesEnabled&&(v++,1===v&&(d=navigator.geolocation.watchPosition(a(),a(),c())))}e.addEventListener.call(this,o,n)},removeEventListener:function(o,n){switch(o){case"heading":g&&(f--,0===f&&u());break;case"location":r.locationServicesEnabled&&(v--,1>f&&navigator.geolocation.clearWatch(d))}e.removeEventListener.call(this,o,n)},constants:{ACCURACY_HIGH:1,ACCURACY_LOW:2,ERROR_DENIED:1,ERROR_LOCATION_UNKNOWN:2,ERROR_TIMEOUT:3,locationServicesEnabled:{get:function(){return!!navigator.geolocation}},MobileWeb:{locationTimeout:1/0,maximumLocationAge:0,maximumHeadingAge:1e3,forwardGeocoderTimeout:void 0,reverseGeocoderTimeout:void 0},hasCompass:function(){return g}},properties:{accuracy:2}})});