﻿var inMove = false;

function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined') {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return { left: offsetLeft, top: offsetTop };
}
function GetPositionOnTrack(percentageInScreen, xIndex) {
    if (xIndex == 0) {
        var distanceForPercentage = distancesA[myStartMarker.Index] + (distancesA[myEndMarker.Index] - distancesA[myStartMarker.Index]) * percentageInScreen;
        var i;
        if (myStartMarker.Index == myEndMarker.Index) {
            return { index: myStartMarker.Index, percentage: 0.0, distance: distancesA[myStartMarker.Index], time: timesA[myStartMarker.Index] };
        }
        for (var i = myStartMarker.Index; i <= myEndMarker.Index; i++) {
            if (distancesA[i] > distanceForPercentage) {
                var remainingDistance = distancesA[i] - distanceForPercentage;
                var remainingPercentage = remainingDistance / (distancesA[i] - distancesA[i - 1]);
                remainingPercentage = 1.0 - remainingPercentage;
                var timeForPercentage = timesA[i - 1] + remainingPercentage * (timesA[i] - timesA[i - 1]);
                return { index: i - 1, percentage: remainingPercentage, distance: distanceForPercentage, time: timeForPercentage };
            }
        }
        return { index: myEndMarker.Index - 1, percentage: 1.0, distance: distancesA[myEndMarker.Index], time: timesA[myEndMarker.Index] };
    }
    else {
        var timeForPercentage = timesA[myStartMarker.Index] + (timesA[myEndMarker.Index] - timesA[myStartMarker.Index]) * percentageInScreen;
        var i;
        if (myStartMarker.Index == myEndMarker.Index) {
            return { index: myStartMarker.Index, percentage: 0.0, distance: distancesA[myStartMarker.Index], time: timesA[myStartMarker.Index] };
        }
        for (var i = myStartMarker.Index; i <= myEndMarker.Index; i++) {
            if (timesA[i] > timeForPercentage) {
                var remainingTime = timesA[i] - timeForPercentage;
                var remainingPercentage = remainingTime / (timesA[i] - timesA[i - 1]);
                remainingPercentage = 1.0 - remainingPercentage;
                var distanceForPercentage = distancesA[i - 1] + remainingPercentage * (distancesA[i] - distancesA[i - 1]);
                return { index: i - 1, percentage: remainingPercentage, distance: distanceForPercentage, time: timeForPercentage };
            }
        }
        return { index: myEndMarker.Index - 1, percentage: 1.0, distance: distancesA[myEndMarker.Index], time: timesA[myEndMarker.Index] };
    }
}
function GetDistanceString(distance) {
    var outputDistanceString = String(Math.round(distance) / 1000);
    outputDistanceString = outputDistanceString.substring(0, 7) + " km";
    outputDistanceString = outputDistanceString.replace(".", ",");
    return outputDistanceString;
}
function GetTimeString(time) {
    if (isNaN(time)) {
        return "";
    }
    var outputTimeString = "";
    var hours = Math.floor(time / 3600.0);
    var strHours;
    if (hours > 99) {
        strHours = 99;
    }
    else if (hours < 10) {
        strHours = "0" + hours;
    }
    else {
        strHours = hours + "";
    }
    time = time - (hours * 3600.0);
    var minutes = Math.floor(time / 60.0);
    var strMinutes;
    if (minutes < 10) {
        strMinutes = "0" + minutes;
    }
    else {
        strMinutes = minutes + "";
    }
    time = time - (minutes * 60.0);
    var seconds = Math.floor(time);
    var strSeconds;
    if (seconds < 10) {
        strSeconds = "0" + seconds;
    }
    else {
        strSeconds = seconds + "";
    }
    return " | " + strHours + ":" + strMinutes + ":" + strSeconds;
}