CategoryTree = function(){        
    var config_data;
    var root;
    
    return {
        getNodeConfiguration : function(node) {
			config_data = new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({url: 'categories/getTimelineInfo?node=' + node.id}),
				reader: new Ext.data.JsonReader({},[ 'id','focusDate', 'topBandType', 'showTimeline']),
				remoteSort: false
			});
			
			config_data.on('load', function() {								
				
				VideoManager.updateVideoList(config_data.getAt(0).data.id,1,'');
				var configShowTimeline = false;
				configShowTimeline = Ext.util.Format.boolean(config_data.getAt(0).data.showTimeline);
				if ( configShowTimeline){
					Application.showTimeline();
					MyTimeline.reloaddata(config_data.getAt(0).data.id,config_data.getAt(0).data.focusDate, config_data.getAt(0).data.topBandType); 
				} else {
					Application.hideTimeline();
				}
				
			});
            
            config_data.load();    	
        },
        
        init : function(){
          
			// shorthand
		    var Tree = Ext.tree;
		    var treeLoader = new Tree.TreeLoader({dataUrl:'categories/getCategories'});
		    treeLoader.on('load', function(This,node,response){
		    	//alert('Node:' + node.id);
		    	//This is the last node loaded during startup
		    	if (node.id == 118) {				    		
		    		//If the user is directed to the site with a url link to open a video
					//Open a new tab with that video
					var id = Application.getQueryVariable('categoryid');
					if (id) {
						var path;
						path = id.split(';');
						var node = CategoryTree.root;
						if (path.length > 0 ){
								//Lets make sure its visible
								for (var x = 0; x < path.length; x++)
								   {
								   node = node.findChild('id',path[x]);
								   }
								node.select();
								CategoryTree.loadNode(node);
						}	
					}
		    	}
		    });
		    
		    var tree = new Tree.TreePanel('categories-tree', {
		        rootVisible: false,
		        lines:false,
		        animate:true, 
		        loader: treeLoader,
		        enableDD:false,
		        containerScroll: true		    		    
		    });
		    
		    //add the listener
		    tree.on('click', function(node){	                
                    //alert('Node:' + node.id);	                				    			    
          		CategoryTree.loadNode(node);        
                   
		    }); 
		
		    		  
		    // set the root node
		    CategoryTree.root = new Tree.AsyncTreeNode({
		        text: 'root',
		        draggable:false,
		        id:'1'
		    });
		    tree.setRootNode(CategoryTree.root);
		
		    // render the tree
		    tree.render();
		
		    CategoryTree.root.expand();
		    
		    		    		    
        },   
        
        loadNode : function(node){
        	  Application.focusVideosTab();
                    
                    CategoryTree.getNodeConfiguration(node);
                    
                    VideoToolbar.clearSearch();

         		    urchinTracker('/category/' + node.text);
        }             

    }
}();
Ext.EventManager.onDocumentReady(CategoryTree.init, CategoryTree, true);
