// var elems = $('elementid', 'elementid2', obj1, obj2);
function $() {
	var elems = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var e = arguments[i];
		if (typeof e == 'string')
			e = document.getElementById(e);
		if (arguments.length == 1)
			return e;
		elems.push(e);
	}
	return elems;
}

// removes item but leaves footprint behind on the screen
function hide(e, showing) {
	if (e) if (e.length) {
		var ii = e.length;
		while (ii--) e[ii].style.visibility = showing ? "visible" : "hidden";
	} else e.style.visibility = showing ? "visible" : "hidden";
}

// removes item and footprint from the screen
function collapse(e, expanding) {
	if (e) if (e.length) {
		var ii = e.length;
		while (ii--) e[ii].style.display = expanding ? "block" : "none";
	} else e.style.display = expanding ? "block" : "none";
}

// var elems = getElementsByClass('navelements');
function getElementsByClass(className, tag) {
	var results = new Array();
	if (!tag) tag = '*';
	var pattern = new RegExp('(^|\\s)'+className+'(\\s|$)');
	var elems = document.getElementsByTagName(tag), ii = elems.length;
	while (ii--)
		if (pattern.test(elems[ii].className))
			results.push(elems[ii]);
	return results;
}

// addEvent(window, 'load', OnLoadFunction);
// calls OnLoadFunction with the element as its argument
function addEvent(e, eventName, f) {
	if (e) if (e.addEventListener) // FF
		e.addEventListener(eventName, function(evt){if (!f(evt.target, evt)) evt.preventDefault()}, false); 
	else if (e.attachEvent) // IE
		e.attachEvent('on'+eventName, function(evt){evt.returnValue = f(evt.srcElement, evt)});
	else
		e['on' + eventName] = f;
}

// convenient macro for load events
function addLoadEvent(f) { addEvent(window, 'load', f); }

// grow width effect
// first call should set initial width 'w'
function grow(e, end, w) {
	if (e.growID) clearInterval(e.growID);
	if (e.growW) w = e.growW; else e.growW = w;
	var steps = 5, dw = (end - w) / steps;
	e.growID = setInterval(function() {
		if (--steps <= 0) { w = end - dw; clearInterval(e.growID); } 
		e.style.width = Math.round(w += dw)+"px";
		e.growW = w;
	}, 50);
}

// color fade effect
// first call should set initial color 'c'
function fade(e, end, c) {
	if (e.fadeID) clearInterval(e.fadeID);
	if (e.fadeC) c = e.fadeC; else e.fadeC = c;
	var steps = 10, dr = (end[0] - c[0]) / steps, dg = (end[1] - c[1]) / steps, db = (end[2] - c[2]) / steps; 
	e.fadeID = setInterval(function() {
		if (--steps <= 0) { c = end; c[0]-=dr; c[1]-=dg; c[2]-=db; clearInterval(e.fadeID); }
    e.style.backgroundColor = "rgb(" + Math.round(c[0]+=dr) + "," + Math.round(c[1]+=dg) + "," + Math.round(c[2]+=db) + ")";
		e.fadeC = c;
	}, 50);
}

// lava glow effect
// leave off 'end' to set the color and clear a running animation
function lava(e, c, end) {
	if (e.lavaID) clearInterval(e.lavaID);
	if (!end) { e.style.backgroundColor = "rgb("+c[0]+ ","+c[1]+","+c[2]+")"; return; }
	var steps = 10, dr = (end[0] - c[0]) / steps, dg = (end[1] - c[1]) / steps, db = (end[2] - c[2]) / steps; 
	e.lavaID = setInterval(function() {
		if (--steps <= 0) { dr = -dr; dg = -dg; db = -db; steps = 10; }
    e.style.backgroundColor = "rgb(" + Math.round(c[0]+=dr) + "," + Math.round(c[1]+=dg) + "," + Math.round(c[2]+=db) + ")";
	}, 50);
}

// limit the length of an element such as textarea
function limitLength(e, maxlength) {
	if (!e) return;
	var f = function(e, evt) {
		if (e.value.length < maxlength) return true;
		if (e.value.length > maxlength) e.value = e.value.substr(0, maxlength);
		return evt.altKey || evt.ctrlKey || (!evt.charCode && evt.keyCode != 10 && evt.keyCode != 13);
	}
	addEvent(e, 'keypress', f);
	addEvent(e, 'input', f);
}

function InstantiateXHR() {
	try { return new XMLHttpRequest(); } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
	alert("This site requires XMLHTTP");
}

var xhr = InstantiateXHR(),
		xhrq = new Array(),
		xhrbusy = false;

function xhrRequest(url, callback, posting, data) {
	if (xhrbusy) {
		xhrq.push({url:url, callback:callback, posting:posting, data:data});
		return;
	}
	xhrbusy = true;

	var params = "sqc="+StaticQueryCookie;
	for (key in data)
		params += "&" + key + "=" + encodeURIComponent(data[key]);

	if (posting) {
		xhr.open("POST", url, true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	} else {
		if (params) url += "?" + params;
		xhr.open("GET", url, true);
	}

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200)
				callback(xhr.responseText);
			xhrbusy = false;
			if (xhrq.length > 0) {
				var next = xhrq.shift();
				xhrRequest(next.url, next.callback, next.posting, next.data);
			}
		}
	}

	xhr.send(posting ? params : null); 
}
