Ext.namespace("Zarafa.plugins.gmaps");
Zarafa.plugins.gmaps.GmapsPlugin=Ext.extend(Zarafa.core.Plugin,{initPlugin:function(){Zarafa.plugins.gmaps.GmapsPlugin.superclass.initPlugin.apply(this,arguments);this.registerInsertionPoint("context.contact.contactcontentpanel.tabs",this.showContactLocation,this);this.registerInsertionPoint("context.addressbook.abuserdetailcontentpanel.tabs",this.showABUserLocation,this)},showContactLocation:function(){return{xtype:"gmaps.contactgmapstab",itemId:"contact"}},showABUserLocation:function(){return{xtype:"gmaps.contactgmapstab",
itemId:"abuser"}}});Zarafa.onReady(function(){container.registerPlugin(new Zarafa.core.PluginMetaData({name:"gmaps",displayName:_("Google Maps"),pluginConstructor:Zarafa.plugins.gmaps.GmapsPlugin}))});Ext.namespace("Zarafa.plugins.gmaps");
Zarafa.plugins.gmaps.GmapsTab=Ext.extend(Ext.Panel,{gmap:undefined,markers:undefined,latitude:null,longitude:null,constructor:function(a){a=a||{};a.plugins=Ext.value(a.plugins,[]);a.plugins.push("zarafa.recordcomponentupdaterplugin");Ext.apply(a,{xtype:"gmaps.contactgmapstab",title:_("Map"),bodyStyle:"width: 100%; height: 500px"});Zarafa.plugins.gmaps.GmapsTab.superclass.constructor.call(this,a)},onRender:function(){Zarafa.plugins.gmaps.GmapsTab.superclass.onRender.apply(this,arguments);this.latitude=
container.getSettingsModel().get("zarafa/v1/plugins/gmaps/lat");this.longitude=container.getSettingsModel().get("zarafa/v1/plugins/gmaps/lng");this.gmap=this.createGmap();this.markers=[]},createGmap:function(){var a={zoom:14,center:new google.maps.LatLng(this.latitude,this.longitude),mapTypeId:google.maps.MapTypeId.ROADMAP};return new google.maps.Map(this.body.dom,a)},update:function(a,c){if(a&&a.isOpened()){this.checkHash();switch(this.itemId){case "contact":if(c||a.isModifiedSinceLastUpdate("home_address")||
a.isModifiedSinceLastUpdate("business_address")||a.isModifiedSinceLastUpdate("other_address")){this.resetMarkers();var b=a.get("home_address");Ext.isEmpty(b.trim())||this.showOnMap(b,_("Home Address"));b=a.get("business_address");Ext.isEmpty(b.trim())||this.showOnMap(b,_("Business Address"));b=a.get("other_address");Ext.isEmpty(b.trim())||this.showOnMap(b,_("Other Address"));if(Ext.isEmpty(container.getSettingsModel().get("zarafa/v1/plugins/gmaps/default_address"))){this.latitude=51.996417;this.longitude=
4.3850826000000325}}break;case "abuser":if(c){this.resetMarkers();b=a.get("street_address")+" "+a.get("state_or_province")+" "+a.get("locality")+" "+a.get("postal_code")+" "+a.get("country");Ext.isEmpty(b.trim())||this.showOnMap(b)}break}}},checkHash:function(){var a=container.getSettingsModel().get("zarafa/v1/plugins/gmaps/default_address"),c=Ext.ux.MDHash(a),b=container.getSettingsModel().get("zarafa/v1/plugins/gmaps/md5");if(Ext.isEmpty(b)||b!=c){container.getSettingsModel().set("zarafa/v1/plugins/gmaps/md5",
c);(new google.maps.Geocoder).geocode({address:a},this.callbackGeocodeForSettings.createDelegate(this))}},callbackGeocodeResult:function(a,c,b,d){if(c==google.maps.GeocoderStatus.OK){a=a[0].geometry.location;this.callbackSetMarkers(d,a);container.getSettingsModel().get("zarafa/v1/plugins/gmaps/show_routes")&&this.renderRoutes(b,a)}},callbackGeocodeForSettings:function(a,c){if(c==google.maps.GeocoderStatus.OK){var b=a[0].geometry.location.lat(),d=a[0].geometry.location.lng();container.getSettingsModel().beginEdit();
container.getSettingsModel().set("zarafa/v1/plugins/gmaps/lat",b);container.getSettingsModel().set("zarafa/v1/plugins/gmaps/lng",d);container.getSettingsModel().endEdit()}},showOnMap:function(a,c){(new google.maps.Geocoder).geocode({address:a},this.callbackGeocodeResult.createDelegate(this,[a,c],true))},renderRoutes:function(a,c){var b=new google.maps.DirectionsService,d={origin:container.getSettingsModel().get("zarafa/v1/plugins/gmaps/default_address"),destination:c,travelMode:google.maps.TravelMode.DRIVING};
if(this.markers.length==1){d.destination=a;b.route(d,this.callbackSetDirections.createDelegate(this))}},callbackSetMarkers:function(a,c){this.gmap.setCenter(c);this.markers.push(new google.maps.Marker({map:this.gmap,position:c,title:a}))},callbackSetDirections:function(a,c){var b=new google.maps.DirectionsRenderer({draggable:true});b.setMap(this.gmap);c==google.maps.DirectionsStatus.OK&&b.setDirections(a)},resetMarkers:function(){for(var a=0;a<this.markers.length;a++)this.markers[a].setMap();this.markers=
[]}});Ext.reg("gmaps.contactgmapstab",Zarafa.plugins.gmaps.GmapsTab);
