/*********************************************************************************************/
/* BlakeMedia Library
/* "Google Map" plugin
/*
/* DESC:    	Customise, draw and modify a Google Map.
/* AUTHOR:		Jared Williams (BlakeMedia)
/* VERSION:		1.33
/* CREATED: 	04 August 2014
/* MODIFIED:	04 August 2014
/*
/* (c) Copyright 2002-2014 BlakeMedia & MediaIQ Group PLC
/*********************************************************************************************/
var googlemap=function(e){function o(){t(),to&&n()}function t(){"name"in e&&(k=e.name),"container"in e&&(h=e.container),"style"in e&&(O=e.style),"center"in e&&(R=e.center),"focustop"in e&&(L=e.focustop),"focusbottom"in e&&(L=-1*e.focusbottom),"focusleft"in e&&(T=e.focusleft),"focusright"in e&&(T=-1*e.focusright),"lat"in e&&(C=e.lat),"lng"in e&&(E=e.lng),"zoom"in e&&(_=e.zoom),"markername"in e&&(A=e.markername),"focusmarkers"in e&&(x=e.focusmarkers),"key"in e&&(S=e.key),"region"in e&&(D=e.region),"styles"in e&&(N=e.styles),"googleui"in e&&(Z=e.googleui),"streetview"in e&&(V=e.streetview),"scrollwheel"in e&&(U=e.scrollwheel),"onclick"in e&&(q=e.onclick),"ondblclick"in e&&(H=e.ondblclick),"onrightclick"in e&&(J=e.onrightclick),"onzoom"in e&&(K=e.onzoom),"ontilt"in e&&(Y=e.ontilt),"ondrag"in e&&(Q=e.ondrag),"onresize"in e&&(X=e.onresize),"onmarkerclick"in e&&(eo=e.onmarkerclick),"oncircleclick"in e&&(oo=e.oncircleclick),"autoload"in e&&(to=e.autoload)}function n(){i(),s(),c(),d(),f.defineMapStyle("default",{},!0),f.defineMarkerStyle("default",{icon:""},!0),f.defineCircleStyle("default",{stroke:{},fill:{}},!0)}function i(){var e,o="";if("undefined"!=typeof google)if("maps"in google)if("version"in google.maps){if(e=parseFloat(google.maps.version).toFixed(1),e>=3&&4>e)return!0;o="API version "+e+" not supported!"}else o="API version number not found!";else o="Google Maps library not loaded!";else o="Google library not loaded!";bm.log("Failed to load Google Map: "+o,LOG_ERROR_FATAL,f)}function r(e,o){o||(o={});var t,n,i;return"string"==typeof o?o={base:o,water:o}:o instanceof Array&&(t=o),"base"in o&&(n=o.base),"water"in o&&(i=o.water),!n&&!i||t?t||(t=[{}]):t=[{featureType:"all",stylers:[,{saturation:0}]},{featureType:"all",elementType:"labels",stylers:[,{hue:n},{saturation:5}]},{featureType:"water",elementType:"all",stylers:[{color:i}]},{featureType:"landscape",elementType:"geometry",stylers:[{hue:n},{saturation:100},{brightness:100}]},{featureType:"poi",elementType:"geometry",stylers:[{hue:n},{saturation:100},{brightness:70}]},{featureType:"road.highway",elementType:"geometry",stylers:[{hue:n},{saturation:100},{brightness:100}]},{featureType:"road.arterial",elementType:"geometry",stylers:[{hue:n},{saturation:100},{brightness:100}]}],t=new google.maps.StyledMapType(t,{name:e})}function a(e){e||(e={}),"icon"in e||(e.icon=e.icon),"bg"in e||(e.bg="f26119"),"outline"in e||(e.outline="000000"),"font"in e||(e.font=e.outline);var o,t={};return"icon"in e?"anchor"in e&&("x"in e.anchor||(e.anchor.x=0),"y"in e.anchor||(e.anchor.y=0),o=new google.maps.Point(e.anchor.x,e.anchor.y)):e.icon="http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld= |"+e.bg+"|"+e.font+"|"+e.outline,t=e.icon?new google.maps.MarkerImage(e.icon,null,null,o):null}function l(e,o){o||(o={});var t={};return t=o,"radius"in o&&(t.radius=o.radius),t}function s(){p=new google.maps.Geocoder,v=new google.maps.LatLngBounds}function c(){var e,o,t;if(_=parseInt(_),_||(bm.log("Default zoom level invalid, resetting to 15...",LOG_WARNING,f),_=15),b=_,R)e=f.convertToGeo(R);else{if(!C||!E)return void bm.log("Failed to draw map: no map center provided!",LOG_ERROR_FATAL,f);e=f.convertToGeo(C,E)}o={zoom:b,center:e,mapTypeControl:!1,mapTypeId:google.maps.MapTypeId.ROADMAP,disableDefaultUI:!Z,streetViewControl:V,scrollwheel:U},t=document.getElementById(h),t?(f.map=new google.maps.Map(t,o),y=$("#"+h),M=y.width(),G=y.height()):bm.log('Failed to draw map: container with ID "'+h+'" not found!',LOG_ERROR_FATAL,f)}function d(){google.maps.event.addListener(f.map,"click",function(e){q(e)}),google.maps.event.addListener(f.map,"dblclick",function(e){H(e)}),google.maps.event.addListener(f.map,"rightclick",function(e){J(e)}),google.maps.event.addListener(f.map,"zoom_changed",function(e){var o={before:b,after:f.map.getZoom()};K(e,o),b=f.map.getZoom()}),google.maps.event.addListener(f.map,"tilt_changed",function(e){Y(e)}),google.maps.event.addListener(f.map,"drag",function(e){Q(e)}),google.maps.event.addListener(f.map,"resize",function(e){X(e,M,G)}),$(window).resize(function(){(y.width()!=M||y.height()!=G)&&(M=elem.width(),G=elem.height(),google.maps.event.trigger(f.map,"resize"))})}function g(){v=new google.maps.LatLngBounds,f.map.fitBounds(v)}function u(e,o,t){var n=function(e){bm.log("Failed to lookup user's address: "+e.message+" (error code "+e.code+")",LOG_ERROR,f),o(e)},i=function(o){var n,i,r;n=o.coords.latitude,i=o.coords.longitude,r=f.convertToGeo(n,i),t&&(o=r),bm.log("Successfully geocoded user's address to "+r.toString(),LOG_NOTICE,f),e(o)};bm.log("Attempting to lookup user's location...",LOG_NOTICE,f),navigator.geolocation.getCurrentPosition(i,n)}function m(e,o,t){var n={};$.getScript("//www.google.com/jsapi",function(){google.loader.ClientLocation&&(n={latitude:google.loader.ClientLocation.latitude,longitude:google.loader.ClientLocation.longitude,lat:function(){return this.latitude},lng:function(){return this.longitude}},n.prototype.toString=function(){return"("+this.lat()+", "+this.lng()+")"},t&&(n=f.convertToGeo(n.lat(),n.lng()))),bm.log("Geocoded user's address to "+n.toString(),LOG_NOTICE,f),e(n)}).fail(function(){bm.log('Failed to geocode address "'+Address+'": Google script not found!',LOG_ERROR,f),o()})}var f=this;this.instantiated=!1,this.map=!1;var p="",v="",k="My Map",y=null,h="map",O="",b=0,R=!1,M=0,G=0,L=0,T=0,_=15,C=0,E=0,I="drop",A="Map Marker",F="",w="",N=[],S="",D="AU",P=[],z=0,B=[],x=!0,j=[],W=[],U=!0,Z=!0,V=!0,q=function(){},H=function(){},J=function(){},K=function(){},Y=function(){},Q=function(){},X=function(){},eo=function(){},oo=function(){},to=!0;this.about=function(){return{name:"Google Map",shortname:"googlemap",description:"Customise, draw and modify a Google Map.",version:"1.33",library:"2.2",api:"3.0",datecreated:"04 Aug 2014",datemodified:"04 Aug 2014",author:"Jared Williams (BlakeMedia)"}},this.changeMapType=function(e){var o;switch(e){case"hybrid":o=google.maps.MapTypeId.HYBRID;break;case"terrain":o=google.maps.MapTypeId.TERRAIN;break;case"satellite":case"sat":o=google.maps.MapTypeId.SATELLITE;break;case"road":default:o=google.maps.MapTypeId.ROADMAP}return f.map.setMapTypeId(o),bm.log('Map type changed to "'+e+'"',LOG_NOTICE,f),!0},this.geocodeAddress=function(e,o,t,n){o||(o=function(){}),t||(t=function(){}),n||n===!1||(n=!0);var i;e?"string"==typeof e?(bm.log('Geocoding address "'+e+'"...',LOG_NOTICE,f),p.geocode({address:e},function(r,a){a==google.maps.GeocoderStatus.OK?(i=r[0],pos=i.geometry.location,pos=f.convertToGeo(pos),n&&(i=pos),bm.log('Successfully geocoded address "'+e+'" to '+pos.toString(),LOG_NOTICE,f),o(i)):(bm.log('Failed to geocode address "'+e+'": '+a+"!",LOG_ERROR,f),t())})):bm.log('Failed to geocode address "'+e+'": invalid address!',LOG_ERROR,f):navigator&&navigator.geolocation?u(o,t,n):m(o,t,n)},this.geocode=function(e,o,t){return f.geocodeAddress(e,o,t)},this.convertToGeo=function(e,o){var t=0,n=0;if(e instanceof Array){if(t=e[0],n=e[1],"undefined"==typeof t)return void bm.log("Failed to convert variable to geolocation: array values empty!",LOG_ERROR,f)}else if("object"==typeof e){if("lat"in e&&"function"==typeof e.lat)return e;if("getPosition"in e)return e.getPosition();if("geometry"in e)return e.geometry.location;for(var i in e)switch(i.toLowerCase()){case"posx":case"x":case"lat":t=e[i];break;case"posy":case"y":case"lng":n=e[i]}}else if("number"==typeof e||"string"==typeof e)t=e,n=o;else if("undefined"==typeof e)return void bm.log("Failed to convert variable to geolocation: no variable provided!",LOG_ERROR,f);return isNaN(t)||isNaN(n)?(o=o?', "'+o+'"':"",void bm.log('Failed to convert variable "'+e+'"'+o+" to geolocation!",LOG_ERROR,f)):new google.maps.LatLng(t,n)},this.convertGeoToArray=function(e){if(e=f.convertToGeo(e)){var o=e.toString();o=o.replace(/\(|\)/g,"");var t=o.split(",",2),n=parseFloat(t[0]),i=parseFloat(t[1]);return[n,i]}bm.log("Failed to convert geolocation to array: invalid geolocation!",LOG_ERROR,f)},this.convertArrayToGeo=function(e){return bm.log("convertArrayToGeo() is deprecated! Use convertToGeo()!",LOG_WARNING,bm),f.convertToGeo(e)},this.plotUser=function(e,o,t){e||(e={}),o||(o=function(){}),t||(t=function(){}),f.geocodeAddress(!1,function(t){e.pos=t;var n=bm.googlemap.addMarker(A,e);o(n)},function(){t()})},this.getMapStyle=function(e){return e?N[e]:O?N[O]:{}},this.defineMapStyle=function(e,o,t){var n;return n=r(e,o),N[e]=n,f.map.mapTypes.set(e,n),bm.log('Defined map style "'+e+'"',LOG_NOTICE,f),t&&f.changeMapStyle(e),!0},this.changeMapStyle=function(e,o){var t;return e&&o&&f.defineMapStyle(e,o),(t=f.getMapStyle(e))?(f.map.setMapTypeId(e),O=e,bm.log('Map style changed to "'+e+'"',LOG_NOTICE,f),!0):void bm.log('Failed to change map style: "'+e+'" style not found!',LOG_ERROR,f)},this.revertMapStyle=function(){return f.changeMapStyle("default")},this.isMarker=function(e){return"type"in e&&"icon"in e},this.getMarker=function(e){var o=!1;return e&&("object"==typeof e?"getPosition"in e&&(o=e):e&&e in P&&(o=P[e])),o},this.getMarkers=function(){return P.length||bm.log("No markers found when getting all markers!",LOG_WARNING,f),P},this.getMarkerID=function(e){if(e){if(e=f.getMarker(e),e&&"id"in e)return e.id;bm.log("Failed to get marker ID: invalid marker!",LOG_ERROR,f)}else bm.log("Failed to get marker ID: no marker provided!",LOG_ERROR,f)},this.addMarker=function(e,o){var t,n;return n="id"in o?o.id:parseInt(z)+1,t=new google.maps.Marker({map:f.map,type:"point",id:n}),P[n]=t,o.name=e,t.custom={},t.temp={},z=n,f.editMarker(t,o,!0),bm.log('Added marker "'+e+'" to '+t.getPosition().toString(),LOG_NOTICE,f),t},this.plotMarker=function(e,o){return f.addMarker(e,o)},this.addMarkers=function(e){var o,t=0,n=0,i=[];if(e&&("object"==typeof e||e instanceof Array)){for(var r in e)o=e[r],"object"==typeof o&&(o=f.addMarker(o.name,o))?(t++,i[i.length]=o):n++;return n=n?" ("+n+" failed)":"",bm.log("Added "+t+" markers "+n,LOG_NOTICE,f),i}bm.log("Failed to add markers: invalid markers passed!",LOG_ERROR,f)},this.editMarker=function(e,o,t){o||(o={}),t&&("id"in o||(o.id=null),"name"in o||(o.name="My Marker"),"title"in o||(o.title=o.name),"lat"in o||(o.lat=0),"lng"in o||(o.lng=0),"pos"in o||(o.pos={lat:o.lat,lng:o.lng}),"zoom"in o||(o.zoom=_),"focus"in o||(o.focus=x),"anim"in o||(o.anim=I),"style"in o||(o.style=F),"onclick"in o||(o.onclick=!0),"custom"in o||(o.custom={}));var n,i={};if(n=f.getMarker(e)){if("style"in o&&(i.icon=f.getMarkerStyle(o.style),i.icon||(i.icon=f.getMarkerStyle(F))),"anim"in o)switch(o.anim){case"bounce":i.animation=google.maps.Animation.BOUNCE;break;case"none":i.animation=null;break;case"drop":default:i.animation=google.maps.Animation.DROP}return"lat"in o&&"lng"in o&&(i.position=f.convertToGeo(o.lat,o.lng)),"pos"in o&&(i.position=f.convertToGeo(o.pos)),"onclick"in o&&o.onclick&&(onclick=o.onclick===!0?function(e){f.focusMarker(e)}:o.onclick,google.maps.event.addListener(n,"click",function(){onclick(this),eo(this)})),"name"in o&&(i.name=o.name,i.title=o.name),"title"in o&&(i.name=o.title,i.title=o.title),"id"in o&&(i.id=o.id),"custom"in o&&(n.custom=o.custom),"temp"in o&&(n.temp=o.temp),n.setOptions(i),"focus"in o&&o.focus&&f.focusMarker(n,o.zoom),n}bm.log('Marker "'+n+'" not found!',LOG_ERROR,f)},this.editMarkers=function(e,o){e||(e=f.getMarkers());var t,n=0,i=[];if("object"==typeof e||e instanceof Array){for(var r in e)t=e[r],(t=f.editMarker(t,o))&&(n++,i[i.length]=t);return bm.log("Edited "+n+" markers",LOG_NOTICE,f),i}bm.log("Failed to edit markers: invalid markers passed!",LOG_WARNING,f)},this.removeMarker=function(e){var o,t,n;if(o=f.getMarker(e),t=f.getMarkers(),o)for(var i in t)if(t[i]==o)return n=f.getMarkerID(o),o.setMap(null),t.splice(i,1),bm.log('Removed marker "'+o.title+'"',LOG_NOTICE,f),!0;bm.log('Failed to remove marker: invalid marker ID "'+MarkerID+'"',LOG_ERROR,f)},this.removeMarkers=function(){var e=[],o=0;if(e=f.getMarkers())for(var t=0;t<e.length;t++)e[t].setMap(null),o++;P=[],bm.log("Removed all "+o+" markers",LOG_NOTICE,f)},this.moveMarker=function(e,o){var t,n,i;if(t=f.getMarker(e)){if(n=t.getPosition(),i=f.convertToGeo(o))return t.setPosition(i),bm.log("Moved marker "+f.getMarkerID(t)+" from "+n.toString()+" to "+i.toString(),LOG_NOTICE,f),!0;bm.log("Failed to move marker: invalid position!",LOG_ERROR,f)}else bm.log('Failed to move marker: invalid marker ID "'+e+'"!',LOG_ERROR,f)},this.focusMarker=function(e,o,t){e||(e=f.getMarker()),o||(o=_);{var n,i;parseInt(o)}return(n=f.getMarker(e))?(i=n.getPosition(),t?f.map.panTo(i):f.map.setCenter(i),o&&f.map.setZoom(o),f.indentMap(),bm.log('Focused on marker "'+n.name+'"',LOG_NOTICE,f),!0):void bm.log('Failed to focus marker: invalid marker "'+e+'"!',LOG_ERROR,f)},this.focusMarkers=function(e,o){e||(e={}),o||(o=0);var t,n;if(g(),e.length?n=e.length:(e=f.getMarkers(),n="all"),e.length){for(var i in e)t=f.getMarker(e[i]),t?v.extend(t.getPosition()):bm.log("Failed to focus on marker in marker set: invalid marker!",LOG_ERROR,f);f.map.fitBounds(v),o&&f.map.setZoom(o),f.indentMap()}bm.log("Focused on "+n+" markers",LOG_NOTICE,f)},this.showMarker=function(e){var o=f.getMarker(e);return o?(o.setVisible(!0),!0):void bm.log('Failed to show marker: invalid marker ID "'+e+'"!',LOG_ERROR,f)},this.hideMarker=function(e){var o=f.getMarker(e);return o?(o.setVisible(!1),!0):void bm.log('Failed to hide marker: invalid marker ID "'+e+'"!',LOG_ERROR,f)},this.clickMarker=function(e){var o=f.getMarker(e);o?new google.maps.event.trigger(o,"click"):bm.log('Failed to click marker: invalid marker ID "'+e+'"!',LOG_ERROR,f)},this.getMarkerStyle=function(e){return e||(e=F),e in B?B[e]:void bm.log('Failed to get marker style "'+e+'": does not exist!',LOG_ERROR,f)},this.defineMarkerStyle=function(e,o,t,n){var i,r="";return i=a(o),B[e]=i,(t||!F)&&(F=e,r=" (as default)"),bm.log('Defined marker style "'+e+'"'+r,LOG_NOTICE,f),n&&f.editMarkers(!1,{style:e}),!0},this.getCircle=function(e){var o=!1;return e&&("object"==typeof e?"getCenter"in e&&(o=e):e in j&&(o=j[e])),o},this.getCircles=function(){return j},this.addCircle=function(e){var o;return o=new google.maps.Circle({map:f.map}),j[j.length]=o,o=f.editCircle(o,e,!0),bm.log("Added circle at "+o.getCenter().toString(),LOG_NOTICE,f),o},this.editCircle=function(e,o,t){o||(o={}),t||(t=!1),t&&("lat"in o||(o.lat=0),"lng"in o||(o.lng=0),"pos"in o||(o.pos={lat:o.lat,lng:o.lng}),"style"in o||(o.style=w),"radius"in o||(o.radius=0),"onclick"in o||(o.onclick=!0),"custom"in o||(o.custom={}));var n,i,r={};return(n=f.getCircle(e))?("style"in o&&(i=f.getCircleStyle(o.style),i&&(r.strokeColor=i.stroke.color,r.strokeOpacity=i.stroke.opacity,r.strokeWeight=i.stroke.weight,r.fillColor=i.fill.color,r.fillOpacity=i.fill.opacity,"radius"in i&&(r.radius=i.radius))),"lat"in o&&"lng"in o&&(r.center=f.convertToGeo(o.lat,o.lng)),"pos"in o&&(r.center=f.convertToGeo(o.pos)),"radius"in o&&o.radius>0&&(r.radius=o.radius),"onclick"in o&&o.onclick&&(onclick=o.onclick===!0?function(){}:o.onclick,google.maps.event.addListener(n,"click",function(){onclick(this),oo(this)})),"custom"in o&&(n.custom=o.custom),n.setOptions(r),n):void bm.log("Failed to edit circle: circle not found!",LOG_NOTICE,f)},this.editCircles=function(e,o){e||(e=f.getCircles());var t,n=0,i=[];if("object"==typeof e||e instanceof Array){for(var r in e)t=e[r],(t=f.editCircle(t,o))&&(n++,i[i.length]=t);return bm.log("Edited "+n+" circles",LOG_NOTICE,f),i}bm.log("Failed to edit circles: invalid circles passed!",LOG_WARNING,f)},this.addCircles=function(e,o){o||(o={});var t,n=0,i=0;if(e&&("object"==typeof e||e instanceof Array)){for(var r in e)t=e[r],t=$.extend({},t,o),f.addCircle(t)?n++:i++;return i=i?" ("+i+" failed)":"",bm.log("Added "+n+" circles "+i,LOG_NOTICE,f),!0}bm.log("Failed to add circles: invalid circles passed!",LOG_ERROR,f)},this.removeCircle=function(e){if(e=f.getCircle(e)){var o=e.id;return j[o].setMap(null),j.splice(key,1),bm.log('Removed circle "'+o+'"',LOG_NOTICE,f),!0}bm.log('Failed to remove circle: invalid marker "'+e+'"',LOG_ERROR,f)},this.removeCircles=function(){var e=[],o=0;if(e=f.getCircles())for(var t=0;t<e.length;t++)e[t].setMap(null),o++;j=[],bm.log("Removed all "+o+" circles",LOG_NOTICE,f)},this.getCircleStyle=function(e){return e||(e=w),e in W?W[e]:void bm.log('Failed to get circle style "'+e+'": does not exist!')},this.defineCircleStyle=function(e,o,t,n){var i,r="";return i=l(e,o),W[e]=i,(t||!w)&&(w=e,r=" (as default)"),bm.log('Defined circle style "'+e+'"'+r,LOG_NOTICE,f),n&&f.editCircles(!1,{style:e}),!0},this.indentMap=function(e,o){return e||0===e||(e=L),o||0===o||(o=T),L=e,T=o,f.map.panBy(-1*o,-1*e),!0},this.getCenter=function(){return f.map.getCenter()},this.sortMarkersByClosest=function(e){var o;if(e){if(f.isMarker(e)&&(e=e.getPosition()),e=f.convertToGeo(e)){o=f.convertGeoToArray(e);var t,n=o[0],i=o[1],r=[n,i],a="",l=f.getMarkers(),s=[];for(var c in l)t=l[c],t.temp={},pos=t.getPosition(),pos=f.convertGeoToArray(pos),lat=pos[0],lng=pos[1],lat!=o[0]&&lng!=o[1]&&(dist=f.calcDistance(pos,o),t.temp.fromorigin=dist,s[s.length]=t);l=s.sort(function(e,o){return parseFloat(e.temp.fromorigin)-parseFloat(o.temp.fromorigin)}),s=[];for(var c in l){t=l[c];var d=t.position.lat(),g=t.position.lng(),u=[d,g];a=f.calcDistance(r,u),t.temp.fromlast=a,s[s.length]=t,r=u}return bm.log("Successfully sorted "+s.length+" markers by closest to "+e.toString(),LOG_NOTICE,f),s}bm.log("Failed to sort markers by closest: invalid origin provided!",LOG_ERROR,f)}else bm.log("Failed to sort markers by closest: no origin provided!",LOG_ERROR,f)},this.getClosestMarker=function(e){var o=f.sortMarkersByClosest(e);return o.length>0?o[0]:void bm.log("Could not get closest marker: failed to sort by closest!",LOG_ERROR,f)},this.calcDistance=function(e,o){if(e&&o)if(e=f.convertToGeo(e),e=f.convertGeoToArray(e),o=f.convertToGeo(o),o=f.convertGeoToArray(o),e&&o){var t,n,i,r,a,l,s=function(e){return e*Math.PI/180};if(t=6371,a=s(o[0]-e[0]),l=s(o[1]-e[1]),n=Math.sin(a/2)*Math.sin(a/2)+Math.cos(s(e[0]))*Math.cos(s(e[0]))*Math.sin(l/2)*Math.sin(l/2),i=2*Math.atan2(Math.sqrt(n),Math.sqrt(1-n)),r=t*i,0===r||r>0)return r;bm.log("Failed to calculate distance: calculations incorrect (result: "+r+")!",LOG_ERROR,f)}else bm.log("Failed to calculate distance: positions invalid!",LOG_ERROR,f);else bm.log("Failed to calculate distance: you must provide 2 positions!",LOG_ERROR,f)},o()};