(function(){
var E = Eanyee;
var D = E.Dom;
var B = D.Browser;
var UI = Eanyee.UI = {};

UI.Control = function(id,tagName){
	this.initialize(id,tagName);
}
var cproto = UI.Control.prototype = {
	initialize : function(cfg,tagName){
		for(var n in cfg){
			//var setter = this[n];
			//if (typeof(setter)==="function") setter.call(this,cfg[n]);
			this[n] = cfg[n];
		}		
		var dom = this.dom,d;
		var t = typeof(dom);
		if (t==='object'){
			d=dom;
		}else if (t==='string'){
			d = document.getElementById(dom);
		}
		if (!d){
			d =  document.createElement(tagName || 'div');	
			if (dom)d.id = dom;
		}
		this.dom = d;
		//d.innerHTML = "";
		d.control = this;
		var p = this._paint_;
		delete this._paint_;
		
		if (p)this.paint(this._display_,true);
		//alert(this.dom.outerHTML);
	},
	
	render : function(force){
		var rnd = this.rendered_;
		var dom = this.dom,s;
		if(force)dom.innerHTML = "";
		if(UI.Control.css) D.addCss(dom,UI.Control.css);
		var css = this._css_;
		if (css) D.addCss(dom,css);
		
		if (this._styleText_)dom.style.cssText = this._styleText_;
		if (this._width_!=null)dom.style.width = this._width_ + "px";
		if (this._height_!=null)dom.style.height = this._height_ + "px";
		if (this._display_!=null) dom.style.display = this._display_;
		if (this._visible_!=null) dom.style.visibility = this._visible_;
		
		//if (s){dom.style.overflow = 'auto';}
		
		if(this._text_) dom.innerHTML = this._text_;
				
		return (rnd)?this.rendered_ = true:this.rendered_ = 'init';
	},
	paint : function(display,force){
	    this.render(force);
	    D.setStyle(this.dom,{"display":display||"","visibility":'visible'});
	},
	destroy : function(){
		this.$base();
		
		var dom = this.dom;
		delete this.dom;
		if(dom){
			dom.control = undefined;
			if (dom.parentNode)dom.parentNode.removeChild(dom);
		}
		this.each(function(ch){
			ch.destroy();
		});
	},
	attach : function(evtN,func){
		var lstnrs = this.$base(evtN,func);
		if (lstnrs._rawevent===undefined && lstnrs.count()==1){
			try{
				D.addEvent(this.dom,evtN,lstnrs);
				lstnrs._rawevent = true;
			}catch(e){
				//alert(e.message);
				lstnrs._rawevent = false;	
			}
		} 
	},
	detach : function(evt,func,remove){
		var lstnrs = this.$base(evt,func,remove);
		if (lstnrs) return;
		if (lstnrs._rawevent && lstnrs.count()===0){
			D.removeEvent(this.dom,evt,lstnrs);
		}
	},
	notify : function(evt,args){
		this.$base(evt,args);
	},
	toggleDisplay : function(){
		if (this.rendered_){
			var dom = this.dom;
			return (dom.style.display=='none')?(dom.style.display=this._display||"") : dom.style.display="none";
		}
	},
	toggleVisible : function(){
		if (this.rendered_){
			var dom = this.dom;
			return (dom.style.visibility=='visible')?dom.style.visibility = 'hidden' : dom.style.visibility ='visible';
		}
	},
	Display : function(d){
		if (d===undefined) return this.rendered_?this.dom.style.display:this._display_;
		this._display_ = d;
		if (this.rendered_){
			this.dom.style.display = d;
		}
	},
	Visible : function(d){
		if (d===undefined) return this.rendered_?this.dom.style.visibility:this._display_;
		this._visible_ = d;
		if (this.rendered_){
			this.dom.style.visibility = d;
		}
	},
	
	Width : function(w){
		if (w===undefined) return this._width_;
		this._width_ = w;
		if (this.rendered_){
			this.dom.style.width = w + "px";
		}
	},
	Height : function(h){
		if (h===undefined) return this._height_;
		this._height_ = h;
		if (this.rendered_){
			this.dom.style.height = h + "px";
		}
	},

	
	Css : function(css){
		if (css===undefined) return this._css_;
		
		if (this.rendered_){
			if(this._css_) D.removeCss(this.dom,this._css_);
			D.addCss(this.dom,css);
			this._css_ = css;
			//this.dom.className = css;
		}else{this._css_ = css;}
	},
	StyleText : function(text){
		if (text===undefined) return this._styleText_;
		this._styleText_ = text;
		if (this.rendered_){
			this.dom.style.cssText = text;
		}
	},
	Text : function(text){
		if (text==undefined) return this._styleText_;
		this._text_ = text;
		if (this.rendered_){
			this.dom.innerHTML = text;
		}
	}
}
cproto.paint.virtual = cproto.render.virtual = 
cproto.Width.virtual = cproto.Height.virtual = cproto.Css.virtual = cproto.StyleText.virtual = 
cproto.Text.virtual = true;

E.registerClass("Eanyee.UI.Control",E.Disposable,E.Conponents,E.Observable);
UI.Layout = function(){}
E.registerClass("Eanyee.UI.Layout",UI.Control);
UI.Decorator = function(){}
UI.Decorator.prototype = {
	init : function(ctrl){
		//var ctrl = this.innerControl;
		for(var n in ctrl){
			var m = ctrl[n];
			if (typeof(m)==='function' && !this[n]) this[n] = E.delegate(ctrl,m);
		}
	}
}
E.registerClass("Eanyee.UI.Decorator",UI.Control);

UI.PicDecorator = function(ctrl,css,t_l,t,t_r,l,r,b_l,b,b_r){
	this.config(arugments.length==1?ctrl:{
		innerControl : ctrl,
		TopLeft : t_l,
		Top : t,
		TopRight : t_r,
		Left : l,
		Right :r,
		BottomLeft : b_l,
		Bottom: b,
		BottomRight : b_r,		
		Css : css
	},'table');
    this.init();

}
UI.PicDecorator.prototype = {
	render : function(force){
		if (!this.$base(force)) return false;
		var dom = this.dom;
		var tbody = document.createElement('tbody');
		var G = D.G;
		G.create(tbody,3,3);
		function setSrc(y,x,n,url){
			var cell = tbody.rows[y].cells[x];
			cell.classname = n;
			if (url){
				var img = document.createElement("img");
				img.src = url;
				cell.appendChild(img);
			}
		}
		setSrc(0,0,'TopLeft',this._t_l);
		setSrc(0,1,'Top',this._t);
		setSrc(0,2,'TopRight',this._t_r);
		setSrc(1,0,'Left',this._l);
		setSrc(1,1,'Center',null);
		setSrc(1,2,'Right',this._r);
		setSrc(2,0,'BottomLeft',this._b_l);
		setSrc(2,1,'Bottom',this._b);
		setSrc(2,2,'BottomRight',this._b_r);
		if (this._w){
			dom.style.width = this._w + "px";
		}
		if(this._h){
			dom.style.height = this._h + "px";
		}
		var ctrl = this.innerControl;
		var md = tbody.rows[1].cells[1];
		ctrl.render();

		ctrl.Width(md.clientWidth);
		ctrl.Height(md.clientHeight);
		md.appendChild(ctrl.dom);
	},
	Width : function(w){
		if(v==undefined) return this._w;
		this._w = w;
		if (this._rendered){
			this.dom.style.width = w;
			var ctrl = this.innerControl;
			var md = this.dom.tBodies.rows[1].cells[1];
			ctrl.Width(md.clientWidth);
			//ctrl.Height(md.clientHeight);
		}
	},
	Height: function(h){
		if(v==undefined) return this._h;
		this._h = w;
		if (this._rendered){
			this.dom.style.width = w;
			var ctrl = this.innerControl;
			var md = this.dom.tBodies.rows[1].cells[1];
			ctrl.Width(md.clientHeight);
			//ctrl.Height(md.clientHeight);
		}
	},
	_setSrc : function(y,x,n,v){
		if(v==undefined) return this[n];
		this[n] = v;
		if (this._rendered){
			var cell = this.dom.tBodies[0].rows[y].cells[x];
			if (cell.firstChild) {
			 	if(v)cell.firstChild.src = v;
				else cell.removeChild(cell.firstChild);
			}else{
				if (!v) return this;
				var img = document.createElement("img");
				img.src = v;
				cell.appendChild(img);
			}
		}
		return this;
	},
	TopLeft : function(v){
		return this._setSrc(0,0,'_t_l',v);
	},
	Top : function(v){
		return this._setSrc(0,1,'_t',v);
	},
	TopRight : function(v){
		return this._setSrc(0,2,'_t_r',v);
	},
	Left : function(v){
		return this._setSrc(1,0,'_l',v);
	},
	Right : function(v){
		return this._setSrc(1,2,'_r',v);
	},
	BottomLeft : function(v){
		return this._setSrc(2,0,'_b_l',v);
	},
	Bottom : function(v){
		return this._setSrc(2,1,'_b',v);
	},
	BottomRight : function(v){
		return this._setSrc(2,1,'_b_r',v);
	}
}
E.registerClass("Eanyee.UI.Decorator",UI.PicDecorator);
})();