
// variables globales du systeme de categories 
var niveau_max = 3; 
var baseurl = 'http://www.jeveuxvoir.com'; 
var lang = 'en';
var loading_msg = "chargement..."; 
var basedstdiv = "categories-level-"; 
var base_fab = "fabricant_"; 
var base_cat = "categories_"; 
var base_prod = "produits_"; 
var click_en_cours = 0; 
var couleur = "#DDD"; 
var couleur_forte = "#AEF"; 
var couleurs         = ['', '#0099CD', '#00CC33', '#FF6600', '#FF3366']; 
var couleurs_sombres = ['', '#005DB5', '#0B7822', '#D74806', '#C03366']; 
var loading_img = '<div style="margin: 0 auto; text-align: center; width: 100%; -moz-opacity: 0.5; -khtml-opacity: 0.5; opacity: 0.5; filter: Alpha(opacity=50); "><img src="' + baseurl + '/img/loading.gif" alt="" /></div>'; 

// utilisees pour les hacks css ie6 de thickbox 
function fixHeight(scrollHeight, offsetHeight) {
    return (scrollHeight > offsetHeight ? scrollHeight : offsetHeight) + 'px';
}

// utilisees pour les hacks css ie6 de thickbox 
function fixMarginTop(offsetHeight,documentElement,scrollTop) {
    return (0 - parseInt(offsetHeight / 2) + (TBWindowMargin = documentElement && documentElement.scrollTop || scrollTop) + 'px');	
}

function startclicking (id, delay) {
    cmd = "$('#" + id + "').click()"; 
    if (!click_en_cours) {
        // eval(cmd); // plus de reactivite mais probleme d'ergonomie : on ne veut pas que ca scrolle quand on ne fait que passer au dessus avec la souris 
        click_en_cours = setInterval(cmd, delay); 
    }
}

function stopclicking (id, delay) {
    clearInterval(click_en_cours); 
    click_en_cours = 0; 
}

function getmenu (id) {
    delai = 400; 
    menu_selectionne = id; 
    $('#recherche_dst').val(""); 
    $('#menu_cparti').css("opacity", "0"); 
    $('#menu_cparti a').hover(function() {
        $(this).css('cursor', 'default');
    }, function() {
        $(this).css('cursor', 'auto');
    }); 
    url = baseurl + '/' + lang + '/index/getmenus/';
    $("#menu_gauche_menu").html(loading_img); 
    // $("#menu_gauche_menu").css("opacity", ".3"); 
    $("#menu_gauche_menu").fadeOut(delai); 
    $.ajax({
        async: 0, 
        type: "get", url: url, data: "id=" +id, 
        success: function(html) {
            // $("#menu_gauche_menu").css("opacity", "1"); 
            // // $("#menu_gauche_menu").html(html); 
            $("#menu_buffer").html(html); 
            setTimeout("$('#menu_gauche_menu').html($(\"#menu_buffer\").html());", delai);
            $("#menu_gauche_menu").fadeIn(delai); 
            /*
            $("#menu a").css("background", "transparent"); 
            // $("#menu a").css("color", "inherit"); 
            $("#menu a").each(function() {
                $(this).css("color", $(this).parent().css("color")); 
            }); 
            */

            // selectionne l'element clique
            $('#menu li').removeClass('selected'); 
            $('#menu li').removeClass('deselected'); 
            $('#menu li').each(function () {
                if ($(this).attr('id') == 'menu_li_' + id) {
                    $(this).addClass('selected'); 
                } else {
                    $(this).removeClass('selected'); 
                }
            }); 

            /*
            $('#menu a').hover(function() {
                $(this).css("background", "transparent url(" + baseurl + "/img/menu_hover.png) no-repeat scroll 1px 2px"); 
                $(this).css("color", "#FFF");
            }, function() {
                $(this).css("background", "transparent");
                // $(this).css("color", "inherit");
                $(this).css("color", $(this).parent().css("color")); 
            }); 
            $("#menu_li_" + id + " a").css("background", "transparent url(" + baseurl + "/img/menu_hover.png) no-repeat scroll 1px 2px"); 
            $("#menu_li_" + id + " a").css("color", "#FFF"); 
            $("#menu_li_" + id + " a").hover(function() {
                $("#menu a").each(function() {
                    $(this).css("background", "transparent");
                    $(this).css("color", $(this).parent().css("color")); 
                }); 
                $(this).css("background", "transparent url(" + baseurl + "/img/menu_hover.png) no-repeat scroll 1px 2px"); 
                $(this).css("color", "#FFF");
            }, function() {
                $(this).css("background", "transparent url(" + baseurl + "/img/menu_hover.png) no-repeat scroll 1px 2px"); 
                $(this).css("color", "#FFF");
            }); 
            */
            $("#sousmenu_gauche_menu").hide(); 
        }
    }); 
}

function getsousmenu (id, couleur, sousmenu) {
    $('#recherche_dst').val(""); 
    $('#menu_cparti').css("opacity", "0"); 
    $('#menu_cparti a').hover(function() {
        $(this).css('cursor', 'default');
    }, function() {
        $(this).css('cursor', 'auto');
    }); 
    url = baseurl + '/' + lang + '/index/getsousmenus/';
    // $("#sousmenu_gauche_menu").html(loading_img); 
    $("#sousmenu_gauche_menu").css("opacity", "0.3"); 
    // $("#sousmenu_gauche_menu").fadeOut("fast"); 
    // $("#menu_menu_div_" + id).fadeOut("fast"); 
    $.ajax({
        async: 0, 
        type: "get", url: url, data: "id=" + id + "&rang=" + couleur, 
        success: function(html) {
            $("#sousmenu_gauche_menu").css("opacity", "1"); 
            // $("#sousmenu_gauche_menu").fadeIn("fast"); 
            // $("#menu_menu_div_" + id).fadeIn("fast"); 
            $("#sousmenu_gauche_menu").html(html); // show the html inside .content div 
            $("#sousmenu_gauche_menu").show(); // show the html inside .content div 
            if (couleur.length) {
                $(".menu_menu_1").css("background-color", "#000"); 
                $(".menu_menu_1").css("background-image", "url(" + baseurl + "/img/menu_1.png)"); 
                $(".menu_menu_2").css("background-color", "#000"); 
                $(".menu_menu_2").css("background-image", "url(" + baseurl + "/img/menu_2.png)"); 
                $(".menu_menu_3").css("background-color", "#000"); 
                $(".menu_menu_3").css("background-image", "url(" + baseurl + "/img/menu_3.png)"); 
                $(".menu_menu_4").css("background-color", "#000"); 
                $(".menu_menu_4").css("background-image", "url(" + baseurl + "/img/menu_4.png)"); 
                $("#menu_menu_div_" + id).css("background-image", "url(" + baseurl + "/img/menu_" + couleur + "_active.png)"); 
                $("#menu_menu_div_" + id).css("background-color", couleurs[couleur]); 
                $("#sousmenu_gauche_menu").css("background-color", couleurs[couleur]); 
                $('#sousmenu_menu_ul a').hover(function() {
                    $(this).css('background', couleurs_sombres[couleur]);
                }, function() {
                    $(this).css('background', 'transparent');
                });
                if (sousmenu) {
                    lancernavigation(sousmenu, couleur); 
                }
            }
        }
    }); 
}

function lancernavigation (id, couleur) {
    url = baseurl + '/' + lang + '/produits/recherche?id=' + id; 
    $('#recherche_dst').val(url); 
    $('#sousmenu_menu_ul li').css('background', 'transparent'); 
    $('#sousmenu_menu_li_' + id).css('background', couleurs_sombres[couleur]); 
    $('#menu_cparti').css("opacity", "1"); 
    $('#menu_cparti a').hover(function() {
        $(this).css('cursor', 'pointer');
    }, function() {
        $(this).css('cursor', 'auto');
    }); 
}

function lancerrecherche (id) {
    url =  $('#recherche_dst').val(); 
    if (url.length) {
        setTimeout("window.location.href = '" + url + "'", 100);
    }
}

function dohover (id, prefixe) {
    // remove class selected_force a tous
    $('a.mini').removeClass('selected_force'); 
    // active class selected_force a l'element choisi
    $('#' + prefixe + id).addClass('selected_force'); 
}

function getzoom (id, nb) {
    $('#close_video').hide(); 
    url = baseurl + '/' + lang + '/produits/zoom/';
    $("#zoom").css("visibility", "hidden"); 
    $("#zoom").css("display", "none"); 
    $('#zoomspacer').css('display', 'block'); 
    $.ajax({
        async: 0, 
        type: "get", url: url, data: "id=" +id + "&nb=" + nb, 
        success: function(html) {
            $("#zoom").html(html); // show the html inside .content div 
            $(document).ready(function () { 
                $('#TB_window').remove();
                setTimeout('MagicMagnify_findMagnifiers()', 200); 
                setTimeout('$("#zoom").css("visibility", "visible")', 300); 
                setTimeout('$("#zoom").css("display", "block")', 400); 
                setTimeout('$("#zoomspacer").css("display", "none")', 400); 
            }); 
        }
    }); 
    $('.mini').removeClass('selected_force'); 
    $('#miniature_' + nb).toggleClass('selected_force'); 
}

function getimage (id) {
    dohover(id, 'mini_'); 

    url = baseurl + '/' + lang + '/produits/image/';
    $("#image_medium").css("opacity", "0.3"); 
    $.ajax({
        async: 0, 
        type: "get", url: url, data: "id=" + id, 
        success: function(html) {
            $("#image_medium").html(html); // show the html inside .content div 
            $("#image_medium").css("opacity", "1"); 
            $("#image_medium img").css("visibility", "visible"); 
        }
    }); 
}

function getvideo (id, urlvideo) {
    url = baseurl + '/' + lang + '/produits/video/';
    $("#zoom").css("visibility", "visible"); 
    $("#zoom").css("display", "block"); 
    $('#zoomspacer').css('display', 'none'); 
    $.ajax({
        async: 0, 
        type: "get", url: url, data: "id=" +id, 
        success: function(html) {
            $("#close_video").show()
            $("#zoom").html(html); // show the html inside .content div 
            $(document).ready(function () { 
                // $('#TB_window').remove();
                // setTimeout('swfobject.registerObject("myId", "9.0.0", "expressInstall.swf")', 200); 
                                
                var params = {
                  bgcolor: "000000"
                };

                var attributes = {
                };

                var flashvars = {
                  flv: urlvideo, 
                  width: "600",
                  height: "400",
                  autoplay: "1",
                  showstop: "1",
                  showtime: "1",
                  bgcolor1: "000000",
                  bgcolor2: "000000", 
                  playercolor: "444444"
                };

                swfobject.embedSWF(baseurl + "/js/player_flv/player_flv_maxi.swf", "myFlash", "600", "400", "9.0.0","expressInstall.swf", flashvars, params, attributes);

                setTimeout('$("#zoom").css("visibility", "visible")', 300); 
                setTimeout('$("#zoom").css("display", "block")', 400); 
                setTimeout('$("#zoomspacer").css("display", "none")', 400); 
            });
        }
    }); 
}

function close_video (id) {
    getzoom(id, '1'); 
    $('#close_video').hide(); 
}

function format(el_id, format) {
    el = $("#" + el_id); 
    switch (format) {
        case 'prix': 
        default:
            val = el.val(); 
            val = val.replace(/,/g, '.'); 
            val = val.replace(/[^0-9,\.]*/g, ''); 
            val = val.replace(/\.(.*)\./g, '.$1'); 
            el.val(val); 
            break; 
    }
}

function maj_thickbox() {
    $(".thickbox").unbind("click");
    tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
}

function getproduits (id_parent, fermer, divdestination) {
    if (!divdestination) {
        // produits classes
        dstdiv = 'produits_categories'; 
    } else if (divdestination == '1') {
        // produits non classes
        dstdiv = 'produits_sanscategories'; 
    } else {
        // produits non publies
        dstdiv = 'produits_nonpublies'; 
    }

    dstdiv = 'gestion-produits'; 

    url = baseurl + '/' + lang + '/produits/getlibo';
    if (id_parent == 'precedent') {
        id_parent = $('#categorie-a-ouvrir').val(); 
    }
    $('#categorie-ouverte').val(id_parent); 
    if (fermer) {
        // alert("raz categorie"); 
        $('#categorie-a-ouvrir').val(''); 
    }
    $.ajax({
        type: "get", url: url, data: "id_parent=" + id_parent, 
        async: 0, 
        beforeSend: function(){$("#" + dstdiv).hide("fast");}, //show loading just when link is clicked 
        complete: function(){ $("#" + dstdiv).show("fast");}, //stop showing loading when the process is complete 
        success: function(html) {
            $("#" + dstdiv).html(html); // show the html inside .content div 
            /* desactive pour utiliser sortable a la place 
            $("#" + dstdiv + " .draggable-product").draggable({helper: 'clone', opacity: '0.7'});
            */
            maj_thickbox(); 
            if ($('#categorie-a-ouvrir').val() > 0 && $('#categorie-a-ouvrir').val() != $('#categorie-ouverte').val()) {
                $('#afficher_produits_non_publies').hide(''); 
                $('#afficher_produits_categorie_selectionnee').show(''); 
            } else {
                $('#afficher_produits_non_publies').show(''); 
                $('#afficher_produits_categorie_selectionnee').hide(''); 
            }
            // fix pour FF2
            $('#footer').animate( { opacity: 0.1 }, 200 ).animate( { opacity: 1 }, 200 ); 
        }
    }); 
    
    $("#produits_categorie").sortable({
        // connectWith: connexions_array, // desactive
        delay: 100, 
        forcePlaceholderSize: true, 
        placeholder: "placeholder",
        update: function(evt, ui) {
            rangerproduits(id_parent, evt, ui); 
            // $("#" + dstdiv).html("chargement..."); 
        }
    });

    // Inutile desormais
    // majnbproduits(); 
}

function getfabricants () {
    url = baseurl + '/' + lang + '/fabricants/getlibo';
    $.ajax({
        type: "get", url: url, 
        beforeSend: function(){$("#fabricants").hide("fast");}, //show loading just when link is clicked 
        complete: function(){ $("#fabricants").show("fast");}, //stop showing loading when the process is complete 
        success: function(html) {
            $("#fabricants").html(html); // show the html inside .content div 
            maj_thickbox(); 
            // Inutile desormais
            // majnbproduitsfab(); 
        }
    }); 
}

function getusers () {
    url = baseurl + '/' + lang + '/users/getlibo';
    $.ajax({
        type: "get", url: url, 
        beforeSend: function(){$("#users").hide("fast");}, //show loading just when link is clicked 
        complete: function(){ $("#users").show("fast");}, //stop showing loading when the process is complete 
        success: function(html) {
            $("#users").html(html); // show the html inside .content div 
            maj_thickbox(); 
        }
    }); 
}

function getcontenus () {
    url = baseurl + '/' + lang + '/contenus/getlibo';
    $.ajax({
        type: "get", url: url, 
        beforeSend: function(){$("#contenus").hide("fast");}, //show loading just when link is clicked 
        complete: function(){ $("#contenus").show("fast");}, //stop showing loading when the process is complete 
        success: function(html) {
            $("#contenus").html(html); // show the html inside .content div 
            maj_thickbox(); 
        }
    }); 
}

function setcontenu () {
    url = baseurl + '/' + lang + '/contenus/modcontenu';
    // recuperation des donnees postees
    tinyMCE.triggerSave(); // pour que les donnees de tinyMCE soient prises en compte aussi 
    donnees = $("#form_contenu").serialize(); 
    $.ajax({
        type: "post", url: url, data: donnees, 
        success: function(html) {
            if (html.length && html.substr(0,6) != 'nok : ') {
                // fermer Thickbox
                top.tb_remove();
                // relancer getcontenus 
                top.getcontenus(); 
            } else {
                alert('Erreur(s) : \n' + html.substr(6)); 
            }
        }
    }); 
}

function delcontenu (id) {
    url = baseurl + '/' + lang + '/contenus/delcontenu';
    if (confirm('Voulez-vous vraiment supprimer ce contenu ?')) {
        $.ajax({
            type: "get", url: url, data: "id=" + id, 
            success: function(html){ 
                getcontenus(); 
            }
        }); 
    }
}

function getnewsletters () {
    url = baseurl + '/' + lang + '/newsletters/getlibo';
    $.ajax({
        type: "get", url: url, 
        beforeSend: function(){$("#newsletters").hide("fast");}, //show loading just when link is clicked 
        complete: function(){ $("#newsletters").show("fast");}, //stop showing loading when the process is complete 
        success: function(html) {
            $("#newsletters").html(html); // show the html inside .content div 
            maj_thickbox(); 
        }
    }); 
}

function setnewsletter () {
    url = baseurl + '/' + lang + '/newsletters/modnewsletter';
    // recuperation des donnees postees
    tinyMCE.triggerSave(); // pour que les donnees de tinyMCE soient prises en compte aussi 
    donnees = $("#form_newsletter").serialize(); 
    $.ajax({
        type: "post", url: url, data: donnees, 
        success: function(html) {
            if (html.length && html.substr(0,6) != 'nok : ') {
                // fermer Thickbox
                top.tb_remove();
                // relancer getnewsletters 
                top.getnewsletters(); 
            } else {
                alert('Erreur(s) : \n' + html.substr(6)); 
            }
        }
    }); 
}

function delnewsletter (id) {
    url = baseurl + '/' + lang + '/newsletters/delnewsletter';
    if (confirm('Voulez-vous vraiment supprimer cette newsletter ?')) {
        $.ajax({
            type: "get", url: url, data: "id=" + id, 
            success: function(html){ 
                getnewsletters(); 
            }
        }); 
    }
}

function setproduit () {
    url = baseurl + '/' + lang + '/produits/modproduit';
    // recuperation des donnees postees
    tinyMCE.triggerSave(); // pour que les donnees de tinyMCE soient prises en compte aussi 
    donnees = $("#form_produit").serialize(); 
    $.ajax({
        type: "post", url: url, data: donnees, 
        success: function(html) {
            if (html.length && html.substr(0,6) != 'nok : ') {
                // relancer getproduits 
                if (top && top.$('#categorie-a-ouvrir').val() > 0) {
                    top.majnbproduits(); 
                    top.getproduits(0, 0, 1); 
                    top.getcategories(top.$('#categorie-a-ouvrir').val(), '3'); 
                } else if (top) {
                    top.majnbproduits(); 
                    top.getproduits(0, 0, 1); 
                    top.$('#produits_categories').html(); 
                } else {
                    document.location.reload(); 
                }
                // fermer Thickbox
                top.tb_remove();
            } else {
                alert('Erreur(s) : \n' + html.substr(6)); 
            }
        }
    }); 
}

function setfabricant () {
    url = baseurl + '/' + lang + '/fabricants/modfabricant';
    donnees = $("#form_fabricant").serialize(); 
    $.ajax({
        type: "post", url: url, data: donnees, 
        success: function(html) {
            if (html.length && html.substr(0,6) != 'nok : ') {
                // fermer Thickbox
                top.tb_remove();
                // relancer getfabricants 
                top.getfabricants(); 
            } else {
                alert('Erreur(s) : \n' + html.substr(6)); 
            }
        }
    }); 
}

function setuser () {
    url = baseurl + '/' + lang + '/users/moduser';
    donnees = $("#form_user").serialize(); 
    $.ajax({
        type: "post", url: url, data: donnees, 
        success: function(html) {
            if (html.length && html.substr(0,6) != 'nok : ') {
                // fermer Thickbox
                top.tb_remove();
                // relancer getusers 
                top.getusers(); 
            } else {
                alert('Erreur(s) : \n' + html.substr(6)); 
            }
        }
    }); 
}

function closeThickBox () {
    msg = 'Voulez-vous vraiment annuler ?'; 
    if (confirm(msg)) {
        top.tb_remove();
    }
}

function rangercategories (evt, ui) {
    // recupere le chemin et calcule les valeurs des parametres (qu'on ne peut pas transmettre a cause de la portee des variables)
    categories_path_array = getchemin(); 
    // id = ui.item.attr('id').substr(base_cat.length); 
    // level = $("#" + base_cat + id).parent().parent().attr('id').substr(basedstdiv.length); 
    // calcul des valeurs 
    id = ui.item.attr('id').substr(base_cat.length); 
    level = $("#" + base_cat + id).parent().parent().attr('id').substr(basedstdiv.length); 
    id_parent = categories_path_array[level]; 
    dstdiv = basedstdiv + level; 
    serstr = $("#" + dstdiv + '-ul').sortable('serialize'); 
    // alert (id + ',' + id_parent + ',' + level + ',' + serstr); 
    // return false; 
    serstr = escape(serstr); 
    // poste la categorie et recharge le div dstdiv
    url = baseurl + '/' + lang + '/categories/rangercategories';
    $.ajax({
        async: 0, 
        type: "get", 
        url: url, 
        data: "id=" + id + "&id_parent=" + id_parent + "&serial=" + serstr, 
        success: function(html) { 
            if (html.length) { // reload tous les div categorie et rouvre l'arborescence 
                // affichage des messages d'erreur
                if (html.substr(0,5) == 'refus') {
                    refus = html.split(':'); 
                    raison = refus[1]; 
                    if (raison == 'profondeur') {
                        alert('Vous ne pouvez pas dépasser 3 niveaux de catégories.'); 
                    }
                    if (raison == 'dependance') {
                        alert('Impossible de deplacer une catégorie mère dans l\'une de ses catégories filles.'); 
                    }
                }
                getcategories(id_parent, level, 1); 
            }
        }
    }); 
}

function rangerproduits (id_categorie, evt, ui) {
    // calcule les valeurs des parametres (qu'on ne peut pas transmettre a cause de la portee des variables)
    id = ui.item.attr('id').substr(base_prod.length); 
    dstdiv = 'produits_categories'; 
    serstr = $('#produits_categorie').sortable('serialize'); 
    serstr = escape(serstr); 
    // poste le produit et recharge le div dstdiv
    url = baseurl + '/' + lang + '/produits/rangerproduits';
    $.ajax({
        async: 0, 
        type: "get", 
        url: url, 
        data: "id=" + id + "&id_categorie=" + id_categorie + "&serial=" + serstr, 
        success: function(html) { 
            if (html.length) { // reload tous les div categorie et rouvre l'arborescence 
                // affichage des messages d'erreur
                if (html.substr(0,5) == 'refus') {
                    refus = html.split(':'); 
                    raison = refus[1]; 
                    if (raison.length) {
                        alert('Impossible de sauver cet ordre.'); 
                    }
                }
                getproduits(id_categorie); 
            }
        }
    }); 
}

function addcategorie (id_parent, level) {
    // poste la categorie et recharge le div dstdiv
    url = baseurl + '/' + lang + '/categories/addcategorie';
    nom = encodeURIComponent(trim('' + $("input#categories-level-" + level  + "-addmenuitem").val())); 
    $.ajax({
        type: "post", url: url, data: "id_parent=" + id_parent + "&nom=" + nom, 
        // beforeSend: function(){$("#loading").show("slow");}, //show loading just when link is clicked 
        // complete: function(){ $("#loading").hide("slow");}, //stop showing loading when the process is complete 
        success: function(html){ //so, if data is retrieved, store it in html 
            if (html.length && html.substr(0,6) == 'nok : ') {
                alert('Erreur(s) : \n' + html.substr(6)); 
            } else {
                getcategories(id_parent, level, 1); 
            }
        }
    }); 
}

function delproduit (id) {
    url = baseurl + '/' + lang + '/produits/delproduit';
    if (confirm('Voulez-vous vraiment supprimer ce produit et tous ses éléments ?')) {
        $.ajax({
            type: "get", url: url, data: "id=" + id, 
            success: function(html){ 
                getproduits($('#categorie-a-ouvrir').val()); 
            }
        }); 
    }
}

function delfabricant (id) {
    url = baseurl + '/' + lang + '/fabricants/delfabricant';
    if (confirm('Voulez-vous vraiment supprimer ce fabricant et tous ses éléments ? \n\nLes produits concernés seront détachés de ce fabricant... ')) {
        $.ajax({
            type: "get", url: url, data: "id=" + id, 
            success: function(html){ 
                getfabricants(); 
            }
        }); 
    }
}

function deluser (id) {
    url = baseurl + '/' + lang + '/users/deluser';
    if (confirm('Voulez-vous vraiment supprimer cet utilisateur ?')) {
        $.ajax({
            type: "get", url: url, data: "id=" + id, 
            success: function(html){ 
                getusers(); 
            }
        }); 
    }
}

function delcategorie (id, level, id_parent, forcer) {
    if (!forcer || !forcer.length) {
        forcer = 0; 
    }
    if (!level) {
        level = 0; 
    }
    url = baseurl + '/' + lang + '/categories/delcategorie';

    $.ajax({
        type: "get", url: url, data: "id=" + id + "&forcer=" + forcer, 
        success: function(html){ //so, if data is retrieved, store it in html 
            if (!html.length) {
                // MAJ le chemin et affiche a nouveau la categorie parente 
                // on ne sauvegarde qu'en cas de chargement avec succes ! 
                getcategories(id_parent, level, 1); 
                if ($('#categorie-a-ouvrir').val() == id) {
                    // getproduits(id_parent); 
                    getproduits(0); 
                    $('#categorie-a-ouvrir').val('0'); 
                }
            } else {
                if (confirm('Cette catégorie n\'est pas vide. Voulez-vous vraiment supprimer cette catégorie et toutes ses sous catégories ? \n\nSi des produits n\'appartiennent qu\'à cette catégorie, ils deviendront "Produits non classés"... ')) {
                    delcategorie(id, level, id_parent, '1'); 
                    if ($('#categorie-a-ouvrir').val() == id) {
                        getproduits(id_parent); 
                    }
                }
            }
        }
    }); 

}

function toggle_modcategorie (id) {
    $('input#categories-' + id + '-modmenuitem').toggle(); 
    if ($('input#categories-' + id + '-modmenuitem').css("display") == 'none') {
    } else {
        $('input#categories-' + id + '-modmenuitem').focus(); 
    }
}

function editcategorie (id) {
    tb_show('', baseurl + '/' + lang + '/categories/edit?id=' + id + '&TB_iframe=true&width=650&height=450&modal=true', '');
}

function setcategorie () {
    url = baseurl + '/' + lang + '/categories/editcategorie';
    // recuperation des donnees postees
    tinyMCE.triggerSave(); // pour que les donnees de tinyMCE soient prises en compte aussi 
    donnees = $("#form_categorie").serialize(); 
    $.ajax({
        type: "post", url: url, data: donnees, 
        success: function(html) {
            if (html.length && html.substr(0,6) != 'nok : ') {
                // fermer Thickbox
                top.tb_remove();
            } else {
                alert('Erreur(s) : \n' + html.substr(6)); 
            }
        }
    }); 
}

function modcategorie (id, level, id_parent) {
    // poste la categorie et recharge le div dstdiv
    url = baseurl + '/' + lang + '/categories/modcategorie';
    nom = encodeURIComponent(trim('' + $("input#categories-" + id  + "-modmenuitem").val())); 
    $.ajax({
        type: "post", url: url, data: "id=" + id + "&nom=" + nom, 
        // beforeSend: function(){$("#loading").show("slow");}, //show loading just when link is clicked 
        // complete: function(){ $("#loading").hide("slow");}, //stop showing loading when the process is complete 
        success: function(html){ //so, if data is retrieved, store it in html 
            if (html.length && html.substr(0,6) == 'nok : ') {
                alert('Erreur(s) : \n' + html.substr(6)); 
            }
            getcategories(id_parent, level, 1); 
            if ($('#categorie-a-ouvrir').val()) {
                getproduits($('#categorie-a-ouvrir').val()); 
            }
        }
    }); 
}

function getcategories (id_parent, level, noreclick) {
        if (!id_parent) {
            id_parent = 0; 
        }
        if (!level) {
            level = 0; 
        }
        if (!noreclick) {
            noreclick = 0; 
        }

        if (level == 3) {
            $('#categorie-a-ouvrir').val(id_parent); 
        } else {
            $('#categorie-a-ouvrir').val(0); // fait planter la coloration de la categorie selectionnee ??? 
        }

        // valeurs par defaut
        dstdiv = "categories"; 
        url = baseurl + '/' + lang + "/categories/getarbobo";
        if (!level) {
            level = 0; 
        }

        // enregistre le chemin suivi
        categories_path_array = getchemin(); 
        if (!noreclick) {
            categories_path_array[level] = id_parent; 
            // categories_path_array.unshift("0"); 
            categories_path_array.length = parseInt(parseInt(level) + 1); 
        }
        chemin = serialize(categories_path_array); 
        // on ne sauvegarde qu'en cas de chargement avec succes ! 
        // $("#categories-path").val(chemin); 

        if (level < niveau_max) {
            $.ajax({
                async: 0, type: "get", url: url, data: "chemin=" + chemin, 
                beforeSend: function(){
                    if (jQuery.browser.msie && jQuery.browser.version < 8) { // take away IE6 and IE7
                        $("#" + dstdiv).css("visibility", "hidden");
                    } else {
                        $("#" + dstdiv).fadeTo("fast", "0.1");
                    }
                }, //show loading just when link is clicked 
                complete: function(){ 
                    if (jQuery.browser.msie && jQuery.browser.version < 8) { // take away IE6 and IE7
                        $("#" + dstdiv).css("visibility", "visible");
                    } else {
                        $("#" + dstdiv).fadeTo("fast", "1");
                    }
                }, //stop showing loading when the process is complete 
                success: function(html){ //so, if data is retrieved, store it in html 
                    $("#" + dstdiv).show(); 
                    $("#" + dstdiv).html(html); //show the html inside .content div 

                    // on ne sauvegarde qu'en cas de chargement avec succes ! 
                    $("#categories-path").val(chemin); 
                    // Desactive la selection sur les zones draggables pour une meilleure experience utilisateur
                    /*
                    categoriearray = $(".sort-handle"); 
                    categoriearray.each(function() {
                        this.onselectstart = function () { return false; }; // ie
                        this.onmousedown   = function () { return false; }; // mozilla
                    }); 
                    */

                    // Rend la liste chargee sortable si elle ne l'est pas deja
                    createsortables(id_parent, level); 

                    // Inutile desormais
                    // majnbproduits(); 

                    // colore le chemin suivi 
                    $(".categories div").css("background-color", ""); 
                    for (i = 0; i < categories_path_array.length; ++i) {
                        $("#categories-level-2-ul div").css("background", ""); 
                        $("#categories_" + categories_path_array[i] + " div").css("background", couleur); 
                    }
                    if ($("#categorie-a-ouvrir").val().length) {
                        $("#categories-level-2-ul div").css("background", ""); 
                        $("#categories_" + $("#categorie-a-ouvrir").val() + " div").css("background", couleur_forte); 
                    }

                    /* Chargement des produits en cas de second click sur le lien deja clique 
                    if (!noreclick) {
                        // enregistre la categorie cliquee
                        if ($("#categorie-cliquee").val() == id_parent) {
                            $("#categories_" + $("#categorie-a-ouvrir").val() + " div").css("background-color", ""); 
                            $("#categorie-cliquee").val(); 
                            $("#categorie-a-ouvrir").val(id_parent); 
                            $("#categories_" + $("#categorie-a-ouvrir").val() + " div").css("background-color", couleur_forte); 
                            getproduits(id_parent); 
                        } else {
                            $("#categorie-cliquee").val(id_parent); 
                        }
                    }
                    */

                    $("#categories-level-" + level + "-add").show(); 
                    maj_thickbox(); 
                }
            }); 
        } else {

            // GESTION : on ne rafraichit pas le div des categories mais on charge quand meme les produits 
            if (categories_path_array.in_array($("#categorie-a-ouvrir").val())) {
                couleur_raz = couleur; 
            } else {
                couleur_raz = ""; 
            }
            $("#categories_" + $("#categorie-a-ouvrir").val() + " div").css("background-color", couleur_raz); 
            $("#categorie-a-ouvrir").val(id_parent); 
            $("#categories-level-2-ul div").css("background", ""); 
            $("#categories_" + id_parent + " div").css("background", couleur_forte); 
            getproduits(id_parent); 

        }
        // return false; // pour eviter que le lien ne soit suivi 
}

function getnbproduits (id_categorie, dstdiv) {
    url = baseurl + '/' + lang + '/categories/getnbproduits'; 
    $.ajax({
        async: 1, type: "get", url: url, data: "id_categorie=" + id_categorie, 
        success: function(html){ //so, if data is retrieved, store it in html 
            $("#" + dstdiv).html(html); //show the html inside .content div 
        }
    }); 
}

function getnbproduitsfab (id_fabricant, dstdiv) {
    url = baseurl + '/' + lang + '/fabricants/getnbproduits'; 
    $.ajax({
        async: 1, type: "get", url: url, data: "id_fabricant=" + id_fabricant, 
        success: function(html){ //so, if data is retrieved, store it in html 
            $("#" + dstdiv).html(html); //show the html inside .content div 
        }
    }); 
}

function majnbproduits () {
    for (i = niveau_max; i >= 0; --i) {
        /* recupere le nb de produits par categorie */
        $("#categories-level-" + i + "-ul li").each(function() {
                if ($(this).attr('id') && $(this).attr('id').substr(0, base_cat.length) == base_cat) {
                    getnbproduits($(this).attr('id').substr(base_cat.length), 'nb_' + $(this).attr('id')); 
                }
        }); 
    }
}

function majnbproduitsfab () {
    /* recupere le nb de produits par fabricant */
    $("#fabricants li").each(function() {
            if ($(this).attr('id') && $(this).attr('id').substr(0, base_fab.length) == base_fab) {
                getnbproduitsfab($(this).attr('id').substr(base_fab.length), 'nb_' + $(this).attr('id')); 
            }
    }); 
}

function createsortables (id_parent, level) {
    // valeurs par defaut
    if (!id_parent) {
        id_parent = 0; 
    }
    if (!level) {
        level = 0; 
    }

    // cree le tableau connexions_array
    connexions_array = new Array(); 
    for (i = 0; i < niveau_max; ++i) {
        connexions_array[i] = "#categories-level-" + i + "-ul"; 
    }
    // chemin
    categories_path_array = getchemin(); 

    categories_path_array_length = categories_path_array.length;  
    for (l = 0; l < categories_path_array_length; ++l) {
        // Rend la liste chargee sortable 
        range_id_parent = categories_path_array[l]; 
        range_level     = l; 
        // alert('CREE : ' + range_id_parent + ',' + range_level); 
        
 
    }
}

function getchemin () {
    categories_path_tmp = $("#categories-path").val(); 
    if (categories_path_tmp.length) {
        categories_path_array = unserialize(categories_path_tmp); 
    } else {
        categories_path_array = new Array("0") 
    }
    return categories_path_array; 
}

function showselection() {
    tb_show('', baseurl + '/' + lang + '/produits/selection?TB_iframe=true&width=650&height=450', '');
}

function closeselection() {
    top.tb_remove(); 
}

function selection(id) {
    // recupere le cookie de la selection dans un tableau
    if ($.cookie('selection')) {
        selection_array = unserialize($.cookie('selection')); 
    } else {
        selection_array = new Array(); 
    }
    // ajoute l'id si necessaire
    ajouter = 1; 
    for (var i in selection_array) {
        if (selection_array[i] == id) {
            ajouter = 0; 
            break; 
        }
    }
    if (ajouter) {
        selection_array.push(id); 
    }
    // enregistre le cookie
    $.cookie('selection', serialize(selection_array), { path: '/', expires: 365 });
    // MAJ affichage
    majselection(); 

    // faire clignoter le menu selection
    delai = 200; 
    $('#menu_selection').animate( { opacity: 0.1 }, delai ); 
    setTimeout("$('#menu_selection').toggleClass('selected')", delai); 
    $('#menu_selection').animate( { opacity: 1 }, delai ); 
    $('#menu_selection').animate( { opacity: 0.1 }, delai ); 
    setTimeout("$('#menu_selection').toggleClass('selected')", parseInt(3*delai)); 
    $('#menu_selection').animate( { opacity: 1 }, delai ); 

    // showselection(); 
    /*
    if (top.$('#id_produit').val() == id) {
        top.$('#deselection').show(); 
        top.$('#selection').hide(); 
    }
    */
}

function deselection(id) {
    // recupere le cookie de la selection dans un tableau
    if ($.cookie('selection')) {
        selection_array = unserialize($.cookie('selection')); 
        // supprime l'id si possible 
        var output = new Array();
        for (var i in selection_array) {
            if (selection_array[i] != id && !isNaN(selection_array[i])) {
                output.push(selection_array[i]);
            }
        }
        selection_array = output; 
    } else {
        selection_array = new Array(); 
    }
    // enregistre le cookie
    $.cookie('selection', serialize(selection_array), { path: '/', expires: 365 });
    // MAJ affichage
    majselection(); 

    // faire clignoter le menu selection
    $('#menu_selection').animate( { opacity: 0.1 }, 200 ); 
    setTimeout("$('#menu_selection').toggleClass('selected')", 200); 
    $('#menu_selection').animate( { opacity: 1 }, 200 ); 
    $('#menu_selection').animate( { opacity: 0.1 }, 200 ); 
    setTimeout("$('#menu_selection').toggleClass('selected')", 600); 
    $('#menu_selection').animate( { opacity: 1 }, 200 ); 

    if (!selection_array.length) {
        closeselection(); 
    } else {
        // recharge la selection sinon
        nom_iframe = top.$('iframe#TB_iframeContent').attr('name');
        if (nom_iframe) {
            eval('top.' + nom_iframe + '.location.reload();'); 
        } 
    }
    /* 
    if (top.$('#id_produit').val() == id) {
        top.$('#selection').show(); 
        top.$('#deselection').hide(); 
    }
    */
}

function razselection() {
    $.cookie('selection', null, { path: '/' });
    nom_iframe = top.$('iframe#TB_iframeContent').attr('name');
    if (nom_iframe) {
        eval('top.' + nom_iframe + '.location.reload();'); 
    } 
    // MAJ affichage
    majselection(); 
    closeselection(); 
    top.$('#selection').show(); 
    top.$('#deselection').hide(); 
}

function printselection() {
    nom_iframe = $('iframe#TB_iframeContent').attr('name');
    if (nom_iframe) {
        eval('top.' + nom_iframe + '.location.href="' + baseurl + '/' + lang + '/produits/selection?startprint=1";'); 
        // eval(nom_iframe + '.focus();'); 
        // eval(nom_iframe + '.print();'); 
    } else {
        alert('Le selection doit être affichée pour pouvoir être imprimée'); 
    }
}

function majselection() {
    // recupere le cookie de la selection dans un tableau
    if ($.cookie('selection')) {
        selection_array = unserialize($.cookie('selection')); 
        var output = new Array();
        for (var i in selection_array) {
            if (!isNaN(selection_array[i]) && selection_array[i]) {
                output.push(selection_array[i]);
            }
        }
        selection_array = output; 
    } else {
        selection_array = new Array(); 
    }
    nbsel = selection_array.length; 
    if (nbsel == 1) {
        top.document.getElementById('menu_selection').innerHTML = nbsel + ' object';
    } else {
        top.document.getElementById('menu_selection').innerHTML = nbsel + ' objects';
    }
    // resauvegarde la selection
    $.cookie('selection', serialize(selection_array), { path: '/', expires: 365 });
}

function envoyerselection() {
        // recharge la selection sinon
        nom_iframe = top.$('iframe#TB_iframeContent').attr('name');
        eval('top.' + nom_iframe + '.location.href="' + baseurl + '/' + lang + '/produits/selectionmail";'); 
}

function recevoirnewsletter() {
        // recharge la selection sinon
        nom_iframe = top.$('iframe#TB_iframeContent').attr('name');
        eval('top.' + nom_iframe + '.location.href="http://app.bloo-mailer.com/formulaire_html.php?idrep=677&champs=MCY";'); 
}

function gotoproduit(id, keywords) {
    // affiche directement la fiche d'un produit dans la fenetre top
    url = baseurl + '/' + lang + '/produits/fiche/' + id + keywords; 
    setTimeout("top.window.location.href = '" + url + "'", 100);
}

function showideescadeaux () {
    $('#idees_cadeaux').show(''); 
    $('#menu_cadeaux').addClass('fakehover'); 
}

function hideideescadeaux () {
    $('#idees_cadeaux').hide(''); 
    $('#menu_cadeaux').removeClass('fakehover'); 
}

function showcontactspro() {
    $('#contact_societe').show();
    $('#contact_fonction').show();
    $('#contact_telephone').show();
}

function hidecontactspro() {
    $('#contact_societe').hide();
    $('#contact_fonction').hide();
    $('#contact_telephone').hide();
}

