/**
 *
 *
 * @author Fabrice Bonny
 */
$(document).ready(function(){
    //
    $('.insidemenu a').click(function(){
        $($(this).attr('href')).click();
    });
    
    //
    count = $('#onglets .onglet').size();
    i = 1;
    
    // Positionnement global
    $('#onglets .onglet').addClass('onglet_inactif');
    $('#onglets .onglet:last').removeClass('onglet_inactif');
    $('#onglets .onglet:last').addClass('onglet_actif');
    $('#onglets .onglet').css('margin-bottom', (count * 1.8 + 1.8) + 'em');
    
    // Mémorisations
    var oldLeft;
    var oldTop;
    var oldIndex;
    var oldCible;
    var firstLeft;
    var firstTop;
    var firstIndex;
    
    // Positionnement individuel
    $('#onglets .onglet').each(function(){
        $(this).css('left', ((count - i) * 20) + 'px');
        $(this).css('top', (i * 1.8) + 'em');
        $(this).css('z-index', String(10 - count + i));
        $(this).css('height', '150px');
        $(this).css('min-height', '150px');
        
        firstLeft = ((count - i) * 20) + 'px';
        firstTop = (i * 1.8) + 'em';
        
        oldCible = $(this);
        oldIndex = $(this).css('z-index');
        
        i++;
    });
    
    // Action sur le clic
    $('#onglets .onglet').bind('click', function(){
        // Position actuelle
        oldLeft = $(this).css('left');
        oldTop = $(this).css('top');
        oldIndex = $(this).css('z-index');
        
        // Élément à permuter
        oldCible.animate({
            left: oldLeft,
            top: oldTop
        }, 'slow', null, function(){
            $(this).css('z-index', String(oldIndex));
        });
        
        // Déplacement
        $(this).animate({
            left: firstLeft,
            top: firstTop
        }, 'slow', null, function(){
            $(this).css('z-index', '10');
        });
        
        //
        oldCible.removeClass('onglet_actif');
        oldCible.addClass('onglet_inactif');
        
        // Mémorisation
        oldCible = $(this);
        
        //
        $(this).removeClass('onglet_inactif');
        $(this).addClass('onglet_actif');
    });
    
    // Accessibility link
    if ($('#onglets')) {
        var aLink = document.createElement('a');
        $(aLink).text('Voir tous les onglets');
        $(aLink).attr('href', '#');
        $(aLink).addClass('hidden');
        
        $(aLink).focus(function(){
            $(this).removeClass('hidden');
            $(this).addClass('all');
        });
        
        $(aLink).click(function(){
            $('#onglets div').each(function(){
                $(this).css('position', 'static');
                $(this).css('margin-bottom', '15px');
            });
            
            $(this).css('display', 'none');
            
            return false;
        });
        
        $('#onglets').prepend(aLink);
    }
});
