Ext.onReady(function(){
  // Configure the nav tree
    var tree = new Ext.tree.TreePanel({
        useArrows: true,
        animate: true,
        border: false,
        loader: new Ext.tree.TreeLoader({dataUrl: 'assets/javascript/nav.js',preloadChildren:true,requestMethod:"GET"}),
        root: new Ext.tree.AsyncTreeNode(),
        rootVisible: false,
        listeners: {
          click: function(node) {
            if(/^\//.test(node.id))
              if(Ext.query('base').length)
                window.location = Ext.query('base')[0].href+node.id.substring(1);
              else
                window.location = node.id;
          },
          append: function(tree, self, node, index) {
            // This needs to parse the current url and based on it
            // open up and select the approprite node in the tree
            var uri = getLocalURI();
            var bits = uri.split('/');
            for(var i=0;i<bits.length;i++) {
              if(!bits[i]) continue;
              var re = new RegExp("("+bits[i]+"$)|("+bits[i]+"/$)");
              if(re.test(node.id)) {
                if(!node.isLeaf())
                  setTimeout(function(){node.expand();},50);
                setTimeout(function(){node.select();},50+(i*10));
              }
            }
          }
        }
    });

    // Render the nav tree
    tree.render('nav');
    //tree.expandAll();
});

function getLocalURI() {
  if(Ext.query('base').length) {
    var re =  new RegExp(Ext.query('base')[0].href);
    if(re.test(window.location.href))
      return '/'+window.location.href.replace(re,'');
  }
  return window.location.pathname;
}
