/**
 *	lib/dhtml.js - Common DHTML routines.
 *
 *	Joe Khoobyar <joe@ankhcraft.com>
 */
var dhtml = {};

/** returns the element identified by the given id (and optionally a type).
 *
 *	Note: the type is not optional for certain elements in NN4, 
 *	because of the way you must reference the element.
 */
dhtml.getElementById = function (id,type) {
	if (document.getElementById) {
		return document.getElementById (id);
	} else if (document.all) {
		return document.all [id];
	} else if (document.layers) {
		if (type=="anchor")
			return document.anchors [id];
		else if (type=="image")
			return document.images [id];
		else
			return document.layers [id];
	} else {
		return null;
	}
}

/** hides an element. */
dhtml.hideElement = function (e) {
	if (typeof (e) == "string")
		e = this.getElementById (e);
	if (e && e.style) {
		e.style.visibility = 'hidden';
		e.style.zIndex = -1;
	}
}

/** shows an element. */
dhtml.showElement = function (e) {
	if (typeof (e) == "string")
		e = this.getElementById (e);
	if (e && e.style) {
		e.style.visibility = 'visible';
		e.style.zIndex = 3;
	}
}

/**
 *	Get's an element's location on the page, or optionally with
 *	reference to a parent.
 *
 *	@param e	Element
 *	@param top	Relative to this parent/ancestor, otherwise the page.
 */
dhtml.getElementLocation = function (e,top) {
	if (typeof (e) == "string")
		e = this.getElementById (e);
	if (document.layers) 
		return { x:e.x, y:e.y }
	if (!top)
		top = document;
	var x=0, y=0;
	while (e.offsetParent && top!=e){
		x+= e.offsetLeft;
		y+= e.offsetTop;
		e = e.offsetParent;
	}
	return { x:x, y:y };
}

/** moves an element. */
dhtml.moveElementTo = function (e,x,y) {
	if (typeof (e) == "string")
		e = this.getElementById (e);
	if (e && e.style) {
		e.style.left = x;
		e.style.top = y;
	}
}

/** sets an image. */
dhtml.setImage = function (e,u) {
	if (typeof (e) == "string")
		e = this.getElementById (e,'image');
	if (e)
		e.src = u;
}
