﻿var timerElement;
var counter;
var interval;
function createCounter(topElement, timeDiff) {
    timerElement = jQuery("#" + topElement);
    counter = timeDiff;
    if (counter == 0) {
        TreasureChestCallAjax();
    } 
    interval = setInterval("countDown()", 1000);
}

function countDown() {
    var timeDiff = counter;
    var hours = Math.floor(timeDiff / 3600);
    timeDiff -= hours * 3600;
    var minutes = Math.floor(timeDiff / 60);
    timeDiff -= minutes * 60;
    var seconds = timeDiff; //Math.floor(timeDiff % 60);
    var resultStr = composeTimeString(hours) + ":" + composeTimeString(minutes) + ":" + composeTimeString(seconds);
    timerElement.text(resultStr);
    counter = counter - 1;
    if (counter == 0) {
        TreasureChestCallAjax();
    }
}

function composeTimeString(value) {
    if (value < 10) {
        return "0" + value;
    }
    else {
        return value.toString();
    }
}
