// NB Utlility functions are liable to assume presence of buglog without documentation

// In general utility functions assume they are being given sensable parameters


DEBUG=false; // Set to true for debugging output

// DEBUG logging---use Firebug if available, else just alert...
if (window.console) {
	buglog=window.console.log;
} else {
	buglog=window.alert;
}

document.getElementsByClassName=function(searchClass,node,tag) {
	//Dustin Diaz's implementation of getElementsByClassName
	// Finds elements with a given class
	//searchClass - the class which elements must have to be returned
	// node - start node for search (defaults to document if null)
	// tag - optional: find only elements with tagname tag
	// Returns: Array of elements  decendants of node(optionally of type tag) which have class searchClass
		if (DEBUG) {
			buglog("Finding classname "+searchClass+" in "+node);
			
		}
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}


function addClass(theClass, theNode) {
	// norcimo May 2009
	// Adds a class to an element
	// theClass - class to add to element
	// theNode - element to which to add class
	// Requires: removeClass()
	if (DEBUG) {
		buglog("Adding class "+theClass+" to "+theNode);
	}
	removeClass(theClass, theNode); //Don't want it twice, so remove if there!!
	var currentClasses=theNode.className;
	// We only do this way to avoid leading spaces---all neat and tidy!
	if (currentClasses)
		currentClasses+=" "+theClass;
	else
		currentClasses=theClass;
	theNode.className=currentClasses;
}

function removeClass(theClass, theNode) {
	// norcimo May 2009
	// Removes a class from an element
	// theClass - the class to be removed
	// theNode - the element from which to remove the class
	if (DEBUG) {
		buglog("Removing class "+theClass+" from "+theNode);
	}
	var buildRegExp="(^|\\s+)("+theClass+")(\\s+|$)";
	var testRegExp=new RegExp(buildRegExp,"g");
	var currentClasses=theNode.className;
	if (!currentClasses)
		return;
	currentClasses=currentClasses.replace(testRegExp," ");
	//Gone. Tidy up multiple spaces to leave just one (doesn't matter, but be neat)
	currentClasses.replace(/\s\s+/g," ");
	// We may have a leading space (if it was the first class), or trailing space (if last).
	// This doesn't really matter, but we'll be tidy
	currentClasses.replace(/^\s+/,"");
	currentClasses.replace(/\s+$/,"");
	theNode.className=currentClasses;
}

function setOpacity(theOpacity, theNode) {
	// norcimo June 2009
	// Sets opacity of an element, using CSS opacity property and filters for IE
	// theOpacity - required opacity [ 0<=theOpacity<=1 else returns false ]
	// theNode - Element on which to set opacity
	if (theOpacity<0 || theOpacity >1)
		return false;
	theNode.style.opacity=theOpacity;
	if (theNode.filters) {
		theNode.filters.item("DXImageTransform.Microsoft.Alpha").opacity=theOpacity*100;
	}
}

function findPos(obj) {
	// Find actual position of object obj
	// From quirksmode.org
	//Returns left, top as array
	var curLeft=curTop=0;
	if (obj.offsetParent) {
		do {
			curLeft+=obj.offsetLeft;
			curTop+=obj.offsetTop;
		} while (obj=obj.offsetParent);
	}
	return [curLeft,curTop];
}

function randomInRange(low, high) {
	// Return a (pseudo)random integer from low to high inclusive
return Math.floor(Math.random()*(high-low+1)+low);
}