function cycleHeadMenu(n,m){
    $('#head-menu li:nth-child('+n+')').find('a').removeClass('selected');
    $('#head-gallery a:nth-child('+n+')').fadeOut('slow');
    $('#head-menu li:nth-child('+m+')').find('a').addClass('selected');
    $('#head-gallery a:nth-child('+m+')').fadeIn('slow');

    setTimeout(function(){cycleHeadMenu(m,($('#head-menu li').size() > m ) ? m+1 : 1)},6000);
}

function resizeOverlay(){
    $('#overlay').height($(document).height());
    $('#overlay').width($(document).width());
}

function initReference(){
    referenceCount = $('.reference').size();
    $('#references').width(referenceCount*200);
    referenceOffset = 0;
    setReferenceButtons();
}

function setReferenceButtons(){
    if (referenceOffset == 0) $('#references-button-left').addClass('disabled');
    else $('#references-button-left').removeClass('disabled');
    
    if (referenceCount - referenceOffset <= referenceVisibles) $('#references-button-right').addClass('disabled');
    else $('#references-button-right').removeClass('disabled');
}



// globals for reference slider
var referenceCount = 0;
var referenceOffset = 0;
var referenceVisibles = 3;

// globals for lightbox
var lightboxIds = [];
var lightboxTop = 0;
var currentLightboxId = null;


$(document).ready(function(){
    
    // set toggle for date items
    $('div#content div.dates-list-item > a').click(function(event){
        event.preventDefault();
        $(this.parentNode).siblings().find('div.text').hide();
        $(this.parentNode).find('div.text').toggle();
        $(this.parentNode).siblings().find('a').removeClass('open');
        $(this).toggleClass('open');
    });
    
    
    // set link for content-2
    $('#content-2').click(function(){
        window.location.href = $('#content-2-link').attr('href');
    });
    
    
    // prepare head-gallery for fade
    $('#head-gallery a:first-child').removeClass('selected').show();
    
    
    // head-menu
    if ($('#head-menu.cycle').size() > 0) cycleHeadMenu(0,1);
    
    
    // page-footer references
    $('div.reference a').click(function(event){
        event.preventDefault();
        var lightboxId = $(this).attr('id').replace(/reference/,'lightbox');
        $('#overlay, #'+lightboxId).show();
        lightboxTop = $(window).scrollTop()+30;
        $('#'+lightboxId).css('top',lightboxTop);
        resizeOverlay();
        currentLightboxId = lightboxId;
    });
    
    initReference();
    
    $('#references-button-right').click(function(){
        if (referenceCount - referenceOffset <= referenceVisibles) return;
        var p = $('#references').position();
        $('#references').animate({left:'-=189'});
        referenceOffset++;
        setReferenceButtons();
    });
    
    $('#references-button-left').click(function(){
        if (referenceOffset == 0) return;
        var p = $('#references').position();
        $('#references').animate({left:'+=189'});
        referenceOffset--;
        setReferenceButtons();
    });
    
    
    // lightbox
    lightboxIds = $('.lightbox').map(function(idx,element){return $(element).attr('id')}).get();
    
    $('#overlay, .lightbox-head img, .lightbox-footer img, .lightbox-footer span.close').click(function(){
        $('#overlay, .lightbox').hide();
    });
    
    $('.lightbox .back').click(function(){
        var indexOfCurrentLightboxId = jQuery.inArray(currentLightboxId,lightboxIds);
        $('#'+currentLightboxId).hide();
        currentLightboxId = (indexOfCurrentLightboxId > 0) 
                            ? lightboxIds[indexOfCurrentLightboxId - 1] 
                            : lightboxIds[lightboxIds.length-1];
        $('#'+currentLightboxId).show().css('top',lightboxTop);
        resizeOverlay();
    });
    
    $('.lightbox .forward').click(function(){
        var indexOfCurrentLightboxId = jQuery.inArray(currentLightboxId,lightboxIds);
        $('#'+currentLightboxId).hide();
        currentLightboxId = (indexOfCurrentLightboxId == lightboxIds.length-1) 
                            ? lightboxIds[0]
                            : lightboxIds[indexOfCurrentLightboxId + 1];
        $('#'+currentLightboxId).show().css('top',lightboxTop);
        resizeOverlay();
    });
   
    /*
    // show only 3 elements of li.box
    $('#content ul.box li:nth-child(n+4)').hide();
    $('#content ul.box').each(function(){
        if ($(this).find('li').size() > 3){
            $(this).append('<li class="show-more"><a href"#">mehr&hellip;</a></li>');
        }
    });
    $('#content ul.box li.show-more a').click(function(event){
        event.preventDefault();
        $(this).parentsUntil("ul.box").hide().siblings().show();
    });
    */
    
    // listen for window resize
    $(window).resize(function(){
        resizeOverlay();
    });
});
