
function fileExplorer(){

	this.currentIconMode;
       	this.tree;

        
	this.changeIconMode = function() {
            	var newVal = parseInt(this.value);
                if (newVal != this.currentIconMode) {
                             	this.currentIconMode = newVal;
                }
        }


	this.buildTree = function(){
   //create a new tree:
   this.tree = new YAHOO.widget.TreeView("treeDiv1");

   
   //turn dynamic loading on for entire tree:
   this.tree.setDynamicLoad(this.loadNodeData, this.currentIconMode);
   
   //get root node for tree:
   var root = this.tree.getRoot();
   
   //add child nodes for tree; our top level nodes are
   //all the states in India:
   var aStates = ["cultural", "doc", "etc", "geopolitical", "health", "hydrologic", "inland_waters", "maps", "natural_resources", "remote_sensing", "transportation", "uncategorized"];
   
   for (var i=0, j=aStates.length; i<j; i++) {
		var tempNode = new YAHOO.widget.TextNode(aStates[i], root, false);
	}
   
   //render tree with these toplevel nodes; all descendants of these nodes
   //will be generated as needed by the dynamic loader.
   this.tree.draw();
	}

	this.loadNodeData = function(node, fnLoadComplete)  {
	
	//We'll create child nodes based on what we get back when we
	//use Connection Manager to pass the text label of the 
	//expanding node to the Yahoo!
	//Search "related suggestions" API.  Here, we're at the 
	//first part of the request -- we'll make the request to the
	//server.  In our Connection Manager success handler, we'll build our new children
	//and then return fnLoadComplete back to the tree.
	
	//Get the node's label and urlencode it; this is the word/s
	//on which we'll search for related words:
	var nodeLabel = "";
	var tlArray = new Array();
	var n = node;
	while( n.parent.label ){
		tlArray.push( n.parent.label ); 
		n = n.parent;
	}
	for( i = (tlArray.length-1); i >= 0; i-- ){
		nodeLabel += "/"+tlArray[i];
	} 
	nodeLabel += "/"+encodeURI(node.label);
	
	
	//prepare URL for XHR request:
	var sUrl = "http://"+document.domain+"/cgi-bin/tools/directoryListing?dir=casil" + nodeLabel;
	
	//prepare our callback object
	var callback = {
	
		//if our XHR call is successful, we want to make use
		//of the returned data and create child nodes.
		success: function(oResponse) {
			YAHOO.log("XHR transaction was successful.", "info", "example");
			var oResults = eval("(" + oResponse.responseText + ")");
			var tlArray = new Array();
			var n = this.argument.node;
			var currentDir = "/casil/"+n.label;
			while( n.parent.label ){
				tlArray.push( n.parent.label ); 
				n = n.parent;
			}
			for( i = (tlArray.length-1); i >= 0; i-- ){
				currentDir += "/"+tlArray[i];
			} 
			if((oResults.files) && (oResults.files.length)) {
				if(YAHOO.lang.isArray(oResults.files)) {
					for (var i=0, j=oResults.files.length; i<j; i++) {
						if( oResults.files[i].name != ".." ){
							var tempNode = new YAHOO.widget.TextNode(oResults.files[i].name, node, false);
							if( !parseInt(oResults.files[i].dir[0]) ){
								// this is a file... figure out how to display as such
								tempNode.href = "http://"+document.domain+"/"+oResults.directory+"/"+oResults.files[i].name;
								tempNode.dynamicLoadComplete = true;
								tempNode.iconMode = 1;
							} else {
								tempNode.href = "http://"+document.domain+"/"+oResults.directory+"/"+oResults.files[i].name;
							}
						}
					}
				} 
			}
								
			//When we're done creating child nodes, we execute the node's
			//loadComplete callback method which comes in via the argument
			//in the response object (we could also access it at node.loadComplete,
			//if necessary):
			oResponse.argument.fnLoadComplete();
		},
		
		//if our XHR call is not successful, we want to
		//fire the TreeView callback and let the Tree
		//proceed with its business.
		failure: function(oResponse) {
			YAHOO.log("Failed to process XHR transaction.", "info", "example");
			oResponse.argument.fnLoadComplete();
		},
		
		//our handlers for the XHR response will need the same
		//argument information we got to loadNodeData, so
		//we'll pass those along:
		argument: {
			"node": node,
			"fnLoadComplete": fnLoadComplete
		},
		
		//timeout -- if more than 7 seconds go by, we'll abort
		//the transaction and assume there are no children:
		timeout: 7000
	};
	
	//With our callback object ready, it's now time to 
	//make our XHR call using Connection Manager's
	//asyncRequest method:
	YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}

}
