var startTime, percentageCompletion = 0;

$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "config.xml?" + Math.random(),
        dataType: "xml",
        success: parseXml
    });
});

function parseXml(xml){
    $(xml).find("first-two-letters").each(function(){
        $('#logo .title').html($(this).text());
    });
    $(xml).find("first-line").each(function(){
        $('.h2').html($(this).text());
    });
    $(xml).find("second-line").each(function(){
        $('.h22').html($(this).text());
    });
    $(xml).find("address").each(function(){
        $('.address span:first').html($(this).text());
    });
    $(xml).find("email").each(function(){
        $('.address span:eq(1)').html('E: ' + $(this).text());
    });
    $(xml).find("phone").each(function(){
        $('.address span:eq(2)').html('T: ' + $(this).text());
    });
    $(xml).find("facebook-link").each(function(){
        $('.f').attr('href', $(this).text());
    });
    $(xml).find("twitter-link").each(function(){
        $('.t').attr('href', $(this).text());
    });
    $(xml).find("rss-link").each(function(){
        $('.r').attr('href', $(this).text());
    });
    $(xml).find("toolbar").each(function(){
        if ($(this).text() == 'yes'){
            $('.panel').show();
        } else {
            $('.panel').hide();
        }
    });
    $(xml).find("percentage-of-completion").each(function(){
        percentageCompletion = $(this).text();
        $('#procent').hide();
        $('#progresbar-bar').css('width', '1%');
        $(document).ready(function(){
            $('#progresbar-bar').animate({'width': percentageCompletion + '%'}, percentageCompletion * 100, function(){
                $('.proc').html(percentageCompletion + '%');
                $('#procent').css('margin-left', percentageCompletion * 703 / 100 - 20).show();
                setCufon();
            });
        });
    });
    $(xml).find("start-date-time").each(function(){
        startTime = Date.parse($(this).text());
    });
    $(xml).find("template-number").each(function(){
        changeStyle($(this).text());
        timeLeft();
        $('.panel a').each(function(key, value){
            $(this).click(function (){
                changeStyle(key + 1);
                return false;
            });
        });
    });
}

function changeStyle(num){
    if (num > 0 && num < 6){
        $('body').removeClass().addClass('chema' + num);
        setCufon();
    }
}

function setCufon(){
    Cufon.replace('.h2, .title, .number', { fontFamily: 'Bebas', hover:true });
    Cufon.replace('.h22', { fontFamily: 'Coolvetica'});
    Cufon.replace('.text, .proc', { fontFamily: 'Humanst521 Cn BT', hover:true});
}

function timeLeft(){
    var
        now = new Date(),
        totalSec = parseInt(startTime - now) / 1000,
        month = Math.floor(totalSec / 60 / 60 / 24 / 31),
        kM = month * 60 * 60 * 24 * 31,
        days = Math.floor((totalSec - kM) / 60 / 60 / 24),
        kD = days * 60 * 60 * 24,
        hours = Math.floor((totalSec - kM - kD) / 60 / 60),
        kH = hours * 60 * 60,
        min = Math.floor((totalSec - kM - kD - kH) / 60),
        sec = Math.floor((totalSec - kM - kD - kH - min * 60)),
        fmonth = 0, smonth = 0,
        fday = 0, sday = 0,
        fhour = 0, shour = 0,
        fmin = 0, smin = 0,
        fsec = 0, ssec = 0;
        
    if (totalSec > 0){
        if (month < 10){
            smonth = month;
        } else {
            fmonth = month.toString().substr(0, 1);
            smonth = month.toString().substr(1, 1);
        }
        $('#dates span.number:eq(0)').text(fmonth);
        $('#dates span.number:eq(1)').text(smonth);
        
        if (days < 10){
            sday = days;
        } else {
            fday = days.toString().substr(0, 1);
            sday = days.toString().substr(1, 1);
        }
        $('#dates span.number:eq(2)').text(fday);
        $('#dates span.number:eq(3)').text(sday);
        
        if (hours < 10){
            shour = hours;
        } else {
            fhour = hours.toString().substr(0, 1);
            shour = hours.toString().substr(1, 1);
        }
        $('#dates span.number:eq(4)').text(fhour);
        $('#dates span.number:eq(5)').text(shour);
        
        if (min < 10){
            smin = min;
        } else {
            fmin = min.toString().substr(0, 1);
            smin = min.toString().substr(1, 1);
        }
        $('#dates span.number:eq(6)').text(fmin);
        $('#dates span.number:eq(7)').text(smin);
        
        if (sec < 10){
            ssec = sec;
        } else {
            fsec = sec.toString().substr(0, 1);
            ssec = sec.toString().substr(1, 1);
        }
        $('#dates span.number:eq(8)').text(fsec);
        $('#dates span.number:eq(9)').text(ssec);
        
        setCufon();
        
        setTimeout(timeLeft, 1000);
    }
}

