Ext.BLANK_IMAGE_URL = '/js/ext-2.0.1/resources/images/default/s.gif';

Ext.onReady(function(){

	Ext.ns('Ext.ux.tree'); 
    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    var getnodesUrl = '/menucategorie/getnodes';


    var Tree = Ext.tree;
    
    var tree = new Tree.TreePanel({
        el:'tree-div',
        id:'tree',
        autoScroll:true,
        autoHeight: true, //Full Height
        animate:true,
        enableDD:false,	//true to enable drag and drop
        lines: false,	//Lines for the tree
        singleExpand: true,	//Only expand one node
        containerScroll: true,
        rootVisible: false,
        singleClickExpand: true, // by clicking the text to start, the default for double-click
        loader: new Ext.tree.TreeLoader({
            dataUrl:getnodesUrl
        }),
        listeners : {   
            'click' : function(node, event) {   
                 NodeClick(node,event);
                
            }   
        },
        plugins:[new Ext.ux.state.TreePanel()]    
    });
    
    var root = new Tree.AsyncTreeNode({
        text:'Componenten',
        draggable:false,
        singleExpand: true,	//Only expand one node       
    	id:'root'
    });
    tree.setRootNode(root);
 
    tree.render();
    root.expand();
 
    var received = function (response) {
		x = Ext.decode( response.responseText );
		//console.log(x);
		//console.log(window.location.pathname);
		tree.expandPath(x.path);
	}
	
	//Ext.EventManager.on(window, 'unload', function() {        
	
		var actie = window.location.pathname.split("/");
		
		if(actie[1] == ''){
			var actie = $.query.get('cat').split("/");
		}
		
		//console.log(actie);
		//Als we cat vinden	
		//console.log(actie);
		//console.log(tree.stateHash );
		
		//Kijk of de actie die we openen ook bestaat in cookie van de gebruiker dit om db aanvragen te verminderen
		if(tree.stateHash.root){
			//console.log("ja");
		}else{
			//console.log("Nee");
			if(actie[1] == 'cat' || actie[1] == 'products'){
		   		GetExpandPath(actie);	//Open het menu
			}
		}
		
		
		
	//}); 
    // Listen for mouse clicks 
 //Ext.get('tree-div').on("click", function(){  
 //	node = tree.getSelectionModel().getSelectedNode();   //Get the selected node
 	
 //	console.log("Node path", node.getPath()); 
 	/*
 	if(node){
 		if(node.isExpanded() == false){
 			//tree.expandPath(node.getPath());	//Expand The tree path
 			node.expand();
 		}else{
 			node.collapse();	
 		}
 		console.log("You clicked on", node.id); 
 		console.log("isExpanded",node.isExpanded()); 
 	}
 	*/
 //});
 

		//tree.expandPath('/root/2/1'); 
function GetExpandPath(actie){
	 Ext.Ajax.request({
			   url: '/menucategorie/GetExpandPath',
			   success: received,
			   failure: function () { 
			   	//alert("Kan het menu niet openen, neem aub contact met ons op.");
			   },
			   headers: {
			       'my-header': 'foo'
			   },
			   params: { qs: actie[1], id:actie[2] }
			});
}  

});

function NodeClick(node, event){
   		//node = tree.getSelectionModel().getSelectedNode();   //Get the selected node
 	if(node){
 		if(node.isExpanded() == false){
 			//tree.expandPath(node.getPath());	//Expand The tree path
 			node.expand();
 		}else{
 			node.collapse();	
 		}
 		
 		//console.log("You clicked on", node.id); 
 		//console.log("isExpanded",node.isExpanded()); 
 	}
} 

