var currentIndex = 1;

function showNews(index, classToCheck){
    currentIndex = index;
    newMargin = -(index-1)*800;
    
    // IE ne comprend pas les transitions CSS, il faut le simuler avec jquery
    if ( $.browser.msie ) {
        $('.innerslider').animate({
        marginLeft: newMargin
        }, 800, function() {});
    }
    else {
        $(".innerslider").css( "marginLeft", newMargin + "px" );
    }
    
    checkNavigation(classToCheck);
    refreshNavButtons();
}

function showNextNews(classToCheck) {
    
    var nbNews = $(classToCheck).length;
    
    if( currentIndex < nbNews ){
        currentIndex++;
        showNews(currentIndex, classToCheck);
    }
    
}

function showPreviousNews(classToCheck) {

    if(currentIndex > 1){
        currentIndex--;
        showNews(currentIndex, classToCheck);
    }
    
}

function initNavigation(classToCheck) {
    var nbNews = $(classToCheck).length;
    currentIndex = 1;
    var margin = 0;
    var width = (nbNews) * 800; 
    $(".innerslider").css( "marginLeft", margin + "px" );
    $(".innerslider").css( "width", width + "px" );
    
}

function checkNavigation(classToCheck) {
    var nbNews = $(classToCheck).length;
    
    if( nbNews < 2){
        $("#navPrevious").css( "visibility", "hidden" );
        $("#navNext").css( "visibility", "hidden" );
    }
    else {
        if( currentIndex < nbNews ){
            $("#navNext").css( "visibility", "visible" );
        }else {
            $("#navNext").css( "visibility", "hidden" );
        }
        
        if( currentIndex > 1 ){
            $("#navPrevious").css( "visibility", "visible" );
        }
        else {
            $("#navPrevious").css( "visibility", "hidden" );
        }
    }
    
}

function refreshNavButtons(){
    $('.current').removeClass('current');
    $('.navButtons a').each(function(index) {
        if(index+1 == currentIndex){
            $(this).addClass('current');
        }
    });
}

