function init() {
    $('p.noscript').hide();
    $('div.counter div.content').show();
    window.setInterval(setDateParameters, 1000);
}

function setDateParameters() {
    var targetDate = new Date(2011, 9, 9, 0, 0, 0);
    var startDate = new Date();
    var elapsed = getElapsed(startDate, targetDate);

    if (elapsed.isValid()) {
        $('div.count p.d').html(padString(elapsed.days, 2, '0') + '<span>GIORNI</span>');
        $('div.count p.h').html(padString(elapsed.hours, 2, '0') + '<span>ORE</span>');
        $('div.count p.m').html(padString(elapsed.mins, 2, '0') + '<span>MINUTI</span>');
        $('div.count p.s').html(padString(elapsed.secs, 2, '0') + '<span>SECONDI</span>');
    } 
}

function getElapsed(fromDate, toDate) {
    var diff = (toDate - fromDate) / 1000;

    var rem;
    var d = Math.floor(diff / (60 * 60 * 24));
    rem = diff - d * 60 * 60 * 24;
    var h = Math.floor(rem / (60 * 60));
    rem = rem - h * 60 * 60;
    var m = Math.floor(rem / 60);
    var s = Math.floor(rem - m * 60);
    var time = new objElapsed( d, h, m, s);
    return time;
}

function objElapsed(d, h, m, s) {
    this.days = d;
    this.hours = h;
    this.mins = m;
    this.secs = s;
    this.isValid = function () {
        return this.days > 0 || this.hours > 0 || this.mins > 0 || this.secs > 0;
    }
}

function padString(text, digitLength, padChar) {
    var _text = text + '';
    while (_text.length < digitLength) {
        _text = padChar + _text;
    }
    return _text;
}


