/*
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
/// <reference path="http://jquery.gmap3.js" />
*/
(function($){$.fn.gmap3=function(options){var defaults={lat:0,lng:0,zoom:4,navControl:true};var options=$.extend(defaults,options);if(this.length>1){this.each(function(){$(this).gmap(options)});return this}this.map;this.overlays=[];this.initialize=function(){var latlng=new google.maps.LatLng(options.lat,options.lng);var settings={zoom:options.zoom,center:latlng,mapTypeControl:true,mapTypeControlOptions:{style:google.maps.MapTypeControlStyle.DROPDOWN_MENU},navigationControl:options.navControl,navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL},mapTypeId:google.maps.MapTypeId.SATELLITE};this.map=new google.maps.Map($(this)[0],settings);return this};this.setTypeRoadMap=function(){this.map.setMapTypeId(google.maps.MapTypeId.ROADMAP)};this.setTypeSatellite=function(){this.map.setMapTypeId(google.maps.MapTypeId.SATELLITE)};this.setTypeHybrid=function(){this.map.setMapTypeId(google.maps.MapTypeId.HYBRID)};this.setTypeTerrain=function(){this.map.setMapTypeId(google.maps.MapTypeId.TERRAIN)};this.setCenter=function(lat,lng){this.map.setCenter(new google.maps.LatLng(lat,lng))};this.getZoom=function(){return this.map.getZoom()};this.setZoom=function(level){this.map.setZoom(level)};this.addMarkerByAddress=function(address,title,html){var localmap=this.map;$.fn.gmap3.geoCodeAddress(address,function(latlng){return _addMarkerByLatLng(latlng,title,html,localmap)})};this.addMarkerByLatLng=function(lat,lng,title,html){var latlng=new google.maps.LatLng(lat,lng);return _addMarkerByLatLng(latlng,title,html,this.map,this.overlays)};this.addPath=function(data,opts){var defOpts={color:"#ff0000",opacity:1,strokeWeight:2};var opts=$.extend(defOpts,opts);if(data!=undefined){var path=new google.maps.Polyline({path:_convertData(data),strokeColor:opts.color,strokeOpacity:opts.opacity,strokeWeight:opts.strokeWeight});path.setMap(this.map);this.overlays.push(path)}return this};this.addPolygon=function(data,opts){return _addPolygonToMap(data,null,opts,this.map,this.overlays)};this.addClickablePolygon=function(data,html,opts){return _addPolygonToMap(data,html,opts,this.map,this.overlays)};this.clear=function(){if(this.overlays!=undefined){for(var i=0;i<this.overlays.length;i++){this.overlays[i].setMap(null)}this.overlays=[]}};this.toggleDebug=function(){var latLngControl=new _latLngControl(this.map);google.maps.event.addListener(this.map,"mouseover",function(mEvent){latLngControl.set("visible",true)});google.maps.event.addListener(this.map,"mouseout",function(mEvent){latLngControl.set("visible",false)});google.maps.event.addListener(this.map,"mousemove",function(mEvent){latLngControl.updatePosition(mEvent.latLng)});return this};this.onclickReverseGeocode=function(callback){geocode=google.maps.event.addListener(this.map,"click",function(me){$.fn.gmap3.geoCodeLatLng(me.latLng.lat(),me.latLng.lng(),function(address){if(callback!=undefined){callback(address)}})})};this.onclickGetLatLng=function(callback){geocode=google.maps.event.addListener(this.map,"click",function(me){var result=[me.latLng.lat(),me.latLng.lng()];if(callback!=undefined){callback(result)}})};$.fn.gmap3.geocoder=new google.maps.Geocoder();$.fn.gmap3.geoCodeLatLng=function(lat,lng,callback){var latlng=new google.maps.LatLng(lat,lng);$.fn.gmap3.geocoder.geocode({latLng:latlng},function(results,status){if(status==google.maps.GeocoderStatus.OK){var str=results[0].formatted_address;callback(str)}else{alert("Geocoder failed due to: "+status)}})};$.fn.gmap3.geoCodeAddress=function(address,callback){$.fn.gmap3.geocoder.geocode({address:address},function(results,status){if(status==google.maps.GeocoderStatus.OK){if(callback!=undefined){callback(results[0].geometry.location)}else{return results}}else{alert("Geocoder failed due to: "+status)}})};function _addMarkerByLatLng(latlng,title,html,theMap,overlays){var marker=new google.maps.Marker({position:latlng,map:theMap,title:title});overlays.push(marker);if(html!=undefined){var infowindow=new google.maps.InfoWindow();google.maps.event.addListener(marker,"click",function(){infowindow.setContent(html);infowindow.open(theMap,marker)})}return this}function _addPolygonToMap(data,html,opts,theMap,overlays){var defOpts={strokeColor:"#ff0000",strokeOpacity:0.8,strokeWeight:2,fillColor:"#ff0000",fillOpacity:0.35};var opts=$.extend(defOpts,opts);if(data!=undefined){var polygon=new google.maps.Polygon({paths:_convertData(data),strokeColor:opts.strokeColor,strokeOpacity:opts.strokeOpacity,strokeWeight:opts.strokeWeight,fillColor:opts.fillColor,fillOpacity:opts.fillOpacity});polygon.setMap(theMap);overlays.push(polygon);if(html!=undefined){var infowindow=new google.maps.InfoWindow();google.maps.event.addListener(polygon,"click",function(event){infowindow.setContent(html);infowindow.setPosition(event.latLng);infowindow.open(theMap)})}}return this}function _convertData(data){var pts=[];for(var i=0;i<data.length;i++){pts[i]=new google.maps.LatLng(data[i].lat,data[i].lng)}return pts}function _latLngControl(map){this.ANCHOR_OFFSET_=new google.maps.Point(8,8);this.node_=this.createHtmlNode_();map.controls[google.maps.ControlPosition.TOP].push(this.node_);this.setMap(map);this.set("visible",false)}_latLngControl.prototype=new google.maps.OverlayView();_latLngControl.prototype.draw=function(){};_latLngControl.prototype.createHtmlNode_=function(){var divNode=document.createElement("div");divNode.id="latlng-control";divNode.index=100;return divNode};_latLngControl.prototype.visible_changed=function(){this.node_.style.display=this.get("visible")?"":"none"};_latLngControl.prototype.updatePosition=function(latLng){var projection=this.getProjection();var point=projection.fromLatLngToContainerPixel(latLng);this.node_.style.left=point.x+this.ANCHOR_OFFSET_.x+"px";this.node_.style.top=point.y+this.ANCHOR_OFFSET_.y+"px";this.node_.innerHTML=[latLng.toUrlValue(4),"<br/>",point.x,"px, ",point.y,"px"].join("")};return this.initialize()}})(jQuery);
