//***********************************************************
// Trail Locator Project
// The Trans Canada Trail
// http://www.tctrail.ca
// 
// Coding by Stephane Simard
// ssoutbox-tctrail@yahoo.com
//************************************************************


// Variable global
var MILES = {getDistance : function (distance) {return distance / 1609.344;}}
var KMS = {getDistance : function (distance) {return distance / 1000;}}
Math.deg2rad = function ( x ){ return x * (Math.PI / 180.0); }


//-------------------------------------------------------------------------------------------
// DISTANCE CLASS

function classDistance()
{
	this.totalDistance = 0;
	this.routePoints = new Array(0);
	this.overlayDist = null;
	this.overlayMarkerStart = null;
	this.overlayMarkerEnd = null;
	this.unitHandler = KMS;
}

	// INSTANCE
	var userDistance = new classDistance();


//-------------------------------------------------------------------------------------------
// Add point in the distance calculator

function clickDistanceEvent(overlay, point)
{
	if (point) 
	{
		removeOverlayDist();	// Remove all Overlay distance on MAP
		userDistance.routePoints.push(point);// Add point
		drawDistance();	
	}
}


//-------------------------------------------------------------------------------------------
// Refresh distance

function updateDistance () 
{
	var dist = userDistance.unitHandler.getDistance(userDistance.totalDistance);
	
	if (userDistance.routePoints.length == 1)
		document.getElementById("distStr").value = "0.000";
	else	
		document.getElementById("distStr").value = dist.toFixed(3);
}


//-------------------------------------------------------------------------------------------
// Takes an array of points and works out the total distance

function calculateDistance (points) {
	if (points.length < 2)	
		return 0;

	var dist = 0.0;
	for (var i = 0; i < points.length - 1; i++) {
		dist += distanceBetweenPnt(points[i], points[i + 1]);		
	}
	return dist;
}


//-------------------------------------------------------------------------------------------
// Calculates the distance (in metres) between 2 points. 

function distanceBetweenPnt ( p1, p2 ) 
{
        var a = Math.deg2rad( 90 - p1.y);
        var b = Math.deg2rad( 90 - p2.y);
        var theta = Math.deg2rad( p2.x - p1.x);
        var c = Math.acos( Math.cos(a) * Math.cos(b) + Math.sin(a) * Math.sin(b) * Math.cos(theta));
        return c * 6367000; 
}


//-------------------------------------------------------------------------------------------
// Draw Overlay distance

function drawDistance()
{
	// Marker icon settings       
	var markerIcon = new GIcon();
	markerIcon.image = "images/distance.png";
	markerIcon.shadow = "";
    markerIcon.iconSize = new GSize(9, 9);    
    markerIcon.shadowSize = new GSize(0, 0);
	markerIcon.iconAnchor = new GPoint(0, 0);
	markerIcon.infoWindowAnchor = new GPoint(0, 0);
	markerIcon.infoShadowAnchor = new GPoint(0, 0);
	
	userDistance.overlayDist = new GPolyline(userDistance.routePoints, "#a49c9c", 3, 0.8);
	map.addOverlay(userDistance.overlayDist);

	// Add a marker at the beginning and the end
	userDistance.overlayMarkerStart = new GMarker(userDistance.routePoints[0], markerIcon);
	map.addOverlay(userDistance.overlayMarkerStart);
	
	if ( userDistance.routePoints.length > 1 ) 
	{
		// Add a marker at the beginning and the end
		userDistance.overlayMarkerEnd = new GMarker(userDistance.routePoints[userDistance.routePoints.length - 1], markerIcon);
		map.addOverlay(userDistance.overlayMarkerEnd);
	
		// Re-calculate the distance so far
		var lastLeg = distanceBetweenPnt(userDistance.routePoints[userDistance.routePoints.length - 2], userDistance.routePoints[userDistance.routePoints.length - 1]);
		userDistance.totalDistance += lastLeg;	
	}
	updateDistance();		
}

//-------------------------------------------------------------------------------------------
// Remove Overlay distance

function removeOverlayDist()
{
	// Remove all Overlay distance on MAP
	if(userDistance.overlayDist != null)
	{
		map.removeOverlay(userDistance.overlayMarkerStart);
		map.removeOverlay(userDistance.overlayDist);
		if(userDistance.overlayMarkerEnd != null)
			map.removeOverlay(userDistance.overlayMarkerEnd);
	}
}


//-------------------------------------------------------------------------------------------
// Remove Overlay distance

function resetOverlayDist()
{
	clearAllRoute();
	userDistance.overlayDist = null;
	userDistance.overlayMarkerStart = null;
	userDistance.overlayMarkerEnd = null;
}


//-------------------------------------------------------------------------------------------
// Clear the last leg

function clearLastPoint ( ) 
{
	if (userDistance.routePoints.length == 1)
			clearAllRoute();
			
	else if (userDistance.routePoints.length > 0)
	{
		userDistance.routePoints.pop();
		removeOverlayDist();
		drawDistance();
	}
}


//-------------------------------------------------------------------------------------------
// Clears the existing route

function clearAllRoute ( ) 
{
	removeOverlayDist();
	userDistance.routePoints = [];
	userDistance.totalDistance = 0.000;
	updateDistance();
}


//-------------------------------------------------------------------------------------------
// Selection of unit mesure

function changeUnit (u) 
{
	if (u == "MILES") 
		userDistance.unitHandler = MILES;
	else if (u == "KMS")
		userDistance.unitHandler = KMS;
	else
		userDistance.unitHandler = KMS;
	
	updateDistance();
}

