/*
 * MapObject - The JavaScript Mapping Library.
 * http://MapObject.net
 * 
 * Copyright (c) 2006-2007, Mashup Technologies, LLC
 * All rights reserved.
 * http://www.MashupTechnologies.com
 * 
 * The JavaScript code distributed with Google Maps Samples (the "Software") is licensed under the
 * Lesser GNU (LGPL) open source license version 3.
 * 
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 */


Ext.namespace('MapObject.util');MapObject.util.Lang=function(){return{isEmpty:function(value){return!((typeof value!="undefined")&&(value!==null)&&(value!=""));},redirect:function(url){window.location=url;}}}();

Ext.namespace('MapObject.util');MapObject.util.Form=function(){return{setSelectedValue:function(element,value){var el=Ext.get(element).dom;for(var i=0;i<el.options.length;++i){if(el.options[i].value==value){el.options[i].selected=true;break;}}}}}();

Ext.namespace('MapObject.proxy');MapObject.proxy.Abstract=function(){this.addEvents({beforeload:true,load:true,loadexception:true,aftergeocoding:true});MapObject.proxy.Abstract.superclass.constructor.call(this);}
Ext.extend(MapObject.proxy.Abstract,Ext.util.Observable);

Ext.namespace('MapObject.proxy');MapObject.proxy.GoogleMap=function(mapEl,opt){MapObject.proxy.GoogleMap.superclass.constructor.call(this);this.init(mapEl,opt);}
Ext.extend(MapObject.proxy.GoogleMap,MapObject.proxy.Abstract,{mapEl:null,mapObject:null,defaultZoom:14,defaultMapType:'Normal',mapTypes:{normal:true,satellite:true,hybrid:true},mapControls:{smallMapControl:false,largeMapControl:false,smallZoomControl:true,scaleControl:false},tooltip:null,markersEngine:'Overlay',gmarkers:[],iconsSet:[],tooltipEngine:'v1',activeMarker:null,activeMarkerURL:'/media/icons/marker_20_active.png',init:function(mapEl,config){this.mapEl=document.getElementById(mapEl);if(GBrowserIsCompatible()){GEvent.addListener(window,'unload',GUnload);if(config&&config.activeMarkerURL){this.activeMarkerURL=config.activeMarkerURL;}
if(config&&config.tooltipEngine){this.tooltipEngine=config.tooltipEngine;}
if(config&&config.mapTypes){this.mapTypes=config.mapTypes;}
if(config&&config.mapControls){this.mapControls=config.mapControls;}
if(config&&config.defaultMapType){this.defaultMapType=config.defaultMapType;}}
else{alert("Sorry, the Google Maps API is not compatible with this browser");}},loadMap:function()
{if(this.mapObject==null)
{var opt={}
opt.mapTypes=[]
if(this.mapTypes.normal){opt.mapTypes.push(G_NORMAL_MAP);}
if(this.mapTypes.satellite){opt.mapTypes.push(G_SATELLITE_MAP);}
if(this.mapTypes.hybrid){opt.mapTypes.push(G_HYBRID_MAP);}
this.fireEvent("beforeloadmap",this);this.mapObject=new GMap2(this.mapEl,opt);this.mapObject.enableDoubleClickZoom();this.mapObject.enableScrollWheelZoom();if(this.mapControls.smallMapControl){this.mapObject.addControl(new GSmallMapControl());}
if(this.mapControls.largeMapControl){this.mapObject.addControl(new GLargeMapControl());}
if(this.mapControls.smallZoomControl){this.mapObject.addControl(new GSmallZoomControl());}
if(this.mapControls.scaleControl){this.mapObject.addControl(new GScaleControl());}
if(opt.mapTypes.length>1){this.mapObject.addControl(new GMapTypeControl());}
this.fireEvent("afterloadmap",this);}},setCenter:function(lat,lon,zoom,type)
{var point=new GLatLng(lat,lon);this.mapObject.setCenter(point,zoom||this.defaultZoom,type||this.getDefaultMapType());},addOverlay:function(lat,lon)
{var point=new GLatLng(lat,lon);if(!this.mapObject.isLoaded()){this.mapObject.setCenter(point,this.defaultZoom);}
this.mapObject.addOverlay(new GMarker(point));},getZoom:function()
{return this.mapObject.getZoom();},clearOverlays:function()
{this.mapObject.clearOverlays();},registerMapClick:function()
{GEvent.bind(this.mapObject,"click",this,function(overlay,point){this.addDragableMarker(point);this.onMapClick(point.lat(),point.lng());});},addDragableMarker:function(point)
{var marker=new GMarker(point,{draggable:true});this.mapObject.addOverlay(marker);GEvent.bind(marker,"dragend",this,function(){var point=marker.getPoint();this.onMapClick(point.lat(),point.lng());});},addDragableOverlay:function(lat,lon)
{var point=new GLatLng(lat,lon);if(!this.mapObject.isLoaded()){this.mapObject.setCenter(point,this.defaultZoom);}
this.addDragableMarker(point);},getTooltip:function(){if(!this.tooltip)
{this.tooltip=document.createElement("div");this.tooltip.className="tooltip";this.mapObject.getPane(G_MAP_FLOAT_PANE).appendChild(this.tooltip);this.tooltip.style.visibility="hidden";}
return this.tooltip;},addMarker:function(id,lat,lng,icon){lat=parseFloat(lat);lng=parseFloat(lng);point=new GLatLng(lat,lng);var markerOptions={};try
{if(!icon){markerOptions.icon=new GIcon(G_DEFAULT_ICON);}
else{markerOptions.icon=icon;}}
catch(e)
{markerOptions.icon=new GIcon(G_DEFAULT_ICON);}
var marker=new GMarker(point,markerOptions);marker.id=id;GEvent.bind(marker,"click",this,function(){this.onMarkerClick(marker);});this.gmarkers[id]=marker;if(this.markersEngine!='MarkerManager')
{try{this.mapObject.addOverlay(marker);}catch(e){alert(e.message)}}
GEvent.bind(marker,"mouseover",this,function(){this.showTooltip(marker);this.onMarkerMouseOver(marker);this.setActiveMarkerIcon(marker,true);});GEvent.bind(marker,"mouseout",this,function(){this.tooltip.style.visibility="hidden";this.onMarkerMouseOut(marker.id);this.setActiveMarkerIcon(marker,false);});return marker;},getMarker:function(id){return this.gmarkers[id];},panTo:function(marker)
{var latLongToCheck=marker.getPoint();if(!this.mapObject.getBounds().contains(latLongToCheck))
{this.mapObject.panTo(latLongToCheck);}},setActiveMarker:function(marker){if(this.activeMarker){this.activeMarker.setImage(this.activeMarker.getIcon().image);}
this.activeMarker=marker;},setActiveMarkerIcon:function(marker,active){if(!marker){return;}
if(this.activeMarker&&(this.activeMarker.id==marker.id)){return;}
if(active){marker.setImage(this.activeMarkerURL);}
else{marker.setImage(marker.getIcon().image);}},showTooltip:function(marker){var tooltip=this.getTooltip();try
{}
catch(e){}
tooltip.innerHTML=this.getToolTipText(marker.id);if(this.tooltipEngine=="v2")
{var point=this.mapObject.getCurrentMapType().getProjection().fromLatLngToPixel(this.mapObject.fromDivPixelToLatLng(new GPoint(0,0),true),this.mapObject.getZoom());var offset=this.mapObject.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),this.mapObject.getZoom());var anchor=marker.getIcon().iconAnchor;var iconW=marker.getIcon().iconSize.width;var iconH=marker.getIcon().iconSize.height;var oTTDim=Element.getDimensions(tooltip);var maxOffset_NE=this.mapObject.getCurrentMapType().getProjection().fromLatLngToPixel(this.mapObject.getBounds().getNorthEast(),this.mapObject.getZoom());var maxOffset_SW=this.mapObject.getCurrentMapType().getProjection().fromLatLngToPixel(this.mapObject.getBounds().getSouthWest(),this.mapObject.getZoom());var ttOffset=this.mapObject.getCurrentMapType().getProjection().fromLatLngToPixel(this.mapObject.fromDivPixelToLatLng(new GPoint(oTTDim.width,oTTDim.height),true),this.mapObject.getZoom());if(this.debug)
{var logMsg="oTTDim.height: "+oTTDim.height;logMsg+=" maxOffset_NE.x:  "+maxOffset_NE.x+" maxOffset_NE.y: "+maxOffset_NE.y;logMsg+=" maxOffset_SW.x:  "+maxOffset_SW.x+" maxOffset_SW.y: "+maxOffset_SW.y;logMsg+=" offset.y: "+offset.y+" point.y: "+point.y+" anchor.y: "+anchor.y;logMsg+=" COND X: "+(maxOffset_NE.x-offset.x)+" < "+oTTDim.width;}
if(maxOffset_NE.x-offset.x<oTTDim.width)
{posX=offset.x-point.x-anchor.x-oTTDim.width;}
else
{posX=offset.x-point.x-anchor.x+iconW;}
if(this.debug)logMsg+=" COND Y: "+(offset.y-maxOffset_NE.y-oTTDim.height)+"< 0";if(offset.y-maxOffset_NE.y-oTTDim.height<0)
{posY=offset.y-point.y-anchor.y;}
else
{posY=offset.y-point.y-anchor.y-oTTDim.height;}
var pos=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(posX,posY));if(this.debug)this.LogWriter.log(logMsg,"info");pos.apply(tooltip);}
else
{var point=this.mapObject.getCurrentMapType().getProjection().fromLatLngToPixel(this.mapObject.fromDivPixelToLatLng(new GPoint(0,0),true),this.mapObject.getZoom());var offset=this.mapObject.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),this.mapObject.getZoom());var anchor=marker.getIcon().iconAnchor;var width=marker.getIcon().iconSize.width;var height=tooltip.clientHeight;var pos=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(offset.x-point.x-anchor.x+width,offset.y-point.y-anchor.y-height));pos.apply(tooltip);}
tooltip.style.visibility="visible";},buildIcons:function(icons){var appIcon=null;var name=null;var x=null;var y=null;for(var i=0;i<icons.length;i++)
{name=icons[i].name;x="";y="";x=icons[i].copy;if(x&&gicons[x])
{appIcon=new GIcon(gicons[x]);}
else
{appIcon=new GIcon();}
appIcon.image=icons[i].image.src;x="";y="";x=icons[i].xanchor;y=icons[i].yanchor;if(x!=null&&y!=null&&x&&y)
{appIcon.iconAnchor=new GPoint(parseInt(x),parseInt(y));}
x="";y="";x=icons[i].xiwanchor;y=icons[i].yiwanchor;if(x!=null&&y!=null&&x&&y)
{appIcon.infoWindowAnchor=new GPoint(parseInt(x),parseInt(y));}
x="";y="";x=icons[i].image.width;y=icons[i].image.height;if(x!=null&&y!=null&&x&&y)
{appIcon.iconSize=new GSize(parseInt(x),parseInt(y));}
x="";x=icons[i].shadow.src;if(x!=null&&x)
{appIcon.shadow=x;}
x="";y="";x=icons[i].shadow.width;y=icons[i].shadow.height;if(x!=null&&y!=null&&x&&y)
{appIcon.shadowSize=new GSize(parseInt(x),parseInt(y));}
this.iconsSet[name]=appIcon;}},getDefaultMapType:function()
{switch(this.defaultMapType)
{case'Normal':return G_NORMAL_MAP;break;case'Satellite':return G_SATELLITE_MAP;break;case'Hybrid':return G_HYBRID_MAP;break;default:return G_NORMAL_MAP;}},getMapProxy:function(){return this;},onGeocode:function(lat,lon,accuracy){},onMapClick:function(lat,lon){},onZoomEnd:function(oldzoom,newzoom){},getToolTipText:function(id){},onMarkerMouseOut:function(marker){},onMarkerMouseOver:function(marker){},onMarkerClick:function(marker){},EOF:null});

Ext.namespace('MapObject.proxy');MapObject.proxy.GoogleGeocoder=function(){this.addEvents({aftergeocoding:true});MapObject.proxy.GoogleGeocoder.superclass.constructor.call(this);}
Ext.extend(MapObject.proxy.GoogleGeocoder,Ext.util.Observable,{gGeocoder:null,accuracy:[{version:'2.67',levels:[1,4,7,9,10,11,13,14,16]}],geocode:function(address)
{if(this.gGeocoder==null)
this.gGeocoder=new GClientGeocoder();if(this.gGeocoder)
{this.gGeocoder.getLocations(address,GEvent.callback(this,function(response)
{if(!response||response.Status.code!=200){alert("\""+address+"\" not found. Error code: "+response.Status.code);}else{place=response.Placemark[0];accuracy=place.AddressDetails.Accuracy;point=new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);if(this.debug)this.LogWriter.log('"'+address+'" geocoded to accuracy '+accuracy+", which is converted to "+this.accuracy[0].levels[accuracy]+' zoom level',"Info");this.fireEvent("aftergeocoding",place.Point.coordinates[1],place.Point.coordinates[0],this.accuracy[0].levels[accuracy]);}}));}},EOF:null});

Ext.namespace('MapObject.widget');MapObject.widget.MapViewer=function(config)
{this.init(config);}
MapObject.widget.MapViewer.prototype={}
Ext.extend(MapObject.widget.MapViewer,Ext.util.Observable,{mapViewer:null,ds:null,grid:null,recordViewer:null,tooltipTemplate:null,messages:{headerHelp:'Click on a grid headers to sort results. Select any record to see detail information.'},datasetLimit:400,init:function(config){this.ds=config.ds;this.grid=config.grid;this.recordViewer=config.recordViewer;this.tooltipTemplate=config.tooltipTemplate;function loadDetails(ds){var mapData=this.ds.reader.jsonData.map;this.mapViewer.defaultZoom=parseInt(mapData.zoom);this.mapViewer.getMapProxy().setCenter(parseFloat(mapData.lat),parseFloat(mapData.lng),this.mapViewer.defaultZoom);this.mapViewer.getMapProxy().mapObject.savePosition();this.mapViewer.buildIcons(this.ds.reader.jsonData.icons);function processDetails(record)
{var id=record.get('id');var lat=record.get('lat');var lng=record.get('lng');this.mapViewer.getMapProxy().addMarker(id,lat,lng,this.mapViewer.iconsSet[record.get('icon')]);}
this.ds.each(processDetails,this);var totalCount=parseInt(this.ds.reader.jsonData.totalRowsCount);if(totalCount==0){Ext.get('gridHelp').update("0 records found. Please adjust your selection criteria.");}
else if(totalCount>this.datasetLimit){Ext.get('gridHelp').update(totalCount+" record"+((totalCount==1)?"":"s")+" found. "+this.datasetLimit+" records displayed. "+this.messages.headerHelp);alert("Please adjust selection criteria to return less than "+this.datasetLimit+" records.");}
else{Ext.get('gridHelp').update(totalCount+" record"+((totalCount==1)?"":"s")+" found. "+this.messages.headerHelp);}}
this.ds.on('load',loadDetails,this);function beforeload(options){this.recordViewer.hide();if(this.mapViewer.getMapProxy().mapObject!=null){this.mapViewer.getMapProxy().clearOverlays();}
return true;}
this.ds.on('beforeload',beforeload,this);this.grid.on('rowclick',function(gridRef,rowIndex,e){var selectedRecord=this.grid.getSelectionModel().getSelected();if(selectedRecord!=undefined){var id=selectedRecord.get('id');this.recordViewer.showDetails(id);this.moveIntoMapView(id);this.mapViewer.getMapProxy().setActiveMarker(this.mapViewer.getMapProxy().getMarker(id));}},this);this.grid.on('mouseover',function(e,t){var rowIdx;var view=this.grid.getView();if((rowIdx=view.findRowIndex(t))!==false){var id=this.ds.getAt(rowIdx).get('id');this.setActiveMarkerIcon(id,true);}},this);this.grid.on('mouseout',function(e,t){var rowIdx;var view=this.grid.getView();if((rowIdx=view.findRowIndex(t))!==false){var id=this.ds.getAt(rowIdx).get('id');this.setActiveMarkerIcon(id,false);}},this);this.grid.render();this.mapViewer=new MapObject.proxy.GoogleMap(config.mapConfig.container,(config.mapConfig||{}));this.mapViewer.loadMap();this.mapViewer.getMapProxy().getToolTipText=this.getToolTipText.createDelegate(this);this.mapViewer.getMapProxy().onMarkerMouseOut=this.onMarkerMouseOut.createDelegate(this);this.mapViewer.getMapProxy().onMarkerMouseOver=this.onMarkerMouseOver.createDelegate(this);this.mapViewer.getMapProxy().onMarkerClick=this.onMarkerClick.createDelegate(this);this.ds.load();},zoomToMax:function(id){if(!id)id=this.mapViewer.getMapProxy().activeMarker.id;this.mapViewer.getMapProxy().mapObject.setCenter(this.mapViewer.getMapProxy().getMarker(id).getPoint(),16);},resetMap:function(){this.mapViewer.getMapProxy().mapObject.returnToSavedPosition();},moveIntoMapView:function(id){var record=this.ds.getById(id);this.mapViewer.getMapProxy().panTo(this.mapViewer.getMapProxy().getMarker(id));},getToolTipText:function(id){return this.tooltipTemplate.getHTML(id);},setActiveMarkerIcon:function(id,active){var record=this.ds.getById(id);this.mapViewer.getMapProxy().setActiveMarkerIcon(this.mapViewer.getMapProxy().getMarker(id),active);},onMarkerClick:function(marker){var index=this.ds.indexOfId(marker.id);this.grid.getSelectionModel().selectRow(index);this.grid.fireEvent('rowclick');},onMarkerMouseOver:function(marker){},onMarkerMouseOut:function(marker){},search:function(){this.ds.removeAll();this.ds.load();},getViewer:function(api){return this.mapViewer;}});

Ext.namespace('MapObject.widget');MapObject.widget.GeoCodingPanel=function(inputEl,actionEl,geocoder,mapProxy){this.init(inputEl,actionEl,geocoder,mapProxy);}
MapObject.widget.GeoCodingPanel.prototype={mapProxy:null,inputEl:null,actionEl:null,geocoder:null,init:function(inputEl,actionEl,geocoder,mapProxy){this.mapProxy=mapProxy;this.geocoder=geocoder;this.inputEl=document.getElementById(inputEl);this.actionEl=document.getElementById(actionEl);Ext.EventManager.addListener(this.inputEl,"keydown",function(e){if(e.getCharCode()==e.ENTER){this.geocode();}},this);Ext.EventManager.addListener(this.actionEl,"click",this.geocode,this);if(this.mapProxy!==null&&(typeof this.mapProxy.setCenter=='function')){this.geocoder.on("aftergeocoding",function(lat,lng,zoom){this.mapProxy.setCenter(lat,lng,zoom);},this);}},geocode:function(){this.geocoder.geocode(this.inputEl.value);}}

Ext.namespace('MapObject.widget');(function(){var self=null;function $(element){if(arguments.length>1){for(var i=0,elements=[],length=arguments.length;i<length;i++)
{elements.push($(arguments[i]));}
return elements;}
if(typeof element=='string')
{return document.getElementById(element);}}
MapObject.widget.StatusBar=function(el,opt){self=this;this.init(el,opt);}
MapObject.widget.StatusBar.prototype={el:null,lat:null,lng:null,zoom:null,init:function(el,opt){this.el=$(el);this.lat=$('mapStatusBarLat__MODULE_ID__');this.lng=$('mapStatusBarLng__MODULE_ID__');this.zoom=$('mapStatusBarZoom__MODULE_ID__');self=this;},show:function(){this.el.style.display="block";},setLat:function(val){this.show();this.lat.innerHTML=val;},setLng:function(val){this.show();this.lng.innerHTML=val;},setZoom:function(val){this.show();this.zoom.innerHTML=val;},EOF:null}})();
