window.preludeJS = { version: "1.0" };

/**
* includeJS
*
* Inspirado en desde FCK_EDITOR > FCKScriptLoader
*/
var includeJS=function (scriptPath, onloadFunc)  {
  includeJS.Queue[includeJS.Queue.length]= includeJS.Path + scriptPath;
  if (!includeJS.IsLoading) includeJS.CheckQueue();
};
includeJS.Path = './';
includeJS.IsLoading = false;
includeJS.Queue = [];
includeJS.CheckQueue=function() {
  if (this.Queue.length>0) {
    this.IsLoading=true;
    var sScriptPath=this.Queue[0];
    var oTempArray=new Array();
    for (i=1; i<this.Queue.length; i++) oTempArray[i-1]=this.Queue[i];
    this.Queue=oTempArray;
    var e=document.createElement("script");
    e.type="text/javascript";
    e.onload=e.onreadystatechange=function() {
      if (!this.readyState||this.readyState=='loaded'||this.readyState=='complete') includeJS.CheckQueue();
    };
    e.src=sScriptPath;
    document.getElementsByTagName("head")[0].appendChild(e);
  }
  else {
    this.IsLoading=false;
    if (this.OnEmpty) { this.OnEmpty(); this.OnEmpty = null; }
  }
}; // /includeJS


/**
* preludeJS.Array
* Algunos de http://www.svendtofte.com/code/usefull_prototypes/, otros mï¿½s
*/
preludeJS.Array = { version: "1.0" };
if ('undefined'==typeof(Array.prototype.push)) {
  Array.prototype.push = function(e){this[this.length]=e;};
}
Array.prototype.foldl = function(fnc,start) {
  for (var a=start, i=0; i < this.length; i++) a = fnc(this[i],a); return a;
}
Array.prototype.foldr = function(fnc,start) {
    return this.reverse().foldl(fnc,start);
}
Array.prototype.map = function (f) {
  return this.foldl( function (i, acc) { acc.push(f(i)); return acc; }, [] );
};
Array.prototype.filter = function (f) {
  return this.foldl( function (i, acc) { if (f(i)) acc.push(i); return acc; }, [] );
};
Array.prototype.exists  = function (elem) {
  return this.foldl( function(i, prev){ return prev || (elem==i) } , false);
};






/*
* preludeJS.Function
*/
preludeJS.Function = { version: "1.0" };
if ('undefined'==typeof(Function.prototype.apply)) {
  Function.prototype.apply = function(f,args){
    for (var txt='',i=0;i<args.length; i++) txt+=(i?',':'')+'args['+i+']'; return eval('this('+txt+');');
  };
}
Function.prototype.curry = function () {
  var f = this;
  return function () {
    if (arguments.length >= f.length) return f.apply(this,arguments);
    else {
      var args = arguments;
      var currified = function (f, oldArgs, newArgs) {
        for (var applyArgs=[], i=0; i<(newArgs.length+oldArgs.length) ; i++)
          applyArgs[i] = (i<oldArgs.length) ? oldArgs[i] : newArgs[ i-oldArgs.length ] ;
        return f.apply(f,applyArgs);
      };
      return function () { return currified(f, args, arguments); };
    }
  };
};
Function.prototype.andThen = function (g) {
  var f=this; return function(){ f.apply(this, arguments); return g.apply(this, arguments); };
};




Function.prototype.ifTrue = function (g) {
  var f=this; return function(){ if (g.apply(this, arguments)) return f.apply(this, arguments); else return false;  };
};
Function.prototype.o = function (g) {
  var f=this; return function() { return f( g.apply(this,arguments) ); };
};



/*
* preludeJS.String
*/
preludeJS.String = { version: "1.0" };
String.prototype.before = function (txt) {
  return (-1 == this.indexOf(txt) ) ? this : this.substr( 0, this.indexOf(txt));
};
String.prototype.after = function (txt) {
  return (-1 == this.indexOf(txt) ) ? '' : this.substr( this.indexOf( txt ) + txt.length );
};
String.prototype.str_replace = function (o,n) {
  for (var t = this, i=0; i<t.length; i++)
    if (t.substring(i,i+o.length) == o)
      t = t.substring(0,i)+n+t.substring(i+o.length,t.length);
  return t;
};
String.prototype.trim = function() {
  return this.replace(/(^\s+)|(\s+$)/g,"");
};


// Nota: Las funciones preludeJS.DOM fueron traspasadas a quickDOM

