// BLT+A ElementClass Ver0.01 alpha-03

var Elements;
function Element(){
	var a = arguments;
	
	switch(a.length){
		case 1 : {
			if(typeof a[0] == "string"){
				this.name = a[0];
				this.layer = getLayer(this.name);
				this.style = !C.NN4 ? this.layer.style : this.layer;
			} else if(typeof a[0] == "object"){
				if(a[0].consttuctor == Element){
					this.name = a[0].name;
					this.layer = a[0].layer;
					this.style = a[0].style;
				} else {
					this.layer = eval(a[0]);
					this.name  = !C.NN4 ? this.layer.id : this.layer.name ;
					this.style = !C.NN4 ? this.layer.style : this.layer ;
				}
			}
			break;
		}
	}
	if(C.NN4){
		function searchLayer(l,id){
			for(var i=0; i<l.length; i++){
				if(l[i].name == id){
					return l[i];
				}else if(l[i].document.layers.length != 0){
					var s = searchLayer(l[i].document.layers, id);
					if(s) return s;
				}
			}
			return null;
		}
	}
	function getLayer(name){
		var D = document;
		if(C.DOM && D.getElementById(name) != null && !( C.IE && C.MAC ) ) return D.getElementById(name);
		else if(C.IE && D.all[ name ] != null) return D.all[ name ];
		else if(C.NN4){
			if(D.layers[name] != null) return D.layers[name];
			var l = searchLayer(D.layers, name);
			if(l) return l;
		}
	}
}

function setElementMember(){
	var E = Element;
	var EP = E.prototype;
	
	if(C.NN4){
		function searchLayer_NN4( a ){
			for( var i in a ) if( a[i].name !== void 0 ){
				Elements[ a[i].name ] = new E( a[i] );
				if( a[i].document.layers.length != 0 ) searchLayer_NN4( a[i].document.layers );
			}
		}
	} else if(C.GK){
		function searchLayer_GK(a){
			for(var i=0; i<a.childNodes.length; i++){
				if(a.childNodes[i].toString().match(/object HTML.*Element/) != null){
					var n = a.childNodes[i];
					if(n.id) Elements[n.id] = new E(n);
					if(n.childNodes.length != 0) searchLayer_GK(n);
				}
			}
		}
	}
	E.setElementArray = function(){
		Elements = new Array();
		E.index = new Array();
		var index;
		var count = 0;
		if(C.IE){
			for(var i in document.all){
				index = i.toString();
				if(isNaN(index) && index != "length"){
					E.index[count++] = index;
					Elements[index] = new E(index);
				}
			}
		} else {
			if(C.NN4) searchLayer_NN4(document.layers);
			if(C.GK) searchLayer_GK(document.body);
			for(var i in Elements) E.index[count++] = Elements[i].name;
		}
	};
	
	EP.getID = function(){
		return this.name;
	};
	
	EP.setVisibility = function(v){
		this.style.visibility = (v ? true: false) ? (!C.NN4 ? "visible" : "inherit") : (!C.NN4 ? "hidden" : "hide");
	};
	EP.getVisibility = function(){
		var v = this.style.visibility;
		return ( v == "visible" || v == "show" || v == "inherit");
	};
	EP.setBackgroundColor = function(v){
		var c = v ? ((typeof v == "string") ? v : v.toColor() ) : '';
		if( c == '' && !C.IE) c = C.NN4 ? null : "transparent";
		if(!C.NN4){
			this.style.backgroundColor = c;
		} else {
			this.style.bgColor = c;
		}
	};
	EP.setBackgroundImage = function(p){
		if(!C.NN4){
			this.style.backgroundImage = "url("+p+")";
		}else{
			this.style.background.src = p;
		}
	};
	EP.initPosition = function(){
		if( !C.NN4 ){
			var o = this.layer;
			var s = o.style;
			if( C.GK ){
				s.left = o.offsetLeft;
				s.top = o.offsetTop;
			}else{
				s.pixelLeft = o.offsetLeft;
				s.pixelTop = o.offsetTop  ;
			}
		}
	};
	EP.setPosition = function(){
		var l,t;
		var a = arguments;
		switch(a.length){
			case 0 : default : l = t = 0 ; break;
			case 1 : {
				if( a[0].constructor == Point2D){
					l = a[0].x;
					t = a[0].y;
				} else {
					l = t = a[0];
				}
				break;
			}
			case 2 : {
				l = a[0];
				t = a[1];
				break;
			}
		}
		if(!C.NN4){
			this.style.left = l;
			this.style.top = t;
		} else {
			this.style.moveTo(l, t);
		}
	};
	EP.getPosition = function(){
		var s = this.style;
		var p = C.IE ? new Point2D(s.pixelLeft,s.pixelTop) : new Point2D(parseInt(s.left),parseInt(s.top));
		if(!C.NN4 && p.isNaN()){
			this.initPosition();
			if(C.GK){
				p.setX(parseInt(s.left));
				p.setY(parseInt(s.top));
			}else{
				p.setX(s.pixelLeft);
				p.setY(s.pixelTop);
			}
		}
		return p ;
	};
	EP.setPositionZ = function(d){
		this.style.zIndex = (!(d) || d < 1) ? 1 : d;
	};
	EP.initSize = function(){
		if( !C.NN4 ){
			var o = this.layer ;
			var s = o.style ;
			if( C.GK ){
				s.width  = o.offsetWidth ;
				s.height = o.offsetHeight ;
			}else{
				s.pixelWidth  = o.offsetWidth ;
				s.pixelHeight = o.offsetHeight ;
			}
		}
	};
	EP.setSize = function(){
		var w,h;
		var a = arguments;
		switch(a.length){
			case 0 : default : w = h = 0; break;
			case 1 :{
				if(a[0].constructor == Point2D ){
					w = a[0].x;
					h = a[0].y;
				} else {
					w = h = a[0];
				}
				break;
			}
			case 2 :{
				w = a[0];
				h = a[1];
				break;
			}
		}
		var s = this.style;
		if(!C.NN4){
			if(C.GK){
				s.width = w;
				s.height = h;
			} else {
				s.pixelWidth = w;
				s.pixelHeight = h;
			}
		} else {
			s.resizeTo(w,h);
		}
	};
	
	EP.setTrim = function(){
		var l,t,r,b;
		var a = arguments;
		switch(a.length){
			case 0 : default: l=t=r=b=0; break;
			case 1 : {
				l = a[0][0];
				t = a[0][1];
				r = a[0][2];
				b = a[0][3];
				break;
			}
			case 2 :{
				l = a[0].x;
				t = a[0].y;
				r = a[1].x;
				b = a[1].y;
				break;
			}
			case 4 :{
				l = a[0];
				t = a[1];
				r = a[2];
				b = a[3];
				break;
			}
		}
		if(!C.NN4){
			this.style.clip = "rect("+t+" "+r+" "+b+" "+l+")";
		} else {
			var c = this.style.clip;
			c.left = l;
			c.top = t;
			c.right = r;
			c.bottom = b;
		}
	}
	EP.getSize = function(){
		var s = this.style ;
		if( !C.NN4 ){
			var p = C.GK ? new Point2D(parseInt(s.width) , parseInt(s.height)) : new Point2D(s.pixelWidth ,s.pixelHeight);
			if(p.isNaN()){
				this.initSize();
				if(C.GK){
					p.set(parseInt(s.width) , parseInt(s.height));
				}else{
					p.set(s.pixelWidth,s.pixelHeight);
				}
			}
			return p ;
		}else{
			return new Point2D(s.clip.width,s.clip.height);
		}
	};
	EP.setText = function(t){
		if(!C.NN4) this.layer.innerHTML = t;
		else{
			var d = this.layer.document;
			d.open("text/html");
			d.write(t);
			d.close();
		}

	};
} setElementMember();
