/* 	Maps/Directions jQuery Plugin by: Adam Kizer

	$([form-used for directions]).directions();
	globalMap and globalDirections are direction global access to the map and directions objects outside of the namespace.
	optional args: mapDiv, directionsDiv, address, lat, lng, forceLatLng, zoom, width, height, onComplete
*/

var globalMap = null;
var globalDirections = null;

Array.prototype.destroy=function(){this.length=0;return this};(function($){$.fn.directions=function(g){var k=null;var l=null;var m=null;var n={mapDiv:'map',directionsDiv:'directions',address:'5909 Nimtz Parkway, South Bend, IN 46628',lat:41.667951,lng:-86.179139,zoom:12,width:530,height:480,forceLatLng:false,onComplete:null};var g=$.extend(n,g);return this.each(function(){init();m=this;$(this).submit(function(e){e.preventDefault();setupDirections()})});function init(){if(GBrowserIsCompatible()){g.mapDiv=document.getElementById(g.mapDiv);g.directionsDiv=document.getElementById(g.directionsDiv);$(g.mapDiv).css({display:'block',width:g.width,height:g.height}).html('');window.onunload=google.maps.Unload;k=new GMap2(g.mapDiv);globalMap=k;k.setCenter(new GLatLng(g.lat,g.lng),g.zoom);k.addControl(new GSmallMapControl);k.addControl(new GMapTypeControl);var b=new GClientGeocoder();b.getLatLng(g.address,function(p){marker=new GMarker((g.forceLatLng?new GLatLng(g.lat,g.lng):p));GEvent.addListener(marker,"dragend",function(){alert(marker.getLatLng())});k.addOverlay(marker);k.setCenter(p,g.zoom)});l=new GDirections(k);globalDirections=l;GEvent.addListener(l,"error",function(){onError()});GEvent.addListener(l,"load",function(){var a=function(){onLoadDirections()};setTimeout(a,1)})}};function onLoadDirections(){if(g.forceLatLng)l.getMarker(1).setLatLng(new GLatLng(g.lat,g.lng));var a=new Array();for(var i=0;i<l.getNumRoutes();i++){var b=l.getRoute(i);var c=b.getStartGeocode();var d=b.getStep(0).getLatLng();a.push(routeDistance(b.getDistance().html," (about ",b.getDuration().html,")"));a.push(waypoint(d,c.address));a.push('<div id="stepsCont"><table id="steps"><tbody>');for(var j=0;j<b.getNumSteps();j++){var e=b.getStep(j);a.push(detail(e.getLatLng(),j+1,e.getDescriptionHtml(),e.getDistance().html,j%2))};a.push('</tbody></table></div>');a.push(topoint(b.getStep(j-1).getLatLng(),c.address))}$(g.directionsDiv).html(a.join(''));a.destroy();($.isFunction(g.onComplete)&&g.onComplete.call())};function waypoint(a,b){var c='"'+"globalMap.showMapBlowup(new GLatLng("+a.toUrlValue(6)+"))"+'"';var h=new Array('<table id="origin"><tr onclick="',c,'"><td><img src="http://maps.google.com/intl/en_us/mapfiles/marker_greenA.png"></td><td style="vertical-align: middle; width: 100%;">',b,'</td></tr></table>');return h.join('')};function routeDistance(a){return'<div id="route-distance">Distance: '+a+'</div>'};function detail(a,b,c,d,e){var f='"'+"globalMap.showMapBlowup(new GLatLng("+a.toUrlValue(6)+"))"+'"';var h=new Array('<tr ',(e?'':'class="even" '),'onclick=',f,'><td class="numbering">',b,'</td><td>',c,'</td><td class="distance">',d,'</td></tr>');return h.join('')};function topoint(a){var b='"'+"globalMap.showMapBlowup(new GLatLng("+a.toUrlValue(6)+"))"+'"';var h=new Array('<table id="destination"><tr onclick=',b,'><td><img src="http://maps.google.com/intl/en_us/mapfiles/marker_greenB.png"></td><td style="vertical-align: middle; width: 100%;">',g.address,'</td></tr></table>');return h.join('')};function setupDirections(){var a='';$(m).find('input').not('input[type=submit]').each(function(){a+=this.value+', '});l.load("from: "+a+" to: "+g.address,{getSteps:true})};function onError(){alert('An error has occured. Please re-enter your address and try again.')}}})(jQuery);