﻿var TC_UIhoursLeft = 0;
var TC_UIminutesLeft = 0;
var TC_remainingSecondsLeft = 0;
var TC_timer;

function TC_RenderTimeUnit(TC_timeUnit) {
    if (TC_timeUnit >= 10) {
        return TC_timeUnit;
    }
    else {
        return "0" + TC_timeUnit;
    };
}

function TC_timerCountdown() {

    // times up
    if (TC_totalSecondsLeft < 1) {

        //if refresh parameter is true then refresh site
        if (TC_NavigateUrlOnTimerZero != '') {
            document.location.href = TC_NavigateUrlOnTimerZero;
        }
        else {
            TC_UpdateTimerUI();
            clearInterval(TC_timer);
        }
    }

    // time left
    else {
        TC_UpdateTimerUI();
        if (TC_timer == null) { TC_timer = setInterval("TC_timerCountdown()", 1000); }
    }

    function TC_UpdateTimerUI() {

        //Calc amount of Hours and remaining seconds left:
        TC_UIhoursLeft = parseInt(TC_totalSecondsLeft / 3600); // Removes total time less than 3600 seconds
        TC_remainingSecondsLeft = TC_totalSecondsLeft - (TC_UIhoursLeft * 3600);

        //Calc amount of Minutes and ramining seconds left:
        TC_UIminutesLeft = parseInt(TC_remainingSecondsLeft / 60); // Removes total time less than 60 seconds
        TC_remainingSecondsLeft = TC_remainingSecondsLeft - (TC_UIminutesLeft * 60);

        //Render TC_timer in userfriendly format
        TC_totalSecondsLeft = TC_totalSecondsLeft - 1;
        $('#TC_timeLeftLabel').text((TC_RenderTimeUnit(TC_UIhoursLeft) + ":" + TC_RenderTimeUnit(TC_UIminutesLeft) + ":" + TC_RenderTimeUnit(TC_remainingSecondsLeft)));
    }

}


