var backgroundImageId = 0;

$(document).ready(function() {

    window.scrollTo(0,1);
    
    $('#ajaxthrobber')
    .ajaxStart(function() {
        $(this).show();
    })
    .ajaxSuccess(function() {
        $(this).hide();
    })
    .ajaxStop(function() {
        $(this).hide();
    })
    .ajaxError(function() {
        $(this).hide();
    });

    $(window).load(fitBackgroundImage);
    $(window).resize(fitBackgroundImage);

    loadPage(location.hash.replace('#/', ''));
    assignLinkHandler();

    $('#lang_switch').click(function(event) {
        event.preventDefault();
        $.post(getURLBase() + 'switchlang', {
            'name' : 'nouse'
        }, function(data) {
            window.location.reload(true);
        });
    });
    
    updateCountdown();
    setInterval(updateCountdown, 60000);
    
});

function updateCountdown() {
    var now = new Date();        
    var msPerDay = 24 * 60 * 60 * 1000;
    var timeLeft = (countdowntimer.getTime() - now.getTime());
    if (timeLeft > 0) {
        var e_daysLeft = timeLeft / msPerDay;
        var daysLeft = Math.floor(e_daysLeft);
        var e_hrsLeft = (e_daysLeft - daysLeft)*24;
        var hoursLeft = Math.floor(e_hrsLeft);
//        var e_minLeft = (e_hrsLeft - hoursLeft) * 60;
//        var minLeft = Math.floor(e_minLeft);
//        var e_secLeft = (e_minLeft - minLeft) * 60;
//        var secLeft = Math.floor(e_secLeft);
        
        $('#countdowndays').html(daysLeft);
        $('#countdownhours').html(hoursLeft);
        if (daysLeft == 1) {
            getCurrentLanguage() == 'de' ?
                $('#countdowndaysstring').html(' Tag'):
                $('#countdowndaysstring').html(' day');
        }
        else {
            getCurrentLanguage() == 'de' ?
                $('#countdowndaysstring').html(' Tage'):
                $('#countdowndaysstring').html(' days');
        }
        
        if (hoursLeft == 1) {
            getCurrentLanguage() == 'de' ?
                $('#countdownhoursstring').html(' Stunde'):
                $('#countdownhoursstring').html(' hour');
        }
        else {
            getCurrentLanguage() == 'de' ?
                $('#countdownhoursstring').html(' Stunden'):
                $('#countdownhoursstring').html(' hours');
        }
    }
}

function fitBackgroundImage() {
    window.scrollTo(0,1);
    if (screen.width >= 1060 && screen.height >= 800) {
        var height = $(window).height();
        var width = $(window).width();

        var backgroundTransition = '<div id="background_transition">';
        backgroundTransition += '<img src="'+getURLBase()+'assets/img/background/0.jpg" id="background_transition_0" />';
        backgroundTransition += '<img src="'+getURLBase()+'assets/img/background/1.jpg" id="background_transition_1" /> ';
        backgroundTransition += '<img src="'+getURLBase()+'assets/img/background/2.jpg" id="background_transition_2" /> ';
        backgroundTransition += '</div>';

        if($('#background_transition').length == 0) {
            $('body').prepend(backgroundTransition);
        }

        var preload = '<div id="preload">';
        preload += '<img src="'+getURLBase()+'assets/img/background/0.jpg"/>';
        preload += '<img src="'+getURLBase()+'assets/img/background/1.jpg"/>';
        preload += '<img src="'+getURLBase()+'assets/img/background/2.jpg"/>';
        preload += '</div>'

        if($('#preload').length == 0) {
            $('body').append(preload);
        }

        if (height > width) {
            $('#background_transition img').css('height', '100%').css('width', '');
            //$('#background_transition img').css('margin-left', (0.25 * (width - height)) + 'px').css('margin-top', 0);
            $('#background_transition img').css('margin-left', 0).css('margin-top', 0);
        }
        else {
            $('#background_transition img').css('width', '100%').css('height', '');
            //$('#background_transition img').css('margin-top', (0.25 * (height - width)) + 'px').css('margin-left', 0);
            $('#background_transition img').css('margin-top', 0).css('margin-left', 0);
        }

        $('#background_transition').cycle('destroy');
        $('#background_transition').cycle({
            speed:   3000,
            timeout: 10000
        });
    }
}

function loadPage(url) {
    var location = url;
    if (location == '') {
        location ='start';
    }
    
    if ($('html').data(location) == undefined) {
        // url must be simple, must match strings below
        var prefix = getURLBase() +  'html/';
        $.post(prefix + location, {
            'name': 'nouse'
        }, function(data) {
            switch(location) {
                default:
                    setupPage(data);

                    $('html').data(location, data);
                    break;
            }
        });
    }
    else {
        setupPage($('html').data(location));
    }
    
}

function assignLinkHandler() {

    $('a[rel=lightbox]').unbind('click');
    $('a[rel=lightbox]').click(function(event) {
        event.preventDefault();
        $('#lightbox img').attr('src', $(this).attr('href'));
        $('#lightbox').fadeIn();
    });

    $('#close').unbind('click');
    $('#close').click(function(event) {
        event.preventDefault();
        $('#lightbox').fadeOut();
    });

    $('a.newpage').unbind('click');
    // assign navigation
    $('a.newpage').click(function(event) {
        event.preventDefault();
        var re = new RegExp("^(" + getURLBase() + ")");
        var href = $(this).attr('href').replace(re, '');
        location.href = location.href.replace(/#(.)*/g, '') + '#/' + href;
        loadPage(href);

    });

    $('.discography_song').unbind('click');
    $('.discography_song').click(function(event) {
        event.preventDefault();
        var songID = $(this).attr('song');
        $('.discography_song_info').hide();
        $('.discography_song_info[song='+songID+']').fadeIn();
    });
}

function setupPage(data) {
    $('#content').fadeOut(function() {
        $('#content').html(data);
        var children = $('#content').children();
        // assign handler
        assignLinkHandler();

        $(children).each(function() {
            $(this).hide()
        })
        $('#content').show();
        var elements = new Array();
        for (var i = 0; i < children.length; i++) {
            elements.push(children[i]);
        }
        fadeInElements(elements);
    });
}

function fadeInElements(elements) {
    // recursive call of rest of elements
    if (elements.length > 0) {
        var element = elements.shift();
        if ($(element).hasClass('no_show')) {
            fadeInElements(elements)
        }
        else {
            $(element).fadeIn(function() {
                fadeInElements(elements)
            });
        }
        
    }
}
