vanilla.namespace("vanilla.lang");vanilla.require("vanilla.text");vanilla.extend(vanilla,{isInstanceOf:function(b,a){if(!b||!b.getClass){return false}return b.getClass().isInstanceOf(a)},Class:function(a,b,c){return vanilla.lang.Class.create(a,b,false,c,false)},Abstract:function(a,b){return vanilla.lang.Class.create(a,b,true,false)},Interface:function(a,b){return vanilla.lang.Class.create(a,b,false,false,true)}});vanilla.lang.classes=new Array();vanilla.lang.Class=function(c,f,a,e,g,d){this.namespace=c;this.classname=f;this.fullname=(c?c+".":"")+f;this._super=null;this._implement=[];this._final=(g==true);this._abstract=(e==true);this._interface=(d==true);this._function=a;if(d&&e){throw new IllegalStateException("An interface can't be abstract")}if(d&&g){throw new IllegalStateException("An interface can't be final")}if(e&&g){throw new IllegalStateException("An abstract class can't be final")}var b=this;this._function.Class=b;this._function.Extends=vanilla.lang.Class.Extends;this._function.Implements=vanilla.lang.Class.Implements;this._function.Prototype=vanilla.lang.Class.Prototype;this._function.Static=vanilla.lang.Class.Static;if(this.fullname!="vanilla.lang.Object"){this._function.prototype.Super=vanilla.lang.Class.prototype.Super}this._function.prototype.getClass=function(){return b};this._function.prototype.self=this._function;this._function.prototype["_"+f+"_"]=this._function};vanilla.extend(vanilla.lang.Class,{Extends:function(a){if(!a||!a.prototype){return}if(a.Class){if(this.Class==a.Class){throw new IllegalStateException("Can't inherits of self class")}if(a.Class._final){throw new IllegalStateException("Can't inherits of a final class")}if(this.Class._super!=null){throw new IllegalStateException("Can't inherits of two classes.")}if(this.Class._interface&&!a.Class._interface){throw new IllegalStateException("An interface can only inherits of interface")}if(!this.Class._interface&&a.Class._interface){throw new IllegalStateException("A class can only inherits from classes. Use the Implements method to implements interfaces")}this.Class._super=a.Class}vanilla.extend(this.prototype,a.prototype,true)},Implements:function(a){if(!a||!a.Class){return}if(!a.Class._interface){throw new IllegalStateException("Can only implements interfaces")}this.Class._implement.push(a.Class);vanilla.extend(this.prototype,a.prototype,true)},AbstractFunction:function(){throw new IllegalStateException("You need to override this method")},Static:function(a){vanilla.extend(this,a)},Prototype:function(a){vanilla.extend(this.prototype,a)},create:function(b,f,e,h,d){if(!f){return null}var a=null;if(e){a=function(){throw new IllegalStateException("Can't instanciate an abstract class")}}else{if(d){a=function(){throw new IllegalStateException("Can't instanciate an interface")}}else{a=function(){this[f].apply(this,arguments)};a.prototype[f]=function(){}}}var g=new vanilla.lang.Class(b,f,a,e,h,d);vanilla.lang.classes[g.fullname]=g;return(vanilla.namespace(b)[f]=a)},bundle:function(b,d,a){if(!a){return false}var e=new vanilla.lang.Class(b,d,a,false,true);vanilla.lang.classes[e.fullname]=e;return true},forName:function(a){return vanilla.lang.classes[a]}});vanilla.lang.Class.prototype={instanciate:function(){return new this._function()},isInstanceOf:function(b){if(!b){return false}if(typeof b=="string"){b=vanilla.lang.Class.forName(b)}else{if(b.Class){b=b.Class}else{if(b.getClass&&b.getClass()!=Class.forName("vanilla.lang.Class")){b=b.getClass()}}}if(this==b){return true}if(this._super&&this._super.isInstanceOf(b)){return true}for(var a=0;a<this._implement.length;a++){if(this._implement[a].isInstanceOf(b)){return true}}return false},Super:function(){var b=Arr(arguments);var f=b.shift();var e=b.shift();var c=null;for(var a=this.getClass();a._super;a=a._super){if(a._super.classname==f){c=a._super;break}}if(!c){throw new IllegalStateException("The class "+this.getClass().classname+" has no super class named : "+f)}var d=c._function.prototype;if(!d[e]){throw new IllegalStateException("The class "+f+" has no function named : "+e)}return d[e].apply(this,b)}};vanilla.lang.Class.bundle("vanilla.lang","Class",vanilla.lang.Class);vanilla.lang.Class.bundle("","Array",Array);vanilla.lang.Class.bundle("","Function",Function);vanilla.lang.Class.bundle("","String",String);vanilla.Class("vanilla.lang","Exception");vanilla.lang.Exception.Prototype({Exception:function(a,b){this.message=a;this.cause=b},toString:function(){return this.getClass().fullname+" : "+this.message}});vanilla.Class("vanilla.lang","IllegalStateException");vanilla.lang.IllegalStateException.Extends(vanilla.lang.Exception);vanilla.lang.IllegalStateException.Prototype({IllegalStateException:function(a,b){this.Exception(a,b);vanilla.console.warn(a)}});vanilla.Interface("vanilla.lang","Runnable");vanilla.lang.Runnable.Prototype({run:vanilla.lang.Class.AbstractFunction,getDelay:vanilla.lang.Class.AbstractFunction});vanilla.Class("vanilla.lang","Thread");vanilla.lang.Thread.Implements(vanilla.lang.Runnable);vanilla.lang.Thread.Static({DefaultDelay:1000,Stop:-1,Pause:0,Continue:1,join:function(){var b=Arr(arguments);var a=b.shift();var c=new Array();var d=function(){for(var e=0;e<c.length;e++){if(c[e].isAlive()){return}}for(var e=1;e<arguments.length;c[e++].onDie.unsubscribe(d)){}a()};b.forEach(function(e,f){e.onDie.subscribe(d);c.push(e)})}});vanilla.lang.Thread.Prototype({Thread:function(a){this.target=a;this.delay=vanilla.lang.Thread.DefaultDelay;this.onSpawn=new vanilla.event.CustomEvent(this);this.onDie=new vanilla.event.CustomEvent(this);this.frozen=false;this.context=null},start:function(a){if(this.context==null){this.context={startTime:new Date(),manual:Thread.Continue,useInterval:a,timer:null};this.onSpawn.notify();this._sleep()}},_sleep:function(){if(this.isAlive()){if(!this.context.useInterval){setTimeout(this._wakeUp.bind(this,this.context),this.getDelay())}else{if(!this.context.timer){this.context.timer=setInterval(this._wakeUp.bind(this,this.context),this.getDelay())}}}},_wakeUp:function(a){var b=a.manual;if(b==Thread.Continue){b=this.run()}switch(b){case Thread.Pause:this.frozen=true;break;case Thread.Continue:this._sleep();break;case Thread.Stop:default:this._terminate(a);break}},_terminate:function(a){if(a==this.context){if(a.useInterval){clearInterval(a.timer);a.timer=null}this.context=null;this.onDie.notify()}},run:function(){if(this.target){return this.target.run()}return Thread.Stop},resume:function(){if(this.isAlive()&&this.frozen){this.frozen=false;this.context.manual=Thread.Continue;this._sleep()}},stop:function(){if(this.isAlive()){this.context.manual=Thread.Stop;this._terminate(this.context)}},pause:function(){if(this.isAlive()){this.context.manual=Thread.Pause;this.frozen=true}},getDelay:function(){return(this.target?this.target.getDelay():this.delay)},isAlive:function(){return(this.context!=null)},isFrozen:function(){return(this.context!=null&&this.frozen)}});vanilla.Class("vanilla.lang","SimpleThread");vanilla.lang.SimpleThread.Extends(vanilla.lang.Thread);vanilla.lang.SimpleThread.Prototype({SimpleThread:function(b,a){this.Thread();this.delay=a;this.callback=b},run:function(){var a=this.callback.apply(window,[]);if(a==Thread.Stop||a==Thread.Pause||a==Thread.Continue){return a}return Thread.Stop}});vanilla.extend(window,vanilla.lang);vanilla.Class("vanilla.event","CustomEvent");vanilla.event.CustomEvent.Prototype({owner:null,CustomEvent:function(a){this.owner=a;this.listeners=new Array()},subscribe:function(d,c,a){if(!d){return}if(c){d=d.bind(c)}var b={callback:d,acceptCtx:a};this.listeners.push(b)},unsubscribe:function(c,b){if(!c){return}if(b){c=c.bind(b)}for(var a=0;a<this.listeners.length;a++){if(this.listeners[a].callback.equals(c)){this.listeners.removeAtIndex(a);break}}},clearListeners:function(){this.listeners.length=0},notify:function(){var a=Arr(arguments);return this.notifyWithArguments(a)},notifyWithArguments:function(c){var b={event:this,owner:this.owner,stopPropagation:false,preventDefault:false};var a=Arr(c);var f=[b];f.addAll(a);for(var d=0;d<this.listeners.length;d++){var e=this.listeners[d];if(e.callback){e.callback.apply(window,e.acceptCtx?f:a);if(b.stopPropagation){break}}}return b}});Function.Static({fid:1,tid:1});Function.Prototype({bind:function(h){var g=this;var c=h;var a=new Array();for(var f=1;f<arguments.length;a[f-1]=arguments[f++]){}var k=function(){var b=new Array();for(var l=0;l<a.length;b.push(a[l++])){}for(var l=0;l<arguments.length;b.push(arguments[l++])){}return g.apply(c,b)};k._binded=g;k._target=c;var j=g.getBinded();var e=c.__tid=c.__tid||"t"+this._Function_.tid++;var d=j.__fid=j.guid=j.__fid||"f"+this._Function_.fid++;k.guid=e+"-"+d;return k},getBinded:function(){if(this._binded){return this._binded.getBinded()}return this},getTarget:function(){if(this._binded){var a=this._binded.getTarget();return(a==window?this._target:a)}return window},equals:function(a){if(!a){return false}return(this.guid==a.guid)}});Array.Static({createFrom:function(d){if(!d){return[]}if(vanilla.isInstanceOf(d,Array)){return d}var b=new Array();for(var c=0;c<d.length;b.push(d[c++])){}return b}});var Arr=Array.createFrom;Array.Prototype({first:function(){return this[0]},last:function(){return this[this.length-1]},clear:function(){this.length=0},shift:function(){var b=null;if(this.length){b=this[0];for(var a=0;a<this.length-1;this[a]=this[++a]){}this.length--}return b},indexOf:function(b){for(var a=0;a<this.length;a++){if(this[a]==b){return a}}return -1},next:function(b){var a=this.indexOf(b);if(a>=0){return this[++a>=this.length?0:a]}return null},previous:function(b){var a=this.indexOf(b);if(a>=0){return this[--a<0?this.length-1:a]}return null},contains:function(a){return(this.indexOf(a)>=0)},removeAtIndex:function(b){if(b<0||b>=this.length){return null}var c=this[b];for(var a=b;a<this.length-1;this[a]=this[++a]){}this.length--;return c},remove:function(b){var a=this.indexOf(b);if(a<0){return null}return this.removeAtIndex(a)},removeAll:function(b){if(!b){return}for(var c=0;c<b.length;this.remove(b[c++])){}},addAll:function(b){if(!b){return}for(var c=0;c<b.length;this.push(b[c++])){}},isEmpty:function(){return(this.length<=0)}});vanilla.extend(Array.prototype,{forEach:function(b){for(var a=0;a<this.length;a++){b(this[a],a)}}},true);String.Prototype({trim:function(){return vanilla.text.trim(this)},replaceAll:function(b,a){return vanilla.text.replaceAll(this,b,a)},startsWith:function(a){return this.indexOf(a)==0}});
