/**
 * Pour compatibilité JQuery 1.4 et version antérieure
 */
function getTrueSize(container, target) {
    container.css({ position: 'absolute', visibility: 'hidden', display: 'block' });
    var trueWidth = parseInt($(container.children(target)).outerWidth(true));
    container.css({ position: '', visibility: '', display: '' });
    
    return trueWidth;
}


/**
 * Méthodes de gestion des pubs du menu
 */
function showHidePubs(id, box, width) {
    
    var pub         = $(id.replace('Menu_', '#Pub_For_'));
    
    $('#menu .pub').hide();
    $('#menu .pub > a').hide();
    $('#menu .pub > .pub-menu').hide();
    
    if (pub.attr('id')) {
        width +=  parseInt(pub.parent().outerWidth(true));
        box.css('width', width);
        pub.parent().show();
        pub.show();
    }
    
    return width;
}

/**
 * Méthodes de gestion des sous-menu
 */
function showHideSubs(id, box, width) {
    
    var sub = $(id.replace('Menu_', '#Sub_Menu_For_'));
    
    $('.menu_level_2').hide();
    
    if (sub.attr('id')) {
        width +=  parseInt(sub.outerWidth(true));
        box.css('width', width);
        sub.show();
    }
    
    return width;
}

$(document).ready(function() {
    
    // Gestion du menu
    $('.menu_level_0 > li > a').mouseenter(function(event){
        
        var id              = $(this).attr('id');
        var box             = $(id.replace(/([^_]+_[^_]+).*/, '#Sub_$1'));
        var expectedWidth   = getTrueSize(box, '.menu_level_1:first');
        var newWidth        = expectedWidth;
        
        $('.sous-menu').hide();
        
        newWidth = showHidePubs(id, box, expectedWidth);
        newWidth = showHideSubs(id, box, newWidth);
        
        if (newWidth == expectedWidth) {
            box.css('width', expectedWidth);
        }
        
        box.show();
    });
    
    $('#header').mouseleave(function(event){
        $('.sous-menu').hide();
        
        showHidePubs(null, null, null);
        showHideSubs(null, null, null);
    });
    
    // Gestion du sous-menu
    $('.menu_level_1 li a').mouseenter(function(event){
        
        var id              = $(this).attr('id');
        var box             = $(id.replace(/([^_]+_[^_]+).*/, '#Sub_$1'));
        var expectedWidth   = parseInt($(this).parent().outerWidth(true));
        var newWidth        = 0;
        
        newWidth = showHidePubs(id, box, expectedWidth);
        newWidth = showHideSubs(id, box, newWidth);
        
        if (newWidth == expectedWidth) {
            box.css('width', expectedWidth);
        }
    });
    
    $('.menu_level_2 li a').mouseenter(function(event){
        
        var id              = $(this).attr('id');
        var box             = $(id.replace(/([^_]+_[^_]+).*/, '#Sub_$1'));
        var expectedWidth   = parseInt($(box.children('.menu_level_1:first')).outerWidth(true))
                                + parseInt($(box.find('.menu_level_2:first')).outerWidth(true));
        var newWidth        = 0;
        
        newWidth = showHidePubs(id, box, expectedWidth);
        
        if (newWidth == expectedWidth) {
            box.css('width', expectedWidth);
        }
    });
    
    /**
     * Méthodes du header et de la navigation
     */
    
    // Gestion du login en overlay
    $("a#identifier, a#identifier_pro").click(function(e) {
        e.preventDefault();
    });
    
    $("a#identifier").fancybox({
        'href': $("a#identifier").attr('true-url'),
        'width' : 695,
        'height' : 423,
        'autoScale' : false,
        'padding' : 0,
        'margin' : 0,
        'transitionIn' : 'fade',
        'transitionOut' : 'fade',
        'type' : 'iframe',
        'scrolling' : 'no',
        'centerOnScroll' : true
    });
    
    $("a#identifier_pro").fancybox({
        'width' : 695,
        'height' : 268,
        'autoScale' : false,
        'padding' : 0,
        'margin' : 0,
        'transitionIn' : 'fade',
        'transitionOut' : 'fade',
        'type' : 'iframe',
        'scrolling' : 'no',
        'centerOnScroll' : true
    });
    
});
