//
// Extend the XmlTreeLoader to set some custom features
//
Ext.app.ExplorerTreeLoader = Ext.extend(Ext.ux.XmlTreeLoader, {
    processAttributes : function(attr){
         // Set the node text that will show in the tree 
         attr.text = attr.term + " (" + attr.occur + ")";
         
         // Author icon
         var buffer = '';
         if(attr.isnew == 'true') { buffer = '-new'; }
         attr.iconCls = attr.tagName + buffer;
         attr.cls = 'item' + buffer;
         
         // Indicate that the tree is fully loaded.
         // Otherwise each node will be loaded asynchronously
         attr.loaded = false;

         // Expand the node
         attr.expanded = false;
         
         attr.tpl = ExplorerApp.getTemplate();
    }
});

ExplorerTree = function() {

    this.version = ExplorerApp.getVersion();
    this.loadMessage = new Ext.LoadMask(Ext.getBody(), {msg:"Loading..."});

    var store = new Ext.data.Store({
        // load using HTTP
        url: '/service/search?style=version',

        // the return will be XML, so lets set up a reader
        reader: new Ext.data.XmlReader({
              record: 'Release'
           }, [	// Extracted fields
               'Version', 'LastUpdate', 'Status'
           ])
    });
    store.load();
    
    this.treeLoader = new Ext.app.ExplorerTreeLoader({
               dataUrl:'http://www.spase-group.org/service/search'
        });
    this.treeLoader.on('beforeload', function(treeLoader, node) {
		  this.loadMessage.show();
        this.treeLoader.baseParams.style = 'browse';
        this.treeLoader.baseParams.version = this.version;
        if(node.attributes.term) this.treeLoader.baseParams.term = node.attributes.term;
        else this.treeLoader.baseParams.term = "Spase";
    }, this);
    this.treeLoader.on('load', function(node) {
		  this.loadMessage.hide();
        this.selectVersion();
    }, this);
    
    this.versionCombo = new Ext.form.ComboBox({
            xtype: 'combo',
            id: 'tree-version',
            fieldLabel: 'Version',
            tooltip: {text:'Version'},
            iconCls: 'expand-icon',
            editable: false,
            width: 80,
            store: store,
            mode: 'local',
            displayField: 'Version',
            forceSelection: true,
            triggerAction: 'all',
            listeners: {'select': this.loadVersion, scope: this },
            tpl: '<tpl for="."><div ext:qtip="{Version} ({Status}); Updated: {LastUpdate}" class="x-combo-list-item">{Version}</div></tpl>',
            scope: this
         });
         
   ExplorerTree.superclass.constructor.call(this, {
        id: 'tree-panel',
        region: 'west',
        title: 'Terms',
        split: true,
        width: 165,
        minSize: 150,
        maxSize: 400,
        collapsible: true,
        margins: '0 0 5 5',
        cmargins: '0 5 5 5',
        rootVisible: false,
        autoScroll: true,
        collapseFirst: false,
        root: new Ext.tree.AsyncTreeNode(),
        loader: this.treeLoader,
        scope: this,
        
        tbar: [this.versionCombo, '-', {
        	   id: 'tree-history',
            text: 'History',
            tooltip: {text:'View Change History'},
            iconCls: 'collapse-icon',
            handler : this.showHistory,
            scope: this
         }]  
		  /*    
        tbar: [this.versionCombo, '-', {
        	   id: 'tree-collapse',
            text: 'Collapse',
            tooltip: {text:'Collapse all'},
            iconCls: 'collapse-icon',
            handler : this.collapseBase,
            scope: this
         }, '-', {
         	id: 'tree-expand',
            text: 'Expand',
            tooltip: {text:'Expand all'},
            iconCls: 'expand-icon',
            handler : this.expandAll,
            scope: this
         }
		]   
		*/   
    });

    this.getSelectionModel().on({
             'selectionchange': function(tree, node){
                    ExplorerApp.getView().openTab(node.attributes.term, this.version);
                  },
                  scope: this
               });
    
    // this.addListener('beforeload', function() { alert('before'); }, this);
};

Ext.extend(ExplorerTree, Ext.tree.TreePanel, {
	collapseBase : function() {
		this.collapseAll();
		for(var i = 0; i < this.getRootNode().childNodes.length; i++) {
			this.expandPath(this.getRootNode().childNodes[i].getPath());
		}
	},

   showHistory : function() {
   	ExplorerApp.getView().openInfoTab('History', 'history.jsp', this.version);
   },
	
	loadVersion : function(combo, record, idx) {
		this.version = record.data['Version'];
		this.getRootNode().collapseChildNodes();
		this.getRootNode().reload(this.treeLoader);
		var view = ExplorerApp.getView();
      view.items.each(function(item){ 
          if(item.closable){ view.remove(item); } 
      }); 
	},
	
	selectVersion : function() {
		this.versionCombo.setValue(this.version);
	}
});
