// Creates a document element and extends that element 
// with additional methods used for DOM manipulation.
function $element(tag, attributes, html) {
	var element = document.createElement(tag);
	Object.extend(element, {
		addAttributes: function(attributes) {
			if (attributes) for (var key in attributes) {this.setAttribute(key, attributes[key]);}
			return this;
		},

		update: function(html) {
			if (html) Element.update(this, html);
			return this;
		},

		addBefore: function(elem) {
			elem.parentNode.insertBefore(this, elem);
			return this;
		},

		addChildren: function() {
			for (var i=0, child; child = arguments[i]; i++) {this.appendChild(child);}
			return this;
		},

		observe: function(action, fun) {
			Event.observe(this, action, fun);
			return this;
		},

		getText: function() {
			return this.textContent || this.innerText || this.text || null;
		}
	});
	return element.addAttributes(attributes).update(html);
}

