function setClass(element, className) {
  element.className = className;
}

function doSwitchTab(msgNum) {
  var thisId = this.id;
  var parentNode = this.parentNode;
  var parentId = parentNode.getAttribute("id");
  var msgNum = parentId.substring(parentId.indexOf("tab") + 3);
  switchTab(1, msgNum);
  return false;
}

function doBlur() {
  this.blur();
}

function setStatusMessage() {
  // only change the status text if this
  // tab is not the active one
  var thisId = this.id;
  var parentNode = this.parentNode;
  var parentId = parentNode.getAttribute("id");
  var msgNum = parentId.substring(parentId.indexOf("tab") + 3);
  //alert("msgNum = " + msgNum);
  
  // get the tab label text, for the status message
  var tabEl = document.getElementById("ad1tab" + msgNum);
  var children = tabEl.childNodes;
  var node;
  var textNode;
  var labelText;
  for (var j = 0; j < children.length; j++) {
    node = children[j];
    if (node.nodeType == 1 /* Node.ELEMENT_NODE */) {
      if (node.nodeName.toLowerCase() == "a") {  
        textNode = node.firstChild;
        labelText = textNode.nodeValue;
      }
    }
  }        
        
  if (!isActiveTab(msgNum)) {
    window.status = "Switch to '" + labelText + "' tab";
  }
  else {
    window.status = "";
  }
  return true;
}

function resetStatusMessage() {
  window.status = window.defaultStatus;
  return true;
}

function isActiveTab(msgNum) {
  // return true if the tab (message) is
  // the currently active one
  var result = true;
  if (document.getElementById("ad1msg" + msgNum).style.display == "none") {
    result = false;
  }  
  return result;
}

function switchTab(adNum, msgNum) {

  // kill the timer, so automatic switching stops if the user clicked the tab
  clearTimeout(adTimeoutId);
  
  msgNum = msgNum - 0; // subtract zero for explicit number conversion
  var adName = "ad" + adNum;

  // get the new current tab's <li> element
  var element = document.getElementById(adName + "tab" + msgNum);
  
  // set the currently active tab to 'current' (not clickable)
  setClass(element, "current");
  //element.className = "current";
  
  // set the previous and next tabs to 'not current' (clickable)
  var prev = element;
  while (prev.previousSibling != null) {
    prev = prev.previousSibling;
    setClass(prev, "");
  }
  var next = element;
  while (next.nextSibling != null) {
    next = next.nextSibling;
    setClass(next, "");
  }
  
  // switch to the proper message
  var msg = adName + "msg" + msgNum;
  var msgElement = document.getElementById(msg);
  
  // turn any siblings off  
  var prevNum = msgNum - 1;
  while (prevNum > 0) {
    document.getElementById(adName + "msg" + prevNum).style.display = "none";
    prevNum--;
  }
  var nextNum = msgNum + 1;
  while (document.getElementById(adName + "msg" + nextNum)) {
    document.getElementById(adName + "msg" + nextNum).style.display = "none";
    nextNum++;
  }    
  
  // turn on the element
  msgElement.style.display = "block";
 
}

function getNumberOfTabs() {
  var numberOfTabs = 0;
  var i = 1;
  while ( (document.getElementById("ad1msg" + i)) && (numberOfTabs < MAX_TABS) ) {
    numberOfTabs++;
    i++;
  }
  return numberOfTabs;
}

// globals
var MAX_TABS = 4;
var DELAY_MS = 15000;

var curAd = 1;
var low = 1;
// set during init
var high;
var curTab;

var adTimeoutId;
  
function initAd(type) {
  high = getNumberOfTabs();

  // assign event handlers
  var tabEl;
  var children;
  var linkEl;
  var node;

  for (var i = 1; i <= high; i++) {
    tabEl = document.getElementById("ad1tab" + i);
    children = tabEl.childNodes;
    for (var j = 0; j < children.length; j++) {
      node = children[j];
      if (node.nodeType == 1 /* Node.ELEMENT_NODE */) {
        if (node.nodeName.toLowerCase() == "a") {
          node.onclick = doSwitchTab;
          node.onfocus = doBlur;
          node.onmouseover = setStatusMessage;
          node.onmouseout = resetStatusMessage;
        }
      }
    }   
  }

  high = getNumberOfTabs();

  // if type is not used when calling initAd then type will be
  // undefined and thus the initial order of the adds do not matter
  // otherwise set type to 1 to have the ads start on the first ad
  if(!type)
    curTab = Math.floor(Math.random() * (high - low + 1)) + low;
  else if(type = 1)
    curTab = low;

  // switch to the initial ad
  switchTab(curAd, curTab);
  // set timer for next tab
  nextAd();
}
  
// switch tabs based on timer
function nextAd() {
  curTab++;
  if (curTab > high) {
    curTab = low;
  }
  adTimeoutId = setTimeout("switchTab(" + curAd + "," + curTab + "); nextAd()", DELAY_MS);
}