//	gnd-core.js
//	Code arranged by T.K.Egan - 15 Mar 2006
//	License: Public Domain
//		Use as you will but I'd appreciate an
//		email at tkegan@greenneondesign.com if 
//		you find a way to improve the code

//	addLoadEvent shamelessly copied from Simon Willison's weblog
//		(http://simon.incutio.com/archive/2004/05/26/addLoadEvent)
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/**
 * Work-around the EOLAS lawsuit changes in IE by document.write'ing
 * an object tag at load time.
 *
 * @param src - the movie file to play either relative to the html file
 * 					using this code or absolute path
 * @param height - the height of the movie in px note 16px are added to accomidate the controller
 * @param width - the width of the movie in px
 * @param autoplay - the string "true" or "false"
 */
function embedQuicktimeMovie(src, height, width, autoplay) {
	document.writeln("<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\" width=\"" + width + "\" height=\"" + height + "\">");
	document.writeln("\t<param name=\"src\" value=\"" + src + "\" />");
	document.writeln("\t<param name=\"controller\" value=\"true\" />");
	document.writeln("\t<param name=\"autoplay\" value=\"" + autoplay + "\" />");
	document.writeln("\t<!--[if !IE]>-->");
	document.writeln("\t<object type=\"video/mp4\" data=\"" + src + "\" width=\"" + width + "\" height=\"" + (height + 16) + "\">");
	document.writeln("\t\t<param name=\"autoplay\" value=\"" + autoplay + "\" />");
	document.writeln("\t\t<param name=\"controller\" value=\"true\" />");
	document.writeln("\t</object>");
	document.writeln("\t<!--<![endif]-->");
	document.writeln("</object>");
}

/**
 *	Finds all elements that are decesdants of the node with id:=root and of
 *	type:=tag with the given classname
 */
function getElementsByClassName(rootContainer, tag, elClass) {
	tag = tag || '*';
	var elementContainers = rootContainer.getElementsByTagName(tag);
	if(!elementContainers.length && (tag == '*' && root.all) ) {
		elementContainers = rootContainer.all; // IE < 6
	}
	
	var nodes = new Array();
	var re = new RegExp('(?:^|\\s+)' + elClass + '(?:\\s+|$)');

	for(var i = 0; i < elementContainers.length; i++) {
		element = elementContainers[i];
		if(re.test(element['className']))
			nodes[nodes.length] = element;
	}

	return nodes;
}

/**
 *	Sets the height of all elements that are decesdants of the node with id:=root
 *	and decesdants of elements with type:=parentType and class:=elClass and are of
 *	type:=type to the same height with a minimum set by minHeight
 */
function fixHeights(root, parentType, elClass, type, minHeight) {
	var maxHeight = minHeight;
	var elementCache = new Array();
	var elementContainers;
	var rootContainer = root != null ? document.getElementById(root) : document;

	if(parentType != null && elClass != null) {
		elementContainers = getElementsByClassName(rootContainer, parentType, elClass);
	} else {
		elementContainers = new Array();
		elementContainers[0] = rootContainer;
	}

	// and iterate through them to find max height
	for(var h = 0; h < elementContainers.length; h++) {
		var elements = elementContainers[h].getElementsByTagName(type);	// form list to search
		for (var i = 0; i < elements.length; i++) {	// iterate over the list
			if(maxHeight < elements[i].offsetHeight) {
				maxHeight = elements[i].offsetHeight;
			}
			elementCache[elementCache.length] = elements[i];
		}
	}
	for (var i = 0; i < elementCache.length; i++) {
		elementCache[i].style.height = maxHeight + 'px';
	}
}
