var slideshow = (function(){
    var ss = {},
        activeContent = $('#slideshow-content>li.active'),
        nextContent,
        activeNav = $('#slideshow-nav>li.active'),
        nextNav,
        activeColor = 5,
        nextColor,
        colors = ['#00890a', '#00d7f6', '#f7009d', '#f50000', '#2e00f3', '#faa500'],
        height = 250;
    
    /*
    Go to next slide.
    @slideId  Take the href of the anchor of the slide or keyword "prev" or "next"
    */
    function slide(slideId) {
        if (slideId && slideId !== "prev" && slideId !== "next") {
            nextContent = $("#slideshow-content a[name="+slideId+"]").next();
            nextNav = $("#slideshow-nav a[href=#"+slideId+"]").parent();
            if (nextContent.length === 0) return;
        }
        else if(slideId === "prev") {
            nextContent = activeContent.prevAll('li').eq(0).length ? activeContent.prevAll('li').eq(0) : $('#slideshow-content>li:last');
            nextNav = activeNav.prev('li').length ? activeNav.prev('li') : $('#slideshow-nav>li:last');
        }
        else {
            nextContent = activeContent.nextAll('li:first').eq(0).length ? activeContent.nextAll('li:first').eq(0) : $('#slideshow-content>li:first');
            nextNav = activeNav.next('li').length ? activeNav.next('li') : $('#slideshow-nav>li:first');
        }
        
        // Change the background color
        nextColor = Math.floor(Math.random()*colors.length);
        while( nextColor === activeColor  ) {
            nextColor = Math.floor(Math.random()*colors.length);
        }
        $('body').css('background-color', colors[nextColor]);
        activeColor = nextColor;
        
        
        nextContent.show().css('left', '700px');
        activeContent.animate(
            { left: '-700px' }, 'fast', function () {
                activeContent.css('left', '0').removeClass('active');
            });
        nextContent.animate(
            { left: "0px" }, 'fast', function () {
                nextContent.addClass('active');
            });
        
        /*
        Fading
        activeContent.fadeOut('slow', function() {
            activeContent.removeClass('active');
            nextContent.fadeIn('slow').addClass('active');
        });
        */
        
        activeNav.removeClass('active');
        nextNav.addClass('active');
        
        activeContent = nextContent;
        activeNav = nextNav;
    }
    
    ss.init = function() {
        // Edit the css
        $('#slideshow-content>li').css({
            'position': 'absolute', 
            'top': '0',
            'left': '0'
        });
        $('#slideshow-content>li:not(.active)').hide();
        
        // Ajust the height of the window to match the content
        $('#slideshow-content li').each(function() {
            if( $(this).height() > height ) height = $(this).height();
        });
        $('#slideshow-content').height(height);
        
        
        var slider = setInterval(slide, 3000);
        
        // Active menu style
        $('<span></span>').appendTo('.main-menu li.current-menu-item a');
        
        // Add controls next and prev
        $('<a href="#" class="prev">Précédent</a><a href="#" class="next">Suivant</a>').appendTo('#slideshow');
        $('#slideshow .prev').bind('click', function() { slide('prev'); clearInterval(slider); slider = setInterval(slide, 5000); return false; });
        $('#slideshow .next').bind('click', function() { slide('next'); clearInterval(slider); slider = setInterval(slide, 5000); return false; });
        
        // Add links to bullets (overwrite the anchor navigation)
        
        $('#slideshow-nav li a').each(function() {
           $(this).bind('click', function() {
               slide( $(this).attr("href").slice(1) );
               clearInterval(slider);
               slider = setInterval(slide, 5000);
               return false;
           });
        });
    };
    
    return ss;
}());

$('#wrapper').removeClass('nojs').addClass('js');

// open external links in a new windows
$('a[rel="external"]').click(function(){
    $(this).attr('target','_blank');
  });

// Fix a bug in the map bubble
$('#map_1').css('color', 'black');

if( $('#slideshow').length ) slideshow.init();
