var currentSelection = null;

function ShowTitles() {
	var params = "context=GET_CHILDREN&msg=root";
	var myAjax = new Ajax.Updater (
		{success: 'root'},
		'ajax_request.cgi',
		{
			method: 'get',
			parameters: params
		}
	);
}

function ShowChildren( elid ) {
	Element.hide(elid + '_p');
	Element.show(elid + '_m');

	if ($(elid).innerHTML == '') {
		var params = "context=GET_CHILDREN&msg=" + elid;
		var myAjax = new Ajax.Updater (
			{success: elid},
			'ajax_request.cgi',
			{
				method: 'get',
				parameters: params
			}
		);
	} else {
		Element.show(elid);
	}
}

function HideChildren(elid) {
	Element.show(elid + '_p');
	Element.hide(elid + '_m');
	Element.hide(elid);
}

function DisplayText(msg) {
	obj = $('text_display');
	obj.innerHTML = "Loading...";
	obj.style.backgroundColor = '#e8e8f0';
	obj.style.width = '40em';

	params = 'context=DISPLAY_TEXT&msg=' + msg;
	var myAjax = new Ajax.Updater (
		{success: 'text_display'},
		'ajax_request.cgi',
		{
			method: 'get',
			parameters: params
		}
	);
	window.scrollTo(0,0);
	OpenTreeToChild(msg);

	plus = $(msg + '_p');
	if (plus != null) {
		if (Element.visible(plus)) {
			ShowChildren(msg);
		} else {
			HideChildren(msg);
		}
	}
}

function OpenTreeToChild(msg) {
	var params = "context=GET_PARENTS&msg=" + msg;
	var myAjax = new Ajax.Request(
		'ajax_request.cgi',
		{
				method: 'get',
				parameters: params,
				onSuccess: OpenTreeToChildResponse
		}
	);
	currentSelection = msg;
}

function OpenTreeToChildResponse(req) {
	var resp = req.responseText;
	resp = resp.replace(/\s/g,'');
	if (resp != "") {
		var list = resp.split(',');
		for(i=0;i<list.length;i++) {
			command = 'ShowChildren(\'' + list[i] + '\')';
			//ShowChildren(list[i]);
			setTimeout(command,500 * i);
		}
	}
	return;
}

function QueryDisplay(id,highlight) {
	obj = $(id);
	link = $(id + '_show');

	if (obj.style.display == 'none') {
		//it's already been loaded and hidden
		obj.style.display = '';
		link.innerHTML = '[Hide text below]';
	} else if (obj.innerHTML == '') {
		//needs to be loaded
		obj.style.backgroundColor = '#e8e8f0';
		obj.style.width = '40em';
		$(id).innerHTML = "Loading...";
		params = 'context=DISPLAY_QUERY_TEXT&msg=' + id;
		params += highlight;
		var myAjax = new Ajax.Updater (
			{success: id},
			'ajax_request.cgi',
			{
				method: 'get',
				parameters: params
			}
		);
		link.innerHTML = '[Hide text below]';
	} else {
		obj.style.display = 'none';
		link.innerHTML = '[Show text below with highlights]';
	}

	return;	
}
