/* bv_utilities.js 050116:1530GMT */
/******** standard * utilities ********/


/*
	the ondocload "event" is triggered by a script call before the closing body tag:
		<script type="text/javascript">window.ondocload();</script>
		</body>

	it is the event before onload that indicates the document is done downloading;
	after the func declaration we set two handlers to make sure events assigned to
	docload are triggered onload if the author forgets to put the call in the doc.
*/
	function bv_addListener(el, evt, fn) {
		if (typeof el == "string") el = document.getElementById(el);
		if (!el) return;
		if (window.addEventListener && evt != 'docload') { // DOM
			el.addEventListener(evt, fn, false); // false, because IE can't handle the truth
		} else if (window.attachEvent && evt != 'docload') { // MS, incl Opera
			el.attachEvent('on'+ evt, fn);
		} else { // Mac IE and the ondocload event
			var prevHandler = (typeof el['on'+ evt] == 'function') ? el['on'+ evt] : function () {};
			el['on'+ evt] = function() { prevHandler(); fn(); } 
		}
	}
	bv_addListener(window, 'docload', function () { window.bv_docInited = true; });
	bv_addListener(window, 'load', function () { if (!window.bv_docInited) window.ondocload(); });
/* END bv_addListener */



	function sortRandom(a,b) {
		if (Math.random() > 0.5) return 1;
		else return -1;
	}
	function bv_arrayShuffle(a) {
		var Last = a[a.length -1];
		do a = a.sort(sortRandom);
		while (Last == a[0]);
		return a;
	}



	function bv_trim(s,internal) {
// Mac IE chokes on the ?
//		s = s.replace(/^\s*(.*?)\s*$/, '$1');
		s = s.replace(/^\s+/g, '');
		s = s.replace(/\s+$/g, '');
		if (internal) s = s.replace(/\s+/g, ' ');
		return s;
	}



	function bv_getElementsByClassName(TagsIn,Class) {
		var It = "\\b"+Class+"\\b";
		var rtn = new Array();
		var Tags = TagsIn.split(",");
		for (var xx=0; xx<Tags.length; xx++) {
			var Els = (this.getElementsByTagName) ? this.getElementsByTagName(Tags[xx]) : this.all;
			for (var ii=0; ii<Els.length; ii++) {
				if (Els[ii].className.match(It))
					rtn[rtn.length] = Els[ii];
			}
		}
		return rtn;
	}
	document.getElementsByClassName = bv_getElementsByClassName;



	function bv_addClassName(el, nm) {
		el.className = bv_trim(el.className +" "+ nm, 1);
	}
	function bv_removeClassName(el, nm) {
		var regex = new RegExp("\\b"+nm+"\\b", "gi");
		el.className = bv_trim(el.className.replace(regex, ''), 1);
	}



	function bv_getOffsetFromParent(El,XY) {
		var Type = (XY == "top") ? "offsetTop" : "offsetLeft";
	// IE will treat the offset as to the parentNode; Moz refers all to the body element
		return (El.offsetParent == El.parentNode) ? 
				El[Type] : 
				El[Type] - El.parentNode[Type];
	}
	function bv_getOffsetFromRoot(El,XY) {
		var Type = (XY == "top") ? "offsetTop" : "offsetLeft";
		var offset = 0;
		while (El) {
			offset += El[Type];
			El = El.offsetParent;
		}
		return offset;
	}


	function bv_attachNewWindows(prefix, allowedTypes, viewerLocation) {
		var pfx = (prefix && prefix.length) ? prefix : "bv_";
		var AllowPop = (/\bpopview\b/.test(allowedTypes) && (viewerLocation && viewerLocation.length));
		var AllowNew = /\bnewwin\b/.test(allowedTypes);
		var As = document.getElementsByTagName('a');
		for (var xx=0; xx<As.length; xx++) {
			if (AllowPop && As[xx].getAttribute('rel') == "popview") {
				As[xx].onclick = function () { window.open(viewerLocation+this.href,pfx+'viewer','resizable,scrollbars=0'); return false; }
			} else if (AllowNew && As[xx].getAttribute('rel') == "newwin") {
				As[xx].onclick = function () { window.open(this.href,pfx+'newwin'); return false; }
			}
		}
	}

/******** /standard * utilities ********/
