// This material copyright of the respective organisation. All rights reserved. Not to be reused without permission. 
var subpart;
var subtype;
var underway;
var moveinto;

function edm_close(t) {
	document.getElementById('edm_'+t).style.display='none';
}

function edmMouseOut(e) {
	e = e ? e : window.Event;
	var tg = e.srcElement || e.target;
	var id = 'edm_'+subtype;
	var dest = e.toElement || e.relatedTarget;
	if( domId(domParent(dest))!=id && domId(dest)!=id && domId(domParent(domParent(dest)))!=id )
		edm_close(subtype);
}

function domParent(i) {
	if(!i) return false;
	return i.parentElement || i.parentNode;
}

function domId(i) {
	if(!i) return false;
	return i.id || i.name;
}

function edm_c(e,n,id) { /* part of a category */
	edm_open(e,n,id,'c');
}

function edm_p(e,n,id) { /* paragraph */
	edm_open(e,n,id,'p');
}

function edm_l(e,id) { /* part of a list */
	edm_open(e,id,id,'l');
}

function edm_d(e,n,id) { /* discussion part */
	edm_open(e,n,id,'d');
}

function edm_sq(e,id) { /* quip (self) */
	edm_open(e,id,id,'sq');
}

function edm_sp(e,id) { /* paragraph (self) */
	edm_open(e,id,id,'sp');
}

function edm_t(e,n,id) { /* document title */
	edm_open(e,n,id,'t');
}

function edm_ti(e,n,id) { /* document title icon */
	edm_open(e,n,id,'ti');
}

function edm_edit() {
	edm_close(subtype);
	if(subtype=='p')
		LoadPage('/'+subpart+'/');
}

function edm_newp() {
	edm_close(subtype);
	document.edmf.func.value = 'newp';
	document.edmf.subpart.value = subpart;
	document.edmf.submit();
}

function choosecat(cat) {
	choosething(cat,0);
}
function choosesearch() {
	choosething('search','search');
}
function choosenothing() {
	choosething('none','none');
}
function choosething(cat,id) {
	document.ctf.cat.value = cat;
	document.ctf.choose.value = id;
	document.ctf.submit();
}

function edm_sicon() {
	edm_close(subtype);
	document.edmf.func.value = 'seticon';
	document.edmf.subpart.value = subpart;
	document.edmf.submit();
}

function edm_goto() {
	edm_close(subtype);
	LoadPage('/'+subpart+'/');
}

function edm_gotop() {
	edm_close(subtype);
	window.open('/'+subpart+'/','_blank');
}

function edm_open(e,n,id,t) { /* event, nidi, subpart, subtype */
	document.edmf.nidi.value = n;
	if(underway=='movestart' && (t=='p' || t=='t' || t=='c' || t=='l' || t=='d') && id!=subpart) {
		/* help message: Moving to after this position */
		document.edmf.applicator.value = id;
		if(moveinto)
			document.edmf.func.value = 'moveinto';
		else
			document.edmf.func.value = 'move';
		document.edmf.submit();
		return false;
	}
	underway = false;
	edm_openmenu(e,t);
	subpart = id;
	subtype = t;
}

function edm_openmenu(e,t) {
	var i = document.getElementById('edm_'+t);
	if(!i) alert("no edm_"+t);
	e = e ? e : window.Event;
	var x,y;
	if (e.clientX) {
		x = e.clientX;
		y = e.clientY;
	}
	x -= 37;
	y -= 9;
	x = Math.max(0,x);
	y = Math.max(0,y);
	var wh = $(window).height();
	var ww = $(window).width();
	i.style.top= parseInt(y)+'px';
	i.style.left= parseInt(x)+'px';
	i.style.display='block';
	var mh = i.offsetHeight;
	var mw = i.offsetWidth;
	if(wh && mh) { /* valid values, so use them to keep the menu from going off the right or bottom */
		y = Math.min(y,wh-mh);
		x = Math.min(x,ww-mw);
		i.style.top= parseInt(y)+'px';
		i.style.left= parseInt(x)+'px';
	}
}

function edm_chatype(e,n,id,targ,m,t) {
	edm_zopen(e,n,id,targ,m);
	document.edmf.applicator.value = t;
}

function edm_zopen(e,n,id,t,m) { /* event, nidi, subpart, target, menu type */
	document.edmf.nidi.value = n;
	if(underway=='movestart' && (m=='p' || m=='t' || m=='c' || m=='l' || m=='d' || m=='wh' || m=='whh' || m=='wc' || m=='wr' || m=='wt') && id!=subpart) {
		/* help message: Moving to after this position */
		document.edmf.applicator.value = id;
		if(moveinto)
			document.edmf.func.value = 'moveinto';
		else
			document.edmf.func.value = 'move';
		document.edmf.totarget.value = t;
		document.edmf.submit();
		return false;
	}
	if(underway=='moveuxte' && (m=='wh' || m=='t')) {
		OrderWidget(m=='t'?'first':id);
		return false;
	}
	document.edmf.target.value = t;
	underway = false;
	edm_openmenu(e,m);
	subpart = id;
	subtype = m;
}

function edm_viewatt() {
	edm_close(subtype);
	viewatt(document.edmf.nidi.value, subpart);
}
function viewatt(n,a) {
	LoadPage('/'+n+'/fd='+a);
}


function edm_choose_vote(v) {
	edm_close(subtype);
	document.edmf.applicator.value = v;
	document.edmf.func.value = 'choosevote';
	document.edmf.subpart.value = subpart;
	document.edmf.submit();
}

function edm_clipb() {
	edm_close(subtype);
	document.edmf.applicator.value = '';
	edm_clipbsub();
}
function edm_clipbn() {
	edm_close(subtype);
	var note = window.prompt('Note for the clipboard:');
	if(note != null && note != 'Null') {
		document.edmf.applicator.value = note;
		edm_clipbsub();
	}
}
function edm_clipbsub() {
	document.edmf.func.value = 'clipb';
	document.edmf.subpart.value = subpart;
	document.edmf.submit();
}

function edm_tedit() {
	document.getElementById('title'+subpart).style.display='none';
	document.getElementById('edittitle'+subpart).style.display='block';
	edm_close(subtype);
}

function cancel_tedit() {
	document.getElementById('title'+subpart).style.display='block';
	document.getElementById('edittitle'+subpart).style.display='none';
}

function edm_addquip(id) {
	document.getElementById('i'+id).style.display='none';
	document.getElementById(id).style.display='block';
	edm_close(subtype);
}

function cancel_addquip(id) {
	document.getElementById('i'+id).style.display='block';
	document.getElementById(id).style.display='none';
}

function edm_rpart() {
	document.edmf.subpart.value = subpart;
	document.edmf.func.value = 'rpart';
	edm_close(subtype);
	document.edmf.submit();
}

function edm_clipjot() {
	edm_close(subtype);
	document.edmf.func.value = 'clipjot';
	var note = window.prompt('Note for the clipboard:');
	if(note != null && note != 'Null') {
		document.edmf.applicator.value = note;
		document.edmf.subpart.value = subpart;
		document.edmf.submit();
	}
}

function edm_nick() {
	edm_close(subtype);
	var note = window.prompt(
"A nickname is your personal name for a thing.\n"+
"It doesn't actually rename the thing. What do you\n"+ 
"want to nickname this? To remove a nickname,\n"+ 
"enter nothing or click 'cancel'.");
	document.edmf.applicator.value = note;
	if(note==null || note=='Null')
		document.edmf.applicator.value = '';
	document.edmf.func.value = 'nickn';
	document.edmf.subpart.value = subpart;
	document.edmf.submit();
}

function customdiscsize(id) {
	var size = window.prompt("How many posts do you want to display per page for this discussion? Click 'cancel' to remove this custom setting. You can set your site-wide custom discussion size using the 'Display Options' special page.");
	if(size==null || size=='Null')
		size = 0;
	document.edmf.applicator.value = size;
	document.edmf.func.value = 'customsize';
	document.edmf.nidi.value = id;
	document.edmf.submit();
}

function edm_rclip() {
	edm_close(subtype);
	document.edmf.subpart.value = subpart;
	clipboardish_delete();
}

function clipboardish_empty(u) {
	document.edmf.subpart.value = 0;
	document.edmf.uxte.value = u;
	clipboardish_delete();
}

function clipboardish_delete() {
	document.edmf.func.value = 'clipboardish_delete';
	document.edmf.submit();
}

function edm_movestart(e,into) {
	underway = 'movestart';
	moveinto = into;
	document.edmf.subpart.value = subpart;
	edm_close(subtype);
	edm_help(e, "Click the object that you want to position this after. Click the first object again to cancel.");
}

function edm_help(e,str) {
	edm_openmenu(e,'help');
	document.getElementById('edm_help').innerHTML = str;
}

function closeHelp() {
	edm_close('help');
}


function empty_cat(c,r) {
	document.edmf.nidi.value = c;
	document.edmf.func.value = 'emptycat';
	document.edmf.applicator.value = r;
	document.edmf.submit();
}

function aewDelete(n) {
	document.edmf.nidi.value = 0;
	document.edmf.func.value = 'aewDelete';
	document.edmf.subpart.value = n;
	document.edmf.submit();
}

function aewEditNumber(n,v) {
	document.edmf.nidi.value = 0;
	document.edmf.func.value = 'aewEdit';
	document.edmf.subpart.value = n;
	document.edmf.applicator.value = window.prompt("Enter the new value:",v);
	document.edmf.submit();
}

function aewNewRad(subpart,thing) {
	document.edmf.func.value = 'newrad';
	document.edmf.subpart.value = subpart;
	document.edmf.applicator.value = thing;
	document.edmf.submit();
}

function edm_DeleteRadux() {
	document.edmf.func.value = 'deleteradux';
	document.edmf.subpart.value = subpart;
	document.edmf.submit();
}

function aewSearchedRad(subpart,thing) {
	document.edmf.func.value = 'searchrad';
	document.edmf.subpart.value = subpart;
	document.edmf.target.value = thing;
	document.edmf.submit();
}

function aewChangedChat(subpart,thing) {
	document.edmf.func.value = 'aewEdit';
	document.edmf.subpart.value = subpart;
	document.edmf.target.value = thing;
	document.edmf.uxte.value = 'wc';
	document.edmf.submit();
}

function aewChooseNode(n,u) {
	document.edmf.nidi.value = 0;
	document.edmf.func.value = 'aewChooseNode';
	document.edmf.subpart.value = n;
	document.edmf.uxte.value = u;
	document.edmf.submit();
}

function aewChangeDate(subpart) {
	var i = window.prompt("When was this? YYYY/MM/DD HH");
	if(i != null && i != 'Null') {
		document.edmf.subpart.value = subpart;
		document.edmf.func.value = 'aewChangeDate';
		document.edmf.applicator.value = i;
		document.edmf.submit();
	}
}

function edm_EditValue(preval) {
	document.edmf.subpart.value = subpart;
	document.edmf.func.value = 'aewEdit';
	document.edmf.target.value = '';
	document.edmf.uxte.value = subtype;
	edm_close(subtype);
	if(subtype=='wt') {
		document.edmf.applicator.value = null;
		document.edmf.submit();
		return;
	}
	if(subtype=='wf')
		var note = window.prompt("Enter the new value:",(preval||''));
	else
		var note = window.prompt("Search for:");
	if(note != null && note != 'Null') {
		document.edmf.applicator.value = note;
		document.edmf.submit();
	}
}

function edm_AddValue(addnew) {
	var chatype = document.edmf.applicator.value;
	document.edmf.subpart.value = subpart;
	edm_close(subtype);
	if(chatype=='t') {
		document.edmf.func.value = 'aewAdd';
		document.edmf.applicator.value = null;
	}
	else if(chatype=='f' || chatype=='r') {
		document.edmf.func.value = 'aewAdd';
		var note = window.prompt("Enter the new value:");
		if(note==null || note=='Null')
			return;
		document.edmf.applicator.value = note;
	}
	else if(chatype=='c') {
		if(addnew==1)
			document.edmf.func.value = 'newrad';
		else if(addnew==2) {
			var s = window.prompt("Search for:");
			if(s==null || s=='Null')
				return;
			document.edmf.target.value = 0;
			document.edmf.applicator.value = s;
			document.edmf.func.value = 'searchrad';
		}
		else
			document.edmf.func.value = 'aewChooseNode';
	}
	document.edmf.submit();
}

function edm_MoveWidget(e) {
	underway = 'moveuxte';
	document.edmf.subpart.value = subpart;
	edm_close(subtype);
	edm_help(e, "Click the attribute that you wish to position this after.");
}

function edm_HideWidget() {
	OrderWidget('hide');
}

function edm_ShowWidget() {
	OrderWidget('last');
}

function OrderWidget(p) {
	edm_close(subtype);
	document.edmf.func.value = 'OrderWidget';
	document.edmf.uxte.value = subpart;
	document.edmf.subpart.value = p;
	document.edmf.submit();
}






function LoadPage(url) {
	window.location.href = url;
}

function NewNamedResource(n,r,thing) {
	var nam = window.prompt('Name of the '+thing+':');
	if(nam != null && nam != 'Null')
		LoadPage('?'+n+'&create='+r+'&name='+nam);
}

function NewResource(n,r) {
	LoadPage('?'+n+'&create='+r);
}

function old_nidi(n) {
	LoadPage('?'+n);
}

function nidi() {
	if ( (window.Event && window.Event.which == 3) || window.event.button==2)
		alert('right-click');
	return false;
}

function nidinewelem_click() {
}

function nidielem_click(e) {
}

function nidiaddcat_click(n) {
	nidiselr();
}

function nidiaddmemb_click(n) {
	nidiselr();
}

function nidisrel_click(r,n) {
	if(document.catsysform.selr.value)
		document.getElementById('rh_'+document.catsysform.selr.value).style.display='none';
	document.catsysform.selr.value = r;
	document.getElementById('rh_'+n).style.display='block';
}

function PopupPage(url,name,h,w) {
	window.open(url,name,'height='+h+',width='+w);
}

