// The Jogging Planner
// (c) Medusis, 2010
// compiled 2010.10.29

$(document).ready(function(){function s(k){k=k+" km / "+k*0.625+" miles";$("#slider_result").html(k)}$("#version").text("v 1.6");var n=function(){for(var k=location.hash.substring(1).split(";"),o={s:"start",c:"center",d:"distance"},p={},q=0;q<k.length;q++){var t=k[q].split(":");if(o.hasOwnProperty(t[0]))p[o[t[0]]]=t[1]}return p}(),h=Mjog.DISTANCE_DEFAULT;if(n.hasOwnProperty("distance"))h=n.distance;h=Mjog.iti.setDist(h);s(h);$("#slider").slider({step:1,value:h,min:1,max:50,animate:true,slide:function(k,
o){s(o.value)},stop:function(k,o){var p=o.value,q=p+" km / "+p*0.625+" miles";$("#slider_result").html(q);Mjog.iti.setDist(p);Mjog.publicFind()}});$("#find_another").click(function(){Mjog.publicFind()});$("#searchForm").submit(function(){Mjog.showAddress();return false});$("#searchAddr").bind("focus",function(){$(this).val("");$(this).removeClass("before")});$("#credits_title").click(function(){$("#credits").toggleClass("hide");return false});$("#elevation_img").click(function(){Mjog.reverse()});
Mjog.init(n)});var Mjog={};
(function(){function s(a,b){var c=[];with(Math)for(var g=b/6378.135*(180/PI),d=g/cos(a.lat()*(PI/180)),f=0;f<361;f++){var m=f*(PI/180),w=a.lng()+d*cos(m);m=a.lat()+g*sin(m);w=new google.maps.LatLng(parseFloat(m),parseFloat(w),true);c.push(w)}return c}function n(a){a=new google.maps.Marker(a,{zIndexProcess:p,icon:A,title:"START",draggable:true});a.importance=2;i.addOverlay(a)}function h(){var a=e.tries;if(a%3==0){e.distanceToTry/=B;e.prepare()}n(e.start);a===2&&$("#wait").removeClass("hide");a=e.roll();
a.join("|");l.loadFromWaypoints(a,u)}function k(){$("#path").html("");$("#path_header").html("");$("#elevation_img").html("");i.clearOverlays();$("#wait").addClass("hide")}function o(){var a=$("#helpZone_at_start").html();if(a.length){$("#helpZone_when_iti_found").html(a);$("#helpZone_at_start").empty()}$("#wait").addClass("hide");i.setCenter(e.start,e.zoom);$("#path_header").html(e.summary);a=e.updateHash();location.hash=a;a="log?h="+a.substring(1);$.ajax({url:a})}function p(a){return google.maps.Overlay.getZIndex(a.getPoint().lat())+
a.importance*1E6}function q(){if(!v.frozen){v.freeze();k();e.tries=1;e.distanceToTry=e.distance;e.prepare()&&h();v.unfreeze()}}function t(a){var b="#";for(elt in a)if(a.hasOwnProperty(elt))b+=elt+":"+a[elt]+";";return b=b.substring(0,b.length-1)}function C(){var a=new google.maps.Directions;i.addControl(new google.maps.ScaleControl);i.addControl(new google.maps.LargeMapControl);i.addControl(new google.maps.MapTypeControl);i.addMapType(G_PHYSICAL_MAP);i.enableContinuousZoom();google.maps.Event.addListener(i,
"click",function(b,c){!b&&!v.frozen&&a.loadFromWaypoints([c.toUrlValue(6),c.toUrlValue(6)],{getPolyline:true})});google.maps.Event.addListener(a,"load",function(){var b=a.getPolyline().getVertex(0);e.setStart(b);q()});google.maps.Event.addListener(a,"error",function(){a.getStatus()});l=new google.maps.Directions(i,document.getElementById("path"));google.maps.Event.addListener(l,"load",function(){var b=e.distanceToTry,c=l.getDistance().meters/1E3,g=(c-b)/c;y([c,b,g].join("|"));if(g>D&&e.tries<x){l.clear();
h()}else{e.summary="Total: "+l.getSummaryHtml();o();b=l.getPolyline();g=0;var d=[];for(elt in b.j)if(b.j.hasOwnProperty(elt)){g%10===0&&d.push([round(b.j[elt].lat(),5),round(b.j[elt].lng(),5)]);g++}d.push([round(b.j[0].lat(),5),round(b.j[0].lng(),5)]);if(d.length<150){b="elevation.php?el="+d.join("|");$.get(b,function(f){f=JSON.parse(f);f=f.results;elevArray=[];for(var m=0;m<f.length;m++)elevArray.push(f[m].elevation);f=Math.floor((elevArray.min()-1)/10)*10;m=Math.ceil((elevArray.max()+1)/10)*10;
f=f+","+m;f="http://chart.apis.google.com/chart?chxt=x,y&chds="+f+"&chs=350x120&cht=lc&chxr=1,"+f+"&chd=t:"+elevArray.join()+"&chco=orange&chxl=0:|0|"+round(c/2,1)+"%20km|"+round(c,1)+"%20km";$("#elevation_img").html("<td width='25'><img src='img/elevation.png'/></td><td><img width='100%' src='"+f+"' title='Elevation in Meters. Click to change directions.'/></td>")})}v.unfreeze()}});google.maps.Event.addListener(l,"error",function(){if(e.tries<x){l.clear();h()}else{var b=l.getStatus().code,c="Code "+
b;if(j[b])c=j[b];k();$("#path_header").html("Error "+b);$("#path").html("<b>Failed to obtain directions</b>: "+c+" <a href='#' onclick='Mjog.publicFind()'>click to retry</a>");v.unfreeze()}})}function y(){}var x=10,D=0.12,B=1.2,i,z,A=new google.maps.Icon(G_DEFAULT_ICON,"img/marker_redA.png"),j=[];j[G_GEO_SUCCESS]="Success";j[G_GEO_MISSING_ADDRESS]="Missing Address: The address was either missing or had no value.";j[G_GEO_UNKNOWN_ADDRESS]="Unknown Address:  No corresponding geographic location could be found for the specified address.";
j[G_GEO_UNAVAILABLE_ADDRESS]="Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";j[G_GEO_BAD_KEY]="Bad Key: The API key is either invalid or does not match the domain for which it was given";j[G_GEO_TOO_MANY_QUERIES]="Too Many Queries: The daily geocoding quota for this site has been exceeded.";j[G_GEO_SERVER_ERROR]="Server error: The geocoding request could not be successfully processed.";j[G_GEO_BAD_REQUEST]="A directions request could not be successfully parsed.";
j[G_GEO_MISSING_QUERY]="No query was specified in the input.";j[G_GEO_UNKNOWN_DIRECTIONS]="The GDirections object could not compute directions between the points. This can happen if the starting point is near the sea or the desert (or otherwise empty region) and you selected a 'long' distance.<br>Try choosing a starting point where there are more roads or selecting a shorter distance, or";var u={};u.travelMode=G_TRAVEL_MODE_WALKING;u.avoidHighways=true;u.preserveViewport=true;var l,e={tries:1,summary:"",
setStart:function(a){this.start=a;n(this.start)},setDist:function(a){this.distanceToTry=this.distance=a<1?1:a>50?50:a;this.zoom=this.distance<3?16:this.distance<4?15:this.distance<7?14:this.distance<16?13:this.distance<34?12:11;return this.distance},prepare:function(){if(!(this.start&&this.distance)){alert("Please set a starting point (click on the map).");return false}this.radius=this.distanceToTry/4*Math.sqrt(3)/3;this.redCircle=s(this.start,this.radius);return true},draw:function(){if(this.start&&
this.distanceToTry){for(var a=s(this.center,this.radius),b=[],c={},g,d=0;d<a.length;d++){g=a[d].distanceFrom(this.start);b.push(g);c[g]=d}d=c[b.min()];d=a.splice(0,d);a=a.concat(d);b=[];for(d=0;d<a.length-1;d++)d%120==0&&b.push(a[d]);b.push(b[0]);return this.searchPoints=b}else alert("Error: itinerary not started; call set first.")},roll:function(){this.tries++;this.center=this.redCircle[Math.round(Math.random()*this.redCircle.length)];return this.draw()},updateHash:function(){var a=[round(this.start.lat(),
6),round(this.start.lng(),6)].join(),b=[round(this.center.lat(),6),round(this.center.lng(),6)].join();return this.hash=t({s:a,c:b,d:this.distance})}},v={frozen:false,freeze:function(){this.frozen=true;$("#find_another").attr("disabled",true)},unfreeze:function(){this.frozen=false;$("#find_another").removeAttr("disabled")}},E=new google.maps.ClientGeocoder,r=Mjog;r.iti=e;r.find=h;r.showAddress=function(){var a=document.getElementById("searchAddr").value;E.getLocations(a,function(b){if(b.Status.code==
G_GEO_SUCCESS){b=b.Placemark[0].Point.coordinates;i.setCenter(new google.maps.LatLng(b[1],b[0]),14)}else{var c="Code "+b.Status.code;if(reasons[b.Status.code])c=j[b.Status.code];alert('Could not find "'+a+'" '+c)}})};r.init=function(a){function b(f){f=f.split(",");return new google.maps.LatLng(f[0],f[1])}if(google.maps.BrowserIsCompatible()){z=document.getElementById("mapDiv");i=new google.maps.Map2(z,{draggableCursor:"crosshair"});var c="32.7688,-94.658203";if(a.hasOwnProperty("start")){c=a.start;
e.setStart(b(c))}else if(google.loader.ClientLocation)c=google.loader.ClientLocation.latitude+","+google.loader.ClientLocation.longitude;$("#slider").slider("option","value");var g=b(c),d=a.hasOwnProperty("start")?g:0;d=a.hasOwnProperty("start")?10:4;C();if(a.hasOwnProperty("start")&&a.hasOwnProperty("center")){d=b(c);a=b(a.center);e.center=a;e.tries=x+1;a=a.distanceFrom(d)/1E3;e.radius=a;a=e.draw();y(a.join());l.loadFromWaypoints(a,u)}else i.setCenter(g,d,G_NORMAL_MAP)}else alert("Incompatible browser. Sorry.")};
r.publicFind=q;r.hashSerialize=t;r.DISTANCE_DEFAULT=5;r.reverse=function(){l.loadFromWaypoints(e.searchPoints.reverse(),u)}})();Array.prototype.max=function(){return Math.max.apply({},this)};Array.prototype.min=function(){return Math.min.apply({},this)};function round(s,n){var h=0;if(n)h=Math.pow(10,n);return Math.round(s*h)/h};

