/************************************************************************************ 
 * ExtJs versione 3.1.0 - Versione csi 1.0.0
 * Plugin che consente di mantenere lo stato di un tree
 * @class Ext.ux.tree.State
 * @extends Ext.tree.TreePanel
 
 * Provides statefull TreePanel 
 *  plugins: [new Ext.ux.tree.State()], 
***********************************************************************************/
Ext.namespace('Ext.ux.tree');
Ext.ux.tree.State = function(config) {
   Ext.apply(this, config);
};
Ext.extend(Ext.ux.tree.State, Ext.util.Observable, {
   init: function(tree) {
      this.tree = tree;
      this.stateName = 'TreePanelState_' + this.tree.id;
      this.idField = this.idField || 'id';
      this.provider = new Ext.state.CookieProvider({expires: null}); //Ext.state.Manager.getProvider();
      this.state = this.provider.get(this.stateName, {});
      this.tree.on({
         scope: this,
         load: {
            fn: function(node) {
                var lastSelectedNodePath = this.provider.get('LastSelectedNodePath_' + this.tree.id);
                var lastSelectedNodeId = this.provider.get('LastSelectedNodeId_' + this.tree.id);
                
                var rootNode = this.tree.getRootNode();
                if(node.id == rootNode.id == lastSelectedNodePath) {
                    this.tree.selectPath(lastSelectedNodePath);
                    node.fireEvent('click', node);
                    return;
                }
	    
	            if(node.id == lastSelectedNodePath) {
                    node.fireEvent('click', node);
                }
                else {                
                    var childNode = node.findChild('id', lastSelectedNodePath);
                
                    if(childNode && childNode.isLeaf()) {
                    	//alert(childNode);
                    		//childNode.select();
                        //childNode.ensureVisible();
                        //this.tree.selectPath(lastSelectedNodePath);
                        //***i figli non sono ancora stati caricati attendo 100ms
                        setTimeout(function(){childNode.fireEvent('click', childNode);}, 100);
                        
                    }
                    else if(childNode && !childNode.isLeaf()) {
                        this.tree.selectPath(lastSelectedNodePath);
                        childNode.fireEvent('click', childNode);
                    }
                }
            }
         },
         collapsenode: this.onCollapse,
         expandnode: this.onExpand,
         append: this.onNodeAdded,
         click: this.onNodeClick
      });
   },
    
   saveState : function(newState) {
      this.state = newState || this.state;
      this.provider.set(this.stateName, this.state);
   },
    
   onNodeAdded: function(tree, parentNode, node, index) {	
      switch (this.state[node.id]) {
         case 'C': 
            node.expanded = false; 
            break;
         case 'E': 
            node.expanded = true; 
            break;
      };
   },
   
   onExpand: function(node) {
      this.state[node.id] = 'E';
      this.saveState();
   },
   
   onCollapse: function(node) {
      this.state[node.id] = 'C';
      this.saveState();
   },
   
   onNodeClick: function(node) {
      this.provider.set('LastSelectedNodeId_' + this.tree.id, node.attributes.uniqueid);
      this.provider.set('LastSelectedNodePath_' + this.tree.id, node.id);
   }
});
