/* All scripts to be triggered onload should be referenced here. */

window.onload = function() { 
	findNav();
	findInfos(); 
	findLinks(); 
	findLookItUps();
	findPops();
	findHighlighters();
	width = "700";
	height = "600";
}


/*	Main Navigation Drop Down Menus 

	Dropdown menu appears on rollover.

	This script applies to any link within a "mainNav" list item
	that is classed "mainLevel" and which has a sibling div classed "subnav".
	
	The rollover states for the mainNav list item link classed "mainLevel" must be
	classed "on" and "off".
	
	If javascript is disabled, nothing happens on rollover.

*/

var srcFlag;
var hsrcFlag;
/*var imgFlag;

function findImages() {
	var W3CDOM = (document.createElement && document.getElementsByTagName);
	if (!W3CDOM) return;
	var test = new Image();
	var tmp = new Date();
	var suffix = tmp.getTime();
	test.src = 'images/tester.gif?'+suffix;
	test.onload = imageTest;
}

function imageTest() {
	var a = myGetElementsByClass('mainLevel',null,'a');
	for ( var z = 0; z < a.length; z++ ) {
		var span = a[z].getElementsByTagName('span')[0];
		span.style.fontSize = "0";
	}
}*/


function findNav() {

	var a = myGetElementsByClass('mainLevel',null,'a');
	for ( var z = 0; z < a.length; z++ ) {
		a[z].onmouseover = function() {
			var mom = this.parentNode;
			var coors = findPos(this);
			var subnav = myGetElementsByClass('subnav',mom,'div')[0];
			if (subnav) {
				subnav.style.top = ( coors[1] + 25 ) + "px";
				subnav.style.left = ( this.offsetLeft ) + "px";
				subnav.style.visibility = "visible";
			}
		}
		a[z].onmouseout = function() {
			var mom = this.parentNode;
			var subnav = myGetElementsByClass('subnav',mom,'div')[0];
			if (subnav) {
				subnav.style.visibility = "hidden";
			}
		}
	}

	var b = myGetElementsByClass('subnav',null,'div');
	for ( var y = 0; y < b.length; y++ ) {
		b[y].onmouseover = function() {
			var mom = this.parentNode;
			var toggle = myGetElementsByClass('mainLevel',mom,'a')[0];
			var coors = findPos(toggle);
				this.style.top = ( coors[1] + 25 ) + "px";
				this.style.left = ( toggle.offsetLeft ) + "px";
				this.style.visibility = "visible";
			toggle.className = toggle.className + " " + "on";
		}
		b[y].onmouseout = function() {
			var mom = this.parentNode;
				this.style.visibility = "hidden";
			var toggle = myGetElementsByClass('mainLevel',mom,'a')[0];
			toggle.className = "mainLevel" + " " + "off";
		}
	}
}



/*	"What is This?" popups

	Popup with explanatory text appears when the user rolls over a question mark icon. 
	
	For each instance of a "What's This", create a span classed "whatsThis".  Inside this span, 
	include the What's This icon image (no class necessary), and a div classed "moreInfo" that 
	contains the info to be revealed on rollover.

	E.g.

		<span class="whatsThis">
			<img alt="What is This?" src="images/ico-help.gif" />
			<div class="moreInfo">
				Morbi pretium orci iaculis ligula. Nulla facilisi. 
			</div>
		</span>

	Also required is a div with id "moreInfo" placed anywhere on the page (typically at the 
	bottom), containing a blank <span> tag.

	E.g.

		<div id="moreInfo"><span> </span></div>

	If javascript is disabled, nothing happens onclick.

*/

function findInfos() {
	var mitt = document.getElementById('moreInfo');
	var infos = myGetElementsByClass('whatsThis',null,'span');
		for ( var x = 0; x < infos.length; x++ ) {
			var img = infos[x].getElementsByTagName('img')[0];
			img.onmouseover = function() { openInfo(this); }
			img.onmouseout = function() {
				mitt.style.visibility = "hidden";
				mitt.getElementsByTagName('span')[0].innerHTML = "";
			}
		}
}

function openInfo(what) {
	var coors = findPos(what);
	var parent = what.parentNode.parentNode;
	var text = parent.getElementsByTagName('span')[0];
	var mitt = document.getElementById('moreInfo');
	mitt.getElementsByTagName('span')[0].innerHTML = text.innerHTML;
	mitt.style.top = ( coors[1] - mitt.offsetHeight ) + "px";
	mitt.style.left = ( coors[0] + what.offsetWidth ) + "px";
	mitt.style.zIndex = "100";
	mitt.style.visibility = "visible";
}


/*	Show more/Show Less Links

	Used in the Related Links section of Article Pages when there are 10 or more links available. 
	Default state is to show first 9 links with a 'show more' link at the end. When the user clicks 
	the link, all links are exposed with a 'show less' link at the end. When the user clicks 
	'show less', the list returns to its original state.

	For each instance of a set of sibling lists classed "first_batch" and "last_batch", include a 
	single link classed "more" at the end of the first_batch and a link classed "less" at the end 
	of the last_batch.  

	If javascript is disabled, no more/less links will appear, but both sets of links will appear 
	fully expanded.
	
*/

function findLinks() {
	var batch1 = myGetElementsByClass('first_batch',null,'ul');
	for ( var x = 0; x < batch1.length; x++ ) {
		var parent = batch1[x].parentNode;
		var batch2 = myGetElementsByClass('last_batch',parent,'ul')[0];
		if ( batch2 ) {
			batch2.style.display = "none";
			var moreItem = myGetElementsByClass('more',batch1[x],'li')[0];
			var moreLink = moreItem.getElementsByTagName('a')[0];
			moreItem.style.display = "block";
			var lessItem = myGetElementsByClass('less',batch2[x],'li')[0];
			var lessLink = lessItem.getElementsByTagName('a')[0];
			lessItem.style.display = "block";
			moreLink.onclick = function() {
				batch2.style.display = "block";
				this.parentNode.style.display = "none";
			}
			lessLink.onclick = function() {
				batch2.style.display = "none";
				moreItem.style.display = "block";
				moreItem.parentNode.style.display = "block";
			}
		}
	}
}

/*	Look It Up

	The user can highlight any word and run quick search on it by clicking the 'look it up' link. 
	
	Class each button that should launch the "search selection" function with "lookItUp".

	If javascript is disabled, nothing happens onclick.
	
*/

function selectSearch() {
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	}
	if (txt == '') {
		alert ("Highlight any word or phrase and click the button to begin a new search.");
		return;
	}

	f = document.getElementById("searchFormForm");
	document.getElementById("query_field").value = "\"" + txt + "\"";
	f.submit();
}

function findLookItUps() {
	var LIUs = myGetElementsByClass('lookItUp',null,'a');
	for ( var x = 0; x < LIUs.length; x++ ) {
		LIUs[x].onmousedown = function() { selectSearch(); return false }
	}
}


/*	Popup windows script

	Links intended to launch popups should be classed "popup" and given a target of "blank".  
	Supply an href for the link to be opened, as you would a normal link.  If you'd like the 
	new window to have a name, include a title.

	E.g.

		<a class="popup" href="popup.html" target="_blank" title="Print">Print</a>

	The script will launch a 700x600 window with no tool or menu bars, but with the ability to 
	be resized and scrolled.

	If javascript is disabled, the link will launch in a new window.
	
*/

function findPops(){
	var a = myGetElementsByClass('popup',null,'a');
	for (var i = 0; i < a.length; i++) {
		a[i].onclick = function () {
			var url = this.href;
			var name = this.getAttribute('title');
			var width; var height;
			if ( this.getAttribute('coords') ) {
				width = this.getAttribute('coords')[0];
				height = this.getAttribute('coords')[0]
			} else {
				width = "700";
				height = "600";
			}
			return pop(url,name,width,height);
		}
	}
}

function pop(url,name,width,height) {
	var newwindow = window.open(url,name,'height='+height+',width='+width+',menubar=yes,toolbar=no,resizable=yes,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}



/*  Highlight On/Off

	Controls the style of search result hit text (e.g. background color) and toggles this style 
	on/off when clicked.

	This script toggles the class of search hits between "hit" and "miss".  To ascribe toggling 
	function to an object, class one span "on" and one "off" and class their parent element 
	"rtHilight".

	If javascript is disabled, nothing happens onclick.
	
*/

function findHighlighters() {
	var highlight = myGetElementsByClass('rtHilight',null,'li');
	for ( var x = 0; x < highlight.length; x++ ) {
		myGetElementsByClass('on',highlight[x],'span')[0].onclick = highlightOn;
		myGetElementsByClass('off',highlight[x],'span')[0].onclick = highlightOff;
	}
}

function highlightOff() {
	var hits = myGetElementsByClass('hit',null,'span');
	for (var i = 0; i < hits.length; i++) {
		hits[i].className = "miss";
	}
}
	
function highlightOn() {
	var hits = myGetElementsByClass('miss',null,'span');
	for (var i = 0; i < hits.length; i++) {
		hits[i].className = "hit";
	}
}

/* Tools used in the other scripts */

function myGetElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
