$(function () {
    var days = $("#tnt_coupons_days");
    var hours;
    var minutes;
    var seconds;
    var n_days = 0;
    var n_hours = 0;
    var n_minutes = 0;
    var n_seconds = 0;

    function setValue(name, val) {
        $(".tnt_coupons .bloc-time." + name).data("value", val);
        val = parseInt(val);
        if (val < 10) {
            val = "0" + val;
        }
        val = val.toString();
        $(".tnt_coupons .countdown .figure." + name + "-1").html(val[0]);
        $(".tnt_coupons .countdown .figure." + name + "-2").html(val[1]);
    }

    setValue("sec", $(".tnt_coupons .bloc-time.sec").data("value"));
    setValue("min", $(".tnt_coupons .bloc-time.min").data("value"));
    setValue("hours", $(".tnt_coupons .bloc-time.hours").data("value"));

    var countdown = setInterval(function () {
        hours = parseInt($(".tnt_coupons .bloc-time.hours").data("value"));
        minutes = parseInt($(".tnt_coupons .bloc-time.min").data("value"));
        seconds = parseInt($(".tnt_coupons .bloc-time.sec").data("value"));

        n_seconds = seconds - 1;
        setValue("sec", n_seconds);
        if (n_seconds < 0) {
            setValue("sec", 59);
            n_minutes = minutes - 1;
            if (n_minutes < 0) {
                setValue("min", 59);
                n_hours = hours - 1;
                if (n_hours < 0) {
                    setValue("hours", 23);
                    n_days = parseInt(days.html()) - 1;
                    if (n_days < 0) {
                        days.html("0");
                        setValue("hours", 0);
                        setValue("min", 0);
                        setValue("sec", 0);
                        clearInterval(countdown);
                    } else {
                        days.html(n_days);
                    }
                } else {
                    setValue("hours", n_hours);
                }
            } else {
                setValue("min", n_minutes);
            }
        }
    }, 1000);
});