(function(){
var disp = Eanyee.Disposable = function(){}
disp.prototype = {
	dispose : function(h){
		var dis = this._disposes;
		if (!dis) dis = this._disposes = [];
		dis.push(h);
	},
	destroy : function(){
		var dis = this._disposes;
		if (dis){
			var n = dis.length;
			for(var i=0;i<n;i++) {try{dis[i]()}catch(e){}};
		}
	}
}
disp.prototype.destroy.virtual = true;
Eanyee.registerClass("Eanyee.Disposable");

var conp = Eanyee.Conponents = function(){}
var conpProto = conp.prototype = {
	add_child : function(ch,force){
		var chs = this._conpChs;
		if (!chs)  chs = this._conpChs = [];
		if (force){ch._conpPar = this; chs.push(ch); return true;}
		var n = chs.length;
		for(var i=0;i<n;i++){
			var c = chs[i];
			if (c===ch) return false;
		}
		chs.push(ch);
		return true;
	},
	remove_child : function(ch){
		var chs = this._conpChs;
		if (!chs) return false;
		var n = chs.length;
		var ret = false;
		for(var i=0;i<n;i++){
			var c = chs.unshift();
			if (c===ch){ch._conpPar = null;ret = true;}  
			else chs.push(c);
		}
		return ret;
	},
	remove_childAt : function(at,len){
		var chs = this._conpChs;
		if (!chs) return false;
		if (at>=chs.length || at<0)return false;
		chs.arrayObj.splice(at,len?len:1);
	},
	get_childAt : function(at){
		var chs = this._conpChs;
		if(!chs) return;
		if (at>=chs.length || at<0) return;
		return chs[at];
	},
	get_childCount : function(){
		var chs = this._conpChs;
		return chs?0:chs.length;
	},
	get_children : function(raw){
		var chs = this._conpChs;
		if(raw) return chs?chs:this._conpChs=[];
		if (!chs)return [];
		var ret = [];
		for(var i=0 ; i<chs.length;i++)ret.push(chs[i]);
		return ret;
	},
	get_parent : function(){return this._conpPar;},
	set_parent: function(p){
		if (this._conpPar == p) return false;
		p.add_child(this);
		return true;
	},
	clear_children : function(){
		delete this._conpChs;
	},
	each_child : function(handle){
		var chs = this._conpChs;
		if (!chs) return;
		var n = chs.length;
		for(var i=0;i<n;i++){
			var ch = chs[i];
			if (handle(ch,i)===false) return ch;
		}
	}
}
for(var n in conpProto){conpProto[n].virtual = true;}
Eanyee.registerClass("Eanyee.Conponents");

var makeClausor = function(){
	var clausor = [];
	var ret = function(){
		var n = clausor.length;
		for(var i=0;i<n;i++){
			var fn = clausor[i];
			fn.apply(this,arguments);
		}
	}
	ret.add = function(fn){
		clausor.push(fn);
	}
	ret.remove = function(fn){
		var s = [];
		var n = clausor.length;
		for(var i=0;i<n;i++) {
			f = clausor[i];
			if (f!==fn) s.push(f);
		}
		clausor = s;
	}
	ret.count = function(){return clausor.length;}
	return ret;
}
var Observ = Eanyee.Observable = function(){}
Observ.prototype = {
	attach : function(evt,handle){
		var evts = this._obsvEvts;
		if (!evts) evts = this._obsvEvts = {};
		var lstnrs = evts[evt];
		if (!lstnrs){lstnrs = evts[evt]= makeClausor();}
		lstnrs.add(handle);
		return lstnrs;
	},
	detach : function(evt,handle,remove){
		var evts = this._obsvEvts;
		if (!evts) return false;
		var lstnrs = evts[evt];
		if (!lstnrs) return false;
		lstnrs.remove(handle);
		if(remove && lstnrs.count()==0) delete evts[evt];
		return lstnrs;
	},
	notify : function(evt,args){
		var evts = this._obsvEvts;
		if (!evts) return false;
		var lstnrs = evts[evt];
		if (!lstnrs) return false;
		lstnrs.apply(this,args || []);
		return true;
	}
}
Observ.prototype.attach.virtual = Observ.prototype.detach.virtual = Observ.prototype.notify.virtual = true;
Eanyee.registerClass("Eanyee.Observable");
})();