function initCarousel(category)
{
    var size=$(category).find('li').size();
    if(size < 4) {
        $(category).parents('.carouselBox').find('.arrow').hide();
    }
    if(size > 2) {
        $(category).jCarouselLite({
            btnNext: ".next",
            btnPrev: ".prev",
            visible: 3
        });
    }
    tb_init(category + ' a');
    initHover(category + ' a');
}
function initHover(element)
{
    $(element).bind("mouseenter",function(){
        var src = $(this).find('img').attr('src');
        $(this).find('img').attr('src', src.replace('front', 'frontProducts'));
    }).bind("mouseleave",function(){
        var src = $(this).find('img').attr('src');
        $(this).find('img').attr('src', src.replace('frontProducts', 'front'));
    });
}
$(document).ready(function(){    
    var ieHeight = $('.accessory').height();
    // Au load, cacher tous les menus qui ne sont pas selectionné
    $('.category').not('.selected').find('div.collection').hide();

    // Au click sur un h2, replier la sidebar
    $('h2 a').click(function(){
       $("#sidebar").animate({ height: 'hide', opacity: 'hide' }, 'slow');
       if(jQuery.browser.msie) $('.accessory').height(ieHeight);
    });

    /**********************************************************************************************************/
    /* Women                                                                                                  */
    /**********************************************************************************************************/

    // Au click sur le h2 de women, replier les autres h2    
    $('.women h2 a').click(function(event){
        event.preventDefault();
        $('a').removeClass('selected');
        $('.category').not('.women').find('div.collection').animate({ height: 'hide', opacity: 'hide' }, 'slow').find('div.carouselLoader').html('');
        $('.women div.collection').animate({ height: 'show', opacity: 'show' }, 'slow');
    });
    
    // Au click sur ul#womenNav, deployer le caroussel
    $('ul#womenNav a').click(function(event){
        event.preventDefault();
        $('a').removeClass('selected');
        if(jQuery.browser.msie) $('.accessory').height(290);
        $("#aCarousel").animate({ height: 'hide', opacity: 'hide' }, 'slow')
        $("#aCarousel").load(this.href, null, function(){
            $(this).animate({ height: 'show', opacity: 'show' }, 'slow', function(){
                  if(jQuery.browser.msie) $('.accessory').height(ieHeight);
            });
            initCarousel('.femmesCarousel');
        });
        $(this).addClass('selected');
    });

    // Au click sur li.femmesNav, deployer le caroussel fermer les autres
    $('li.femmesNav a').click(function(event){
        event.preventDefault();
        $('.women h2 a').click();
        $('ul#womenNav .'+$(this).attr('class')).click();
    });

    /**********************************************************************************************************/
    /* Men                                                                                                    */
    /**********************************************************************************************************/

    // Au click sur le h2 de men, replier la sidebar des autres h2    
    $('.men h2 a').click(function(event){
        event.preventDefault();
        $('a').removeClass('selected');
        $('.category').not('.men').find('div.collection').animate({ height: 'hide', opacity: 'hide' }, 'slow').find('div.carouselLoader').html('')
        $('.men div.collection').animate({ height: 'show', opacity: 'show' }, 'slow');
    });

    // Au click sur ul#menNav, deployer le caroussel   
    $('ul#menNav a').click(function(event){
        event.preventDefault();
        $('a').removeClass('selected');
        if(jQuery.browser.msie) $('.accessory').height(350);
        $("#bCarousel").animate({ height: 'hide', opacity: 'hide' }, 'slow');
        $("#bCarousel").load(this.href, null, function(){
            $(this).animate({ height: 'show', opacity: 'show' }, 'slow', function(){
                  if(jQuery.browser.msie) $('.accessory').height(ieHeight);
            });
            initCarousel('.hommesCarousel');
        });
        $(this).addClass('selected');
    });

    // Au click sur li.hommesNav, deployer le caroussel fermer les autres
    $('li.hommesNav a').click(function(event){
        event.preventDefault();
        $('.men h2 a').click();
        $('ul#menNav .'+$(this).attr('class')).click();
    });    
    
    /**********************************************************************************************************/
    /* Maroquinerie                                                                                           */
    /**********************************************************************************************************/

    // Au click sur le h2 de maroquinerie, replier la sidebar des autres h2    
    $('.maroquinerie h2 a').click(function(event){
        event.preventDefault();
        $('.category').not('.maroquinerie').find('div.collection').animate({ height: 'hide', opacity: 'hide' }, 'slow').find('div.carouselLoader').html('')
        $('.maroquinerie div.collection').show();
        if(jQuery.browser.msie) $('.accessory').height(350);
        $("#cCarousel").animate({ height: 'hide', opacity: 'hide' }, 'slow');
        $("#cCarousel").load(this.href, null, function(){
            $(this).animate({ height: 'show', opacity: 'show' }, 'slow', function(){
                  if(jQuery.browser.msie) $('.accessory').height(ieHeight);
            });
            initCarousel('.maroquinerieCarousel');
        });
    });

    // Au click sur li.maroquinerieNav, deployer le caroussel fermer les autres
    $('li.maroquinerieNav a').click(function(event){
        event.preventDefault();
        $('.maroquinerie h2 a').click();
    });
    
    /**********************************************************************************************************/
    /* Accessoires                                                                                            */
    /**********************************************************************************************************/

    // Au click sur le h2 d'accessoires, replier la sidebar des autres h2    
    $('.accessory h2 a').click(function(event){
        event.preventDefault();
        $('.category').not('.accessory').find('div.collection').animate({ height: 'hide', opacity: 'hide' }, 'slow').find('div.carouselLoader').html('')
        $('.accessory div.collection').show();
        if(jQuery.browser.msie) $('.accessory').height(450);
        $("#dCarousel").animate({ height: 'hide', opacity: 'hide' }, 'slow');
        $("#dCarousel").load(this.href, null, function(){
            $(this).animate({ height: 'show', opacity: 'show' }, 'slow');
            initCarousel('.accessoiresCarousel');
        });
    });

    // Au click sur li.maroquinerieNav, deployer le caroussel fermer les autres
    $('li.accessoiresNav a').click(function(event){
        event.preventDefault();
        $('.accessory h2 a').click();
    });
    
    // gestion du focus pour newsletter
    $("#bocage_extras_mail_newsletter_mail").focus(function(){
	    $(this).attr('value','');
	})
});