 
/***************************************************************/
/*********************** Common Functions **********************/
/***************************************************************/
/**
 * Shortcut for document.getElmementById()
 */
var $tb = function(id) {
  return document.getElementById(id);
};

/**
 * Returns all elements of type 'nodeType' with class 'className'.
 */
var $$tb = function(nodeType, className) {
  var ret = new Array();
  var arr = document.getElementsByTagName(nodeType.toUpperCase());
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].className) {
	  if (arr[i].className.split(" ").contains(className)) {
	    ret.push(arr[i]);
	  }
	}
  }
  return ret;
};

Array.prototype.contains = function (element) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == element) {
      return true;
    }
  }
  return false;
};

/**
 * Checks if on the home page.
 */
function isHomePage() {
  var loc = window.location.toString();
  return loc.indexOf("acatalog") == -1;
}

/**
 * Checks for IE
 */
function isIE() {
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}


/***************************************************************/
/******** Fully selectable section link functionality **********/
/***************************************************************/
function initClickableSections() {
  var sections = $$tb('DIV', 'tec-section');
  for (var i = 0; i < sections.length; i++) {
    stripWhitespace(sections[i]);
	
	sections[i].onmouseover = function() {
	  this.className = "tec-section ts-hov";
	};
	sections[i].onmouseout = function() {
	  this.className = "tec-section";
	};
	
	var link = searchForLink(sections[i]);
	if (link != null) {
	  sections[i].setAttribute("rel", link);
	  sections[i].onclick = function() {
	    this.className = "tec-section ts-hov";
	    window.location = this.getAttribute("rel");
	  };
	}
  }
}

/**
 * Removes excess whitespace from a node
 */
var notWhitespace = /\S/;
function stripWhitespace(node) {
  for (var i = 0; i < node.childNodes.length; i++) {
    var childNode = node.childNodes[i];
    if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue))) {
      node.removeChild(node.childNodes[i]);
      i--;
    }
  }
}

/**
 * Searches a node for a link element and returns the href value if found.
 */
function searchForLink(node) {
  var links = node.getElementsByTagName('A');
  if (links.length > 0) {
    return links[0].href;
  }
  return null;
}

if (window.attachEvent) { 
  window.attachEvent("onload", initClickableSections); 
} 
else {  
  window.addEventListener("load", initClickableSections, false); 
}


/***************************************************************/
/**************** Image swapping functionality *****************/
/***************************************************************/
function swapImg(newImg) {
  var mimg = $tb('main-img');
  if (mimg) {
    mimg.src = newImg;
  }
}



/***************************************************************/
/**************************** Height Fix ***********************/
/***************************************************************/
function heightFix() {
  var toFix = $$tb('DIV', 'fh');
  var theHighest = 0;
  for (var i = 0; i < toFix.length; i++) {
    if (toFix[i].offsetHeight > theHighest) theHighest = toFix[i].offsetHeight;
  }
  for (var i = 0; i < toFix.length; i++) {
    toFix[i].style.height = theHighest + "px";
  }
}

if (window.attachEvent) { 
  window.attachEvent("onload", heightFix); 
} 
else {  
  window.addEventListener("load", heightFix, false); 
}

function searchResultsHeightFix() {
  var toFix = $$tb('DIV', 'teclan-search-result');
  var theHighest = 0;
  for (var i = 0; i < toFix.length; i++) {
    if (toFix[i].offsetHeight > theHighest) theHighest = toFix[i].offsetHeight;
  }
  for (var i = 0; i < toFix.length; i++) {
    toFix[i].style.height = theHighest + "px";
  }
}
