﻿/*
AdvancedCountDown.js
Written By: Yahav Braverman, 2010
	
ANY CHANGE IN THE CODE BELOW MIGHT CAUSE UNEXPECTED PROBLEMS.
PLEASE USE THE SAMPLE CODE THAT COME WITH THIS FILE.
*/

var _arrCountDownContainers = new Array();
var _arrCountDownSeconds = new Array();
var _arrCountDownCallbacks = new Array();

var _countDownTimer = 0;

function _cdt_CountDownTick() {
    var activeCount = 0;
    for (var key in _arrCountDownSeconds) {
        var curSeconds = _arrCountDownSeconds[key];
        if (curSeconds < 0)
            continue;

        if (curSeconds == 0) {
            _cdt_TimeOver(key);
            continue;
        }

        _cdt_ApplyCountdownText(key);
        activeCount++;
    }

    if (activeCount > 0)
        _countDownTimer = window.setTimeout("_cdt_CountDownTick()", 100);
}

function _cdt_TimeOver(key) {
    _arrCountDownSeconds[key] = -1;
    var strCallback = _arrCountDownCallbacks[key];
    if (strCallback && strCallback.length > 0) {
        eval(strCallback + "();");
    }
}

function ActivateCountDown(strContainerID, initialValue, strCallback) {

    if (typeof initialValue == "undefined") {
         if (_arrCountDownSeconds[strContainerID] && _arrCountDownSeconds[strContainerID] < 0) {
            _arrCountDownSeconds[strContainerID] = _arrCountDownSeconds[strContainerID] * -1;
            _arrCountDownContainers[strContainerID].setAttribute("initial_timer_value", _arrCountDownSeconds[strContainerID] + "");
            _arrCountDownContainers[strContainerID].setAttribute("activation_time", _cdt_GetCurrentTime() + "");
            RestartCountdownTimer(1);
        }
        return;
    }

    var objContainer = document.getElementById(strContainerID);
    if (!objContainer) {
        // alert("count down error: container does not exist: " + strContainerID + "\nmake sure html element with this ID exists");
        return;
    } else {
        if (is_array(initialValue)) {
            var dtNow = new Date();
            var dtFuture = _cdt_ArrayToDate(initialValue);
            if (dtFuture < dtNow) {
                alert("count down error: can't use past date as initializer");
                return;
            }
            initialValue = parseInt(((dtFuture.getTime() - dtNow.getTime()) / 1000));
        }

        objContainer.setAttribute("activation_time", _cdt_GetCurrentTime() + "");
        objContainer.setAttribute("initial_timer_value", initialValue + "");

        _arrCountDownContainers[strContainerID] = objContainer;
        _arrCountDownCallbacks[strContainerID] = strCallback;
        _cdt_ApplyCountdownText(strContainerID);

        RestartCountdownTimer(1000);
    }

   
}


function RestartCountdownTimer(value) {
    if (_countDownTimer)
        window.clearTimeout(_countDownTimer);
    _countDownTimer = window.setTimeout("_cdt_CountDownTick()", value);
}

function DeactivateCountDown(strContainerID) {
    if (_arrCountDownSeconds[strContainerID] && _arrCountDownSeconds[strContainerID] > 0) {
        _arrCountDownSeconds[strContainerID] = _arrCountDownSeconds[strContainerID] * -1;
    }
}

function DeactivateAllCountdowns() {
    for (var key in _arrCountDownSeconds) {
        DeactivateCountDown(key);
    }
}

function ActivateAllCountdowns() {
    for (var key in _arrCountDownSeconds) {
        ActivateCountDown(key);
    }
}

function SetInitialTime(strContainerID, initialValue) {
    var nValue = parseInt(initialValue);
        if (isNaN(nValue)) {
        alert("invalid number: " + initialValue);
        return;
    }

    if (nValue < 0)
        nValue = 0;

    var objContainer = _arrCountDownContainers[strContainerID];
    if (objContainer) {
        objContainer.setAttribute("activation_time", _cdt_GetCurrentTime() + "");
        objContainer.setAttribute("initial_timer_value", nValue + "");
    }
   
}

function GetCurrentTime(strContainerID) {
    var objContainer = _arrCountDownContainers[strContainerID];
    if (objContainer) {
        return _cdt_GetRealSeconds(objContainer);
    }
    return 0;
}


function is_array(input) {
    return (typeof (input) == 'object' && (input instanceof Array));
}

function _cdt_ApplyCountdownText(strContainerID) {
    //get container:
    var objContainer = _arrCountDownContainers[strContainerID];

    //get format:
    var strFormat = objContainer.getAttribute("time_format");
    var blnCustomFormat = true;
    if (!strFormat || strFormat.length == 0) {
        strFormat = "%h:%m:%s";
        blnCustomFormat = false;
    }

    //get real seconds
    var seconds = _cdt_GetRealSeconds(objContainer);

    //store:
    _arrCountDownSeconds[strContainerID] = seconds;

    //build text:
    var strText = "";

    //time over?
    var strFinishTime = objContainer.getAttribute("finish_value");
    if (strFinishTime && strFinishTime.length > 0) {
        var nFinishTime = parseFloat(strFinishTime);
        if (!isNaN(nFinishTime) && seconds < nFinishTime) {
            _cdt_TimeOver(strContainerID);
            return;
        }
    }

    //raw?
    if (strFormat == "RAW") {
        strText = parseFloat(seconds.toFixed(2));
    }
    else {
        //get minutes:
        var minutes = parseInt(seconds / 60);

        //shrink:
        seconds = (seconds % 60);

        //get hours:
        var hours = parseInt(minutes / 60);

        //shrink:
        minutes = (minutes % 60);

        //get days:
        var days = parseInt(hours / 24);

        //shrink:
        hours = (hours % 24);

        //need to add zero?
        if (!blnCustomFormat) {
            hours = AddZero(hours);
            minutes = AddZero(minutes);
            seconds = AddZero(seconds);
        }
        strText = strFormat.replace("%d", days + "").replace("%h", hours + "").replace("%m", minutes + "").replace("%s", seconds + "");
    }

    //apply:
    objContainer.innerHTML = strText;
}

function AddZero(num) {
    return ((num >= 0) && (num < 10)) ? "0" + num : num + "";
}

function _cdt_GetCurrentTime() {
    var objDate = new Date();
    return objDate.getTime();
}

function _cdt_GetRealSeconds(objContainer) {
    var nCurrentTime = _cdt_GetCurrentTime();
    var nActivationTime = parseInt(objContainer.getAttribute("activation_time"));
    var nInitialValue = parseFloat(objContainer.getAttribute("initial_timer_value"));
    var nMiliSecondsDiff = (nCurrentTime - nActivationTime);
    var nTotalDifference = parseInt(nMiliSecondsDiff / 1000);
    var strSecondValue = objContainer.getAttribute("second_value");
    if (strSecondValue && strSecondValue.length > 0) {
        var nSecondValue = parseFloat(strSecondValue);
        if (!isNaN(nSecondValue))
            nTotalDifference = parseFloat(nTotalDifference) * nSecondValue;
    }
    return (nInitialValue - nTotalDifference);
}

function _cdt_ArrayToDate(arrDateTimeParts) {
    var dtFuture = new Date();
    var day = _cdt_ParseArrayItemToNumber(arrDateTimeParts, 0, 1, 31, "day", 0, dtFuture.getDate());
    var month = _cdt_ParseArrayItemToNumber(arrDateTimeParts, 1, 1, 12, "month", -1, dtFuture.getMonth());
    var year = _cdt_ParseArrayItemToNumber(arrDateTimeParts, 2, 200, 3200, "year", 0, dtFuture.getFullYear());
    var hours = _cdt_ParseArrayItemToNumber(arrDateTimeParts, 3, 0, 23, "hour", 0, dtFuture.getHours());
    var minutes = _cdt_ParseArrayItemToNumber(arrDateTimeParts, 4, 0, 59, "minute", 0, dtFuture.getMinutes());
    var seconds = _cdt_ParseArrayItemToNumber(arrDateTimeParts, 5, 0, 59, "second", 0, dtFuture.getSeconds());
    dtFuture.setDate(day);
    dtFuture.setMonth(month);
    dtFuture.setFullYear(year);
    dtFuture.setHours(hours);
    dtFuture.setMinutes(minutes);
    dtFuture.setSeconds(seconds);
    return dtFuture;
}

function _cdt_ParseArrayItemToNumber(arrValues, index, minValue, maxValue, strCaption, offset, defaultValue) {
    if (index >= 0 && index < arrValues.length) {
        var nValue = parseInt(arrValues[index]) + offset;
        if (isNaN(nValue) || nValue < minValue || nValue > maxValue) {
            alert(arrValues[index] + " is invalid " + strCaption + ", using current " + strCaption);
        }
        else {
            return nValue;
        }
    }
    return defaultValue;
}

function TimeOut() {
    var flg = true;
    for (var key in _arrCountDownSeconds) {
        var objContainer = document.getElementById(key);
        if (!objContainer) {
            flg = false;
        }
    }
    if (flg == true) {
        window.alert("Đã hết thời gian hiệu lực. Xin quý khách vui lòng chọn lại.");
        timeout();
    }
} 
