// Basic JS functions - Sean Catchpole

// Array.map 
//Original: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map
if(!Array.prototype.map) Array.prototype.map = function(f) {
  var l = this.length;
  if (typeof f != "function") throw new TypeError();
  var r = new Array(len);
  var t = arguments[1];
  for(var i=0; i<l; ++i)
    if(i in this)
      r[i] = f.call(t, this[i], i, this);
  return r;
}

// Array.filter 
//Original: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter
if(!Array.prototype.filter) Array.prototype.map = function(f) {
  var l = this.length;
  if (typeof f != "function") throw new TypeError();
  var r = new Array();
  var t = arguments[1];
  for(var i=0; i<l; ++i)
    if(i in this) {
      var v = this[i];
      if(f.call(t, v, i, this))
        r.push(v);
    }
  return r;
}

// Array.foldl 
//Original: http://satta.org/2007/01/08/foldr-and-foldl-in-javascript/
if(!Array.prototype.foldl) Array.prototype.foldl = function(f,z) {
  return (this.length==0)?z:this.slice(1).foldl(f, f(z,this[0]) );
}

// Array.foldr 
//Original: http://satta.org/2007/01/08/foldr-and-foldl-in-javascript/
if(!Array.prototype.foldr) Array.prototype.foldr = function(f,z) {
  return (this.length==0)?z:f(this[0], this.slice(1).foldr(f,z) );
}

// Array.Sort 
//Original: http://weetbixthecat.com/blog/2006/faster-javascript-sorting/
if(!Array.prototype.Sort) Array.prototype.Sort = function(p,f) {
  var o = Object.prototype.toString;
  var a = Array.prototype.toString;
  var i = function(){ return this[p]; }
  Object.prototype.toString = i;
  Array.prototype.toString = i;
  (f)?this.sort(f):this.sort();
  Object.prototype.toString = o;
  Array.prototype.toString = a;
}


//Shortcut Functions 
//Original: http://www.wowhead.com/js/global.js
Node.prototype.ac = function(z) { this.appendChild(z); return this; }
Node.prototype.gE = function(z) { return toArray(this.getElementsByTagName(z)); }
Node.prototype.dE = function(z) { (isArray(z))?z.map(de):this.gE(z).map(de); return this; }
function ge(z) { return document.getElementById(z); }
function gE(z) { return document.gE(z); }
function ce(z,t) { e = document.createElement(z); if(t) (t.nodeType)?e.ac(t):(e.innerHTML=t); return e; }
function ct(z) { return document.createTextNode(z); }
function de(z) { z.parentNode.removeChild(z) }
function dE(z) { document.dE(z); }
function dw(z) { document.write(z); }
function rf() { return false }
function isArray(o) { return !(o.constructor.toString().indexOf("Array") == -1); }
function toArray(o) { var l=[]; for(var i=0; i<o.length; ++i) l[l.length]=o[i]; return l; }
