debug=function(message,clear){
	var c = clear || false
	var m = message || '';
	if(!$('debugger')){
		var dw=document.createElement('div');
		dw.setAttribute('id','debugger');
		dw.style.position='absolute';
		dw.style.height='140px';
		dw.style.backgroundColor='white';
		dw.style.overflow='scroll';
		var fc = document.body.firstChild;
		fc.parentNode.insertBefore(dw,fc);
	}
	var db = $('debugger');
	(clear) ? db.innerHTML = m : db.innerHTML += "<br>--> " + m;
}
ajaxob = function(){
	var req = false;
	if(req = new ActiveXObject("Msxml2.XMLHTTP")) return req;
	if(req = new ActiveXObject("Microsoft.XMLHTTP")) return req;
	if(req = new XMLHttpRequest()) return req;
}
ajupload = function(i){
	var ifm = document.createElement('iframe');
	with(ifm){
		setAttribute('id','uploadframe');
		setAttribute('src','savedata.php');
	}
}

callserver = function(url, box, f){
	if(box) give(box,'');
	var ob = ajaxob();
	ob.onreadystatechange = function(){
		if(ob.readyState < 4 && box) box.innerHTML = "loading...";
		if(ob.readyState == 4){
			if(ob.status == 200){
				if(box) box.innerHTML = ob.responseText;
				f && f();
			} else alert('ajax problem')
		}
	}
	ob.open("GET", url, true);
	ob.send(null) 
}
call = function(url, f){
	var ob = ajaxob();
	ob.onreadystatechange = function(){
		if(ob.readyState == 4){
			if(ob.status == 200) f && f(); 
			else alert('ajax problem')
		}
	}
	ob.open("GET", url, true);
	ob.send(null) 
}

Function.prototype.then = function(g){ 
	return function(){
		(this)();
		g()
	} 
}	
Object.prototype.set = function(x,y){
	(this.setAttribute) ? this.setAttribute(x,y) : this[x] = y;
	return this;
}
Object.prototype.get = function(x){
	return (this.getAttribute) ? this.getAttribute(x) : this[x];
}
function now(){
	var cur = new Date();
	return cur.getTime();
}

Function.prototype.andthen = function(g){
   var f = this;
   return function(){
        f();g();
   }
}   

Function.prototype.after = function(g){
	var f = this;
	return function(x){
		g(f(x));
	}
}
function mousePos(e){
	var e = e || window.event;
	px = (e.pageX) ? e.pageX : e.clientX ? e.clientX + document.body.scrollLeft : 0;
	py = (e.pageY) ? e.pageY : e.clientY ? 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;
}
animclip = function(id,speed){
	var endWidth = parseInt($(id).style.width) + 10;
	var endHeight = parseInt($(id).style.height) + 10;
	var dh =  parseInt(endWidth)  / (2 * speed);
	var dv =  parseInt(endHeight) / (2 * speed);
	var left = parseInt($(id).style.left);
	var top  = parseInt($(id).style.top);
	var _anim = function(l,t,timer){
		timer['reg'] && clearInterval(timer['reg']);
		var l = l-dh; 
		var t = t-dv;
		var b = endHeight - t;
		var r = endWidth - l; 
		$(id).style.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
		if((r-l) > endWidth+10) return;
		timer['reg'] = setInterval(function(){_anim(l,t,timer)},speed);
	}
	this.act = _anim(endWidth/2,endHeight/2,{'reg':0});
}
function anim(e,a,f,b,c,d,rev,then){
    var st = now(); 
    var _anim = function(f,b,c,d,timer){
	timer['reg'] && clearInterval(timer['reg']);
	var dt = (now() - st) / 1000;
	var x = f(b,dt,c,d);
	if(rev) x = c - x;
	eval("$('"+e+"')."+a+"='"+x+"px';");
	if(dt >= d) {
		then && then();
		return;
	}
	timer['reg'] = setInterval(function(){_anim(f,b,c,d,timer)},10);
    }
    this.act = _anim(f,b,c,d,{'reg':0});
}

lin = function(b,t,c,d){ return c * t/d + b; }
zin = function(b,t,c,d){return c*(t/=d)*t*t+b}
zot = function(b,t,c,d){return(-c*(t/=d)*(t-2))+b}
zio = function(b,t,c,d){
    return((t/=d/2) < 1) ? c/2*t*t+b : -c/2*((--t)*(t-2)-1)+b;
}

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);
}

Array.prototype.reduce = function( acc ){
	var acc = acc || '';
	this.map( function(x){ acc += x });
	return acc
}

Array.prototype.fold = function(f, acc){
	this.map(function(x){
		var acc = f(x,acc)
	});
	return acc;
}
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;
}
addLoadEvent = function(func){
	var	oldonload =	window.onload;
	window.onload = (typeof window.onload != 'function') ?
	func : 	function(){	if(oldonload)oldonload(); func();}
}
