var selectDefault=true;
var firstTimeLoad=true;
var nodesReturned=Array();
var titleOfCat='';
/*var firstId=13;
var stat =	[
	{ attributes : { "id" : firstId }, data : "Medicine", state : "closed"},
];*/

$(document).ready(function() {
	var temp=Array();
	var fullurl = window.location.href;
	temp=fullurl.split("#");
	if(temp.length==2){
		temp=temp[1].split("=");
		if(temp.length==2)
			if(temp[0]=='id')
				selectedNodeId=temp[1];
			else
				selectedNodeId=firstId;		
		else
			selectedNodeId=firstId;		
	}else
		selectedNodeId=firstId;	
		

	$("#tree_cat").tree({
		ui : {
			theme_name : "classic"
		},
		plugins : { },
		data : { 
			type : "json",
			async : true,
			opts : {
				method : "POST",
				url : "/cat"
			}
		},
		callback : { 
			beforedata : function (n, t) { 
				if(n == false) t.settings.data.opts.static = stat; 
				else t.settings.data.opts.static = false;		
				return { id : $(n).attr("id")|| firstId } 
			},
			oncreate : function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) { 

			},
			onopen: function(n,t) {
					t.children(n).each(function(){
						$(this).attr('id');	
						var title="hid_title_"+$(this).attr('id');		
						var titleToShow=$("#"+title).html();
						load_qtip_tree($("#tree_title_"+$(this).attr('id')),titleToShow) 										
					});
				 	t.children(n).each(function() {
				 		
				 		if($(this).attr('id')==last){
							hideTotal();
									 			
				 			t.select_branch($(this));
				 			
				 		}else
				 		for(i=0;i<nodesReturned.length;i++)		 		
				 			if($(this).attr('id')==nodesReturned[i]){
				 				t.open_branch($(this));
				 				nodesReturned[i]='end';
				 			}					 
				  });
		 
			},
			onselect : function (n, t) {
				
				catId=$(n).attr('id');
				titleOfCat=$("#tree_title_"+catId).html();
				if(catId==0 || !titleOfCat)
					titleOfCat='All Subjects'
				$("#cat_recognize").html("<div style='padding-top:5px;height:20px;background-color: rgb(255, 255, 162)'>Collections of: "+"<strong>"+titleOfCat+"</strong></div>");
				
				page=1;
				t.open_branch($(n)); 
				//alert(catId);
				ajaxRequest();
			},
			onload: function (t){
				if(selectedNodeId==firstId){
					t.select_branch($("#"+firstId));				
					catId=firstId;
				}else{
					//showMaskFastTotal('tree_mask');
					//$('#mask').fadeTo('slow',0.2);
					
					//nodePath(showMaskManipulator);
					$.ajax({
   					type: "POST",
  						url: '/nodepath',
  						data: "id="+selectedNodeId,
  						dataType: "json",
   					success: function(msg){	
   						nodesReturned=msg.path.split('-');
							last=nodesReturned[nodesReturned.length-2];
							t.open_branch($("#"+firstId));
   					}
   				});
				}  
			}
			
		}
	});

 $('#cat_left').scroll(function () { 
      alert('wow'); 
 });



});
function nodePath(id){

}
