/*
    jquery.wTreeTable.js

    Permet de grouper les lignes d'un tableau pour les afficher ou les masquer
    Paramètres :
    * showImg        : URL de l'image à afficher lorsque le contenu du groupe est masqué
    * hideImg        : URL de l'image à afficher lorsque le contenu du groupe est affiché
    * hideLevel      : niveau de profondeur masquée par défaut
    * imgSize        : taille max de l'image (width ou height) en pixels
    
    22/10/2010, BBO, Création
    08/11/2010, BBO, Ajout du param imgSize pour éviter le déplacement du texte lorsque l'image est modifiée
*/
(function($){
$.fn.wTreeTable = function(options) {
    var defaults = {
      showImg: "images/puce_plus.gif",
      hideImg: "images/puce_moins.gif",
      imgSize: 7,
      hideLevel: null
    }
    var options = $.extend(defaults,options);
    return this.each(function() {  
      obj = $(this);
      // placer une image sur tous les th dont le suivant a une profondeur plus grande
      var trPos=0;
      $("tr:has(td)",obj).each(function(){
        var tr=$(this);
        var trDepth=getRowDepth(this);
        var nextTr=tr.next("tr");
        var nextTrDepth=(nextTr.length>0)?getRowDepth(nextTr):null;
        var hImg=$('<div style="width:'+options.imgSize+'px;height:'+options.imgSize+'px" class="wtt-toggleBtn"><img src="'+options.hideImg+'" alt=""/></div>').css("cursor","pointer");
        if (nextTrDepth > trDepth) $("*:first",tr).prepend(hImg);
        trPos++;
      });
      
      $("div.wtt-toggleBtn").click(function(){
        var parentTr=$(this).parent().parent();
        currentDepth=getRowDepth(parentTr);
        toggleRow(parentTr.next("tr"),currentDepth);
        toggleBtn($("img",this));
      });
      
      // initialisation avec hideLevel
      if (options.hideLevel>=0) {
        $("tr:has(td)").each(function(){
          
          if (getRowDepth(this)==options.hideLevel) {
            toggleRow($(this).next("tr"),getRowDepth(this));
            toggleBtn($("*:first img:first",this));
          }
        });
      }
      
      function toggleBtn(img) {
        var toggleImg=$(img).attr("src");
        if (toggleImg==options.showImg) $(img).attr("src",options.hideImg);
        else if (toggleImg==options.hideImg) $(img).attr("src",options.showImg);
      }
      
      function toggleRow(cn,depth,actionType){
        if (getRowDepth(cn) > depth) {
          if ((cn.css("display")=="none") && (!(actionType) || (actionType == "show")) && (!(cn.data("hidden") == "true") || (getRowDepth(cn) == (depth*1 +1)))) {
            cn.show();
            if (getRowDepth(cn) == (depth*1 +1)) cn.data("hidden","false");
            if (!(actionType)) var actionType = "show";
          }
          else {
            cn.hide();
            if (getRowDepth(cn) == (depth*1 +1)) cn.data("hidden","true");
            if (!(actionType)) var actionType = "hide";
          }
          var nextRow=cn.next("tr");
          if (nextRow.length>0) toggleRow(nextRow,depth,actionType);
        }
      }
      
      // récupère la profondeur d'un tr à partir de sa classe level*
      function getRowDepth(domElt) {
        var eltClass=$("*:first",$(domElt)).attr("class");
        if (eltClass) {
          var classes=$("*:first",$(domElt)).attr("class").split(" ");
          var depth=0;
          for (var i in classes) {
            if ((classes[i].indexOf) && (classes[i].indexOf("level") != -1 && classes[i].split("level")[1] > 0)) {
              depth=classes[i].split("level")[1];
            }
          }
          return depth;
        }
        else {
          //console.log("Didn't found depth for element: "+$(domElt).html());
          return null;
        }
      }
  });
};

})(jQuery);

