/**
 * Attaches an onclick event to each link with rel="external" that opens the
 * link in a new window.
 */
function initExternalLinks() {
  var as = document.getElementsByTagName("a");
  for (var i = 0; i < as.length; i++) {
    if (as[i].getAttribute("rel") == "external") {
      as[i].onclick = openNewWindow;
    }
  }

  function openNewWindow() {
    window.open(this.href);
    return false;
  }
}

function swapNewsTab(trigger) {
  var elm = $('tabbed-panel');

  if (trigger == 'trigger_blog') {
    elm.className = 'show-blog';
  } else {
    elm.className = 'show-news';
  }

  return false;
}

function showRiskForm(elm) {
  el = $(elm);

  if (el.className.indexOf('show-risk-form') != -1) {
    el.className = el.className.replace(/show-risk-form/, 'hide-risk-form');
  } else {
    el.className = el.className.replace(/hide-risk-form/, 'show-risk-form');
  }

  return false;
}

function showForm(elm) {
  el = $(elm);

  if (el.className.indexOf('show-form') != -1) {
    el.className = el.className.replace(/show-form/, 'hide-form');
  } else {
    el.className = el.className.replace(/hide-form/, 'show-form');
  }

  return false;
}

function showEl(elm) {
  el = document.getElementById(elm);
  if (el.className == 'show-fields') {
    el.className = 'hide-fields';
  } else {
    el.className = 'show-fields';
  }
}

function prevImg() {
  if (lastIndex - 1 < 1) {
    showImg(adCount-1);
    lastIndex = adCount-1; //override last index
    return false;
  }
  return showImg(lastIndex-1);
}

function nextImg() {
  if (lastIndex + 1 >= adCount) {
    showImg(1);
    lastIndex = 1; //override last index
    return false;
  }
  return showImg(lastIndex+1);
}

function showImg(i) {
  $('promo_' + lastIndex).className = $('promo_' + lastIndex).className.replace(/ current/, '');
  $('promo_' + i).className += ' current';
  lastIndex = i;

  return false;
}

function lbAjaxDeactivator(elm) {
  if (elm == null) elm = document;

  var anchors = elm.getElementsByClassName('lbAction');

  for(i = 0; i < anchors.length; i++) {
    var lb = new lightbox(document.createElement('a'));
    Event.observe(anchors[i], 'click', lb[anchors[i].rel].bindAsEventListener(lb), false);
    anchors[i].onclick = function(){
      return false;
    };
  }
}

function styleTables() {
  var tbodies = document.getElementsByTagName("tbody");

  for (var ii = 0; ii < tbodies.length; ii++) {
    var rows = tbodies[ii].getElementsByTagName("tr");

    for (var jj = 0; jj < rows.length; jj++) {
      if (jj % 2 != 0) {
        rows[jj].className = "row-even";
      }

      // assign 'th-left' class to first column
      var cols = rows[jj].getElementsByTagName("td");

      // assign 'th-top' class to all columns in first row
      if (jj == 0) {
        for (kk = 0; kk < cols.length; kk++) {
          cols[kk].className += " th-top";
        }
      }

      // assign  'col-even' to every second column
      for (kk = 0; kk < cols.length; kk++) {
        if (kk % 2 != 0) {
          cols[kk].className += " col-even";
        }
      }
    }
  }

}

function lbFix() {
  if (!$('theImage').complete) {
    setTimeout("lbFix()", 100);
  }
  $('lightbox').style.width = $('theImage').width + 'px';
  $('lightbox').style.height = $('theImage').height + 'px';
  $('lightbox').style.marginLeft = '-' + ($('theImage').width / 2) + 'px';
  $('lightbox').style.marginTop = '-' + ($('theImage').height / 2) + 'px';
}

function switchClass(targetFld, newClass) {
  targetFld.className = newClass;  
  return false;
}