ajaxob = function(){
	return (window.XMLHttpRequest) ?
	new XMLHttpRequest() :
	new ActiveXObject('Microsoft.XMLHTTP'); 
}
callserver = function(url, atts){
	var atts = atts || [];
	var box = atts['box'] || false;
	var f   = atts['func'] || false;
	if(box) give(box,'');
	var ob = ajaxob();
	ob.onreadystatechange = function(){
		if(ob.readyState == 4){
			if(ob.status == 200){
				if(box) x.innerHTML = ob.responseText;
				f && f(ob.responseText);
			} else alert('ajax problem')
		}
	}
	ob.open("GET", url, true);
	ob.send(null) 
}

Function.prototype.then = function(g){ 
	return function(){
		(this)();
		g()
	} 
}	
function now(){
	var cur = new Date();
	return cur.getTime();
}


function $(x){ return	document.getElementById(x);}

$A = function(x){
	var a = [];
	for(var i = 0;i < x.length;i++) {
		a.push(x[i]); 
	}
	return a;
}
function toArray(x){return $A(x)}

//This is not finished let, Lloyd (Note to self) 

tree = function(n,l,r){
	var l = l || {'value':false, 'left':false, 'right':false};
	var r = r || {'value':false, 'left':false, 'right':false};
	var t = { 'value':n, 'left':l, 'right':r }
	return t;
}
tree.prototype.add = function(n){
	if(n < this.value)  return tree(n,(this.add(n)),false);
	if(n > this.value)  return tree(n,false,(this.add(n)));
	return tree(n);
}
$T = function(tag){ 
	return $A(document.getElementsByTagName(tag)); }

$C = function(c,xs){

	var r = new RegExp("^" + c);
	var xs = xs || $A(document.body.childNodes);
	var found = [];

	var aux = function(xs){
		xs.map(function(elm){ 
			if(r.test(elm.className)) found.push(elm); 
			if(elm.hasChildNodes()) aux($A(elm.childNodes));
		});
	}
	aux(xs);
	return found;
}
String.prototype.split = function(x, acc){
	acc = acc || [];
	var index = this.indexOf(x);
	if(this.length < x.length || index < 0) return acc.concat(this);
	acc.push(this.slice(0,index));
	var nstr = this.slice(this.indexOf(x) + x.length);
	return nstr.split(x, acc);
}

String.prototype.fun = function(){
	var me = this;
	var args = 'x';
	var body = this;
	if(this.indexOf('->') > -1){
		var args = this.split('->')[0].replace(/ $/,'').replace(/ /g,',');
		var body = this.split('->')[1];
	}
	eval( "var f = function(" + args + "){ return " + body + ";};" );
	return f;
}

function $T(tag, root){
	var root = $(root) || document;
	var lst = root.getElementsByTagName(tag);
	if (!lst) return;
	return $A(lst);
}
function nextone(x,d){
	var n = (d == 0) ? x.previousSibling : x.nextSibling;
	if(!n) return false;
	if(n.nodeType == 1) return n;
	return nextone(n,d)
}
function oneBefore(x){ return nextone(x,0); }
function oneAfter(x){ return nextone(x,1); }
function give(ctnr, stf){ ctnr.innerHTML = stf; }

Array.prototype.head = function(){
    if (!this.length) return false;
    return this[0];
}

Array.prototype.tail = function(){
    if (this.length < 1) return false;
    return this.slice(1);
}


Array.prototype.loop = function(fw, f, acc){
	var f = (typeof(f) == 'function') ? f : f.fun();
	var i = -1; 
	while(++i < this.length) fw(this[i],acc);
	return acc;
}

Array.prototype.map = function(f){
	var f = (typeof(f) == 'function') ? f : f.fun();
	return this.loop(function(x,acc){ acc.push(f(x))}, f, []);
}
Array.prototype.filter = function(f){
	var f = (typeof(f) == 'function') ? f : f.fun();
	return this.loop(function(x,acc){if(f(x)) acc.push(x)}, f, []);
}
Array.prototype.iter = function(f){
	var f = (typeof(f) == 'function') ? f : f.fun();
	return this.loop(function(x){ f(x) }, f);
}

addInts = function(x,y){
	return parseInt(x) + parseInt(y);
}
foldl = function(f, acc, lst){
	if(lst == []) return false;
	if(lst.length == 1) return acc;
	return ( foldl(f, (f(acc, lst[0])), lst.slice(1)) )
}
foldr = function(f, acc, lst){
	if(lst == []) return false;
	if(lst.length == 1) return acc;
	return f(lst[0], foldr(f,acc,lst.slice(1)));
}
Array.prototype.member = function(x){
	var i = this.length;
	while(i>-1){
		if(this[i] == x){ 
			return true;
			alert("this[i]="+this[i]+" and x="+x);
		}
		i--;
	}
	return false;
}
String.prototype.humanize = function(){
	var cap = this.charAt(0).toUpperCase();
	var rest = this.substring(1).replace(/_/, ' ');
	return cap + rest
}
basename = function(s){
	if(s == '')return;
	var bits = String.split('\\');
	return bits[bits.length-1];
}

toggle = function(thing){
	if(thing) thing.style.display = (thing.style.display == '') ? 'none' : '';
}
function splitlist(list,splits,property){
	var group = [];
	var curs = [];
	list.iter(function(x){
		var txt = x[property].replace(/ +$/,'');
		if(splits.length > 0) 
			if(txt == splits[0]){
				group.push(curs);
				curs = [x];
				splits = splits.slice(1);
			}else{
				curs.push(x);
			}
	});
	return group;
}
function camelize(word){
	if(word.indexOf('-')>1){
		var minus = word.indexOf('-');
		var first = word.substring(0,minus);
		var cappd = word.substring(minus+1,minus+2).toUpperCase();
		var rest  = word.substring(minus+2);
		var camel = "" + first + cappd + rest;
		return camel;
	}else{
		return word;
	}
}
function getstyle(x,s){
	var attr = camelize(s);
	var y = (x.currentStyle) ?  x.currentStyle[attr] :  document.defaultView.getComputedStyle(x,null).getPropertyValue(s);
	return y;
}
function rounded(n,d){
	var pat = /\d+\.\d{0,2}/;
	var res = n.toString().match(pat);
	if(!res) return n;
	return parseFloat(res[0].replace(/(\.[^0])$/,"$10")); 
}
function mousePos(e){
	var e = e || window.event;
	var px = (e.pageX) ? e.pageX : e.clientX ? document.body.scrollLeft : 0;
	var py = (e.pageY) ? e.pageY : e.clientY ? document.body.scrollTop : 0;
	return {x:px, y:py};
}
	
function getPos(e){
	var left = 0;
	var top = 0;
	while(e.offsetParent){
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}
	left += e.offsetLeft;
	top += e.offsetTop;
	return { x:left, y:top }
}

function pagex(ob){
	return (ob.offsetParent) ? ob.offsetLeft + pagex(ob.offsetParent) : ob.offsetLeft;
}
function pagey(ob){
	return ob.offsetParent ? ob.offsetTop + (pagey(ob.offsetParent)) : ob.offsetTop;
}
addLoadEvent = function(func){
	var	oldonload =	window.onload;
	window.onload = (typeof window.onload != 'function') ?
	func : 	function(){	if(oldonload)oldonload(); func();}
}
