// JavaScript Document

var c8m_link = "_link";
var c8m_class_name = "current";
var c8m_displayed_tab = "";
var c8m_link_count = 0;
var c8m_tab_links = [ ];
var c8m_interval_id = 0;
var c8m_delay = 15000; // delay in milliseconds

(function() {
  if (window.addEventListener) {
    window.addEventListener("load", c8m_setup_tabs, false);    
  } else {
    window.attachEvent("onload", c8m_setup_tabs);
  }
})();

function c8m_display_next_tab(){
	if (c8m_displayed_tab != null) {
		// alert("c8m_displayed_tab is not null");
		// we're displaying something other than the first item
		var ix = c8m_tab_links.indexOf(c8m_displayed_tab.id);
		if (ix < 0) ix = 0;
		ix++;
		// alert("ix = " + ix);
		if (ix < c8m_tab_links.length){
			// alert("calling: c8m_display_tab(" + c8m_tab_links[ix] + ")"); 
			c8m_display_tab(c8m_tab_links[ix]);			
		} else {
			// alert("calling: c8m_display_tab(" + c8m_tab_links[0] + ")"); 
			c8m_display_tab(c8m_tab_links[0]);		
		}
	} else {
		// we're displaying the first item, show the second and set the c8m_displayed_tab value
		alert("c8m_displayed_tab is null");
		c8m_display_tab(c8m_tab_links[1]);		
	}
	
	
}


function c8m_setup_tabs() {
  var tab_links = document.getElementById("c8m_tabs").getElementsByTagName("a");
  c8m_link_count = tab_links.length;
  
  for (var i = 0; i < tab_links.length; i++) {    
    c8m_setup_tab_link(tab_links[i]);
  }
  c8m_interval_id = setInterval("c8m_display_next_tab()", c8m_delay);
  // c8m_display_next_tab();
}

function c8m_setup_tab_link(link) {  
  var num = link.id.lastIndexOf("_");
  var link_id = link.id.substring(0, num);	
  c8m_tab_links.push(link_id);
  link.onclick = function(){c8m_display_tab(link_id);};
}

function c8m_hide_all_tabs() {
  var tab_divs = document.getElementsByTagName("div");

  //Only need those div elements that have an id value starting with 'c8m_tab'
  for (var i = 0; i < tab_divs.length; i++) {
    if (tab_divs[i].id.indexOf("c8m_tab") === 0) {
      c8m_change_tab_display(tab_divs[i], "none", tab_divs[i].id, "");
    }  
  }  
}

function c8m_display_tab(id) {	 
	// alert("c8m_display_tab was called with value: '" + id + "'");
  // check to see if we have a previous value for the displayed tab variable
  if (!c8m_displayed_tab) {	    
  	// nope... we didn't so all tabs should be hidden
    c8m_hide_all_tabs();
  } else {
	// we did have a value
    c8m_change_tab_display(c8m_displayed_tab, "none", c8m_displayed_tab.id, "");
  }

  // Update our currently displayed tab variable
  c8m_displayed_tab = document.getElementById(id);
  
  // turn on the currently displayed tab and assign the "current" class value to it's style
  c8m_change_tab_display(c8m_displayed_tab, "block", id, c8m_class_name);
}


// turns on the display of the tab and assigns a new class name to it
function c8m_change_tab_display(tab_content_element, display, tab_link_id, class_name) {

  //Make the selected tab visible or not depending on the input variable
  tab_content_element.style.display = display;
  
  //Change the class name of the tab link
  document.getElementById(tab_link_id + c8m_link).className = class_name;
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

