$.fn.setTree = function(opt){

    var TREE = this;
    var ROOT = $('.root',this);
    TREE.option = {
      remember_folding: true,
      animate: true,    // this parameter has a value "true/false" (enable/disable animation for expanding/collapsing menu items) 
      speed: 'fast'    // speed open/close folder
    };
    TREE.option = $.extend(TREE.option,opt);
    
    TREE.setEventToggler = function (obj) {
      
      $('>.toggler', obj).unbind('click');
      
      if(!$(obj).children('.toggler')[0])
        $(obj).prepend('<span class="toggler"></span>');
      
      $('>.toggler', obj).bind('click', function() {
        
        var childUl = $('>ul',obj);
        var className = obj.className;
        
        if(childUl.is(':visible')) {
          
          className = className.replace('open','close');
          $(obj).attr('class',className);
          //remove closing window
          
          if(TREE.option.remember_folding == true)
            $.jCache.removeItem($(obj).attr('id'));
          
          $('li[class*="open"]', obj).each(function() {
            this.className = this.className.replace('open','close');
            $('ul',this).hide();
            
            if(TREE.option.remember_folding == true)
              $.jCache.removeItem($(this).attr('id'));
            
          });
          
          if(TREE.option.animate) {
            childUl.animate({height:"toggle"},TREE.option.speed);
          } else {
            childUl.hide();
          }
          
        } else {
          
          className = className.replace('close', 'open');
          $(obj).attr('class',className);
            
          //save opening window
          if(TREE.option.remember_folding == true)
            $.jCache.setItem($(obj).attr('id'), '1');
            
          if(TREE.option.animate) {
            childUl.animate({height:"toggle"},TREE.option.speed);
          } else {
            childUl.show();
          }
        }
      });
    };
    
    TREE.setTreeNodes = function(obj, useParent) {
      
      obj = useParent ? obj.parent() : obj;
      
      $('li', obj).each(function(i) {
        
        var className = this.className;
        var childNode = $('>ul',this);
        
        if(TREE.option.remember_folding == true)
          status = $.jCache.getItem($(this).attr('id'));
        
        if(TREE.option.remember_folding == true && status === '1')
          childNode.show();
        else
          childNode.hide();
        
        if(childNode.size() > 0) {
          
          var setClassName = 'folder-';
          
          if(status == '1')
            setClassName = setClassName + 'open';
          else
            setClassName = setClassName + 'close';
          
          this.className = setClassName + ($(this).is(':last-child')? '-last':'');
          
          TREE.setEventToggler(this);
          
        } else if(this.className == 'page') {
          
          var setClassName = 'page';
          this.className = setClassName + ($(this).is(':last-child')? '-last':'');
          
        } else if(this.className == 'page-last') {
          
          var setClassName = 'page';
          this.className = setClassName + ($(this).is(':last-child')? '-last':'');
          
        } else {
          
          var setClassName = 'folder-wpam-close';
          this.className = setClassName + ($(this).is(':last-child')? '-last':'');
          
        }
      });
    };
    
    
    TREE.init = function(obj) {
      TREE.setTreeNodes(obj);
    };
    
    TREE.init(ROOT);
    
}

$.fn.updateTree = function(opt) {
  
  var self = this;
  
  options = {
    remember_folding: true,
    animate: true
  };
  
  options = $.extend(options,opt);
  
  setEventToggler = function (obj) {
    
    $('>.toggler', obj).unbind('click');
    
    if(!$(obj).children('.toggler')[0])
      $(obj).prepend('<span class="toggler"></span>');
    
    $('>.toggler', obj).bind('click', function(){
      
      var childUl = $('>ul',obj);
      var className = obj.className;
      if(childUl.is(':visible')){
              
        className = className.replace('open','close');
        $(obj).attr('class',className);
        
        //remove closing window
        if(options.remember_folding == true)
          $.jCache.removeItem($(obj).attr('id'));
        
        $('li[class*="open"]', obj).each(function() {
          this.className = this.className.replace('open','close');
          $('ul',this).hide();
          
          if(options.remember_folding == true)
            $.jCache.removeItem($(this).attr('id'));
          
        });
        
        if(options.animate) {
          childUl.animate({height:"toggle"}, 'fast');
        } else {
          childUl.hide();
        }
        
      } else{
        
        className = className.replace('close','open');
        $(obj).attr('class',className);
        
        //save opening window
        if(options.remember_folding == true)
          $.jCache.setItem($(obj).attr('id'), '1');
        
        if(options.animate) {
          childUl.animate({height:"toggle"}, 'fast');
        } else {
          childUl.show();
        }
        
      }
    });
  };
  
  $('li[class!="root"]', self).each(function() {
    
    var className = this.className;
    
    if(options.remember_folding == true && $.jCache.getItem($(this).attr('id') == '1'))
      var open = true;
    else
      var open = false;
    
    var childNode = $('>ul',this);
    
    if(childNode.size() > 0) {
      
      var setClassName = 'folder-';
      
      if(open == true) {
        setClassName = setClassName + 'open';
        childNode.show();
      } else {
        setClassName = setClassName + 'close';
      }
      this.className = setClassName + ($(this).is(':last-child')? '-last':'');
      setEventToggler(this);
      if(!open)childNode.hide();
      
    } else if(this.className == 'page') {
      
      var setClassName = 'page';
      this.className = setClassName + ($(this).is(':last-child')? '-last':'');
      
    } else if(this.className == 'page-last') {
      
      var setClassName = 'page';
      this.className = setClassName + ($(this).is(':last-child')? '-last':'');
      
    } else {
      
      var setClassName = 'folder-wpam-close';
      this.className = setClassName + ($(this).is(':last-child')? '-last':'');
      
    }
    
  });
  
  
}
