var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var allmarkers = [];

function initialize() {
	directionsDisplay = new google.maps.DirectionsRenderer({preserveViewport:true});
		var latlng = new google.maps.LatLng(40.961523,-95.580078);
		var myOptions = {
		mapTypeControl: false,
		zoom: 4,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);
	
	var icn = "";
	
	for (var i=0;i<allwaypts.length;i++){
		if(i == (allwaypts.length -1)){
			icn = "http://www.planetpayne.com/imgs/currentmarker.png";
		}
		var ll = new google.maps.LatLng(allwaypts[i][0],allwaypts[i][1]);
		var marker = new google.maps.Marker({
			position:ll, 
			map:map,
			title:allwaypts[i][2],
			icon:icn
		});
		marker.setZIndex(100);
		allmarkers.push(marker);
	}
	
	var routeline = [];
	for (var i=0;i<allwaypts.length;i++){
		routeline[i] = new google.maps.LatLng(allwaypts[i][0],allwaypts[i][1]);
	}

	var flightPath = new google.maps.Polyline({
		path: routeline,
		strokeColor: "#cc3333",
		strokeOpacity: 0.7,
		strokeWeight: 3
	});
	flightPath.setMap(map);
	allmarkers.push(flightPath);
}

var tripcount = 0;
var routeline = [];

function animatetrip(){
	if (tripcount == 0){
		$(allmarkers).each(function () {
			 this.setMap(null);
		});
	    allmarkers = [];
	}
	
	
	routeline[tripcount] = new google.maps.LatLng(allwaypts[tripcount][0],allwaypts[tripcount][1]);

	var flightPath = new google.maps.Polyline({
		path: routeline,
		strokeColor: "#333399",
		strokeOpacity: 1,
		strokeWeight: 1
	});

	flightPath.setMap(map);
 	tripcount++;

	if (allwaypts[tripcount]){
		tripcount--;
		document.getElementById("stopinfo").innerHTML = "Stop "+(tripcount+1)+": "+allwaypts[tripcount][2];
		tripcount++;
		setTimeout("animatetrip()",1000);
	}else{
		tripcount--;
		document.getElementById("stopinfo").innerHTML = "Current Location / Stop "+(tripcount+1)+": "+allwaypts[tripcount][2];
		var ll = new google.maps.LatLng(allwaypts[tripcount][0],allwaypts[tripcount][1]);
		var marker = new google.maps.Marker({
			position:ll, 
			map:map,
			title:allwaypts[tripcount][2],
			icon:"http://www.planetpayne.com/imgs/currentmarker.png"
		}); 
		allmarkers.push(marker);
	}
}