// JavaScript Document

// define some initial variables
var numberOfProfiles = 0;
var currentProfile = 0;
var currentProfileIndex=2;
var numberOfHeadings = 0;
var currentHeading = 0;
var firstTimeThrough = true;
//variables for left hand menu functions
var oldVisibleList1="";
var oldVisibleList2="";
// hdg variables for dropdown menu
var timeout	= 400;
var closeTimer	= 0;
var ddmenuItem	= 0;
var submenuItem = 0;
var highlightMenuItem = 0;
var liaisonItem = 0;

// function to load the xml
function loadXML()
{
	// prevent xml caching
	var timestamp = new Date();
	var uri = "xml/homePrograms.xml";
	var uniqueURI = uri + (uri.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
	try
	{
		if (window.ActiveXObject)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(uniqueURI);
			getNav();
		}
		else if(window.XMLHttpRequest)
		{
			var errorHappendHere = "Error handling XMLHttpRequest request";
			var d = new XMLHttpRequest();
			d.open("GET", uniqueURI, false);
			d.send(null);
			xmlDoc=d.responseXML;
			getNav();
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.async=false;
			xmlDoc.load(uniqueURI);
			xmlDoc.onload=getNav();
		}
	}	
	catch(e)
	{
		alert(errorHappendHere);
	}
}
/**************************************************************************/
/*HOMEPAGE HEADING CODE                                                   */
/**************************************************************************/
//function to load the images
// do not edit this function
var getHomeHeadings = function getHomeHeadings()
{
	// array of all of the image paths to be loaded
	var images=new Array;
	// variable to build all of the heading images with in the innerHTML
	var topHeadingCode="";
	// create an object with all of the image nodes
	var xmlTopNodes=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading"); 
	// find out how many top nodes there are
	var numTopNodes=xmlTopNodes.length; 
	// assign the number of nodes found to a global level variable
	numberOfHeadings = numTopNodes;
	// loop through the images
	for(var x=0; x<numberOfHeadings; x++){
		// get the value of the image source
		var newImage = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[x].getElementsByTagName("location")[0].childNodes[0].nodeValue;
		// get the value of the image alt text
		var newAlt = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[x].getElementsByTagName("alt")[0].childNodes[0].nodeValue;
		// get the value of the image link path
		var newLinkPath = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("homeHeading")[x].getElementsByTagName("linkPath")[0].childNodes[0].nodeValue;
		// put the links and/or images together
		if(newLinkPath != "none"){
			topHeadingCode+='<a class="profileLink" href="' + newLinkPath + '" title="' + newAlt + '"><img src="' + newImage + '" border="0" id="heading'+x+'" /></a><br />';
		}else{
			topHeadingCode+='<img src="' + newImage + '" border="0" id="heading'+x+'" /><br />';	
		}
		// populate the images array
		images[x]=newImage;
	}
	// set the global currentProfile number to the randome number generated
	currentHeading = 0;
	// put everything together and create the image
	document.getElementById('headingContainer').innerHTML = topHeadingCode;
	// Preloader of large images
	new Asset.images(images, {
		onComplete: function(){
			timedCount();
		}
	});
}
// starting z-index
var counter=2;
// timed loop for heading images
// do not edit this function
function timedCount()
{	
	// time in between loops
	setTimeout("timedCount()",8000);
	// determin which heading to work with
	var newId = "heading" + currentHeading;
	// set the z index of the new heading to be on top
	document.getElementById(newId).style.zIndex=counter;
	// only chang the first heading if this is the first time through the loop
	if(firstTimeThrough){
		var myElementsEffects = new Fx.Elements($$('#headingContainer img#heading0'),{duration: 1500});
		myElementsEffects.start({
			'0': { //let's change the first element's opacity
				'opacity': [0,1]
			}
		});
		firstTimeThrough = false;
	}else{
		// set the new heading's opacity from 0 to 1
		var myElementsEffects = new Fx.Elements($$('#headingContainer img#heading'+currentHeading),{duration: 1500});
		myElementsEffects.start({
			'0': { //change the headings's opacity
				'opacity': [0,1]
			}
		});
		// change the opacity of the old heading from 1 to 0
		if(currentHeading == 0){
			var myElementsEffectsOld = new Fx.Elements($$('#headingContainer img#heading'+(numberOfHeadings-1)),{duration: 8000});
		}else{
			var myElementsEffectsOld = new Fx.Elements($$('#headingContainer img#heading'+(currentHeading-1)),{duration: 8000});	
		}
		myElementsEffectsOld.start({
			'0': { //change the headings's opacity
				'opacity': [1,0]
			}
		});
	}
	// reset the current Heading value appropriately if it reaches the end of the images or not
	if(currentHeading == numberOfHeadings-1){
		currentHeading = 0;
	}else{
		currentHeading++;	
	}
	counter++;
}
/**************************************************************************/
// function to create needed homepage profiles from the loadXML function.
// do not edit this function
/**************************************************************************/
var getNav = function getImages()
{
	// create an object with all of the image nodes
	var xmlTopNodes=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image"); 
	// find out how many top nodes there are
	var numTopNodes=xmlTopNodes.length; 
	// assign the number of nodes found to a global level variable
	numberOfProfiles = numTopNodes;
	// keep all data for the new images together
	var newProfileData="";
	
	
	for(var x=0; x<numberOfProfiles; x++){
		// get the value of the randomly selected image source
		var newImage = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image")[x].getElementsByTagName("location")[0].childNodes[0].nodeValue;
		// get the value of the randomly selected image alt text
		var newAlt = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image")[x].getElementsByTagName("alt")[0].childNodes[0].nodeValue;
		// get the value of the randomly selected image link path
		var newLinkPath = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image")[x].getElementsByTagName("linkPath")[0].childNodes[0].nodeValue;
		// put the image together
		newProfileData += '<a href="' + newLinkPath + '" title="' + newAlt + '"><img class="profileImage" id="profile'+x+'" src="' + newImage + '" border="0" /></a>';
	}
	// add the two invisible buttons
	newProfileData += '<a href="javascript:profileLeft();"><img src="images/spacer.gif" id="profileLeft" /></a>'+
    	'<a href="javascript:profileRight();"><img src="images/spacer.gif" id="profileRight" /></a>';
	// set the global currentProfile number to the first image
	currentProfile = 0;
	// put everything together and create the image
	document.getElementById('profileContainer').innerHTML = newProfileData;
	// put the first image on top
	document.getElementById('profile0').style.zIndex = 2;
	getHomeHeadings();
}

// functions to switch between profiles on the homepage
function profileRight(){
	if(currentProfile == numberOfProfiles-1){
		currentProfile = 0;
	}else{
		currentProfile++;	
	}
	currentProfileIndex++;
	document.getElementById(('profile'+currentProfile)).style.zIndex = currentProfileIndex;
}
function profileLeft(){
	if(currentProfile == 0){
		currentProfile = numberOfProfiles-1;
	}else{
		currentProfile--;	
	}
	currentProfileIndex++;
	document.getElementById(('profile'+currentProfile)).style.zIndex = currentProfileIndex;
}

/*****************************************/
/* SEARCH BOX 
// do not edit this function
/*****************************************/
// function to clear text from search box if default text exists
function clearSearch(){
	if(document.getElementById('searchBox').value == "Search"){
		document.getElementById('searchBox').value = "";
	}
}

// function to add default text back if text box is blank
function addSearch(){
	if(document.getElementById('searchBox').value == ""){
		document.getElementById('searchBox').value = "Search";
	}
}
/**********************/
/*show lists functions*/
/**********************/
function showList(curList, curLevel){
	//select the first button
	var button1 = $(curList);
	alert($(curList).get('height'));
	if(curLevel=='1'){
		if(oldVisibleList1 != ""){
			document.getElementById(oldVisibleList1).style.display = 'none';
			if(oldVisibleList2 != ""){
				document.getElementById(oldVisibleList2).style.display = 'none';
			}
			oldVisibleList1 = curList;
			document.getElementById(curList).style.display = 'block';
		}else{
			oldVisibleList1 = curList;
			document.getElementById(curList).style.display = 'block';
		}
	}else{
		if(oldVisibleList2 != ""){
			document.getElementById(oldVisibleList2).style.display = 'none';
			oldVisibleList2 = curList;
			document.getElementById(curList).style.display = 'block';
		}else{
			oldVisibleList2 = curList;
			document.getElementById(curList).style.display = 'block';
		}
	}
}
// **************************************************
// Code for dropdown menus
// modified JavaScript from 
// http://www.javascript-array.com/scripts/simple_drop_down_menu/
// **************************************************

function showMenu(parent_id, ddmenu_id) {	//mopen
	cancelTimer();
	if (ddmenuItem) {
		ddmenuItem.style.display = 'none';
	}
	if (submenuItem) {
		submenuItem.style.display = 'none';
	}

	ddmenuItem = document.getElementById(ddmenu_id);
	
	var ref = document.getElementById(parent_id);

	ddmenuItem.style.display ='';	
	
	var left = findLeft(ref);
	var top = findTop(ref);
	ddmenuItem.style.left = ref.offsetLeft + "px";
	ddmenuItem.style.top = (top + ref.offsetHeight + 7) + "px";
}

function showddMenu(leftref_id, topref_id, ddmenu_id) {	//mopen
	cancelTimer();
	if (ddmenuItem) {
		ddmenuItem.style.display = 'none';
	}
	if (submenuItem) {
		submenuItem.style.display = 'none';
	}

	ddmenuItem = document.getElementById(ddmenu_id);
	
	var refL = document.getElementById(leftref_id);
	var refT = document.getElementById(topref_id);
	
	ddmenuItem.style.display ='';	

	var top = findTop(refT);

ddmenuItem.style.left = (refL.offsetLeft - 10) + "px";
	ddmenuItem.style.top = top + "px"; 
/*	ddmenuItem.style.top = (top + refT.offsetHeight + 6) + "px"*/
}


function findLeft(obj){
	var curleft = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	return curleft;
}

function findTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		do {
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return curtop;
}

function showSubMenu(parent_id,menu_id,submenu_id) {		//mopen
	cancelTimer();
	if (submenuItem) {
		submenuItem.style.display = 'none';
	}

	submenuItem = document.getElementById(submenu_id);

	var ref = document.getElementById(parent_id);
	var menuItem = document.getElementById(menu_id);
	highlightMenuItem = menuItem;
	
	submenuItem.style.display ='';	
	submenuItem.style.left = (ref.offsetLeft + ref.offsetWidth) + "px";
	submenuItem.style.top = (ref.offsetTop + menuItem.offsetTop - 1) + "px";

    // change menu item background to highlight item
	highlightMenuItem.style.backgroundColor='#F5D79B';
	submenuItem.style.backgroundColor='#f2ca72';
}

function closeMenu() {	//mclose
	if (ddmenuItem) {
		ddmenuItem.style.display = 'none';
	}
	if (submenuItem) {
		submenuItem.style.display = 'none';
	}
	if (highlightMenuItem) {
		highlightMenuItem.style.backgroundColor='#eeb74d';
	}
}

function closeSubMenu(menuItem) {
	if (submenuItem) {
		submenuItem.style.display = 'none';
	}
	// change menu item background back to normal
	var itemId = document.getElementById(menuItem);
	itemId.style.backgroundColor='#eeb74d';

}
function setTimer() {	//mclosetime
	closeTimer = window.setTimeout(closeMenu, timeout);
}

function cancelTimer() {	//mcancelclosetime
	if (closeTimer) {
		window.clearTimeout(closeTimer);
		closeTimer = null;
	}
}

function highlightMenu(menuItem){
	var itemId = document.getElementById(menuItem);
	itemId.style.backgroundColor='#F5D79B';
}

function regularMenu(menuItem){
	var itemId = document.getElementById(menuItem);
	itemId.style.backgroundColor='#eeb74d';
}

function highlightSubMenu(menuItem){
	var itemId = document.getElementById(menuItem);
	itemId.style.backgroundColor='#eeb74d';
}

function regularSubMenu(menuItem){
	var itemId = document.getElementById(menuItem);
	itemId.style.backgroundColor='#f5d79b';
}

// hdg - end new code for dropdown menus - testing

//hdg - code for searchboxes
function OnSearchRadioClick()
{
  var catSearch = document.radiogroup.searchradio[0].checked;
  var webSearch = document.radiogroup.searchradio[1].checked;

  if(webSearch == true)
  {
  	document.searchsite.style.visibility = "visible";
	document.searchcat.style.visibility = "hidden";
	document.searchcat.searcharg.value = "";
  }
  else
  if(catSearch == true)
  {
  	document.searchsite.style.visibility = "hidden";
	document.searchcat.style.visibility = "visible";
	document.searchsite.q.value = "";	
  }
  return true;
}

function OnCatSearch()
{
	document.searchcat.removeChild(document.searchcat.sa);
	return true;
}
// hdg - end code for searchboxes


function book_or_articleReq()
{
	var bookReq = document.ILLForm.requestType[0].checked;
	var articleReq = document.ILLForm.requestType[1].checked;

  if(bookReq == true)
  {
	  var show = document.getElementById("bookRequest");
	  var hide = document.getElementById("articleRequest");
	  show.style.display="";
	  hide.style.display="none";
	  var email_order = document.ILLForm.data_order;
	  email_order.value="NAME, PHONE, EMAIL, PATRON_STATUS, JOURNAL, BOOK_TITLE, BOOK _AUTHOR, BOOK_PUBLISHER, BOOK_PUBLICATION_DATE, BOOK_EDITION_OR_VOLUME_NUMBER, BOOK_COMMENTS";
  }
  else
  if(articleReq == true)
  {
	  var show = document.getElementById("articleRequest");
	  var hide = document.getElementById("bookRequest");
	  show.style.display="";
	  hide.style.display="none";
	  var email_order = document.ILLForm.data_order;
	  email_order.value="NAME, PHONE, EMAIL, PATRON_STATUS, JOURNAL, DELIVERY_OF_ARTICLE, PERIODICAL_OR_JOURNAL, TITLE_OF_ARTICLE, AUTHOR_OF_ARTICLE, VOLUME, ISSUE_NUMBER, DATE, BEGINNING_PAGE, ENDING_PAGE, PHOTOCOPY_COMMENTS"
  }
  return true;
}

//breadcrumbs
function breadCrumbs(delimiterStr) {
	loc2 = window.location.toString();
	loc = loc2.toLowerCase();
	subs = loc.substr(7).split("/"); // Makes the assumption that the first 7 characters are "HTTP://"
	document.write("<li>");
	
	a = (loc.indexOf('index.') == -1) ? 1 : 2;
	if (subs[subs.length-1].length == 0) {
		a++;
	}
	for (i = 1; i < (subs.length - a); i++) {
		subs[i] = makeCaps(unescape(subs[i]));
		document.write('<a href="' + getLoc(subs.length - i - 2) + '">' + subs[i] + '<\/a></li> ' + delimiterStr + ' <li>');
	}
	document.write(document.title);
	document.write("</li>")
}

function makeCaps(a) {
	g = a.split("-");
	for (l = 0; l < g.length; l++) {
	g[l] = g[l].toUpperCase().slice(0, 1) + g[l].slice(1);
	}
	return g.join(" ");
}

function getLoc(c) {
	var d = "";
	if (c > 0) {
		for (k = 0; k < c; k++) {
  		  d = d + "../";
		}
	}
	else{
		d=d+"./";
	}
	return d;
}

//end breadcrumbs

// ***********************************************
// Show Liaison Function for Faculty Liaison Page
// ***********************************************
function showLiaison(parent_id, liaison_id) {
	if (liaisonItem) {
		// close current liaison div if open
		liaisonItem.style.display = 'none';
	}

	liaisonItem = document.getElementById(liaison_id);
	
	var ref = document.getElementById(parent_id);

	liaisonItem.style.display ='';	
	
	var top = findTop(ref);

	liaisonItem.style.left = "425px";
	liaisonItem.style.top = (top - 10) + "px";
	return false;
}
function nolink(){
	alert('test');
	return confirm('Do you really want to follow this link?');
}

function closeLiaison(){
	if (liaisonItem){
		liaisonItem.style.display = 'none';
	}
	return false;
}

//end liaison functions

function test(a,b){
	var x= a+b;
	return x;
}
// End Show Liaison Function


// do not remove this function even though it is blank
function blank(){
	
}
// *************************************
// do not edit anything below this line
// *************************************
window.addEvent('domready', function() {
	// HERE IS WHERE YOU READ IN JS CODE
	var accordion = new Accordion('a.atStart', 'ul.atStart', {
	opacity: true,
	alwaysHide:true,
	show:500
	}, $('leftNavContainer'));
	var accordion2 = new Accordion('a.atStart2', 'ul.atStart2', {
	onActive: function(toggler, element){
		var parent = toggler.getParent();
		while (!parent.hasClass('atStart')) {
			parent = parent.getParent();
		}
		parent.setStyle('height', '');
	},
	onBackground: function(toggler, element){
		var parent = toggler.getParent();
		while (!parent.hasClass('atStart')) {
			parent = parent.getParent();
		}
		parent.setStyle('height', '');
	},
	opacity: true,
	alwaysHide:true,
	show:500
	}, $('leftNavContainer'));
});
