
/* ---------- DYNAMIC LOADING (CSS, JS) */

// dynamically load CSS
function loadCSS(url) {
	var e = document.createElement("link")
	e.setAttribute('rel', 'stylesheet');
	e.setAttribute('type', 'text/css');
	e.setAttribute('href', url);
	document.getElementsByTagName("head")[0].appendChild(e);
}

// dynamically loading scripts is a bit tricker... we try to
// ensure that we don't load one if it's already been loaded

	var gScriptList;
	
	// takes a long URL and returns only the filename (no path)
	function fileName(url) {
		var lastSlash = url.lastIndexOf('/');
		return ( lastSlash != -1 ? url.substr(lastSlash+1) : url );
	}
	function listScripts() {
		var scr = document.getElementsByTagName("script"),
			i, ct = scr.length, s = "", src, lastSlash;
		
		for ( i = 0; i < ct; ++i) {
			src = scr[i].src;
			if ( src.length) {
				lastSlash = src.lastIndexOf('/');
				if ( lastSlash != -1) src = src.substr(lastSlash+1);
				gScriptList.push( src.toLowerCase());
			}
		}
	}

// this only works for scripts with unique filenames; won't work if
// you have two "common.js" files in different directories. also,
// if the JS file has a load event, it WILL get called!
//
function ensureScript(url) {
	var i,ct,fNm;
	if ( typeof gScriptList == 'undefined') {
		gScriptList = new Array;
		listScripts();
	}
	fNm = fileName(url);
	for( var i=0, ct=gScriptList.length; i < ct; ++i)
		if ( fNm == gScriptList[i]) return;

	// ok, add the JS
	var e = document.createElement("script");
	e.src = url;
	e.language='javascript';
	e.type="text/javascript";
	document.getElementsByTagName("head")[0].appendChild(e);

	gScriptList.push(fNm.toLowerCase());
}

/* ---------- DOM EXTENSIONS */

// if there is no Node obj, define one with these properties & values.
if (!window.Node) {
	var Node = {
		ELEMENT_NODE: 1,
		ATTRIBUTE_NODE: 2,
		TEXT_NODE: 3,
		COMMENT_NODE: 8,
		DOCUMENT_NODE: 9,
		DOCUMENT_FRAGMENT_NODE: 11
	}
}

// very useful companion to the built-in "getElementById"
//
document.getElementsByClass = function (needle) {
	function _GetElementsByClass(outArray, seed, needle) {
		while (seed) {
			if (seed.nodeType == Node.ELEMENT_NODE) {
				if ( classContains( seed, needle))
					outArray.push(seed);
				_GetElementsByClass(outArray, seed.firstChild, needle)
			}
			seed = seed.nextSibling;
		}
	}

	var outArray = new Array();
	_GetElementsByClass(outArray, document.documentElement, needle);
	return outArray;
}


/* ---------- ODDBALLS */

// netlobo.com/url_query_string_javascript.html
function getQueryParam( qp ) {
	var re = new RegExp( "[\\?&]"+qp+"=([^&#]*)", "i"),
		rslt = re.exec( window.location.search);
	delete re;
	if ( rslt == null)
		return "";
	return rslt[1];
}

function scrollToShow( elt ) {
	if (elt.scrollIntoView)
		elt.scrollIntoView();
	else {

		var coords = {x:0, y:0};
		do {
			coords.x += elt.offsetLeft;
			coords.y += elt.offsetTop;
		}
		while ((elt = elt.offsetParent));
		window.scrollTo (coords.x, coords.y);
	}
}
