var Loreal = Loreal || {};
$.extend(Loreal, {
    initMenu: function(newMenu) {
        // On met sur toutes les li du menu une classe temporaire qui enleve le display: none
        $(".navbar_item_off").addClass("navbar_item_wait").removeClass("navbar_item_off");

        var total_page = $($('div#global')[0]).innerWidth();

        if ($('li.list_item_level_1')[0]) {

            var taille_categorie = $($('li.list_item_level_1')[0]).innerWidth();

            $("ul.list_container_level_1").each(function(num) {

                var div_menu_container = $(this).parent();
                var li_container = div_menu_container.parent();
                var items_niv1 = $(this).children("li.list_item_level_1");

                // Pour contrer bug IE, on fixe les tailles
                if (div_menu_container.innerWidth() != total_page && newMenu) {
                    div_menu_container.css('width', Math.max(Math.min(items_niv1.length * taille_categorie, total_page - (total_page % taille_categorie)), li_container.innerWidth()) + 'px');
                }

                $(this).css('max-width', div_menu_container.innerWidth() + 'px');
                if (items_niv1.length > 0) {
                    var tampon_top = 0;
                    var nb_categories = 0;

                    // On met a la ligne les categories
                    while (items_niv1[nb_categories]) {
                        if (tampon_top != items_niv1[nb_categories].offsetTop) {
                            $(items_niv1[nb_categories]).before('<div class="clear_left_menu"></div>');
                            tampon_top = items_niv1[nb_categories].offsetTop;
                        }
                        nb_categories++;
                    }

                    var pos_gauche = li_container.position()['left'];
                    var taille_colonnes = taille_categorie * nb_categories;
                    var reste_droite = total_page - pos_gauche;

                    // On donne une taille aux bordures
                    $(this).prev().prev().css('height', div_menu_container.innerHeight() + 'px'); // Bordure gauche
                    $(this).prev().css('top', div_menu_container.innerHeight() + 'px'); // Coin gauche
                    $(this).next().css('height', div_menu_container.innerHeight() + 'px'); // Bordure droite
                    $(this).next().next().css('top', div_menu_container.innerHeight() + 'px'); // Coin droit
                    $(this).next().next().next().css('width', div_menu_container.innerWidth() + 'px'); // Bordure haut

                    // Gestion du decalage avec le cote gauche, uniquement en cas de menu a plusieurs lignes
                    if (newMenu) {

                        // Si on n'est pas en disposition "100%" on doit decaler le conteneur entier, sinon le contenu seulement
                        var element_modifie = (div_menu_container.innerWidth() == total_page) ? $(this) : div_menu_container;

                        // Si on peut placer toutes les categories sur la droite on le fait
                        if (taille_colonnes <= reste_droite) {
                            element_modifie.css('margin-left', pos_gauche + 'px');
                            // Sinon on les dispose par rapport a la droite
                        } else {
                            // var margin = pos_gauche - Math.abs(reste_droite - Math.min(taille_colonnes, total_page));
                            // En cas de depassement, on prend le rapport position/taille
                            var rapport_reste = Math.round((total_page % taille_categorie) * (pos_gauche / total_page));
                            var margin = Math.max(total_page - taille_colonnes, rapport_reste);
                            if (!isNaN(margin)) {
                                element_modifie.css('margin-left', margin + 'px');
                            }
                        }
                    }

                    // Replacement des bordures
                    if (div_menu_container.innerWidth() == total_page) {
                        $(this).prev().prev().css('left', 0); // Bordure gauche
                        $(this).prev().css('left', 0); // Coin gauche
                        $(this).next().css('right', 0); // Bordure droite
                        $(this).next().next().css('right', 0); // Coin droit
                        $(this).next().next().next().css('width', div_menu_container.innerWidth() - $(this).prev().innerWidth() - $(this).next().innerWidth() + 'px'); // Bordure haut
                        $(this).next().next().next().css('left', $(this).prev().innerWidth() + 'px'); // Bordure haut
                    } else if (taille_colonnes >= reste_droite) {
                        $(this).next().css('right', 0); // Bordure droite
                        $(this).next().next().css('right', 0); // Coin droit
                        $(this).next().next().next().css('width', div_menu_container.innerWidth() - $(this).prev().innerWidth() + 'px'); // Bordure haut
                    }

                } else {
                    // Cas pas de categorie : on cache
                    div_menu_container.css('width', 0);
                    div_menu_container.css('visibility', 'hidden');
                }
            });
        }

        // On enleve la classe temporaire
        $(".navbar_item_wait").addClass("navbar_item_off").removeClass("navbar_item_wait");
    }
});
