!function(){var slice=Array.prototype.slice,toString=Object.prototype.toString,hasOwnProperty=Object.prototype.hasOwnProperty;var nativeForEach=Array.prototype.forEach,nativeIsArray=Array.isArray,nativeKeys=Object.keys,nativeIndexOf=Array.prototype.indexOf,nativeLastIndexOf=Array.prototype.lastIndexOf;var utils={};var jSQL_KEY_NAME="jSQL_Key";var jSQL,_jSQL,_jsql,_DB={},_DBIndexMap={},_protected={},_events={};var interpolation=function(str){var args=[].slice.call(arguments,1);return str.replace(/%s/gim,function(){return args.shift()||""})};var logcat={error:function(error){error=interpolation.apply(this,arguments);if(typeof console!=="undefined"){if(console.warn){console.warn(error);return}if(console.log){console.log(error);return}}throw error},info:function(info){info=interpolation.apply(this,arguments);if(typeof console!=="undefined"){if(console.info){console.info(info);return}if(console.log){console.log(info);return}}}};if(typeof this.jSQL!=="undefined"){_jSQL=this.jSQL}if(typeof this.jsql!=="undefined"){_jsql=this.jsql}jSQL=function(){this.init.apply(this,arguments)};jSQL.prototype={version:"0.7.1-dev",init:function(){this._jSQL=_jSQL;this._jsql=_jsql;this._DB=_DB;this._currentDB=null;this._buffer=null;this._currentDBName=null;this._DBIndexMap=_DBIndexMap;this._protected=_protected;this._indexList=null;this._events=_events;this.utils=utils},create:function(dbname,db){var indexList;var that=this;if(this._DB.hasOwnProperty(dbname)){logcat.error("DB Already Exist.")}if(utils.isArray(db)){indexList=utils.listSlice(arguments,"2:");utils.appendKey(db,indexList);_DBIndexMap[dbname]=utils.arrayToObject(db);this._indexList=indexList||null}if(utils.isPlainObject(db)){_DBIndexMap[dbname]=utils.clone(db);db=utils.objectToArray(db)}if(typeof db==="string"&&db.match(/^http(s)?:\/\//gim)){var scope=arguments[2]||"*";var proxyCallback=function(data){db=typeof scope==="function"?scope.call(this,data):scope==="*"?data:utils.deep(data,scope);that._DB[dbname]=utils.clone(db);that._events[dbname]=that._events[dbname]||new that.Events;that.trigger(dbname,"create");that.trigger(dbname,"io:success");that.trigger(dbname,"io:complete")};var proxyFallback=function(){that._events[dbname]=that._events[dbname]||new that.Events;that.trigger(dbname,"io:error")};if(db.match("callback=")){this.io.jsonp(db,{},proxyCallback,proxyFallback)}else{this.io.ajax(db,{},proxyCallback,proxyFallback)}this._events[dbname]=this._events[dbname]||new this.Events;this.trigger(dbname,"io:start");return this}this._DB[dbname]=utils.clone(db);this._events[dbname]=this._events[dbname]||new this.Events;this.trigger(dbname,"create");return this},use:function(dbname){if(!this._DB.hasOwnProperty(dbname)){throw"Database Not Exist."}this._currentDB=this._DB[dbname];this._currentDBName=dbname;this.rebase();return this},drop:function(dbname){if(this._DB.hasOwnProperty(dbname)){delete this._DB[dbname];this.trigger(dbname,"drop")}return this},dbs:function(){return utils.keys(this._DB)},db:function(){return this._currentDBName},select:function(key){if(!this._currentDB){throw"Please Select Database First."}this._protected["field"]=[].slice.call(utils.isArray(key)?key:arguments);if(key==="*"){return this}return this},count:function(){var result;result=this._buffer.length;return result},total:function(scope){var rs=0,_tmp;for(var _key in this._currentDB){if(this._currentDB.hasOwnProperty(_key)){_tmp=scope==="*"?this._currentDB[_key]:typeof scope==="function"?scope.call(this,this._currentDB[_key],_key)===true?true:undefined:utils.deep(this._currentDB[_key],scope);if(typeof _tmp!=="undefined"){rs++}}}return rs},orderby:function(field,callback,order){var _array=this._buffer;var _this=this;if(typeof callback!=="function"){callback=[order,order=callback][0]}_array.sort(function(a,b){a=utils.deep(a,field);b=utils.deep(b,field);if(callback){a=callback(a);b=callback(b)}return order&&order.toLowerCase()==="asc"?a-b:b-a});this._buffer=_array;this._protected["sort"]=true;return this},where:function(fn){var _tmp=[],_swap;this._buffer=this._buffer||this._currentDB;fn=utils.parseFn(fn);for(var i in this._buffer){if(this._buffer.hasOwnProperty(i)){if(typeof fn==="function"){_swap=fn.call(this,utils.clone(this._buffer[i]),i)}if(utils.isArray(fn)){_swap=false;for(var f in fn){if(fn.hasOwnProperty(f)){if(fn[f].call(this,utils.clone(this._buffer[i]),i)){_swap=true}}}}if(_swap){_tmp.push(this._buffer[i])}}}this._buffer=_tmp;return this},iterate:function(fn){this._buffer=this._buffer||this._currentDB;for(var i in this._buffer){if(this._buffer.hasOwnProperty(i)){fn.call(this,this._buffer[i])}}return this},findAll:function(){var result;result=utils.clone(utils.arrayToObject(this._select()));return result},find:function(key){var result;var _tmp=this._DBIndexMap[this._currentDBName];if(!key){for(var i in _tmp){if(_tmp.hasOwnProperty(i)){if(key){break}if(this._buffer.hasOwnProperty(i)){key=i}}}}result=utils.clone(_tmp[key]);return result},listAll:function(){var result;result=utils.clone(this._select());return result},update:function(fn){var _swap=this.utils.arrayToObject(this._currentDB);var _tmp;this._buffer=this._buffer||this._currentDB;if(!this._currentDB){throw"Please Select Database First."}for(var i in this._buffer){if(this._buffer.hasOwnProperty(i)){_tmp=fn.call(this,utils.clone(this._buffer[i]));if(_tmp){_swap[this._buffer[i][jSQL_KEY_NAME]]=_tmp}}}this._currentDB=this.utils.objectToArray(_swap);this._DB[this._currentDBName]=this.utils.objectToArray(_swap);this.trigger(this._currentDBName,"update");return this},insert:function(item,key){var item=utils.clone(item);var fromIndex=arguments[2];item[jSQL_KEY_NAME]=item.key||key;fromIndex?this._currentDB.splice(fromIndex,0,item):this._currentDB.push(item);this.trigger(this._currentDBName,"update");return this},append:function(database,data){if(arguments.length>1){this.use(database)}else{data=arguments[0]}data=utils.clone(data);if(utils.isArray(data)){utils.appendKey(data,this._indexList);this._currentDB=this._currentDB.concat(data)}if(utils.isPlainObject(data)){this._currentDB=this._currentDB.concat(utils.objectToArray(data))}this._DB[this._currentDBName]=this.utils.objectToArray(this._currentDB);this.trigger(this._currentDBName,"update");return this},remove:function(){var that=this;var _swap=this.utils.arrayToObject(this._currentDB);this._buffer=this._buffer||this._currentDB;for(var i in this._buffer){if(this._buffer.hasOwnProperty(i)){delete _swap[this._buffer[i][jSQL_KEY_NAME]]}}this._currentDB=this.utils.objectToArray(_swap);this._DB[this._currentDBName]=this.utils.objectToArray(_swap);return this},limit:function(start,end){var _tmp=this._buffer;var limit;if(!end){start=[0,end=start][0]}limit=start+":"+(start+end);this._buffer=utils.listSlice(_tmp,limit);return this},keys:function(){return utils.keys(this.findAll())},first:function(fn){if(fn){return this.where(fn).first()}return utils.listSlice(this._select(),":1")},last:function(fn){if(fn){return this.where(fn).last()}return utils.listSlice(this._select(),"-1:")},distinct:function(field){return utils.distinct(this.listAll())},rebase:function(){this._protected={};this.select("*");this._resetBuffer();this._updateIndexMap();return this},noConflict:function(){if(window.jSQL===jSQL){window.jSQL=this._jSQL}if(window.jsql===jsql){window.jsql=this._jsql}return this},io:new function(){var that=this;this.ajax=function(uri,data,success,error){var args=[].slice.call(arguments);if(args.length<4){args.splice(1,0,{})}uri=args[0],data=args[1],success=args[2],error=args[3];data._t=utils.uuid();this.reqwest({url:uri,type:"json",method:"get",data:data,success:success,error:error})},this.jsonp=function(uri,data,success,error){var args=[].slice.call(arguments);if(args.length<4){args.splice(1,0,{})}uri=args[0],data=args[1],success=args[2],error=args[3];if(!uri.match("callback=")){if(uri.match(/\?/gim)){if(uri.lastIndexOf("&")===uri.length-1){uri+="callback=?&_t="+utils.uuid()}else{uri+="&callback=?&_t="+utils.uuid()}}else{uri+="?callback=?&_t="+utils.uuid()}}this.reqwest({url:uri,type:"jsonp",data:data,success:success,error:error})}},on:function(database,event,callback){this._events[database]=this._events[database]||new this.Events;return this._events[database].on(event,callback)},off:function(database,event,callback){return this._events[database].off(event,callback)},trigger:function(database,event){var args=[].slice.call(arguments,1);if(!this._events.hasOwnProperty(database)){return false}logcat.info("%s: trigger - %s",database,event);return this._events[database].trigger.apply(this._events[database],args)},alias:function(name){window[name]=this;return this},_select:function(field){var tmp,result=[];field=field||this._protected["field"];if(this._protected["sort"]===true){this.trigger(this._currentDBName,"sort")}if(field==="*"||field.join&&field.join("")==="*"){return this._buffer}if(typeof field==="string"){field=field.split(",")}utils.each(this._buffer,function(o,i,r){tmp={};tmp[jSQL_KEY_NAME]=utils.deep(o,jSQL_KEY_NAME);if(field.length===1){result.push(utils.deep(o,field[0]));return}utils.each(field,function(_o,_i,_r){if(o.hasOwnProperty(_o)){tmp[_o.split(".").pop()]=utils.deep(o,_o)}});result.push(tmp)});return result},_updateIndexMap:function(){_DBIndexMap[this._currentDBName]=utils.arrayToObject(this._currentDB)},_resetBuffer:function(){this._buffer=this._currentDB}};utils={deep:function(data,scope){var _tmp=data,scope=scope.split(".");for(var i=0;i1?range[1]||list.length:list.length;return[].slice.call(list,start,end)},appendKey:function(list,indexList){var that=this;that.each(list,function(o,i,r){o[jSQL_KEY_NAME]=that.keygen(o,indexList)||i})},keys:nativeKeys||function(){var hasDontEnumBug=true,dontEnums=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"],dontEnumsLength=dontEnums.length;for(var key in{toString:null}){hasDontEnumBug=false}return function keys(object){if(typeof object!="object"&&typeof object!="function"||object===null){throw new TypeError("Object.keys called on a non-object")}var keys=[];for(var name in object){if(object.hasOwnProperty(name)){keys.push(name)}}if(hasDontEnumBug){for(var i=0,ii=dontEnumsLength;i>>0;if(!length){return-1}var i=0;if(arguments.length>2){i=arguments[2]}i=i>=0?i:Math.max(0,length+i);for(;i>>0;if(!length){return-1}var i=length-1;if(arguments.length>1){i=Math.min(i,toInteger(arguments[1]))}i=i>=0?i:length-Math.abs(i);for(;i>=0;i--){if(i in self&&sought===self[i]){return i}}return-1},uuid:function(){return(new Date).getTime()+"_"+parseInt(Math.random()*1e3)},distinct:function(arr){var tmp=[];for(var i=0;i0){self._completeHandlers.shift()(resp)}}function success(resp){var filteredResponse=globalSetupOptions.dataFilter(resp.responseText,type),r=resp.responseText=filteredResponse;if(r){switch(type){case"json":try{resp=win.JSON?win.JSON.parse(r):eval("("+r+")")}catch(err){return error(resp,"Could not parse JSON in response",err)}break;case"js":resp=eval(r);break;case"html":resp=r;break;case"xml":resp=resp.responseXML&&resp.responseXML.parseError&&resp.responseXML.parseError.errorCode&&resp.responseXML.parseError.reason?null:resp.responseXML;break}}self._responseArgs.resp=resp;self._fulfilled=true;fn(resp);while(self._fulfillmentHandlers.length>0){self._fulfillmentHandlers.shift()(resp)}complete(resp)}function error(resp,msg,t){self._responseArgs.resp=resp;self._responseArgs.msg=msg;self._responseArgs.t=t;self._erred=true;while(self._errorHandlers.length>0){self._errorHandlers.shift()(resp,msg,t)}complete(resp)}this.request=getRequest.call(this,success,error)}Reqwest.prototype={abort:function(){this._aborted=true;this.request.abort()},retry:function(){init.call(this,this.o,this.fn)},then:function(success,fail){if(this._fulfilled){success(this._responseArgs.resp)}else if(this._erred){fail(this._responseArgs.resp,this._responseArgs.msg,this._responseArgs.t)}else{this._fulfillmentHandlers.push(success);this._errorHandlers.push(fail)}return this},always:function(fn){if(this._fulfilled||this._erred){fn(this._responseArgs.resp)}else{this._completeHandlers.push(fn)}return this},fail:function(fn){if(this._erred){fn(this._responseArgs.resp,this._responseArgs.msg,this._responseArgs.t)}else{this._errorHandlers.push(fn)}return this}};function reqwest(o,fn){return new Reqwest(o,fn)}function normalize(s){return s?s.replace(/\r?\n/g,"\r\n"):""}function serial(el,cb){var n=el.name,t=el.tagName.toLowerCase(),optCb=function(o){if(o&&!o.disabled)cb(n,normalize(o.attributes.value&&o.attributes.value.specified?o.value:o.text))},ch,ra,val,i;if(el.disabled||!n)return;switch(t){case"input":if(!/reset|button|image|file/i.test(el.type)){ch=/checkbox/i.test(el.type);ra=/radio/i.test(el.type);val=el.value;(!(ch||ra)||el.checked)&&cb(n,normalize(ch&&val===""?"on":val))}break;case"textarea":cb(n,normalize(el.value));break;case"select":if(el.type.toLowerCase()==="select-one"){optCb(el.selectedIndex>=0?el.options[el.selectedIndex]:null)}else{for(i=0;el.length&&i=0;i-=2){if(!(callback&&list[i]!==callback||context&&list[i+1]!==context)){list.splice(i,2)}}}return this};Events.prototype.trigger=function(events){var cache,event,all,list,i,len,rest=[],args,returned={status:true};if(!(cache=this.__events))return this;events=events.split(eventSplitter);for(i=1,len=arguments.length;i