
$(document).ready(function() {

    if ($.browser.msie) {
        if ($.browser.version == '6.0') {
            DD_belatedPNG.fix('img, .center, .pastille_confiance a, .pastille_confiance a:hover, #pub_geant, #menu_principal, #enseigne_list-left, #enseigne_list-right, ul, .title, .select, .cartouche, #logo_enseigne, .cartouche_chiffres, .nb');
        }
    }

    $('.btn_menu').each(function() {
        if ($(this).hasClass('selected')) {
            $(this).find('ul').show(0);
            $(this).find('#rootMenu').addClass('fleche');
            $(this).find('.link_menu').addClass('roll');

        }
    });

    $('.btn_menu').mouseenter(function() {
        $('.btn_menu').find('ul').hide(0);
        $('.btn_menu').find('a').removeClass('roll');
        $('.btn_menu').find('#rootMenu').removeClass('fleche');
        $(this).find('ul').show(0, function() {
            $(this).parent().find('.link_menu').addClass('roll');
            $(this).parent().find('#rootMenu').addClass('fleche');
        });

    });

    $('.btn_menu').mouseleave(function() {
        $(this).find('ul').hide(0);
        $(this).find('.link_menu').removeClass('roll');
        $(this).find('#rootMenu').removeClass('fleche');
        $('.btn_menu').each(function() {
            if ($(this).hasClass('selected')) {
                $(this).find('ul').show(0, function() {
                $(this).parent().find('#rootMenu').addClass('fleche');
                    $(this).parent().find('.link_menu').addClass('roll');
                });
            }
        });
    });

    $('.block_vignette .right_content img:first').show(0);
    $('.block_vignette .left_content img')
		.show(0)
		.mouseenter(function() {
		    var $right = $(this).parent().parent().find('.right_content');
		    var Rid = $right.find('img:first').attr('id').split('_');
		    var Lid = $(this).attr('id').split('_');
		    $right.find('img').stop().hide(0).css({ opacity: 1 });
		    $right.find('#' + Rid[0] + '_' + Lid[1]).fadeIn(500);
		});


    var showToolTips = function() {
        var $parent = $(this).parent();
        var calculated = $parent.data('calculated');
        var $content = $parent.find('.content');
        if (!calculated) {
            $content.css({ display: 'block', visibility: 'hidden' });
            var height = $content.find('.center .middle').height();
            var width = $content.find('.center .middle').width();
            $content.find('.top').width(width + 24)
            $content.find('.top .middle').width(width);
            $content.find('.bottom').width(width + 24)
            $content.find('.bottom .middle').width(width);
            $content.find('.center').width(width + 24)
            $content.find('.center .left').height(height);
            $content.find('.center .right').height(height);
            $parent.data('calculated', true);
        }
        $content.css({ display: 'none', visibility: 'visible' }).show(50);
    };

    $('.tooltips .title').mouseenter(showToolTips).mouseleave(function() {
        var $parent = $(this).parent();
        var $content = $parent.find('.content').hide(0);
    });

    //Carrousel

    var numCompany = $("#enseigne_listing img").length;
    var padding = 22;
    var currentCompany = 0;
    var widthCarrousel = padding * 4;

    $("#enseigne_list-left").hide();

    $('#enseigne_listing img').each(function() {
        var newWidth = 0;
        newWidth = $(this).width() + padding * 2;
        if (newWidth <= (padding * 2)) newWidth = 100 + padding * 2;
        widthCarrousel += newWidth;
    });

    $("#enseigne_listing").width(widthCarrousel);

    if (widthCarrousel <= $("#enseigne_container").width() + 4 * padding) $("#enseigne_list-right").hide();



    $("#enseigne_list-right").click(function() {
        $("#enseigne_list-left").show();
        if (currentCompany < (numCompany - 1)) {
            if ($("#enseigne_list-right").hide()) $("#enseigne_list-right").show();
            currentCompany++;
            //alert('widthCarrousel : ' + widthCarrousel + "//" + (widthCarrousel - $("#enseigne_container").width() + 4 * padding) + "//" + checkNewPositionX(currentCompany + 1) + "//" + currentCompany)
            if ((widthCarrousel - $("#enseigne_container").width() + 4 * padding) < checkNewPositionX(currentCompany + 1)) $("#enseigne_list-right").hide();
            $("#enseigne_listing").animate({
                marginLeft: -checkNewPositionX(currentCompany)
            });
        }
    });

    $("#enseigne_list-left").click(function() {
        $("#enseigne_list-right").show();
        if (currentCompany > 0) {
            if ($("#enseigne_list-left").hide()) $("#enseigne_list-left").show();
            currentCompany--;
            if (currentCompany < 1) $("#enseigne_list-left").hide();
            $("#enseigne_listing").animate({
                marginLeft: -checkNewPositionX(currentCompany)
            });
        }
    });

    function checkNewPositionX(id) {
        var padding = 22;
        var x = 0;
        var i = 0;

        $('#enseigne_listing img').each(function() {
            if (i < id) x += $(this).outerWidth() + padding * 2;
            i++;
        });

        return x;
    }
});
