// JavaScript Document
function insBefore(i) {
	site.insertBefore(i,'<content/>');
	navigate(location);
}
function insAfter(i) {
	site.insertAfter(i,'<content/>');
	navigate(location);
}
function delNode(i) {
	if (confirm('Deseja eliminar este elemento e a informação associada?')) {
		site.removeNode(i);
		navigate(location);
	}
}

function makeTeaser(i) {
	var n=site.getNode(i);
	var t='<container><teaser><![CDATA['+n.text+']]></teaser><page><content/></page></container>';
	//alert(n.text);
	//site.overNode(i,t);
	site.setNode(i,t);
	navigate(location);
}

function makeLink(i) {
	//alert(i);
	var n=site.getNode(i);
	var t='<repository><link_teaser><![CDATA['+n.text+']]></link_teaser><link/></repository>';
	//alert(n.text);
	site.setNode(i,t);
	navigate(location);
}

function makeTabular(i) {
	var n=site.getNode(i);
	var t='<tabular rows="*" cols="3">'+n.xml+'</tabular>';
	site.setNode(i,t);
	navigate(location);
}

function makePagination(i) {
	var n=site.getNode(i);
	var t='<pagination><page>'+n.xml+'</page></pagination>';
	site.setNode(i,t);
	navigate(location);
}

function makeList(i) {
	var n=site.getNode(i);
	var t='<list rows="2" cols="3" style="" elementstyle="" structstyle="">'+n.xml+'</list>';
	site.setNode(i,t);
	navigate(location);
}

//-------------- control box functions
function inpress_delNode(o) {
	if (confirm("apagar este item?\n"+o.pos)) {
		var langfree=site.getNode(o.pos).selectSingleNode("@langfree");
		if (langfree&&langfree.text=="true")
			site._removeNode(o.pos);
		else
			site.removeNode(o.pos);
		navigate(document.location);
	}
}
function inpress_setText(o,value) {
	var langfree=site.getNode(o.pos).selectSingleNode("@langfree");
	if (langfree&&langfree.text=="true")
		site._setText(o.pos,value);
	else
		site.setText(o.pos,value);
	navigate(location);
}
function inpress_upNode(o) {
	var langfree=site.getNode(o.pos).selectSingleNode("@langfree");
	if (langfree&&langfree.text=="true")
		site._moveUp(o.pos);
	else
		site.moveUp(o.pos);
	navigate(location);
}

function inpress_dnNode(o) {
	var langfree=site.getNode(o.pos).selectSingleNode("@langfree");
	if (langfree&&langfree.text=="true")
		site._moveDown(o.pos);
	else
		site.moveDown(o.pos);
	navigate(location);
}

//-------------------------------------------------------------------------------------------
//---------------- prototypes
function getId(serial) {
	if (!serial) serial="id";
	return ctrl.aquireId("/serial[@id='"+serial+"']").text;
}

function expandCheckList(chk,list) {
    //alert("expandCheckList");
	for(var c=list.firstChild;c;c=c.nextSibling) {
        if(c.nodeName=="checklist") {
            //alert("we have checklists");
            var newNode=chk.appendChild(makeNode('<'+c.selectSingleNode("@name").text+' id="'+getId()+'" as="checklist" src="//checklist[@id=\''+c.selectSingleNode("@id").text+'\']" name="'+c.selectSingleNode("@name").text+'"/>'));
            expandCheckList(newNode,c);
        }
		//alert(c.xml);
		else chk.appendChild(makeNode(c.xml));
	}
}

//adiciona o novo elemento replicando um prototipo
function createPrototype(protoPath,pos,insert) {
	//alert("createPrototype: "+protoPath);
	//var ref=getId();
	var protoNode=proto.getNode(protoPath);
	var px=protoNode.firstChild.xml;
	var npa=px.split("{$id}");
	var res=npa[0];
	for(n=0;n<npa.length-1;n++)
		res+=getId()+npa[n+1];
	
	//agora vamos expandir os checklists utilizando os actualizados
	var cp=makeNode(res);
	//alert(cp.xml);
	//var id=cp.selectSingleNode("@id").text;
	var chk=cp.selectNodes("checklists|*[@as='checklist']");//<--- o prototipo aponta uma checklist (ou lista de checlists)
	//if (chk.length) alert("it has a check list!"+chk.length);
	for(var n=0;n<chk.length;n++) {
		if (!chk[n].hasChildNodes()) {
			//alert(chk[n].selectSingleNode("@id").text);
			var list=site.getNode(chk[n].selectSingleNode("@src").text);
			//alert(list.xml);
            expandCheckList(chk[n],list);
		}
	}
	//alert(cp.xml);
	res=cp.xml;
	if (insert) site.insertNode(pos,res);
	else site.appendNode(pos,res);
	navigate(location);
}

function applyPrototype(p,insert) {
	//alert("applyPrototype:"+p.protoPath,p.pos);
	createPrototype(p.protoPath,p.pos,insert);
}

function applySelPrototype(o,insert) {applyPrototype(o.options[o.selectedIndex],insert);}

//cria os botoes para novos elementos
function expandPrototype(p,pos,protoPath,mult) {
	//alert("expandPrototype:"+p+"\n"+pos+"\n"+protoPath);
	var pid="prototypes_"+p.selectSingleNode("@id").text;
	switch (p.nodeName) {
	case "protoform":
		document.write('<div>expandir aqui um formulário para novo elemento e possivelmente editar elementos existentes? (era bom)</div>');
		p=p.selectSingleNode("prototype");
	case "prototype":
		var name=p.selectSingleNode("@name").text;
		var desig=p.selectSingleNode("@desig").text;
		//alert(p.xml);
		//alert(protoPath);
		var proto='//prototype[@id=\''+p.selectSingleNode("@id").text+'\']';
		if (mult) document.write('<option value="'+name+'" title="'+desig+'" pos="'+pos+'" protoPath="'+proto+'">'+name+'</option>');
		else {
			document.write('<input id="'+pid+'" type="button" value="Inserir" title="Inserir este elemeto no inicio." desig="'+desig+'" pos="'+pos+'" protoPath="'+proto+'" onclick="applyPrototype(this,true)"/>');
			document.write('<input id="'+pid+'_append" type="button" value="Adicionar" title="Adicionar este elemento no fim." pos="'+pos+'" protoPath="'+proto+'" onclick="applyPrototype(this,false)"/>');
		}
		break;
	default:
		document.write('<select id="'+pid+'" onChange="document.getElementById(id+\'_info\').innerHTML=options[selectedIndex].title">');		
		for(var n=p.firstChild,i=0;n;n=n.nextSibling,i++)
			expandPrototype(n,pos,protoPath+"/*["+i+"]",true);//expande nós de prototipos (em arvore)
		document.write('</select>');
		document.write('<input type="button" onclick="applySelPrototype(document.getElementById(\''+pid+'\'),true)" value="Inserir" title="Inserir este elemeto no inicio.">');
		document.write('<input type="button" onclick="applySelPrototype(document.getElementById(\''+pid+'\'),false)" value="Adicionar" title="Adicionar este elemento no fim.">');
	}
	
}
function prototyper(protoPath,pos) {//cria div para os botões (criar novos elementos)
    //alert(protoPath);
    try {
	    var p=proto.getNode("/prototypes"+protoPath);
	    //alert(p.xml);
	    if (p) {
		    var pid="prototypes_"+p.selectSingleNode("@id").text;
		    document.write('<div class="prototypes resume"><span>Prototipos:</span><div style="display:none">');
		    expandPrototype(p,pos,protoPath);
		    document.write('<span id="'+pid+'_info"></span>');
		    document.write('</div></div>');
		    var sel=document.getElementById(pid);
		    var info=document.getElementById(pid+'_info');
		    if (sel.tagName=="SELECT") info.innerHTML=sel.options[sel.selectedIndex].title;
		    else info.innerHTML=sel.desig;
	    }
	} catch(e) {if (editing) alert(e.message);}
}



