
function SetupTabbedFrames () {

  function getElementsByClassName (root, clsname) {
    var nodeList = new Array();
    var clsstr, nodes = root.getElementsByTagName('*');
    for (i = 0; i < nodes.length; i++) {
      if (nodes[i].className != '') {
        clsstr = nodes[i].className;
        clsstr = clsstr.match(/[A-Za-z0-9_\-]+/g);
        for (j = 0; j < clsstr.length; j++)
          if (clsname == clsstr[j])
            nodeList.push(nodes[i]);
      }
    }
    return nodeList;
  }

  function setClassName (el, clsname) {
    if (el.className != '') {
      var flag = true;
      var clsstr = el.className;
      clsstr = clsstr.match(/[a-zA-Z0-9_\-]+/g);
      for (var i = 0; i < clsstr.length; i++)
        if (clsstr[i] == clsname)
          flag = false;
      if (flag) {
        clsstr.push(clsname);
        el.className = clsstr.join(' ');
      }
    }
    else {
      el.className = clsname;
    }
  }

  function unsetClassName (el, clsname) {
    if (el.className != '') {
      var clsstr = el.className;
      clsstr = clsstr.match(/[a-zA-Z0-9_\-]+/g);
      for (var i = 0; i < clsstr.length; i++)
        if (clsstr[i] == clsname)
          clsstr.splice(i, 1);
      el.className = clsstr.join(' ');
    }
  }

  function setupTabbedFrame (el) {
    
    var current = 0;
    
    function mousedown () {
      
      if (this.index == current)
        return;
      
      contents[current].style.display = 'none';
      unsetClassName(selectors[current], 'selected');
      
      current = this.index;
      
      contents[current].style.display = 'block';
      setClassName(selectors[current], 'selected');
      
    }
    
    var contents = getElementsByClassName(el,'tf-content');
    var selectors = getElementsByClassName(el,'tf-selector');
    var i, length = selectors.length;
    
    if (length < 1 || length != contents.length)
      return;
    
    contents[0].style.display = 'block';
    setClassName(selectors[0], 'selected');
    selectors[0].onmousedown = mousedown;
    selectors[0].index = 0;
    
    for (i = 1; i < length; i++) {
      contents[i].style.display = 'none';
      unsetClassName(selectors[i], 'selected');
      selectors[i].onmousedown = mousedown;
      selectors[i].index = i;
    }
    
  }

  var i, tflist = getElementsByClassName(document.body, 'tabbed-frame');
  
  //window.alert(tflist.length);
  //return;
  
  for (i = 0; i < tflist.length; i++) {
    tflist[i] = setupTabbedFrame(tflist[i]);
  }
  
}
