/**
 * This function opens a sequence window for the given gene id and accession
 * number. With these two pieces of information, we should be able to load the
 * sequence data in the sequence JSP.
 * 
 * @param The gene id to query
 * @param The accession number of the variant to query sequence for.
 */
function OpenSequenceWindow(geneid, accession, exonname) {
	//Build a variant sequence JSP URL
	var i = location.pathname.lastIndexOf('/');
	if(i != -1) {
		var sURL = location.protocol+"//"+location.host+
			location.pathname.substr(0,i+1)+
			"VariantSequence.jsp?GeneID="+geneid+"&Accession="+accession+"&ExonName="+exonname;
	
		//Open a new window
		var windowRef = window.open(sURL, '', 'width=684,height=480,status=no,location=no,resizable=0');	
		
	}
}

/**
 * This function switches visibility of protein and nucleic sequences in the 
 * sequence popup. It also changes the visibility of the tabs to indicate which
 * sequence is being displayed. The function depends on the following:
 * 
 * The nucleic tab <TD> has the id "NucleicTab"
 * The protein tab <TD> has the id "ProteinTab"
 * The sequence <DIV>s have the ids "NucleicSequence" and "ProteinSequence".
 * 
 * The class of the tab <TD>'s will be changed according to which <DIV> is being
 * made visible. The function takes one argument which is the leading string
 * for the <DIV> and <TD> ids, either "Nucleic" or "Protein".
 * 
 * @param active 		The string indicating which should be active
 * @param inactive	The string indicating which is inactive 
 */
function toggleTab(active, inactive) {
	//Define ids
	var activeTab = active+'Tab';
	var inactiveTab = inactive+'Tab';
	var activeSequence = active+'Sequence';
	var inactiveSequence = inactive+'Sequence';
	
	//Toggle tabs
	var tabObj = document.getElementById(activeTab);
	if(tabObj != null) tabObj.className = "activetab";
	tabObj = document.getElementById(inactiveTab);
	if(tabObj != null) tabObj.className = "inactivetab";
	
	//Toggle sequence windows
	var seqObj = document.getElementById(activeSequence);
	if(seqObj != null) seqObj.style.display = "block";
	seqObj = document.getElementById(inactiveSequence);
	if(seqObj != null) seqObj.style.display = "none";
}

/**
 * 
 */
function highlightSequence(opt) {
	var optvalue = opt.value.toString();
	var nucleicdiv = document.getElementById("NucleicSequence");
	var proteindiv = document.getElementById("ProteinSequence");
	
	//See if we can find the DIV for nucleic spans
	if(nucleicdiv != null) {
		var scrolly = 0;
		var spans = nucleicdiv.getElementsByTagName('span');
		for(var i = 0; i < spans.length; i++) {
			var thespan = spans[i];
			var thespanid = thespan.getAttribute('id');
			if(thespanid == 'Nucleic'+optvalue) {
				scrolly = thespan.offsetTop;
				thespan.className = "sequencehighlight";
			}
			else {
				thespan.className = "";
			}
		}//For each span

		//Scroll the window
		nucleicdiv.scrollTop = scrolly;
	}//If nucleic div
	
	//See if we have the protein div
	if(proteindiv != null) {
		var scrolly = 0;
		var spans = proteindiv.getElementsByTagName('span');
		for(var i = 0; i < spans.length; i++) {
			var thespan = spans[i];
			var thespanid = thespan.getAttribute('id');
			if(thespanid == 'Protein'+optvalue) {
				scrolly = thespan.offsetTop;
				thespan.className = "sequencehighlight";
			}
			else {
				thespan.className = "";
			}
		}//For each span
		
		//Scroll the window
		proteindiv.scrollTop = scrolly;
	}//If proteindiv div
}

/**
 * This function takes all of the span nodes within the td object provided and
 * wraps their collective texts using <br>'s. 
 */
function wrapSequenceString(tdobject) {
	var maxwidth = 79;
	var count = 0;
	if(tdobject != null) {
		var children = tdobject.childNodes;
		for(var i = 0; i < children.length; i++) {
			var node = children[i];
			if(node.nodeName.toUpperCase() == "SPAN") {
				if(node.childNodes.length > 0) {
					//Node is a span, get text
					var text = node.childNodes[0];
					var textstring = text.nodeValue;
					node.removeChild(text);
				
					//Loop through the node text value and create fragments
					var outputtext = "";
					for(var ii = 0; ii < textstring.length; ii++) {
						outputtext += textstring.charAt(ii);
						count += 1;
						if(count > maxwidth) {
							//Build a text node from output text and append it
	//						alert("Counter="+count+", ii="+ii+", Creating text fragment");
							var textfragment = document.createTextNode(outputtext);
							node.appendChild(textfragment);
							
							//Build a br node and append it
	//						alert("Creating <BR> tag");
							var wordbreak = document.createElement("br");
							node.appendChild(wordbreak);
							
							//Clear output text and reset counter
							outputtext = "";
							count = 0;
						}
					}
					
					//Append what's left
					if(outputtext.length > 0) {
						var textfragment = document.createTextNode(outputtext);
						node.appendChild(textfragment);
					} 
				}//If child node list is >0
			}
		}
	}
}