var nav_2_id = 0;
var nav_3_id = 0;

function ajax_load_nav_bar (url, into_node_id, from_node) {
  var into_node = document.getElementById (into_node_id);
  if (into_node == null) return;
  
  // set our on-state
  if (from_node != null) {
    clear_on_children (from_node.parentNode.parentNode);
    from_node.parentNode.className = 'current';
  }
  
  if (into_node_id == 'nav-third') {
    // clear the fourth level
    var node = document.getElementById ('nav-fourth');
    while (node.firstChild) {
      node.removeChild(node.firstChild);
    }
    
    // and call the AJAX
    var handler = new NavigationHandler (into_node, true);
    
    
  } else {
    // just call the AJAX
    var handler = new NavigationHandler (into_node, false);
  }
  
  queue.request ('GET', url, handler, null);
}

function NavigationHandler (into_node, use_onclick) {
  this.into_node = into_node;
  this.use_onclick = use_onclick;
  
  /**
  * Processes the returned XML DOM nodes
  **/
  this.process = function (top_node) {
    while (this.into_node.firstChild) {
      this.into_node.removeChild(this.into_node.firstChild);
    }
    
    var ul_node = create_element ('ul', {});
    this.into_node.appendChild (ul_node);
    
    var nodes = top_node.getElementsByTagName ('item');
    
    if (nodes.length == 0) {
      this.error ('Nothing...');
      
    } else {
      for (var x = 0; x < nodes.length; x++) {
        var url = nodes[x].getAttribute ('url');
        
        var li_node = create_element ('li', {});
        ul_node.appendChild (li_node);
        
        var a_node = create_element ('a', {'href':url});
        li_node.appendChild (a_node);
        
        a_node.appendChild (document.createTextNode (nodes[x].firstChild.nodeValue));
        
        if (this.use_onclick) {
          url += '&output=xml';
          this._setOnclick (a_node, url);
          
          if ((nav_3_id == nodes[x].getAttribute ('id')) && (nav_2_id == top_node.getAttribute ('id'))) {
            li_node.className = 'current';
          }
        }
      }
    }
  }
  
  // wrapper to make it work properly
  this._setOnclick = function (node, url) {
    node.onclick = function () {
      ajax_load_nav_bar (url, 'nav-fourth', node);
      return false;
    }
  }
  
  /**
  * Processes an error. Optional. If omitted, errors are outputted as an alert
  **/
  this.error = function (message) {
    while (this.into_node.firstChild) {
      this.into_node.removeChild(this.into_node.firstChild);
    }
    
    var node = create_element ('p', {});
    node.appendChild (document.createTextNode (message));
    this.into_node.appendChild (node);
  }
  
  /**
  * Is fired when a request is put onto the queue. Optional
  **/
  this.onQueue = function () {
    while (this.into_node.firstChild) {
      this.into_node.removeChild(this.into_node.firstChild);
    }
    
    var node = create_element ('p', {});
    node.appendChild (document.createTextNode ('Loading...'));
    this.into_node.appendChild (node);
  }
}



/**
* Clears onstate for any children LI nodes
**/
function clear_on_children (node) {
  var nodes = node.getElementsByTagName ('li');
  
  for (var x = 0; x < nodes.length; x++) {
    nodes[x].className = '';
  }
}




/**
* Detect IE
**/
var is_msie = false;
if (navigator.userAgent.match ('MSIE') !== null) is_msie = true;


///
/// Functions to make IE play nice with DOM hacking
///

/**
* Create an element
**/
function create_element (tag_name, attribs) {
  
  var element = null;
  
  tag_name = String (tag_name);
  tag_name = tag_name.toLowerCase ();
  
  // IE is broken with many elements but its non-W3C methods seem to work
  if (is_msie) {
    var tag_text;
    tag_text = '<' + tag_name;
    for (var key in attribs) {
      tag_text += ' ' + String (key) + '="' + String (attribs[key]) + '"';
    }
    tag_text += '>';
    element = document.createElement (tag_text);
  } else {
    element = document.createElement (tag_name);
    for (var key in attribs) {
      element.setAttribute (String (key), String (attribs[key]));
    }
  }
  return element;
  
}
