
jQuery.fn.initBogoFolders = function(opts,rootFolder) {
	var self = this;
	opts = jQuery.extend({
			//icon:'folder.png',
			separator:'',
			debuggering:false,
			parentDirLabel:'..'
	}, opts ? opts : {});

	var dbgdiv = null;
	var startingDir = rootFolder;
	var target = $(opts.target);
	/** Internal debuggering function. */
	function dbg(msg) { if( dbgdiv ) dbgdiv.prepend("BogoFolders: "+msg+"<br/>"); };
	if( opts.debuggering ) {
		target.after("<div id='BogoFoldersDebugDiv'>BogoFolders debugging area<br/></div>").after('<hr/>');
		dbgdiv = jQuery('#BogoFoldersDebugDiv');
		dbgdiv.css('border','1px dashed #000');
		dbg("debugging activated.");
	}

	/* i hate this. Walk the folders and add parent information so 'cd ..' can work. */
	function tagFolders(theFolder,parentFolder) {
		dbg("Tagging folder named \""+theFolder.name+"\"");
		theFolder.parentFolder = parentFolder;
		if( theFolder.selected ) {
			startingDir = theFolder;
		}
		if( ! theFolder.children ) {
			// looks like it has no walkable children.
			return;
		}
		for( ndx in theFolder.children ) {
			tagFolders( theFolder.children[ndx], theFolder );
		}
	};
	tagFolders(rootFolder,null);

	function showContent(theFolder) {
		target.empty();
		var ctype = theFolder.contentType ? theFolder.contentType : 'html';
		var content = theFolder.content ? theFolder.content : '';
		dbg('typeof content == '+typeof(content)); // weird: typeof(null) == 'object'
		if( 'function' == typeof(content) ) {
			dbg("Calling .content function.");
			content = content();
		}
		if( 'object' == typeof(content) ) {
			ctype = 'object';
		}
		switch( ctype ) {
			case 'object':
				target.append( content );
				break;
			case 'html':
				target.html( content );
				break;
			case 'text':
			case 'txt':
				target.text( content );
				break;
			default:
				target.html("Error: theFolder.contentType could not be determined");
				break;
		};
	}; // showContent()

	var addFolderEntry = function(theFolder) {
		var label = theFolder.name ? theFolder.name.replace( /\'/, '&apos;' ) : null;
		if( theFolder.chdirProxy ) { // kludge for chdir()
			var tmp = theFolder.chdirProxy;
			delete theFolder.chdirProxy;
			theFolder = tmp;
		}
		var icon = (theFolder.icon || (theFolder.icon===null)) ? theFolder.icon : opts.icon;
		var anchor = jQuery("<a href='#'/>");
		anchor[0].folderObj = theFolder;
		theFolder.anchor = anchor; // i don't like this. It's only here to support startingDir.
		if( icon ) {
			anchor.append("<img src='"+icon+"' alt='"+(label ? label : '')+"'/>");
		}
		if( label ) {
			anchor.append( label );
		}
		anchor.click(function(){
				showContent( this.folderObj );
				if( this.folderObj.children ) chdir( this.folderObj );
				return false;
		});
		self.append( anchor );
		return self;
	};

	var chdir = function(theFolder) {
		dbg('chdir(<code>'+(theFolder.name?theFolder.name:theFolder.toSource())+"</code>)");
		self.empty();
		if( theFolder.parentFolder ) {
			var cdup = {name:opts.parentDirLabel,chdirProxy:theFolder.parentFolder};
			//dbg("Adding '..' entry <code>"+cdup.toSource()+"</code>");
			addFolderEntry(cdup);
		}
		if( ! theFolder.children ) {
			return;
		}
		var count = theFolder.children.length;
		if( count && theFolder.parentFolder ) {
			self.append(opts.separator);
		}
		for( var i = 0; i < count; ++i ) {
			addFolderEntry(theFolder.children[i]);
			if( (i!=(count-1)) && opts.separator ) {
				self.append(opts.separator);
			}
		}
	};
	chdir( rootFolder );
	// i would like to do the following instead, but the problem is that the root node
	// is special case (blech!) and doesn't get an associated anchor.
	//if( startingDir ) {
	//	startingDir.anchor.click();
	//}
	showContent( rootFolder );
	return self;

}; // initBogoFolders()
