// Copyright (C) 2007 Stephane Lavergne <http://www.imars.com/>
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.

// DOM stuff
imars.dom = {

	// Get elements by class name and optionally tag name.
	//   Written by Jonathan Snook, http://www.snook.ca/jonathan
	//   Add-ons by Robert Nyman, http://www.robertnyman.com
	//   Improved cross-browser portability by Stephane Lavergne, http://www.imars.com
	getElementsByClassName: function (doc, tName, cName) {
		var r = [];
		var e = (tName == "*" && doc.all) ? doc.all : (doc.getElementsByTagName ? doc.getElementsByTagName(tName) : []);
		cName = cName.replace(/\-/g, "\\-");  // I admit not knowing why dashes are escaped.
		var re = new RegExp("(^|\\s)" + cName + "(\\s|$)");
		var oe;
		for (var i = 0, max = e.length; i < max; i++) {
			oe = e[i];      
			if (re.test(oe.className)) {
				r.push(oe);
			};
		};
		return r;
	},

	// Get elements by id wildcard.
	//   Directly inspired by getElementsByClassName() above.
	getElementsById: function (doc, tName, idre) {
		var r = [];
		var e = (tName == "*" && doc.all) ? doc.all : (doc.getElementsByTagName ? doc.getElementsByTagName(tName) : []);
		idre = idre.replace(/\-/g, "\\-");  // I admit not knowing why dashes are escaped.
		idre = idre.replace(/\*/g, ".*");
		var re = new RegExp("(^|\\s)" + idre + "(\\s|$)");
		var oe;
		for (var i = 0, max = e.length; i < max; i++) {
			oe = e[i];      
			if (re.test(oe.id)) {
				r.push(oe);
			};
		};
		return r;
	},

	// Create a new style block with supplied CSS contents
	addCSS: function (cssText) {
		var node = document.createElement('style');
		node.type = 'text/css';
		node.rel = 'stylesheet';
		if (node.styleSheet) {
			node.styleSheet.cssText = cssText;  // MSIE...
		} else {
			node.appendChild(document.createTextNode(cssText));
		};
		document.getElementsByTagName('head')[0].appendChild(node);
		return false;
	},

	// Set display to none for node or node id.
	hide: function (el) {
		var e = el;
		if (!e.style && document.getElementById) e = document.getElementById(el);
		if (e.style) e.style.display = 'none';
		return false;
	},

	// Set display to block for node or node id.
	show: function (el) {
		var e = el;
		if (!e.style && document.getElementById) e = document.getElementById(el);
		if (e.style) e.style.display = 'block';
		return false;
	},

	// Toggle between display none and block for node or node id.
	toggle: function (el) {
		var e = el;
		if (!e.style && document.getElementByid) e = document.getElementById(el);
		if (e.style) {
			if (e.style.display == 'none') {
				e.style.display = 'block';
			} else {
				e.style.display = 'none';
			};
		};
		return false;
	},

	// Create new DOM node
	create: function (obj) {
		if (!obj.tagName) return false;  // Long live MSIE
		var result = document.createElement(obj.tagName);
		for (key in obj) {
			if (key == 'childNodes') {
				for (i=0, iMax=obj.childNodes.length; i < iMax; i++) {
					result.appendChild(imars.dom.create(obj.childNodes[i]));
				};
			} else if (key == 'innerText') {
				result.appendChild(document.createTextNode(obj.innerText));
			} else if (key != 'tagName') {
				result[key] = obj[key];
			};
		};
		return result;
	}

};
