// Simple tab switcher
var Tabs = {
  start: function(element){
    if (!element) return false;
    this.switch_uls = $$('tabs', element, 'ul')
    this.activate_switches();
  },

  activate_switches : function(){
    if (!this.switch_uls) return false;
    for(i=0;i<this.switch_uls.length;i++){
      switches = this.switch_uls[i].getElementsByTagName('a');
      for (j=0;j<switches.length;j++){
        if (Element.hasClassName(switches[j], 'active')){ // currently selected
          this.active_switch = switches[j];
          this.active_tab = this.get_tab(this.active_switch);
        }
        switches[j].onclick = function(){
          Tabs.switch_to(this);
          return false;
        }
      }// end switches loop
    }// end switch_uls loop
  },

  get_tab : function(switch_element){
    pattern = /#([a-z][\w.:-]*)$/i
		matches = pattern.exec(switch_element.href);
    return $(matches[1]);
  },

  switch_to : function(switch_element){
    switch_tab = this.get_tab(switch_element);
		this.hide_tab(this.active_tab);
    this.deactivate_switch(this.active_switch);

    this.show_tab(switch_tab);
    this.activate_switch(switch_element);
  },

  hide_tab : function(element){
    element.style.display = 'none';
  },

  deactivate_switch : function(element){ 
    Element.removeClassName(element, 'active');
  },

  show_tab : function(element){
    element.style.display = 'block';
    this.active_tab = element;
  },

  activate_switch : function(element){
    Element.addClassName(element, 'active');
    this.active_switch = element;
  }
}

function $$(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function addLoadEvent(func){
	var oldonload = window.onload;
	
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;

    elements.push(element);
  }

  return elements;
}

var Element = {
  remove: function(element) {
    element = $(element);
    element.parentNode.removeChild(element);
  },

  hasClassName: function(element, className) {
    element = $(element);
    if (!element)
      return;
    var a = element.className.split(' ');
    for (var i = 0; i < a.length; i++) {
      if (a[i] == className)
        return true;
    }
    return false;
  },

  addClassName: function(element, className) {
    element = $(element);
    Element.removeClassName(element, className);
    element.className += ' ' + className;
  },
  
  removeClassName: function(element, className) {
    element = $(element);
    if (!element)
      return;
    var newClassName = '';
    var a = element.className.split(' ');
    for (var i = 0; i < a.length; i++) {
      if (a[i] != className) {
        if (i > 0)
          newClassName += ' ';
        newClassName += a[i];
      }
    }
    element.className = newClassName;
  },
  
  // removes whitespace-only text node children
  cleanWhitespace: function(element) {
    element = $(element);
    for (var i = 0; i < element.childNodes.length; i++) {
      var node = element.childNodes[i];
      if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
        Element.remove(node);
    }
  }
};