jQuery(document).ready(function($) {
	//var map = new GMap2($('#map'));
	//map.setCenter(new GLatLng(42.4, -82.183333), 8);
	//map.setUIToDefault();

	//directionsPanel = document.getElementById("map-directions");
	d1 = new GDirections();
	d2 = new GDirections();
	d3 = new GDirections();
	d4 = new GDirections();
	d5 = new GDirections();
	d6 = new GDirections();
	//dir = new GDirections(map, directionsPanel);
	GEvent.addListener(d1, "load", onGDirectionsLoad);
	GEvent.addListener(d2, "load", onGDirectionsLoad);
	GEvent.addListener(d3, "load", onGDirectionsLoad);
	GEvent.addListener(d4, "load", onGDirectionsLoad);
	GEvent.addListener(d5, "load", onGDirectionsLoad);
	GEvent.addListener(d6, "load", onGDirectionsLoad);

	//$('#map-directions').fadeIn(500);
	//setTimeout(function(){map.checkResize();}, 500);
	
	d1.load("from: " + geolocation + " to: Chatham, ON");
	d2.load("from: " + geolocation + " to: Windsor, ON");
	d3.load("from: " + geolocation + " to: Leamington, ON");
	d4.load("from: " + geolocation + " to: Sarnia, ON");
	d5.load("from: " + geolocation + " to: London, ON");
	d6.load("from: " + geolocation + " to: Wingham, ON");
});

function onGDirectionsLoad() {
	
	if (d1.getDistance() && d2.getDistance() && d3.getDistance() && d4.getDistance() && d5.getDistance() && d6.getDistance()) {
		
		var l = new Array();
		var m = new Array();
		l[1] = "chathamon";
		l[2] = "windsoron";
		l[3] = "leamingtonon";
		l[4] = "sarniaon";
		l[5] = "londonon";
		l[6] = "winghamon";
		m[1] = d1.getDistance().meters;
		m[2] = d2.getDistance().meters;
		m[3] = d3.getDistance().meters;
		m[4] = d4.getDistance().meters;
		m[5] = d5.getDistance().meters;
		m[6] = d6.getDistance().meters;
		
		var min = 999999999;
		var k = 0;
		for (var i = 1; i < 7; i++) {
			if (m[i] < min) {
				min = m[i];
				k = i;
			}
		}
		
		$('#closest').show();
		$('.'+l[k]).show();
		$('.station.'+l[k]).hide();
	}
}

