/* hash.js
 * Handles the Radlex Treee
 * 
 * John Paulett
 */

var currentHighlightedTerm = null;

function highlightTerm(radlexID){
	if (currentHighlightedTerm != null && document.getElementById("tna_"+currentHighlightedTerm) != null) {
		document.getElementById("tna_"+currentHighlightedTerm).className='treeNodeLink'
	}
	currentHighlightedTerm = radlexID;
	if (document.getElementById("tna_"+radlexID) != null){
		document.getElementById("tna_"+radlexID).className='treeNodeLinkSelected';
	}
}

function loadTree(funcCall){
	hash = getHash();
	str = new Array();

	if (hash['radlexID'] != "" && hash['radlexID'] != null) str.push('radlexID='+hash['radlexID']);
	if (hash['baseID'] != "" && hash['baseID'] != null) str.push('baseID='+hash['baseID']);
	if (hash['relationType'] != "" && hash['relationType'] != null) {
		str.push('relationType='+hash['relationType']);
		document.forms['radRelation'].radRelationSelect.value = hash['relationType'];
	}
	var url = 'getTree?'+str.join('&');
	runAjax(url,funcCall,false);
}

function parseTreeAndTermForHistoryRestore(response){
	parseTree(response);
	if (getRadlexID() != null){
		getTerm(getRadlexID(),false);
	}
}

function parseTreeAndTerm(response){
	parseTree(response);
	if (getRadlexID() != ''){
		getTerm(getRadlexID(),true);
	}
}
function parseTree(response){
	clearHTML('tree');
	//alert(document.getElementById('tree'));
	var treeElm = document.getElementById('tree');
	
	var hash = getHash();
	var links = response.getElementsByTagName('link');//
	
	for (i=0; i<links.length; i++){
		if (links[i].getAttribute('parent') == 'ROOT' || (hash['baseID']!=null && links[i].getAttribute('id') == hash['baseID'])){
			var parentNode = treeElm;
			var parentTNL = null;
		} else {
			var parentNode = document.getElementById('tnc_'+links[i].getAttribute('parent'));
			var parentTNL = document.getElementById('tnl_'+links[i].getAttribute('parent'));
			//alert('tnc_'+links[i].getAttribute('parent'));
		}
		parentNode.innerHTML += makeTreeNode(links[i].getAttribute('id'),links[i].firstChild.data, links[i].getAttribute('childIsParent'));
		expandNode(links[i].getAttribute('parent'));
		//parentNode.style.display = 'block';
	}
}


function parseChildren(response,parent){
	if(parent == null){
		parent = response.attributes[0].value
	}
	var children = response.getElementsByTagName('child');
	var parentNode = document.getElementById('tnc_'+parent);
	clearHTML('tnc_'+parent);
	var relationType = getRelationType();
	//alert(relationType);
	if (children != null && children.length > 0){
		for (i=0; i<children.length; i++){
			if ((relationType == 'preferred' && children[i].getAttribute('preferred') == '1') || (relationType != 'preferred' && relationType == children[i].getAttribute('relationType'))) {
				parentNode.innerHTML += makeTreeNode(children[i].getAttribute('id'),children[i].firstChild.data, children[i].getAttribute('childIsParent'));
			}
		}
	} else {
		document.getElementById('tnb_'+parent).innerHTML='<a class="treeNodeExpander" id="tnl_'+parent+'" style="cursor:default"><img src="images/empty.gif" alt="Empty" /></a>';//style.display='none';
		document.getElementById('tnc_'+parent).style.display = 'none';
		
	}
}

/* makes the display open for sub list, but does not fill in that space */
function expandNode(parentID){
	var tnc = document.getElementById('tnc_'+parentID);
	if (tnc != null){
		tnc.style.display = 'block';
		document.getElementById('tnl_'+parentID).innerHTML='<img src="images/minus.gif" alt="Collapse" />';
	}
}
function collapseNode(parentID){
	var tnc = document.getElementById('tnc_'+parentID);
	tnc.style.display = 'none';
	document.getElementById('tnl_'+parentID).innerHTML='<img src="images/plus.gif" alt="Expand" />';
}

function changeViewable(parentID){
	var tnc = document.getElementById('tnc_'+parentID);
	if (tnc.style.display == '' || tnc.style.display == 'block'){
		collapseNode(parentID);
	} else if (tnc.style.display == 'none') {
		insertChildren(parentID);
	} 
}

function insertChildren(parentID){
	var tnc = document.getElementById('tnc_'+parentID);
	expandNode(parentID);
	if(tnc.innerHTML == ''){
		loading(tnc);
		
		var hash = getHash();
		var str = new Array();
	
		str.push('radlexID='+parentID);
		if (hash['relationType'] != "" && hash['relationType'] != null) {
			str.push('relationType='+hash['relationType']);
		}
		var url = 'getChildren?'+str.join('&');
		runAjax(url,parseChildren,false);
	}
}

/* 
 * returns the html needed for each node of the tree (a li and an embedded ul for storage of the children)
 * tn_ is the li, tnb_ is the holder for the bullet of the li, tnl_ is the actual link for that bullet
 * tnc_ is the embedded ul for storing any children.
 */
function makeTreeNode(id, name, hasChildren){
	var html = '<li class="treeNode" id="tn_'+id+'"><span class="tnb" id="tnb_'+id+'">';
	if (hasChildren == '1'){
		html += '<a class="treeNodeExpander" id="tnl_'+id+'" onclick="changeViewable(\''+ id +'\')" style="cursor:pointer"><img src="images/plus.gif" alt="Expand" /></a>';
	} else {
		html += '<a class="treeNodeExpander" id="tnl_'+id+'" style="cursor:default"><img src="images/empty.gif" alt="Empty" /></a>';
	}
	html += '</span><a class="treeNodeLink" id="tna_'+id+'" onclick="getTerm(\''+id+'\')" style="cursor:pointer">'+name+'</a>'+
			'<ul class="treeList" id="tnc_'+id+'" style="display:none;"></ul>'+
			'</li>';
	return html;
}

function limitRelationPullDown(relBool){
	var rSel = document.forms['radRelation'].radRelationSelect;
	var previous = rSel.options[rSel.selectedIndex].value;
	rSel.options.length = 0
	rSel.options[0] = new Option('Preferred', 'preferred');
	j = 1;
	for(i in relBool){
		switch (i){
			case 'partof':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Part of', 'partof');
					j++;
				}
				break;
			case 'isa':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Is a', 'isa');
					j++;
				}
				break;
			case 'branchof':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Branch of', 'branchof');
					j++;
				}
				break;
			case 'segmentof':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Segment of', 'segmentof');
					j++;
				}
				break;
			case 'tributaryof':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Tributary of', 'tributaryof');
					j++;
				}
				break;
			case 'continuouswith':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Continuous with', 'continuouswith');
					j++;
				}
				break;
			case 'containedin':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Contained in', 'containedin');
					j++;
				}
				break;
			case 'memberof':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Member of', 'memberof');
					j++;
				}
				break;
			case 'bloodsupplyof':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Blood Supply of', 'bloodsupplyof');
					j++;
				}
				break;
			case 'innervates':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Innervates', 'innervates');
					j++;
				}
				break;
			case 'hasentrapmentsite':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Has Entrapment Site', 'hasentrapmentsite');
					j++;
				}
				break;
			case 'synonymof':
				if (relBool[i] == true){
					rSel.options[j] = new Option('Synonym of', 'synonymof');
					j++;
				}
				break;
			default: ;
		}
	}
	
	// since we delete everything, set it back to the selection before we deleted
	var resetRel = true;
	for (i=0; i < rSel.options.length; i++){
		if (rSel.options[i].value == previous){
			rSel.selectedIndex = i;
			resetRel = false;
			break;
		}
	}
	
	// default to preferred if somehow you navigate to place they will cause error
	if (resetRel){
		rSel.selectedIndex = 0;
		setRelationType('preferred');
	}
}

