/* Copyright 2008 Marc Mongenet */

var w2mlTemplates = new Array();
var w2mlTemplatesInstances = new Array();

function w2mlCountInstantiations(tid)
{
	if (!w2mlTemplatesInstances[tid])
		w2mlTemplatesInstances[tid] = 1;
	else 
		++w2mlTemplatesInstances[tid];
	return w2mlTemplatesInstances[tid];
}

function w2mlPrepareInstance2(elm, id, instance_nb, field_nb, syncIds, syncElms)
{
	var edId = elm.getAttribute("w2mledid");
	if (edId) { // Transform conficting edition into initialization.
		elm.setAttribute("w2mledid", "w2mli2"+id+"-"+instance_nb+"-"+field_nb+"-"+edId.slice(6+10+10));
		++field_nb;
	}
	edId = elm.getAttribute("w2mldel");
	if (edId) {
		syncIds[edId] = id+"-"+instance_nb+"-"+field_nb;
		elm.setAttribute("w2mldel", syncIds[edId]);
		++field_nb;
	}
	edId = elm.getAttribute("w2mlnew");
	if (edId) {
		syncIds[edId] = id+"-"+instance_nb+"-"+field_nb;
		elm.setAttribute("w2mlnew", syncIds[edId]);
		++field_nb;
	}
	if (elm.getAttributeNode("w2mlfrom") || elm.getAttributeNode("w2mlto")) {
		syncElms.push(elm);
	}
	for (var child = elm.firstChild; child; child = child.nextSibling)
		if (child.nodeType == 1)
			field_nb = w2mlPrepareInstance2(child, id, instance_nb, field_nb, syncIds, syncElms);
	return field_nb;
}

function w2mlPrepareInstance(elm, id, instance_nb)
{
	var syncIds = new Object();
	var syncElms = new Array();
	w2mlPrepareInstance2(elm, id, instance_nb, 1, syncIds, syncElms);

	for (var i in syncElms) {
		var a = syncElms[i].getAttributeNode("w2mlfrom");
		if (a && syncIds[a.value])
			a.value = syncIds[a.value];
		a = syncElms[i].getAttributeNode("w2mlto");
		if (a && syncIds[a.value])
			a.value = syncIds[a.value];
	}
}


/* Instantiate a template with data from 'base' element. */
function w2mlNew(base)
{
	/* Instantiate template */
	var w2mlfrom = base.getAttribute("w2mlfrom");
	var from = w2mlTemplates[w2mlfrom];
	if (!from) return;
	from = from.cloneNode(true);
	from.removeAttributeNode(from.getAttributeNode("w2mltemplate"));

	/* Update attributes for template edition */
	var w2mlnew = base.getAttribute("w2mlnew");
	var instance_nb = w2mlCountInstantiations(w2mlnew);
	var n2 = "w2mln2" + w2mlnew + "-" + instance_nb;
	w2mlPrepareInstance(from, w2mlnew, instance_nb);
	
	/* Insert instantiated template */
	var to = base.getAttribute("w2mlto");
	if (to == "firstSibling")
		base.parentNode.insertBefore(from, base.parentNode.firstChild);
	else if (to == "previousSibling")
		base.parentNode.insertBefore(from, base);
	else if (to == "here")
		base.parentNode.replaceChild(from, base);
	else if (to == "nextSibling")
		base.parentNode.insertBefore(from, base.nextSibling);
	else if (to == "lastSibling")
		base.parentNode.appendChild(from);
	else if (to == "firstChild")
		base.insertBefore(from, base.firstChild);
	else if (to == "lastChild")
		base.appendChild(from);
	else
		return;  // Internal error

	var instance_obj = { name: n2, value: "" };
	if (w2mlRegisterEditedObject(instance_obj)) {
		// No other editor UI (yet), provide a save button
		w2mlMakeSaveUI(w2mlUnderElmOnScreen(from));
	}

	// Place template then link editors (may open editor windows).
	w2mlLinkEditors(from);
}

function w2mlNewTo(edelm)
{
	var to = edelm.getAttribute("w2mlto");
	var dest = w2mlNewDestinations[to];
	if (dest) {
		w2mlNew(dest);
	}
}

function w2mlRegisternewEditor(elm)
{
	if (elm.getAttributeNode("w2mlfrom")) {
		elm.onclick = function () {
			w2mlNew(this);
			return false;
		}
	} else {
		elm.onclick = function () {
			w2mlNewTo(this);
			return false;
		}
	}
}

