/* ---( = begin global js functions )------------------------------- */


// window.onload work around

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// MattB's generic new window function
function openWin(URL, width, height) {
	window.open(URL, 'selWin' , 'width=' + width + ',height=' + height + ',toolbar=0,directories=0,menubar=0,status=yes,resizable=1,location=0,scrollbars=1,copyhistory=0');
}

// generic class-name creator (used for stripy tables)
function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}


// Ben's unobtrusive pop-up function

function prepareLinks() {
  var links = document.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    if (links[i].getAttribute("class") == "popup") {
      links[i].onclick = function() {
        popUp(this.getAttribute("href"));
        return false;
      }
    }
  }
}

function popUp(winURL) {
  window.open(winURL,"popup","width=725,height=550,toolbar=0,directories=0,menubar=0,status=yes,resizable=1,location=0,scrollbars=1,copyhistory=0");
}


// generic show-hide function 
function showSection(id) {
  var div = document.getElementById(id);
  if (div.style.display == "block") {
    div.style.display = "none";
  } else {
    div.style.display = "block";
  }
}

function showHide() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i<links.length; i++ ) {
    var sectionId = links[i].getAttribute("href").split("#")[1];
    if (!document.getElementById(sectionId)) continue;
    document.getElementById(sectionId).style.display = "none";
    links[i].destination = sectionId;
    links[i].onclick = function() {
      showSection(this.destination);
      return false;
    }
  }
}


// price table stripes and highlight
function priceTables () {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("container-1")) return false;
	var stripe = document.getElementById("container-1");
	var tables = stripe.getElementsByTagName("table");
	for (var i=0; i<tables.length; i++) {
	  var odd = false;
	  var rows = tables[i].getElementsByTagName("tr");
	  for (var j=0; j<rows.length; j++) {
	    if (odd == true) {
	      addClass(rows[j],"odd");
	      odd = false;
	    } else {
	      odd = true;
	    }
	  }
	}
}

function priceHighlightRows() {
  if(!document.getElementsByTagName) return false;
  if (!document.getElementById("container-1")) return false;
  var stripe = document.getElementById("container-1"); 
  var rows = stripe.getElementsByTagName("tr");
  for (var i=0; i<rows.length; i++) {
    rows[i].oldClassName = rows[i].className
    rows[i].onmouseover = function() {
      	addClass(this,"highlight");
		}
		rows[i].onmouseout = function() {
      	this.className = this.oldClassName
		}
	}
}

function stripyTables () {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("stripy")) return false;
	var stripe = document.getElementById("stripy");
	var tables = stripe.getElementsByTagName("table");
	for (var i=0; i<tables.length; i++) {
	  var odd = false;
	  var rows = tables[i].getElementsByTagName("tr");
	  for (var j=0; j<rows.length; j++) {
	    if (odd == true) {
	      addClass(rows[j],"odd");
	      odd = false;
	    } else {
	      odd = true;
	    }
	  }
	}
}

function highlightRows() {
  if(!document.getElementsByTagName) return false;
  if (!document.getElementById("stripy")) return false;
  var stripe = document.getElementById("stripy"); 
  var rows = stripe.getElementsByTagName("tr");
  for (var i=0; i<rows.length; i++) {
    rows[i].oldClassName = rows[i].className
    rows[i].onmouseover = function() {
      	addClass(this,"highlight");
		}
		rows[i].onmouseout = function() {
      	this.className = this.oldClassName
		}
	}
}

function rowClick () {
	if(!document.getElementsByTagName) return false;
	if (!document.getElementById("stripy")) return false;
	var stripe = document.getElementById("stripy");
	var rows = stripe.getElementsByTagName("tr");		
	for (var i=0; i<rows.length; i++) {
		var links = rows[i].getElementsByTagName("a");
		if (links[0] != null) {
			rows[i].linkHref = links[0].getAttribute("href");
			rows[i].onclick = function() {
				window.location.href = this.linkHref;
			}
		}	    
	}
}

addLoadEvent(prepareLinks);
addLoadEvent(priceTables);
addLoadEvent(priceHighlightRows);
addLoadEvent(stripyTables);
addLoadEvent(highlightRows);