﻿	var myMarker;

   	var travelIndex=0;
   	var travelPoints;

	var playSpeed=300;
	var wayDirection=1;
	var map;
	var carIcon;
	var totalLength=0;
	var mapLoaded=0;

	var g_preTime;
	var g_preGPoint=new GLatLng(0,0);

	function getHeight(s){
		var result;
		if( typeof( window.innerHeight ) == 'number' ) {
			//Non-IE
			result = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			result = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			result = document.body.clientHeight;
		}
		return result-30;
	}

	function load() {

		if (GBrowserIsCompatible()) {
			document.getElementById("map").style.height=getHeight("height")+"px";
			map = new google.maps.Map2(document.getElementById("map"));

			map.setCenter(new google.maps.LatLng(33.754317, -84.389677),15);

			var baseIcon = new GIcon();
			baseIcon.iconSize=new GSize(32,32);
			baseIcon.shadowSize=new GSize(56,32);
			baseIcon.iconAnchor=new GPoint(16,16);
			baseIcon.infoWindowAnchor=new GPoint(16,0);

			carIcon = new GIcon(baseIcon, "images/star_marker.png", null,
			"images/icon31s.png");

			myMarker = new google.maps.Marker(map.getCenter(), carIcon);
			map.addOverlay( myMarker );

			mapLoaded=1;
		}else{
			alert("Your browser is not support this system!");
			history.back();
		}

		DrawPath();

	}


	function PlayMode(mode){
		if (mode==0){  //STOP
			wayDirection=0;
		}else if(mode==1){  //Fast Backward
			if(wayDirection<=0){
				playSpeed-=100;
				if (playSpeed<100) playSpeed=100;
				wayDirection=-1;
			}
			else{
				playSpeed+=100;
				if (playSpeed>500){
					playSpeed=600;
					wayDirection=0;
				}
			}
		}else if(mode==2){  //Fast Forward
			if(wayDirection>=0){
				playSpeed-=100;
				if (playSpeed<100) playSpeed=100;
				wayDirection=1;
			}
			else{
				playSpeed+=100;
				if (playSpeed>500){
					playSpeed=600;
					wayDirection=0;
				}
			}
		}
	}

	function ClearAll(){
		map.clearOverlays();
		map.addOverlay(myMarker); // car
	}

	function SetGPSInformation(Latitude, Longitude, DateTime, Speed)
	{
		var newPos=new google.maps.LatLng(Latitude, Longitude);
		var strTitle;
		var htmlTitle;
		var lengthMi;

		if(totalLength>0) {
			lengthMi = totalLength/1.609;
			lengthMi = Math.round(lengthMi)/1000; //小數3位

			if(DateTime!="") {
				strTitle="<table border=0>"+
			  "<tr><td><font color=green>"+DateTime+"</font> ["+Speed+"/h]"+
			  "</td><tr><td><center><font color=green> TOTAL: "+lengthMi+" mi</font>"+
			  "</td></tr></table>";
			}
			else {
				strTitle="<table border=0>"+
			  "<tr><td><center><font color=green> TOTAL: "+lengthMi+" mi</font>"+
			  "</td></tr></table>";
			}
		}
		else {
			strTitle="<table border=0>"+
			  "<tr><td><font color=green>"+DateTime+"</font> ["+Speed+"/h]"+
			"</td></tr></table>";
		}

		htmlTitle="<table broder=0 cellspacing=0 cellpadding=2 width=250>"+
			      "<tr><td>"+
			      "<table border=0 cellspacing=0 cellpadding=0>"+
			      	"<tr><td bgcolor=#ffffff STYLE=\"filter:Alpha(Opacity=85,style=1,Finishopacity=85);padding-top:2px;padding-bottom:2px;font:9pt Arial,Verdana,Tahoma;color:#0000ff\">"+
			      	"<font color=red>"+strTitle+"</font>"+
			      	"</tr></td></table>"+
			      "</td></tr></table>";
		var opts = {
			"icon": carIcon,
			"clickable": false,
			"labelText": htmlTitle,
			"labelOffset": new GSize(-56,16)
		};

		map.removeOverlay(myMarker);
		myMarker = new LabeledMarker(newPos, opts);
		map.addOverlay(myMarker);
		CheckAndPan(newPos);
	}

	function DrawPath(){

        var path=travelPath.split("*");
        var points = [];
        var item=path[0].split(":");

        if (travelPath=="") return;
        if (travelPath=="Error") {
			alert("Invalid file format!");
			return;
		}

		if (item[0] != "") {
			strTime=item[0].substring(0,2)+":"+item[0].substring(2,4)+":"+item[0].substring(4,6);
		}
		else{
			strTime="";
		}

		SetGPSInformation(parseFloat(item[1]), parseFloat(item[2]), strTime, item[3]+" mi");

		//Draw poly line
		var gpPoint, gpPrePoint, gpZero, iTime, iPreTime=0;
		gpZero=new GLatLng(0,0);
		gpPrePoint=gpZero;
		for (var i=0;i<path.length; i++){
			if (path[i] != ""){
				item=path[i].split(":");
				gpPoint = new GLatLng(parseFloat(item[1]),parseFloat(item[2]));
				iTime=parseInt(item[0]);
				// Drop point over 100 meter.
				if( Math.abs(iTime-iPreTime)<20  && !gpPrePoint.equals(gpZero)
					 && gpPoint.distanceFrom(gpPrePoint)>100) {
					//alert("path[" + i + "]=" + path[i]);
					path.splice(i,1);
				}
				else {
					points.push(gpPoint);
					// event handle
					if(item[5]=="0E"){
						//AddStopMark(gpPoint, busId, Date, Time);
						strDate="20"+item[4].substring(4,6)+"/"+item[4].substring(2,4)+"/"+item[4].substring(0,2);
						strTime=item[0].substring(0,2)+":"+item[0].substring(2,4)+":"+item[0].substring(4,6);
						AddStopMark(gpPoint, item[6], strDate, strTime);
					}
					// calulate total length
					if ( !gpPrePoint.equals(gpZero) ) {
						totalLength += gpPoint.distanceFrom(gpPrePoint);
					}
					gpPrePoint = gpPoint;
				}
				iPreTime = iTime;
			}
		}
		var polyline = new GPolyline(points, "#008000", 5);
		map.addOverlay(polyline);
        travelIndex=0;
        travelPoints=path;
		traveler();
	}

	function traveler(){
		if (wayDirection!=0){
			if (wayDirection==-1){
				travelIndex--;
				if (travelIndex<=0){
					travelIndex=1;
					wayDirection=0;
					playSpeed=600;
				}
			}
			if (wayDirection==1){
				travelIndex++;
				if (travelIndex>=travelPoints.length){
					wayDirection=0;
					playSpeed=600;
					travelIndex=travelPoints.length-1;
				}
			}

			if (travelPoints[travelIndex]=="" || travelPoints[travelIndex]==null){
				// Clear error point.
				travelPoints.splice(travelIndex,1);
				travelIndex--;
			}
			var wp=travelPoints[travelIndex].split(":");
			var gpLat = parseFloat(wp[1]);
			var gpLng = parseFloat(wp[2]);

			// for kml no date/time and speed
			if (wp[0]!="") {
				SetGPSInformation(gpLat,gpLng, wp[0].substring(0,2)+":"+wp[0].substring(2,4)+":"+wp[0].substring(4,6), wp[3]+" mi");
			}
			else {
				SetGPSInformation(gpLat,gpLng, "", "");
			}
		}

		setTimeout("traveler();", playSpeed);
	}

	// Check point for Pan to center.
	function CheckAndPan(gpPoint){
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
        var lngBound = northEast.lng() - southWest.lng();
        var latBound = northEast.lat() - southWest.lat();
		northEast = new GLatLng(northEast.lat() - latBound/4, northEast.lng()-lngBound/4)
		southWest = new GLatLng(southWest.lat() + latBound/4, southWest.lng()+lngBound/4)
		viewBounds = new GLatLngBounds(southWest, northEast);

		if(!viewBounds.containsLatLng(gpPoint)) map.panTo(gpPoint,16);
	}

	function AddStopMark(gpPoint, strBusId, strDate, strTime){
		marker = new GMarker(gpPoint);

		var strTitle="BUS#: "+strBusId+"<br>"+
			  "DATE: "+strDate+"<br>"+
			  "TIME: "+strTime+"<br>"+
			  "LOCATION: "+gpPoint.toString()+"<br>";

		GEvent.addListener(marker, "click", function() {
			map.openInfoWindowHtml(gpPoint, strTitle);
		});

		map.addOverlay( marker );
	}

	// use by dvr player to add stop mark
	function AddMark(Latitude, Longitude, strBusId, strDate, strTime)
	{
		var newPos=new GLatLng(Latitude, Longitude);
		AddStopMark(newPos, strBusId, strDate, strTime );
	}

	// call by change browser size
	function onResize()
	{
		document.getElementById("map").style.height=getHeight("height")+"px";
		if(mapLoaded) map.checkResize();
	}

