frontend = {};
frontend.init = function() {
    $("div.tabs > ul").tabs();

    frontend.customCounter.init();

    // Alle Links mit einer bestimmten Klasse in einem neuen Fenster oeffnen
    $('.new-window').bind('click', function(event) {
        event.preventDefault();
        window.open(this.href, '_blank', '');
    });
}

frontend.customCounter = {};
frontend.customCounter.endDate = Date.parse(cdata.customCounterEndDate);
frontend.customCounter.list = {};
frontend.customCounter.init = function() {
    frontend.customCounter.list = $('#custom-counter-box ul');

    if (frontend.customCounter.list.length < 1) {
        return;
    }

    window.setInterval(function() {
        var
          now = new Date(),
          dayMilliSeconds = 86400000,
          hourMilliSeconds = 3600000,
          minMilliSeconds = 60000,
          time = frontend.customCounter.endDate - now,
          days = Math.floor(time / dayMilliSeconds),
          hours = Math.floor((time - (days * dayMilliSeconds)) / hourMilliSeconds),
          mins = Math.floor((time - (days * dayMilliSeconds) - (hours * hourMilliSeconds)) / minMilliSeconds),
          secs = Math.floor((time - (days * dayMilliSeconds) - (hours * hourMilliSeconds) - (mins * minMilliSeconds)) / 1000);

        if (now > frontend.customCounter.endDate) {
            $('#counter-days .time', frontend.customCounter.list).text('0');
            $('#counter-hours .time', frontend.customCounter.list).text('00');
            $('#counter-minutes .time', frontend.customCounter.list).text('00');
            $('#counter-seconds .time', frontend.customCounter.list).text('00');
        } else {
            $('#counter-days .time', frontend.customCounter.list).text(days);
            $('#counter-hours .time', frontend.customCounter.list).text(hours > 9 ? hours : '0' + hours);
            $('#counter-minutes .time', frontend.customCounter.list).text(mins > 9 ? mins : '0' + mins);
            $('#counter-seconds .time', frontend.customCounter.list).text(secs > 9 ? secs : '0' + secs);
        }
    }, 1000);
}

$(document).ready(frontend.init);
