/**
  * core.js
  *
  * $Id: core.js,v 1.22 2001/09/04 16:56:20 rainwater Exp $
  *
  * jsDOM API
  * Copyright (C) 2001 Robert Rainwater
  */

is={min :document.getElementById?true:false,
	ie  :document.getElementById&&document.all?true:false}

function DOMElement(id,style) {
	if (!id) return null;
	if (DOMElement.all[id]) return DOMElement.all[id];
	this.elm=document.getElementById(id);
	if (!this.elm) {
		this.elm=document.createElement("DIV");
		this.elm.style.position="absolute";
	}
	this.elm.id=id;
	this.elm.className=style||this.elm.className||id;
	this.style=this.elm.style;
	DOMElement.all[this.elm.id]=this;
	return this;
}
DOMElement.all=[];
DOMElement.prototype.toString	=	function()			{ return "DOMElement.all."+this.elm.id;					}
DOMElement.prototype.getX		=	function()			{ return this.elm.offsetLeft||0;						}
DOMElement.prototype.getY		=	function()			{ return this.elm.offsetTop||0;							}
DOMElement.prototype.getWidth	=	function() 			{ return this.elm.offsetWidth||0;						}
DOMElement.prototype.getHeight	=	function() 			{ return this.elm.offsetHeight||0;						}
DOMElement.prototype.getZIndex	=	function()			{ return this.style.zIndex;								}
DOMElement.prototype.getBgColor	=	function()			{ return this.style.backgroundColor;					}
DOMElement.prototype.getHTML	=	function()			{ return this.elm.innerHTML||"";						}
DOMElement.prototype.setHTML	=	function(h)			{ this.elm.innerHTML=h;									}
DOMElement.prototype.setClip	=	function(t,r,b,l)	{ this.style.clip="rect("+t+" "+r+" "+b+" "+l+")";		}
DOMElement.prototype.setX		=	function(x)			{ if (x) this.style.left=x+"px";						}
DOMElement.prototype.setY		=	function(y)			{ if (y) this.style.top=y+"px";							}
DOMElement.prototype.moveTo		=	function(x,y)		{ this.setX(x); this.setY(y);							}
DOMElement.prototype.setWidth	=	function(w)			{ if (w) this.style.width=w+"px";						}
DOMElement.prototype.setHeight	=	function(h)			{ if (h) this.style.height=h+"px";						}
DOMElement.prototype.setSize	=	function(w,h)		{ this.setWidth(w); this.setHeight(h);					}
DOMElement.prototype.setVisible =	function(b)			{ this.style.visibility=b?"visible":"hidden";			}
DOMElement.prototype.setBgColor =	function(c)			{ this.style.backgroundColor=c;							}
DOMElement.prototype.setStyle	=	function(s)			{ this.elm.className=s;									}
DOMElement.prototype.appendChild=	function(c)			{ this.elm.appendChild(c.elm);							}
//Events
DOMElement.prototype.addEventListener=function(type,fn) {
	DOMEvent.addEvent(this.elm,type,fn);
	eval(this["on"+type]=fn)
}
DOMElement.prototype.removeEventListener=function(type,fn) {
	DOMEvent.removeEvent(this.elm,type,fn);
	eval(this["on"+type]=null)
}
DOMElement.prototype.invokeEvent=function(type,args) {
    if(this["on"+type]) {
        this["on"+type](args);
    }
}
DOMElement.prototype.cancelBubble=function() {
	this.addEventListener("mouseup",new Function("e","var e=e||event; e.cancelBubble=true;"));
	this.addEventListener("mouseout",new Function("e","var e=e||event; e.cancelBubble=true;"));
	this.addEventListener("mouseover",new Function("e","var e=e||event; e.cancelBubble=true;"));
	this.addEventListener("mousedown",new Function("e","var e=e||event; e.cancelBubble=true;"));
}

function DOMEvent(e) {
	this.evt=e||event;
	for (this.src=is.ie?this.evt.srcElement:this.evt.target;this.src.tagName!='DIV'&&this.src.parentNode&&this.src.parentNode!=this.src;this.src=this.src.parentNode);
	this.target=this.evt.target||this.evt.srcElement||null;
	this.type=this.evt.type;
	this.pageX=this.evt.clientX;
	this.pageY=this.evt.clientY;
	return this;
}
DOMEvent.addEvent=function(obj,type,fn,useCapture) {
	if (is.ie) obj.attachEvent("on"+type,fn);
	else obj.addEventListener(type,fn,useCapture||false);
}
DOMEvent.removeEvent=function(obj,type,fn,useCapture) {
	if (is.ie) obj.detachEvent("on"+type,fn);
	else obj.removeEventListener(type,fn,useCapture||false);	
}
