(function($){$.cobalt=$.cobalt||{};$.cobalt.gmap=function(el,o){this.init(el,o);};$.fn.gmap=function(o){return this.each(function(){new $.cobalt.gmap(this,o);});};$.extend($.cobalt.gmap,{icons:{'call':{image:'/Shared/images/modules/map/m_call.png',shadow:'',iconSize:new GSize(32,17),iconAnchor:new GPoint(13,7)},'email':{image:'/Shared/images/modules/map/m_email.png',shadow:'',iconSize:new GSize(29,16),iconAnchor:new GPoint(14,7)},'visit':{image:'/Shared/images/modules/map/s_visit.png',shadow:'',iconSize:new GSize(21,14),iconAnchor:new GPoint(10,7)},'tcall':{image:'/Shared/images/modules/map/t_call.png',shadow:'',iconSize:new GSize(40,31),iconAnchor:new GPoint(13,27)},'temail':{image:'/Shared/images/modules/map/t_email.png',shadow:'',iconSize:new GSize(40,31),iconAnchor:new GPoint(14,27)},'tvisit':{image:'/Shared/images/modules/map/t_visit.png',shadow:'',iconSize:new GSize(26,16),iconAnchor:new GPoint(10,7)}}});$.extend($.cobalt.gmap.prototype,{latitude:function(val){if(val)this.setValue('Latitude',val);else return $.toFloat(this.getValue('Latitude'));},longitude:function(val){if(val)this.setValue('Longitude',val);else return $.toFloat(this.getValue('Longitude'));},radius:function(val){if(val)this.setValue('Radius',val);else return $.toInt(this.getValue('Radius'));},zoom:function(val){if(val)this.setValue('Zoom',val);else return $.toInt(this.getValue('Zoom'))||13;},setValue:function(field,val){if(this.options.Controls[field])this.options.Controls[field].val(val||'');this.options.Values[field]=val||'';},getValue:function(field){if(this.options.Controls[field])return this.options.Controls[field].val();else return this.options.Values[field];},pinid:1,pins:{},addPin:function(latlng){var pin=this.pins["center"];if(pin){pin.setLatLng(latlng);return;}pin=new GMarker(latlng,{draggable:this.options.MoveablePin});this.map.addOverlay(pin);this.pins["center"]=pin;pin.map=this;if(this.options.MoveablePin){GEvent.addListener(pin,"dragend",this.updatePin);GEvent.addListener(this.map,"dblclick",this.moveMap);}},updatePin:function(latlng){var map=this.map.map._gmap;map.latitude(latlng.y);map.longitude(latlng.x);if(map.options.AutoCircle)map.drawCircle();},moveMap:function(overlay,latlng){var map=this._gmap;map.movePin.call(this,overlay,latlng);this.panTo(latlng);},movePin:function(overlay,latlng){var map=this._gmap;map.latitude(latlng.y);map.longitude(latlng.x);map.addPin(latlng);if(map.options.AutoCircle)map.drawCircle();},_geoCoder:null,geoCoder:function(){if(!this._geoCoder){this._geoCoder=new GClientGeocoder();this._geoCoder._gmap=this;}return this._geoCoder;},getGeoAddress:function(){var sb=[];var commas=/,/g;var elem;elem=(this.getValue('Address')||'').replace(commas,'');if(elem)sb.push(elem);elem=(this.getValue('City')||'').replace(commas,'');if(elem)sb.push(elem);elem=(this.getValue('State')||'').replace(commas,'');if(elem)sb.push(elem);elem=(this.getValue('ZipCode')||'').replace(commas,'');if(elem)sb.push(elem);return sb.join(', ');},updateFromAddress:function(e){var map=this.updateFromAddress?this:$(this).data('gmap');var address=map.getGeoAddress();if(address){if(this.gmapint){clearInterval(this.gmapint);this.gmapint=0;}var fn=function(map){return function(latlng){if(!latlng){alert('That address could not be found.  Please double-check the spelling and try again.');return;}if(!map.mapset){map.map.setCenter(latlng,map.zoom());map.mapset=true;}else if(latlng.y!=map.latitude()||latlng.x!=map.longitude()){map.map.setCenter(latlng);}map.latitude(latlng.y);map.longitude(latlng.x);if(map.options.AutoPin){map.movePin.call(map.map,null,latlng);}if(map.options.UseExtents){map.populateExtents();}map=null;};}(map);map.geoCoder().getLatLng(address,fn);return true;}else return false;},checkAddress:function(){if(this.gmapint>0&&this.latitude()!=0&&this.longitude()!=0){clearInterval(this.gmapint);this.gmapint=0;return;}if(this.getValue('ZipCode')){this.updateFromAddress();clearInterval(this.gmapint);this.gmapint=0;}},drawCircle:function(e){var map=this.drawCircle?this:$(this).data('gmap');var miles=map.radius();var meters=miles*1609.344;var point=new GLatLng(map.latitude(),map.longitude());if(map.circle){map.map.removeOverlay(map.circle);map.circle=null;}map.circle=GPolyline.Circle(point,meters,'#000066',2,.5);map.map.addOverlay(map.circle);var bounds=map.circle.getBounds();var zoom=map.map.getBoundsZoomLevel(bounds);if(map.map.getZoom()>zoom)map.map.setCenter(point,zoom);if(!map.map.getBounds().containsBounds(bounds))map.map.panTo(point);map.populateList(point,miles);},populateList:function(point,miles){var pmap=this;if(!point||!point.x)point=new GLatLng(pmap.latitude(),pmap.longitude());if(!miles)miles=pmap.radius();if(pmap.options.CallAjax||pmap.options.AjaxList){var url;if(pmap.options.CallAjax)url=$.getAjaxUrl(window.location.href,{CallAjax:pmap.options.CallAjax,LT:point.y,LN:point.x,R:miles});else url=$.getAjaxUrl(pmap.options.AjaxList,{LT:point.y,LN:point.x,R:miles});if(url){$.ajax({url:url,dataType:'json',success:function(map){return function(results){map.Results=results;map.renderList();map=null;};}(this)});}}},_populateExtents:function(){var map=this._gmap;if(map._killmove){map._killmove=false;clearTimeout(map._killtimeout);map._killtimeout=null;return;}var latlng=this.getCenter();this._gmap.latitude(latlng.y);this._gmap.longitude(latlng.x);this._gmap.zoom(this.getZoom());this._gmap.populateExtents();},populateExtents:function(){var pmap=this;if(pmap.ajaxing){return;}var bounds=pmap.map.getBounds();var sw=bounds.getSouthWest();var ne=bounds.getNorthEast();var MinLat=sw.lat();var MinLng=sw.lng();var MaxLat=ne.lat();var MaxLng=ne.lng();if(pmap.options.CallAjax||pmap.options.AjaxList){pmap.ajaxing=true;var url;if(pmap.options.CallAjax){url=$.getAjaxUrl(window.location.href,{CallAjax:pmap.options.CallAjax,MinLat:MinLat,MinLng:MinLng,MaxLat:MaxLat,MaxLng:MaxLng});}else{url=$.getAjaxUrl(pmap.options.AjaxList,{MinLat:MinLat,MinLng:MinLng,MaxLat:MaxLat,MaxLng:MaxLng});}if(url){$(this.element).loading();$.ajax({url:url,dataType:'json',success:function(map){return function(results){$(map.element).doneLoading();map.ajaxing=false;map.Results=results;map.renderList();map=null;};}(this),error:function(map){return function(xhr){map.ajaxing=false;$(map.element).doneLoading();var m=/<title>([\s\S]*?)<\/title>/i.exec(xhr.responseText);var message;if(m&&m[1]){alert('Error - '+$.trim(m[1]));}map=null;};}(this)});}}},clearList:function(pins){if(pins){for(var p in pins){var pin=pins[p];if(p==='center'){center=pin;continue;}else{this.map.removeOverlay(pin);delete pins[p];}}}},renderList:function(){var n,e,s,w,results;if($.isFunction(this.onrender)){results=this.onrender(this)||this.Results;}else{results=this.Results;}var oldpins=this.pins;this.pins={};if(oldpins.center){this.pins.center=oldpins.center;delete oldpins.center;}for(var i=0;i<results.length;i++){var r=results[i];if(!n||r.Latitude>n){n=r.Latitude;}if(!e||r.Longitude>e){e=r.Longitude;}if(!s||r.Latitude<s){s=r.Latitude;}if(!w||r.Longitude<w){w=r.Longitude;}if(r.ID&&oldpins[r.ID]){delete oldpins[r.ID];}else{this.renderPin(r.Latitude,r.Longitude,r.HTML,r.Alert,r.Icon,r.ID);}}this.clearList(oldpins);oldpins=null;if(this.options.ZoomResults&&n&&e&&s&&w){var ne=new GLatLng(n,e);var sw=new GLatLng(s,w);var bounds=new GLatLngBounds(sw,ne);var zoom=this.map.getBoundsZoomLevel(bounds);if(this.options.Values.Zoom&&zoom>this.options.Values.Zoom){zoom=this.options.Values.Zoom;}var center=new GLatLng((n+s)/2, (w+e)/2);this.map.setCenter(center,zoom);}},renderPin:function(lat,lng,html,special,icon,id){var map=this;if(!map.pins.length&&map.options.AutoPin){map.addPin(map.map.getCenter());}var latlng=new GLatLng(lat,lng);var pin;if(special){pin=new GMarker(latlng,{icon:this.getIcon(icon||'pin2'),zIndexProcess:this.specialZIndex});}else{pin=new GMarker(latlng,{icon:this.getIcon(icon||'pin')});}this.map.addOverlay(pin);if(!id){id="PIN"+(this.pinid++)}this.pins[id]=pin;if(html){pin.myhtml=html;var e=map.options.PopupOn||'click';GEvent.addListener(pin,e,function(){this.openInfoWindowHtml(this.myhtml);if(this.cn){var map=$(this.cn).parents('div.imap:first');if(map.length){map[0]._gmap._killmove=true;var fn=function(map){return function(){map._killmove=false;map._killtimeout=null;map=null;};}(map[0]._gmap);map[0]._gmap._killtimeout=setTimeout(fn,1500);}}});}},getIcon:function(img){var properties=$.cobalt.gmap.icons[img];if(!properties){if(img&&img.indexOf('/')>=0){properties={image:img,shadow:null};if(this.options.IconSize){if($.isArray(this.options.IconSize)){this.options.IconSize=new GSize(this.options.IconSize[0],this.options.IconSize[1]);}properties.iconSize=this.options.IconSize;}}else{properties={image:"/Shared/images/modules/map/"+(img||'pin')+".png",shadow:"/Shared/images/modules/map/shadow.png"};}}var icon=new GIcon(G_DEFAULT_ICON);for(var p in properties){icon[p]=properties[p];}return icon;},specialZIndex:function(marker,b){return GOverlay.getZIndex(marker.getPoint().lat())+1000000;},setUSA:function(){var map=this;var ne=new GLatLng(48,-70);var sw=new GLatLng(28,-122.5);var usa=new GLatLngBounds(sw,ne);var zoom=map.map.getBoundsZoomLevel(usa);map.map.setCenter(usa.getCenter(),zoom);},init:function(el,o){el._gmap=this;this.element=el;this.options=o||{};if(!this.options.Controls)this.options.Controls={};if(!this.options.Values)this.options.Values={};var $el=$(el);if($el.is(':visible')){this._init();$el.trigger('mapready');$el.data('map',this);}},_init:function(){if(this._initialized){return;}else{this._initialized=true;}if(GBrowserIsCompatible()){var lat=this.latitude();var lng=this.longitude();this.map=new GMap2(this.element);this.map.disableDoubleClickZoom();if(!this.options.NoWheel){this.map.enableScrollWheelZoom();}this.map._gmap=this;this.map.addControl(new GSmallZoomControl());if(this.options.AutoCircle&&this.options.Controls.Radius)this.options.Controls.Radius.data('gmap',this).bind('change',this.drawCircle);if(this.options.UseExtents){GEvent.addListener(this.map,'moveend',this._populateExtents);}var centered=false;if(lat!=0&&lng!=0){var center=new GLatLng(lat,lng);this.map.setCenter(center,this.zoom());this.mapset=true;if(this.options.AutoPin)this.addPin(center);if(this.options.AutoCircle)this.drawCircle();if(this.options.UseExtents||this.options.Single)this.populateExtents();centered=true;}if(this.options.MatchAddress&&this.options.Controls.ZipCode){this.options.Controls.ZipCode.data('gmap',this).bind('blur',this.updateFromAddress).onenter(function(e){this.blur();return false;});if(!centered){var result=this.updateFromAddress();if(!result){var map=this;this.gmapint=setInterval(function(){map.checkAddress();},1000);this.setUSA();}}}else if(!centered){if(!this.updateFromAddress()){this.setUSA();this.populateExtents();}}}}});})(jQuery);GPolyline.Shape=function(point,r1,r2,r3,r4,rotation,vertexCount,colour,weight,opacity,opts,tilt){var rot=-rotation*Math.PI/180;var points=[];var latConv=point.distanceFrom(new GLatLng(point.lat()+0.1,point.lng()))*10;var lngConv=point.distanceFrom(new GLatLng(point.lat(),point.lng()+0.1))*10;var step=(360/vertexCount)||10;var flop=-1;if(tilt){var I1=180/vertexCount;}else{var I1=0;}for(var i=I1;i<=360.001+I1;i+=step){var r1a=flop?r1:r3;var r2a=flop?r2:r4;flop=-1-flop;var y=r1a*Math.cos(i*Math.PI/180);var x=r2a*Math.sin(i*Math.PI/180);var lng=(x*Math.cos(rot)-y*Math.sin(rot))/lngConv;var lat=(y*Math.cos(rot)+x*Math.sin(rot))/latConv;points.push(new GLatLng(point.lat()+lat,point.lng()+lng));}return(new GPolyline(points,colour,weight,opacity,opts));};GPolyline.Circle=function(point,radius,colour,weight,opacity,opts){return GPolyline.Shape(point,radius,radius,radius,radius,0,100,colour,weight,opacity,opts)};GPolyline.RegularPoly=function(point,radius,vertexCount,rotation,colour,weight,opacity,opts){rotation=rotation||0;var tilt=!(vertexCount&1);return GPolyline.Shape(point,radius,radius,radius,radius,rotation,vertexCount,colour,weight,opacity,opts,tilt)};GPolyline.Star=function(point,r1,r2,points,rotation,colour,weight,opacity,opts){rotation=rotation||0;return GPolyline.Shape(point,r1,r1,r2,r2,rotation,points*2,colour,weight,opacity,opts)};GPolyline.Ellipse=function(point,r1,r2,rotation,colour,weight,opacity,opts){rotation=rotation||0;return GPolyline.Shape(point,r1,r2,r1,r2,rotation,100,colour,weight,opacity,opts)};GPolygon.Shape=function(point,r1,r2,r3,r4,rotation,vertexCount,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts,tilt){var rot=-rotation*Math.PI/180;var points=[];var latConv=point.distanceFrom(new GLatLng(point.lat()+0.1,point.lng()))*10;var lngConv=point.distanceFrom(new GLatLng(point.lat(),point.lng()+0.1))*10;var step=(360/vertexCount)||10;var flop=-1;if(tilt){var I1=180/vertexCount;}else{var I1=0;}for(var i=I1;i<=360.001+I1;i+=step){var r1a=flop?r1:r3;var r2a=flop?r2:r4;flop=-1-flop;var y=r1a*Math.cos(i*Math.PI/180);var x=r2a*Math.sin(i*Math.PI/180);var lng=(x*Math.cos(rot)-y*Math.sin(rot))/lngConv;var lat=(y*Math.cos(rot)+x*Math.sin(rot))/latConv;points.push(new GLatLng(point.lat()+lat,point.lng()+lng));}return(new GPolygon(points,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts))};GPolygon.Circle=function(point,radius,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts){return GPolygon.Shape(point,radius,radius,radius,radius,0,100,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts)};GPolygon.RegularPoly=function(point,radius,vertexCount,rotation,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts){rotation=rotation||0;var tilt=!(vertexCount&1);return GPolygon.Shape(point,radius,radius,radius,radius,rotation,vertexCount,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts,tilt)};GPolygon.Star=function(point,r1,r2,points,rotation,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts){rotation=rotation||0;return GPolygon.Shape(point,r1,r1,r2,r2,rotation,points*2,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts)};GPolygon.Ellipse=function(point,r1,r2,rotation,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts){rotation=rotation||0;return GPolygon.Shape(point,r1,r2,r1,r2,rotation,100,strokeColour,strokeWeight,Strokepacity,fillColour,fillOpacity,opts)};function EOffset(point,easting,northing){var latConv=point.distanceFrom(new GLatLng(point.lat()+0.1,point.lng()))*10;var lngConv=point.distanceFrom(new GLatLng(point.lat(),point.lng()+0.1))*10;return new GLatLng(point.lat()+northing/latConv,point.lng()+easting/lngConv)}function EOffsetBearing(point,dist,bearing){var latConv=point.distanceFrom(new GLatLng(point.lat()+0.1,point.lng()))*10;var lngConv=point.distanceFrom(new GLatLng(point.lat(),point.lng()+0.1))*10;var lat=dist*Math.cos(bearing*Math.PI/180)/latConv;var lng=dist*Math.sin(bearing*Math.PI/180)/lngConv;return new GLatLng(point.lat()+lat,point.lng()+lng)}
