NavigationCtrl = function()
{
  this.selection_automatique = false;

  this.initialise = function()
  {
    $(document).ready(function()
    {
      if(typeof(json_arborescence) != 'undefined')
      {
        navigationCtrl.initialiseJsTree();
      }
    });
  }

  this.initialiseJsTree = function()
  {
    /**
     * Initialise l'arbre des familles
     */
    $("#arborescence").tree({
      types:
      {
        "default" : {
          clickable	 : true,
          renameable : false,
          deletable	 : false,
          creatable	 : false,
          draggable	 : false,
          max_children	: -1,
          max_depth	: -1,
          valid_children	: "all",
          icon : {
            image : false,
            position : false
          }
        }
      },
      rules : {
        use_max_children : false,
        use_max_depth : false
      },
      data : {
        type : "json",
        opts : {
          static : json_arborescence
        }
      },
      callback : {
        onload: function(TREE_OBJ)
        {
          var node = TREE_OBJ.search(famille_courante);
        },

        onsearch: function(NODES, TREE_OBJ)
        {
          navigationCtrl.selection_automatique = true;
          $(NODES).each(function(i, node)
          {
            if(TREE_OBJ.get_text(node).replace(/^\s+|\s+$/g, '') == famille_courante)
            {
              TREE_OBJ.select_branch(node);
              return false;
            }
          });
        },

        /**
         * Charge la liste des produits associés à la famille sélectionnée
         */
        onselect: function(NODE, TREE_OBJ)
        {
          if(navigationCtrl.selection_automatique)
          {
            navigationCtrl.selection_automatique = false;
            return;
          }

          location.href = url_navigation_arborescence + '/' + $(NODE).find('span:first').html();
/*
          var chemin = TREE_OBJ.get_text(NODE);
          var n = NODE;
          while(TREE_OBJ.parent(n) != -1 && TREE_OBJ.parent(n) != false)
          {
            n = TREE_OBJ.parent(n);
            chemin = TREE_OBJ.get_text(n) + '>' + chemin;
          }
          location.href = url_navigation_arborescence + chemin;
*/
        }
      }
    });
  }


  this.ajouteProduit = function(id)
  {
    
  }
}

var navigationCtrl = new NavigationCtrl();
navigationCtrl.initialise();

