// myDiv             - element to show countdown
//                     example <td id="test">[CountDown]</td> then myDiv = "test"
// targetDatemillis  - date in format: 1208772000000
// afterTimeout      - what should be put in indicator when time will run out
// Counter shows up in local time.
var timeTargets = new Array();
var finishTime = new Array();
var timeOut = new Array();
var timeOutTrigger = new Array();
var stDate = (new Date()).getTime();

function AddCountDown(myDiv, targetDatemillis, afterTimeout)
{
    var counterName = myDiv;
    timeTargets[counterName] = myDiv;
    finishTime[counterName] = targetDatemillis;
    if (afterTimeout) {
        timeOut[counterName] = afterTimeout;
    } else {
        timeOut[counterName] = "--:--:--";
    }
    timeOutTrigger[counterName] = null;
}

function resetCounters() {
    timeTargets = [];
    finishTime = [];
    timeOut = [];
}

/*
    Count down from the number of milliseconds in myDiv to zero, optionally trigger a function at timeout
 */
function AddCountDownToZero(myDiv, targetTime, counterName, afterTimeoutString, timeoutTrigger) {
    timeTargets[counterName] = myDiv;
    finishTime[counterName] = targetTime;

    if (afterTimeoutString) {
        timeOut[counterName] = afterTimeoutString;
    } else {
        timeOut[counterName] = "--:--:--";
    }

    if (timeoutTrigger) {
        timeOutTrigger[counterName] = timeoutTrigger;
    } else {
        timeOutTrigger[counterName] = null;
    }
}

function addSecondsToCounter(counterName, secs) {
    var now = new Date();
    finishTime[counterName] = now.getTime() + secs * 1000;
}

function Calcage(secs, num1, num2)
{
    var s = ((Math.floor(secs / num1)) % num2).toString();
    if (s.length < 2)
    {
        s = "0" + s;
    }
    return (s);
}

function calcTimeLeft(currentTime, finishTime, num1, num2) {
    var millisecs = finishTime - currentTime; // calc date diff
    var s = ((Math.floor(millisecs / 1000 / num1)) % num2).toString();
    if (s.length < 2) {
        s = "0" + s;
    }
    return (s);
}

function formatTime(currentTime, counterName) {
    if (currentTime >= finishTime[counterName]) {
        return timeOut[counterName];
    }
    var DisplayStr;
    var displayFormat = "%%D%% D %%H%%:%%M%%:%%S%%";
    if (calcTimeLeft(currentTime, finishTime[counterName], 86400, 100000) > 0) {
        DisplayStr = displayFormat.replace(/%%D%%/g, calcTimeLeft(currentTime, finishTime[counterName], 86400, 100000));
    } else {
        DisplayStr = displayFormat.replace(/%%D%% D /g, '');
    }
    DisplayStr = DisplayStr.replace(/%%H%%/g, calcTimeLeft(currentTime, finishTime[counterName], 3600, 24));
    DisplayStr = DisplayStr.replace(/%%M%%/g, calcTimeLeft(currentTime, finishTime[counterName], 60, 60));
    DisplayStr = DisplayStr.replace(/%%S%%/g, calcTimeLeft(currentTime, finishTime[counterName], 1, 60));
    return DisplayStr;
}

// timeRelative - boolean [true|false]
//                TRUE  = tells us to treat timeInMillis as actual millis till target date
//                FALSE = tells us to treat timeInMillis as absolute target date (thats default)
function CountBack(timeRelative)
{
    var timeQuant = 0;

    var now = new Date();
    if (stDate) {
        timeQuant = now.getTime() - stDate;
    }
    stDate = now.getTime();

    for (var counterName in timeTargets) {
        // TODO: we should get rid of this absolute date stuff and always send relative time from server
        var timeDiff = timeRelative === true ? timeQuant : now.getTime();
        var DisplayStr = formatTime(timeDiff, counterName);
        if (finishTime[counterName] - timeDiff > 0) {
            document.getElementById(timeTargets[counterName]).innerHTML = DisplayStr;
            if (timeRelative === true) {
                finishTime[counterName] = finishTime[counterName] - timeQuant;
            }
        } else {
            if (timeOutTrigger[counterName]) {
                var triggerMethod = timeOutTrigger[counterName];
                eval(triggerMethod);
            } else {
                document.getElementById(timeTargets[counterName]).innerHTML = timeOut[counterName];
                // TODO: Do we really need to do that on every CountBack ? Maybe we should remove timeTargets[counterName] ?
            }
        }
    }
}
