/* $Id: editorns.js,v 1.16.2.3 2007/08/16 09:55:58 max Exp $ */

function lmo(obj, event) {
	if (!obj || !obj.id)
		return false;

	if (!obj.onmouseout)
		obj.onmouseout = lmu;

	if (!obj.onclick)
		obj.onclick = lmc;

	// find template div or span
	var templates = grabTemplateNames(event.currentTarget);
	setTimeout("setStatus(\""+templates+"\", \""+obj.id+"\")", 20);
	lastID = obj.id;
	var aElement = findAElement(event.currentTarget);
	// check if there is Label dialog open
	if (aElement != null && (labelWindow == null || labelWindow.closed)) {
		aElement.addEventListener("keypress", lkp, true);
		// focus the link
		aElement.focus();
	}
}

function grabTemplateNames(elem) {
	var templates = "", tst=""
	while (elem != null) {
		if (elem.tagName != null && (elem.tagName.toUpperCase() == 'DIV' || elem.tagName.toUpperCase() == 'SPAN' || elem.tagName.toUpperCase() == 'TBODY')) {
			var attr = elem.attributes.getNamedItem("id");
			if (attr != null && attr.name != null && attr.value.indexOf('.tpl') > 0)
				templates = attr.value.replace(/(\w)0(\w)/g, "$1/$2").replace(/\.tpl\d+/, ".tpl") + " > " + templates;
		}	
		elem = elem.parentNode;
	}
	return templates;
}

function findAElement(elem) {
	while ((elem.parentNode.tagName.toUpperCase() != 'A' || elem.parentNode.href == '') && elem.parentNode.tagName.toUpperCase() != 'BODY')
		elem = elem.parentNode;
	if (elem.parentNode.tagName.toUpperCase() != 'A')
		return null;
	return elem.parentNode;
}

function lamo(event) {
	setStatus (grabTemplateNames(event.currentTarget), lastID);
}

function lmu(event) {
	lastID = window.status = '';
}

function lkp(event){
	if (event.charCode == 69 || event.charCode == 101) { /* 'E', 'e' */
		if (lastID!='')
			showLabelForm(lastID);
	}
}

function lmc(event) {
	if (findAElement(event.currentTarget) == null)
		showLabelForm(this.id);
}

function setLabel() {
	if (!labelWindow || !labelWindow.document || !labelWindow.document.lf || !labelWindow.document.lf.val)
		return;

	// find all spans having onMouseOver=lom('labelWindow.lf.val.value')
	allElem = document.body.getElementsByTagName("DIV");
	var v = labelWindow.document.lf.val.value ? labelWindow.document.lf.val.value : ("&lt;"+labelID+"&gt;");
	for (var a = 0; a < allElem.length; a++) {
		var attr = allElem[a].attributes.getNamedItem("id");
		if (attr != null && attr.value == labelID) {
			allElem[a].innerHTML = v;
			lng_labels[labelID] = labelWindow.document.lf.val.value ? xescape(v) : '';
		}
	}	
}

function showLabelForm(id) {
	if (labelWindow != null && !labelWindow.closed)
		labelWindow.close();
	if (defaultLabelWindowX == 0) {
		defaultLabelWindowX = window.screenX+100;
		defaultLabelWindowY = window.screenY+100;
	}
	openLabelForm(id);
}

function dmo(obj, event) {
	if (!obj)
		return false;

	if (!obj.onmouseout)
		obj.onmouseout = dmu;

	if (window.status == '')
		window.status = grabTemplateNames(event.currentTarget);
}

/*
	functions to run from debug console
*/

/*
	make a border around the template
*/
function markTemplate(tmplt, remove) {
	/* find all spans and divs having name=tmplt */
	var tags = ['SPAN',"DIV","TBODY"];
	var reg = new RegExp("^"+tmplt.replace(/\./, "\."), "");
	var borderStyle = remove ? "0pt none black" : "1pt solid black";

	for (var i = 0; i < tags.length; i++) {
		var allelem = document.body.getElementsByTagName(tags[i]);
		for (var a = 0; a < allelem.length; a++) {
			var attr = allelem[a].attributes.getNamedItem("id");
			if (attr == null || attr.value.search(reg) == -1)
				continue;

			if (tags[i] == 'TBODY') {
				for (var j = 0; j < allelem[a].childNodes.length; j++) {
					var row = allelem[a].childNodes[j];
					for (var k = 0; k < row.childNodes.length; k++) {
						if (row.childNodes[k].tagName == 'TD')
							row.childNodes[k].style.border = borderStyle;
					}	
				}
			} else {
				markElement(allelem[a], remove);
			}
		}
	}
}

function tmo(tmplt, event) {
	markTemplate(tmplt, 0);
}
function tmu(tmplt, event) {
	markTemplate(tmplt, 1);
}


