var geocoder;var map;var currentLocation;var currentLocationMarker;var currentInfoWindow;var NUM_POINTS=32;var distanceRadious=3;var distanceCircle;var distanceColor="#FF0000";var circleStrokeOpacity=0.8;var circleStrokeWeight=2;var circleFillOpacity=0.1;var CIRCLE_DEGREE=180;var MINS_IN_HR=60;var speedPerKm=4;var minsWalk=30;var timeRadious;var timeCircle;var timeColor="#00FF00";var ready=false;var displayMenu=true;var met=2;var weight=70;var MIN_TIME=1;var MAX_TIME=180;var MIN_DISTANCE=1;var MAX_DISTANCE=15;var ZOOM_LEVEL=13;var EARTH_RADIOUS=6371;var SCHOOL_LEGEND='schoolLegend';var schoolInfoWindowList=new Array();var schoolMarkerList=new Array();var SCHOOL_MARKER='images/school-marker.png';var PLAYGROUND_LEGEND='playgroundLegend';var playgroundMarkerList=new Array();var playgroundInfoWindowList=new Array();var PLAYGROUND_MARKER='images/playground-marker.png';var MY_MARKER='images/me-marker.png';var PUBLIC_TOILET_LEGEND="publicToiletLegend";var publicToiletMarkerList=new Array();var publicToiletInfoWindowList=new Array();var PUBLIC_TOILET_MARKER="images/public-toilet-marker.png";var BBQ_LEGEND='bbqLegend';var bbqMarkerList=new Array();var bbqInfoWindowList=new Array();var BBQ_MARKER='images/bbq-marker.png';var PUBLIC_INTERNET_LEGEND='publicInternetLegend';var publicInternetMarkerList=new Array();var publicInternetInfoWindowList=new Array();var PUBLIC_INTERNET_MARKER='images/public-internet-marker.png';var CUSTOM_MARKER='images/custom-marker.png';var carType=137.5;function initialize(){currentLocation=new google.maps.LatLng(-32.066997,115.834951);geocoder=new google.maps.Geocoder();if(google.loader.ClientLocation){zoom=ZOOM_LEVEL;currentLocation=new google.maps.LatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);var location=getFormattedLocation();}
var myOptions={zoom:ZOOM_LEVEL,center:currentLocation,mapTypeId:google.maps.MapTypeId.HYBRID,keyboardShortcuts:false,scrollwheel:false}
map=new google.maps.Map($("map"),myOptions);plotCurrentLocation(location);updateCircles();initializeSliders();$('weight').value=weight;ready=true;}
function plotCurrentLocation(location){location="<p>Based on the GPS or Internet IP address.<br />Your current location is "+location+".</p><p>Is this your correct location? <br />If not, please drag the marker to adjust your current location <br />or perform a search for the location.</p>";currentInfoWindow=new google.maps.InfoWindow({content:location});currentLocationMarker=new google.maps.Marker({position:currentLocation,map:map,draggable:true,icon:MY_MARKER,zIndex:9000});currentInfoWindow.open(map,currentLocationMarker);google.maps.event.addListener(currentLocationMarker,'click',function(){currentInfoWindow.open(map,currentLocationMarker);});google.maps.event.addListener(currentLocationMarker,"dragstart",function(){currentInfoWindow.close();});google.maps.event.addListener(currentLocationMarker,"dragend",function(){currentLocation=currentLocationMarker.getPosition();currentInfoWindow.setContent('');updateCircles();clearLegends();});}
function getFormattedLocation(){if(google.loader.ClientLocation.address.country_code=="AU"&&google.loader.ClientLocation.address.region){return google.loader.ClientLocation.address.city+", "
+google.loader.ClientLocation.address.region.toUpperCase();}else{return google.loader.ClientLocation.address.city+", "
+google.loader.ClientLocation.address.country_code;}}
function searchForLocation(){var address=$('location').value;if(address&&geocoder){geocoder.geocode({'address':address},function(results,status){if(status==google.maps.GeocoderStatus.OK){currentLocation=results[0].geometry.location;map.setCenter(currentLocation);currentLocationMarker.setPosition(results[0].geometry.location);currentInfoWindow.setContent("<p>Based on your search result.<br />Your current location is "+address.camelize()+".</p><p>Is this your correct location? <br />If not, please drag the marker to adjust your current location <br />or perform a search for the location.</p>");updateCircles();clearLegends();}else{flashMessage("<p>Geocoding was not successful. Please try again</p>");}});}}
function calculateCirclePlots(radious){var latLngList=[];var lat=currentLocation.lat();var lng=currentLocation.lng();var d2r=Math.PI/CIRCLE_DEGREE;var r2d=CIRCLE_DEGREE/Math.PI;var Clat=(radious/EARTH_RADIOUS)*r2d;var Clng=Clat/Math.cos(lat*d2r);for(var i=0;i<NUM_POINTS;i++){var theta=Math.PI*(i/(NUM_POINTS/2));Cx=lng+(Clng*Math.cos(theta));Cy=lat+(Clat*Math.sin(theta));latLngList.push(new google.maps.LatLng(Cy,Cx));}
latLngList.push(latLngList[0]);return latLngList;}
function drawCircle(color,latLngList){var options={paths:latLngList,strokeColor:color,strokeOpacity:circleStrokeOpacity,strokeWeight:circleStrokeWeight,fillColor:color,fillOpacity:circleFillOpacity};var myCircle=new google.maps.Polygon(options);myCircle.setMap(map);return myCircle;}
function updateCircles(){updateDistainceCircle();updateTimeCircle();}
function updateDistainceCircle(){if(!(distanceRadious>=MIN_DISTANCE&&distanceRadious<=MAX_DISTANCE)){return;}
if(distanceCircle){distanceCircle.setMap(null);}
var latLngList=calculateCirclePlots(distanceRadious);distanceCircle=drawCircle(distanceColor,latLngList);}
function updateTimeCircle(){if(!(minsWalk>=MIN_TIME&&minsWalk<=MAX_TIME)){return;}
timeRadious=(speedPerKm/MINS_IN_HR)*minsWalk;if(timeCircle){timeCircle.setMap(null);}
var latLngList=calculateCirclePlots(timeRadious);timeCircle=drawCircle(timeColor,latLngList);}
function clearLegends(){if($(SCHOOL_LEGEND).className.match("selected")){getSchools();}
if($(PLAYGROUND_LEGEND).className.match("selected")){getPlaygrounds();}
if($(PUBLIC_INTERNET_LEGEND).className.match("selected")){getPublicInternet();}
if($(PUBLIC_TOILET_LEGEND).className.match("selected")){getPublicToilets();}
if($(BBQ_LEGEND).className.match("selected")){getBBQs();}}
function getSchools(){if(!ready){flashMessage('<p>The website is still loading, please wait for a moment</p>');}
if($(SCHOOL_LEGEND).className.match("selected")){$(SCHOOL_LEGEND).className=$(SCHOOL_LEGEND).className.replace("selected","");schoolMarkerList.each(function(item){item.setMap(null);});schoolMarkerList=new Array();}else{var lat=currentLocation.lat();var lng=currentLocation.lng();new Ajax.Request('getSchools.php',{method:'get',parameters:{lat:lat,lng:lng},onSuccess:function(transport){var json=transport.responseText.evalJSON(true);json.result.records.each(function(item){createMarker(item.name,item.address,item.lat,item.lng,item.type,item.distance);});if(json.result.count>0){$(SCHOOL_LEGEND).className+="selected";}
flashMessage("<p>Total: "+json.result.count+" records found within "+MAX_DISTANCE+" kilometres.<br />The nearst location is :"+json.result.nearest+" kilometres away</p>");},onFailure:function(){flashMessage("<p>Sorry we are unable to complete your search.</p>");}});}}
function getPlaygrounds(){if(!ready){flashMessage('<p>The website is still loading, please wait for a moment</p>');}
if($(PLAYGROUND_LEGEND).className.match("selected")){$(PLAYGROUND_LEGEND).className=$(PLAYGROUND_LEGEND).className.replace("selected","");playgroundMarkerList.each(function(item){item.setMap(null);});playgroundMarkerList=new Array();}else{var lat=currentLocation.lat();var lng=currentLocation.lng();new Ajax.Request('getPlaygrounds.php',{method:'get',parameters:{lat:lat,lng:lng},onSuccess:function(transport){var json=transport.responseText.evalJSON(true);json.result.records.each(function(item){createMarker(item.name,item.address,item.lat,item.lng,item.type,item.distance);});if(json.result.count>0){$(PLAYGROUND_LEGEND).className+="selected";}
flashMessage("<p>Total: "+json.result.count+" records found within "+MAX_DISTANCE+" kilometres.<br />The nearst location is :"+json.result.nearest+" kilometres away</p>");},onFailure:function(){flashMessage("<p>Sorry we are unable to complete your search.</p>");}});}}
function getPublicInternet(){if(!ready){flashMessage('<p>The website is still loading, please wait for a moment</p>');}
if($(PUBLIC_INTERNET_LEGEND).className.match("selected")){$(PUBLIC_INTERNET_LEGEND).className=$(PUBLIC_INTERNET_LEGEND).className.replace("selected","");publicInternetMarkerList.each(function(item){item.setMap(null);});publicInternetMarkerList=new Array();}else{var lat=currentLocation.lat();var lng=currentLocation.lng();new Ajax.Request('getPublicInternet.php',{method:'get',parameters:{lat:lat,lng:lng},onSuccess:function(transport){var json=transport.responseText.evalJSON(true);json.result.records.each(function(item){createMarker(item.name,item.address,item.lat,item.lng,item.type,item.distance);});if(json.result.count>0){$(PUBLIC_INTERNET_LEGEND).className+="selected";}
flashMessage("<p>Total: "+json.result.count+" records found within "+MAX_DISTANCE+" kilometres.<br />The nearst location is :"+json.result.nearest+" kilometres away</p>");},onFailure:function(){flashMessage("<p>Sorry we are unable to complete your search.</p>");}});}}
function getPublicToilets(){if(!ready){flashMessage('<p>The website is still loading, please wait for a moment</p>');}
if($(PUBLIC_TOILET_LEGEND).className.match("selected")){$(PUBLIC_TOILET_LEGEND).className=$(PUBLIC_TOILET_LEGEND).className.replace("selected","");publicToiletMarkerList.each(function(item){item.setMap(null);});publicToiletMarkerList=new Array();}else{var lat=currentLocation.lat();var lng=currentLocation.lng();new Ajax.Request('getPublicToilets.php',{method:'get',parameters:{lat:lat,lng:lng},onSuccess:function(transport){var json=transport.responseText.evalJSON(true);json.result.records.each(function(item){createMarker(item.name,item.address,item.lat,item.lng,item.type,item.distance);});if(json.result.count>0){$(PUBLIC_TOILET_LEGEND).className+="selected";}
flashMessage("<p>Total: "+json.result.count+" records found within "+MAX_DISTANCE+" kilometres.<br />The nearst location is :"+json.result.nearest+" kilometres away</p>");},onFailure:function(){flashMessage("<p>Sorry we are unable to complete your search.</p>");}});}}
function getBBQs(){if(!ready){flashMessage('<p>The website is still loading, please wait for a moment</p>');}
if($(BBQ_LEGEND).className.match("selected")){$(BBQ_LEGEND).className=$(BBQ_LEGEND).className.replace("selected","");bbqMarkerList.each(function(item){item.setMap(null);});bbqMarkerList=new Array();}else{var lat=currentLocation.lat();var lng=currentLocation.lng();new Ajax.Request('getBBQs.php',{method:'get',parameters:{lat:lat,lng:lng},onSuccess:function(transport){var json=transport.responseText.evalJSON(true);json.result.records.each(function(item){createMarker(item.name,item.address,item.lat,item.lng,item.type,item.distance);});if(json.result.count>0){$(BBQ_LEGEND).className+="selected";}
flashMessage("<p>Total: "+json.result.count+" records found within "+MAX_DISTANCE+" kilometres.<br />The nearst location is: "+json.result.nearest+" kilometres away</p>");},onFailure:function(){flashMessage("<p>Sorry we are unable to complete your search.</p>");}});}}
function flashMessage(message){$('message').innerHTML="<p style='text-align: center;'><img src='images/info.png' alt='Information' /></p>"+message;new Effect.Appear('message',{duration:3.0,afterFinish:function(obj){new Effect.Fade('message',{duration:3.0})}});}
function calculateDistance(location){var pi80=Math.PI/180;var lat1=currentLocation.lat()*pi80;var lng1=currentLocation.lng()*pi80;var lat2=location.lat()*pi80;var lng2=location.lng()*pi80;var dlat=lat2-lat1;var dlng=lng2-lng1;var a=Math.sin(dlat/2)*Math.sin(dlat/2)+Math.cos(lat1)*Math.cos(lat2)*Math.sin(dlng/2)*Math.sin(dlng/2);var c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));return EARTH_RADIOUS*c;}
function generateCustomInfoWindowContent(location){var distance=calculateDistance(location);var str="<p>Name: Custom Marker<br />Distance: "+roundNumber(distance,2)+" kilometre(s)</p>";return str;}
function addAMarker(){var customMarkerLocation=map.getCenter();var customMarker=new google.maps.Marker({position:customMarkerLocation,map:map,draggable:true,icon:CUSTOM_MARKER,zIndex:9009});var str=generateCustomInfoWindowContent(customMarkerLocation);var customInfoWindow=new google.maps.InfoWindow({content:str});google.maps.event.addListener(customMarker,'click',function(){customInfoWindow.open(map,customMarker);walkameter(customInfoWindow);});google.maps.event.addListener(customMarker,"dragstart",function(){customInfoWindow.close();});google.maps.event.addListener(customMarker,"dragend",function(){str=generateCustomInfoWindowContent(customMarker.getPosition());customInfoWindow.setContent(str);});flashMessage('<p>A custom marker has been added.</p>');}
function createMarker(name,address,lat,lng,type,distance){var location=new google.maps.LatLng(lat,lng);var str="<p>Name: "+name.camelize()+"<br />Address: "+address.camelize()+"<br />Distance: "+roundNumber(distance,2)+" kilometre(s)</p>";var image=null;if(type==1){image=SCHOOL_MARKER;}else if(type==2){image=PLAYGROUND_MARKER;}else if(type==3){image=PUBLIC_INTERNET_MARKER;}else if(type==4){image=PUBLIC_TOILET_MARKER;}else if(type==5){image=BBQ_MARKER;}
var infoWindow=new google.maps.InfoWindow({content:str});var marker=new google.maps.Marker({position:location,map:map,draggable:false,icon:image});google.maps.event.addListener(marker,'click',function(){infoWindow.open(map,marker);walkameter(infoWindow);});if(type==1){schoolInfoWindowList.push(infoWindow);schoolMarkerList.push(marker);}else if(type==2){playgroundInfoWindowList.push(infoWindow);playgroundMarkerList.push(marker);}else if(type==3){publicInternetInfoWindowList.push(infoWindow);publicInternetMarkerList.push(marker);}else if(type==4){publicToiletInfoWindowList.push(infoWindow);publicToiletMarkerList.push(marker);}else if(type==5){bbqInfoWindowList.push(infoWindow);bbqMarkerList.push(marker);}}
function showMenu(){if(!displayMenu){Effect.BlindDown('menu');displayMenu=true;}else{Effect.BlindUp('menu');displayMenu=false;}}
function walkameter(infoWindow){var content=infoWindow.getContent();content=content.match(/<p>Name:.+ometre\(s\)<\/p>/i).toString();if(!content){content=infoWindow.getContent();}
if(speedPerKm==20){met=8;}else if(speedPerKm==12){met=8;}else if(speedPerKm==10){met=6;}else{met=2;}
var distance=content.match(/[0-9]+\.[0-9]+/i).toString();var calories=((met*3.5*weight)/200)*(distance/speedPerKm*MINS_IN_HR);var freddoFrogs=roundNumber(calories/79,2);calories=roundNumber(calories,2);var minutes=roundNumber(distance/speedPerKm*MINS_IN_HR,2);var co2Produced=roundNumber(carType*distance/1000,2);var batterPerHr=roundNumber(co2Produced*32,2);var txt='<p>By '+$('currentSpeed').innerHTML+' for '+minutes+' minutes to: </p>'+content+'<p>You will burn '+calories+' Calorie(s), which is the equivalent of '+freddoFrogs+' freddo frog(s).</p><p>If you drove, you would produce '+co2Produced+' kilograms of CO<sub>2</sub>.<br />Alternatively '+co2Produced+' kilograms of CO<sub>2</sub> can be used to power a laptop for '+batterPerHr+' hour(s)</p>';infoWindow.setContent(txt);}
function roundNumber(num,dec){var result=Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);return result;}
function updateWeight(){var value=$('weight').value;var msg="The following changes were successful: Weight updated. <br />Please click the marker again to recalculate the values.";if(value>0){weight=value;}else{msg="Weight contains an invalid value";}
flashMessage('<p>'+msg+'</p>');}
function updateCarType(){var value=$('carType').value;var msg="The following changes were successful: Car type updated. <br />Please click the marker again to recalculate the values.";if(value>0){carType=value;}else{msg="Car type contains an invalid value";}
flashMessage('<p>'+msg+'</p>');}
function updateSpeed(value){var str="";if(value==20){str="Cycling at ~ 20 Kilometres/hour";}else if(value==12){str="Running at ~ 12 Kilometres/hour";}else if(value==10){str="Jogging at ~ 10 Kilometres/hour";}else{str="Walking at ~ 4 Kilometres/hour";}
$('currentSpeed').innerHTML=str;speedPerKm=value;updateTimeCircle();}
function updateTime(value){minsWalk=value;$('currentTime').innerHTML=value;updateTimeCircle();}
function updateDistance(value){distanceRadious=value;$('currentDistance').innerHTML=distanceRadious;updateDistainceCircle();}
function initializeSliders(){new Control.Slider('timeHandle',$('timeSlider'),{range:$R(1,180),values:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180],sliderValue:minsWalk,onSlide:function(value){updateTime(value);},onChange:function(value){updateTime(value);}});new Control.Slider('distanceHandle',$('distanceSlider'),{range:$R(1,15),values:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],sliderValue:distanceRadious,onSlide:function(value){updateDistance(value);},onChange:function(value){updateDistance(value);}});new Control.Slider('speedHandle',$('speedSlider'),{range:$R(4,20),values:[4,10,12,20],sliderValue:1,onSlide:function(value){updateSpeed(value);},onChange:function(value){updateSpeed(value);}});$('currentTime').innerHTML=minsWalk;$('currentDistance').innerHTML=distanceRadious;}
