function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

// Courtesy of http://www.quirksmode.org/dom/getElementsByTagNames.html
function getElementsByTagNames(list,obj)
{
	if (!obj) var obj = document;
	
	var tagNames = list.split(',');
	var resultArray = new Array();
	
	for (var i=0;i<tagNames.length;i++) {
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++) {
			resultArray.push(tags[j]);
		}
	}
	
	var testNode = resultArray[0];
	if (!testNode) return [];
	if (testNode.sourceIndex) {
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition) {
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
				
	return resultArray;
}

function getElementsByClassName(elementArray, NameofClass)
{
	var matchedArray = new Array();
	
	for (var i = 0; i < elementArray.length; i++)
	{
 		var pattern = new RegExp("(^| )" + NameofClass + "( |$)");

		if (pattern.test(elementArray[i].className))
		{
			matchedArray[matchedArray.length] = elementArray[i];
		}
      }
      return matchedArray;
 }

//Create alternate row styles on correct table types
function highlightAlternateRows()
{
	var tables = document.getElementsByTagName('table');
	var contentTables = getElementsByClassName(tables, 'contentTable');
	
	for(var i = 0; i < contentTables.length; i++)
	{
		//get child nodes of type TBODY - but only the first so we don't affect tables within the one we want to update
		var TBodyNode = contentTables[i].getElementsByTagName('TBODY')[0];
	
		//get child nodes of TBODY that are TRs
		var TBodyChildren = TBodyNode.childNodes;
		var TRs = new Array();
		
		//get an array of all TRs
		for (var m = 0; m < TBodyChildren.length; m++)
		{
			var isHeader = false;
			
			if (TBodyChildren[m].nodeName == 'TR')
			{	
				var cells = TBodyChildren[m].childNodes;
								
				for (var cell = 1; cell < cells.length; cell++) //start at one so we keep row highlighting for tables with vertical headers
				{
					//var thClassName = /\b\s?th\b/; //regex pattern for classname of th
					if (cells[cell].nodeName == 'TH') /*|| (thClassName.test(cells[cell].className )) )*/
						 isHeader = true;
				}
				
				if (isHeader == false)
					TRs[TRs.length] = TBodyChildren[m];
			}
		}
			
		for(var j = 0; j < TRs.length; j++)
		{
			if ( j % 2 != 0) //even row
				TRs[j].className = " altRow"; //leave a space so we don't interfere with existing classes
		}	
	}
}



/* 
Simple JQuery menu.
HTML structure to use:

Notes: 

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
  <li><a href="#">Sub menu heading</a>
	<ul>
	  <li><a href="http://site.com/">Link</a></li>
	  <li><a href="http://site.com/">Link</a></li>
	  <li><a href="http://site.com/">Link</a></li>
	...
	...
	</ul>
  <li><a href="#">Sub menu heading</a>
	<ul>
	  <li><a href="http://site.com/">Link</a></li>
	  <li><a href="http://site.com/">Link</a></li>
	  <li><a href="http://site.com/">Link</a></li>
	...
	...
	</ul>
</ul>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

function initMenus() {
	$('ul.menu .accordionContainer').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst .accordionContainer:first').slideDown();
	});
	$('ul.menu .accordionContainer a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('.accordionContainer')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' .accordionContainer:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('.accordionContainer')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' .accordionContainer:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}
//$(document).ready(function() {initMenus();});
