
jQuery.fn.initBogoFolders=function(opts,rootFolder){if(!opts){opts=[];}
var self=this;self.opts=jQuery.extend({separator:'',debugBogoFolders:false},opts);self.dbgdiv=null;self.startingDir=rootFolder;self.target=$(opts.target);function dbg(msg){if(self.dbgdiv)self.dbgdiv.prepend("BogoFolders: "+msg+"<br/>");};if(self.opts.debugBogoFolders){$(self.opts.target).after("<div id='BogoFoldersDebugDiv'>BogoFolders debugging area<br/></div>").after('<hr/>');self.dbgdiv=jQuery('#BogoFoldersDebugDiv');self.dbgdiv.css('border','1px dashed #000');dbg("debugging activated.");}
function tagFolders(theFolder,parentFolder){dbg("Tagging folder named \""+theFolder.name+"\"");theFolder.parentFolder=parentFolder;if(theFolder.selected){self.startingDir=theFolder;}
if(!theFolder.children){return;}
for(ndx in theFolder.children){tagFolders(theFolder.children[ndx],theFolder);}};tagFolders(rootFolder,null);function showContent(theFolder){self.target.empty();var ctype=theFolder.contentType?theFolder.contentType:'html';var content=theFolder.content?theFolder.content:'';dbg('typeof content == '+typeof(content));if('function'==typeof(content)){dbg("Calling .content function.");content=content();}
if('object'==typeof(content)){ctype='object';}
switch(ctype){case'object':self.target.append(content);break;case'html':self.target.html(content);break;case'text':case'txt':self.target.text(content);break;default:self.target.html("Error: theFolder.contentType could not be determined");break;};};self.addFolderEntry=function(theFolder){var label=theFolder.name?theFolder.name.replace(/\'/,'&apos;'):null;if(theFolder.chdirProxy){var tmp=theFolder.chdirProxy;delete theFolder.chdirProxy;theFolder=tmp;}
var icon=(theFolder.icon||(theFolder.icon===null))?theFolder.icon:self.opts.icon;var anchor=jQuery("<a href='#'/>");anchor[0].folderObj=theFolder;theFolder.anchor=anchor;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)self.chdir(this.folderObj);return false;});self.append(anchor);return self;};self.chdir=function(theFolder){dbg('chdir(<code>'+(theFolder.name?theFolder.name:theFolder.toSource())+"</code>)");self.empty();if(theFolder.parentFolder){var cdup={name:'..',chdirProxy:theFolder.parentFolder};self.addFolderEntry(cdup);}
if(!theFolder.children){return;}
var count=theFolder.children.length;for(var i=0;i<count;++i){self.addFolderEntry(theFolder.children[i]);if((i!=(count-1))&&self.opts.separator){self.append(self.opts.separator);}}};self.chdir(rootFolder);showContent(rootFolder);return self;};