$(document).ready(function(){
    bannerNav();

    $('.reference').slice(1).hide();
    $('.reference').slice(1).hide();
    $('.reference').prepend('<img class="prevButton" src="../images/button-zurueck-on.png" /><img class="nextButton" src="../images/button-vor-on.png" />');
    $('.prevButton').first().attr('src', '../images/button-zurueck-off.png');
    $('.nextButton').last().attr('src', '../images/button-vor-off.png');

    $('.nextButton').slice(0, -1).click(function () { 
        $(this).parent().hide();
        $(this).parent().next().show();
    });

    $('.prevButton').slice(1).click(function () { 
        $(this).parent().hide();
        $(this).parent().prev().show();
    });

    if (typeof initFilter == 'function') initFilter(); // defined in _cfg/jobs_de.html

    if ($('.zeitleiste').length>0) {
        $('#content').append('<div id="zeitleisteBig"></div><div id="carousel_container" class="dreispaltig"></div><div style="clear: both;"><!-- --></div>');
        $('#carousel_container').append('<div id="leftscroll"><a href="javascript:slide(\'left\');"><img src="../images/button-zurueck-off.png" /></a></div>');
        $('#carousel_container').append('<div id="carousel_inner"><ul id="carousel_ul"></ul></div>');
        $('#carousel_container').append('<div id="rightscroll"><a href="javascript:slide(\'right\');"><img src="../images/button-vor-off.png" /></a></div>');

        $('.zeitleiste').toggle();
        $('.zeitleiste a').clone().appendTo('#carousel_ul');
        $('#carousel_ul').children().wrap('<li>');

        $('#carousel_ul').css('width', $('#carousel_ul li').outerWidth() * $('#carousel_ul li').length);

        $('#zeitleisteBig').empty();
        $('#zeitleisteBig').append($('.zeitleiste:first').clone());
    }
        /*
        $('#leftscroll img').css('border-color', 'red');
        $('#rightscroll img').css('border-color', 'green');
        
        */

        setFontSize('cookie');
});
  
function slide(where){
    var item_width = $('#carousel_ul li').outerWidth();  
    var carouselWidth = $('#carousel_ul').outerWidth(); //('width');

    var scrollByImages = ($('#carousel_inner').outerWidth()/item_width);
    
    if (where=='left' && parseInt($('#carousel_ul').css('left'))<0) {
        var left_indent = parseInt($('#carousel_ul').css('left')) + item_width * scrollByImages;
    }

    if (where=='right' && parseInt($('#carousel_ul').css('left')) > -(carouselWidth - ($('#carousel_inner').outerWidth()/item_width) * item_width)) { // Gesamtbreite - (Anzahl sichtbarer Bilder * Bildbreite)
        var left_indent = parseInt($('#carousel_ul').css('left')) - item_width * scrollByImages;

    }  

    if (typeof left_indent=='undefined') return;

    $('#carousel_ul:not(:animated)').animate({'left':left_indent}, 500, function(){  

        if (parseInt($('#carousel_ul').css('left'))>=0) $('#leftscroll img').css('border-color', 'red');
        else {$('#leftscroll img').css('border-color', 'green')}

        if (parseInt($('#carousel_ul').css('left')) > -carouselWidth+item_width) {
            $('#rightscroll img').css('border-color', 'green');
        } else {
            $('#rightscroll img').css('border-color', 'red');
        }

    });
}

function showItem(which)
{
    $('#zeitleisteBig').empty();
    $('#zeitleisteBig').append($('div.'+which).clone());
}

function setBanner() { $('#header').css('background-image', newBanner); }

timeoutId = null;
newBanner = '';
function bannerNav()
{
    $('#navigation li a').not($('#navigation li li a')).bind({
        //mouseover: function() {/*window.clearTimeout(timeoutId);*/ $('#header').css('background-image', 'url(../images/' + $(this).parent().attr('class') + '.jpg)')},
        mouseover:  function() {window.clearTimeout(timeoutId); newBanner = 'url(../images/' + $(this).parent().attr('class') + '.jpg)'; timeoutId = window.setTimeout(setBanner, 150)}
    });
    $('#navigation li li').bind({
        //mouseover: function() {/*window.clearTimeout(timeoutId);*/ $('#header').css('background-image', 'url(../images/' + $(this).attr('class') + '.jpg)')},
        mouseover:  function() {window.clearTimeout(timeoutId); newBanner = 'url(../images/' + $(this).attr('class') + '.jpg)'; timeoutId = window.setTimeout(setBanner, 150)}
    });
    $('#navigation').bind({
        //mouseout:  function() {$('#header').css('background-image', '')} 
        mouseout:  function() {window.clearTimeout(timeoutId); newBanner = ''; timeoutId = window.setTimeout(setBanner, 150)}
    });

    $('#navigation li').each(function(index, Element) {
            $img = '../images/' + $(this).attr('class') + '.jpg';
            $('#page').prepend('<img src="' + $img + '" style="display: none;" class="preload" />');;
    });
}

function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

function setFontSize(aFontSize) {
    document.getElementById('fontSizeSmaller').style.textDecoration = 'none';
    document.getElementById('fontSizeNormal').style.textDecoration  = 'none';
    document.getElementById('fontSizeLarger').style.textDecoration  = 'none';

    if (aFontSize=='cookie') {
        a = getCookie('fontSize');
        if (a) aFontSize = a;
        else aFontSize = '';
    }

    setCookie('fontSize', aFontSize, 7); 
    switch (aFontSize) {
    case 'smaller': document.getElementById('fontSizeSmaller').style.textDecoration = 'underline'; break;
    case '':        document.getElementById('fontSizeNormal').style.textDecoration  = 'underline'; break;
    case 'larger':  document.getElementById('fontSizeLarger').style.textDecoration  = 'underline'; break;
    }

    document.getElementById('content').style.fontSize = aFontSize;
    document.getElementById('sidebar').style.fontSize = aFontSize;
}

