/*

Подсветка рядов таблицы.

Прописывает обработчики события всем элементам TR, имеющим
указаный аттрибут (в данном случае - "parent")

Плохо стыкуется с сортировкой

*/

var onColor = '#ECEBEB'; // Цвет подсветки
var offColor = '#ffffff'; //Цвет без подсветки
var tagAttr = 'parent'; // Аттрибут для проверки, какие элементы обрабатывать

// суть скрипта
function setColor(el, _color) {
	if (el.tagName != "TR" || el.attributes.getNamedItem(tagAttr) == null) {
		while (el != null && !(el.tagName == "TR" && el.attributes.getNamedItem(tagAttr) != null)) {
			el = el.parentNode;
		}
	}
	if (el != null && el.tagName == "TR" && el.attributes.getNamedItem(tagAttr) != null) {
		el.style.backgroundColor = _color;
	}
	return false;
}

function onLight(e) {
	var el = window.event ? window.event.srcElement : e.currentTarget;
	return (setColor(el, onColor));
}

function offLight(e) {
	var el = window.event ? window.event.srcElement : e.currentTarget;
	return (setColor(el, offColor));
}

// ф-ция инициализации всего процесса
function init_highlight(e) {
	if (!document.getElementsByTagName) return;
	for (var j = 0; (table = document.getElementsByTagName("table").item(j)); j++) {
		var node;
		for (var i = 0; (node = table.getElementsByTagName("tr").item(i)); i++) {
			if (node != null && node.attributes.getNamedItem(tagAttr) != null) {
				if (node.addEventListener) {
					node.addEventListener("mouseover", onLight, false);
					node.addEventListener("mouseout", offLight, false);
				} else if (node.attachEvent) {
					node.attachEvent("onmouseover", onLight);
					node.attachEvent("onmouseout", offLight);
/*
					if (node.hasChildNodes) {
						var subnode = node.firstChild;
						detachBoth(subnode);
						while (subnode = subnode.nextSibling) {
							detachBoth(subnode);
						}
					}
*/
				}
			}
		}
	}
}

function detachBoth(node) {
	if (node != null) {
		if (node.detachEvent) {
			node.detachEvent("onmouseover", onLight);
			node.detachEvent("onmouseout", offLight);
		}
		if (node.hasChildNodes) {
			var subnode = node.firstChild;
			detachBoth(subnode);
			while (subnode != null) {
				subnode = subnode.nextSibling;
				detachBoth(subnode);
			}
		}
	}
}

// запускаем ф-цию init() при возникновении события load
var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root){
	if (root.addEventListener) root.addEventListener("load", init_highlight, false);
	else if (root.attachEvent) root.attachEvent("onload", init_highlight);
}
