// CSU Library Website
// Journal Databases JavaScript file
// Joshua Gross, August 2009

// This function shows or hides the help box on the Journal Databases pages. It is activated when the user clicks the question mark icon.
function showHelp(){
	if(document.getElementById("help-box").style.visibility == "hidden"){
		document.getElementById("help-box").style.visibility = "visible";
		for (var y = 0; y < document.getElementById("help-box").getElementsByTagName("p").length; y++){
			if(document.getElementById("help-box").getElementsByTagName("p")[y].className == "toggle on-state") {
				document.getElementById("hidden_" + document.getElementById("help-box").getElementsByTagName("p")[y].id.split("_")[1]).style.visibility = "visible";
			}
		}
	} else {
		document.getElementById("help-box").style.visibility = "hidden";
		for (var y = 0; y < document.getElementById("help-box").getElementsByTagName("p").length; y++){
			if(document.getElementById("help-box").getElementsByTagName("p")[y].className == "toggle on-state") {
				document.getElementById("hidden_" + document.getElementById("help-box").getElementsByTagName("p")[y].id.split("_")[1]).style.visibility = "hidden";
			}
		}
	}
}

// This function controls the "Popular Databases" drop-down menu on the databases home page and the "Browse by Subject" drop-down menu in the databases top nav. It is activated when the user clicks the "Go" button for the "Popular Databases" menu, or the user selects a different option in the "Browse by Subject" menu.
function dropDownLink(selectId,target){
	if(document.getElementById(selectId).options[document.getElementById(selectId).selectedIndex].value != ""){
		var url = document.getElementById(selectId).options[document.getElementById(selectId).selectedIndex].value;
		if(target != undefined){
			var newwindow = window.open(document.getElementById(selectId).options[document.getElementById(selectId).selectedIndex].value,target);
			if (window.focus){
				newwindow.focus();
			}
		} else {
			location = url;
		}
	return false
	} else {
		alert("Please select a Journal Database from the drop-down menu.");
	}
}

/* This function sets up the Journal Databases pages. It runs automatically when the page is loaded. */
function setupPage(pageHeading){
	
	// This function removes the default link from the question mark icon in the top-right of the Journal Databases pages and some of the Tutorial pages. It is activated when the page is loaded.
	if (document.getElementById("help-box-link") != null && document.getElementById("help-box-link").attributes.href != undefined) {
		document.getElementById("help-box-link").attributes.removeNamedItem("href");
	}
	
	/* Check value of "pageHeading". If the value is undefined it means this is the walk-in access page. */
	if (pageHeading != undefined) {
		
		/* Highlight appropriate option in databases top nav */
		if(pageHeading.length == 1){
			document.getElementById("database-nav-" + pageHeading).className = "selected";
		} else {
			document.getElementById("database-nav-" + pageHeading).selected = "selected";
		}
		
		/* Check page heading to see whether this page is an alpha page, a subject page or a full page */
		if(pageHeading.length == 1){
			listType = "alpha";
		} else {
			if(pageHeading == "new" || pageHeading == "trial"){
				listType = "full-page";
			} else {
				listType = "subject";
			}
		}
		
	} else {
		pageHeading = "walk-in";
		listType = "alpha";
	}
	
	/* Run the "addDBTriggers" function (below) */
	addDBTriggers(pageHeading);
}

/* This function adds the show / hide functionality to the page. */
function addDBTriggers(pageHeading){
	
	/* Check to see if the current page includes a reference to the mootools javascript file. If not, add one.  */
	if (document.getElementById("mootools-script") == undefined) {
		var JSfile = document.createElement("script");
		JSfile.setAttribute("type","text/javascript");
		JSfile.setAttribute("src","/division/library/styles/js/mootools.js");
		JSfile.id = "mootools-script";
		document.getElementsByTagName("head")[0].appendChild(JSfile);
	}
	
	/* Extend or contract database information. This is activated every time the user clicks a "show" or "hide" link.  */
	function extendContract(){
		var num = this.id.split("_")[1];
		var ID = "hidden_" + num;
		if (isExtended[num] == 0) {
			sideBarSlide(ID, 0, height[num], speed[num]);
			if (document.all == undefined && navigator.vendor.split("Apple")[1] == undefined){
				sideBarOpacity(ID, 0, 1, speed[num]);
			}
			isExtended[num] = 1;
			document.getElementById(ID).style.borderColor = "#D5D7E3";
			if (this.innerHTML == "Show database summary"){
				this.innerHTML = "Hide database summary";
			}
			if (this.className == "expand") {
				this.style.visibility = "hidden";
				this.style.display = "none";
				document.getElementById("collapse_" + num).style.visibility = "visible";
				document.getElementById("collapse_" + num).style.display = "block";
			}
			if (this.className == "toggle" || this.className == "toggle off-state" ) {
				this.className = "toggle on-state";
			}
			if(this.title.split("show")[0] != undefined && this.title.split("show")[0] == "Click to "){
				this.title = "Click to hide" + this.title.split("show")[1];
			}
		} else {
			sideBarSlide(ID, height[num], 0, speed[num]);
			if (document.all == undefined && navigator.vendor.split("Apple")[1] == undefined){
				sideBarOpacity(ID, 1, 0, speed[num]);
			}
			isExtended[num] = 0;
			document.getElementById(ID).style.borderColor = "#fff";
			if (this.innerHTML == "Hide database summary"){
				this.innerHTML = "Show database summary";
			}
			if (this.className == "collapse") {
				this.style.visibility = "hidden";
				this.style.display = "none";
				document.getElementById("expand_" + num).style.visibility = "visible";
				document.getElementById("expand_" + num).style.display = "inline";
			}
			if (this.className == "toggle" || this.className == "toggle on-state" ) {
				this.className = "toggle off-state";
			}
			if(this.title.split("hide")[0] != undefined && this.title.split("hide")[0] == "Click to "){
				this.title = "Click to show" + this.title.split("hide")[1];
			}
		}
	}
	
	/* Extend or contract all database information. This is activated when the user clicks the "Expand all databases" or "Collapse all databases links at the top of the page.  */
	function extendContractAll(){
		switch (this.id) {
			case "expand-all":
				var action = 0;
				break;
			case "collapse-all":
				var action = 1;
				break;
			default:
				break;
		}
		for (var z = 0; z < height.length; z++){
			if(isExtended[z] == action){
				if(document.getElementById("toggle_" + z) != undefined && document.getElementById("toggle_" + z).parentNode.id != "help-box-content"){
					document.getElementById("toggle_" + z).onclick();
				}
				if(document.getElementById("expand_" + z) != undefined && action == 0){
					document.getElementById("expand_" + z).onclick();
				}
				if(document.getElementById("collapse_" + z) != undefined && action == 1){
					document.getElementById("collapse_" + z).onclick();
				}
			}
		}
	}
	
	/* This controls the "sliding" effect.  */
	function sideBarSlide(id, fromHeight, toHeight, thisDuration){
		var myEffects = new Fx.Styles(id, {duration: thisDuration, transition: Fx.Transitions.linear});
		myEffects.custom({
			 'height': [fromHeight, toHeight]
		});
	}

	/* This controls the "fade-in" / "fade-out" effect. Doesn't work in Safari.  */
	function sideBarOpacity(id, from, to, thisDuration){
		var myEffects = new Fx.Styles(id, {duration: thisDuration, transition: Fx.Transitions.linear});
		myEffects.custom({
			 'opacity': [from, to]
		});
	}

	var speed = new Array();
	var expandCounter = 0;
	var isExtended = new Array();
	var height = new Array();
	var toggleCounter = 0;
	var closeCounter = 0;

	/* The code below checks the page to see if it contains a div with the ID "database-list". If it does, the code checks the class of every HTML tag within the "database-list" div and adds "show / hide" information to all the tags that match certain classes.  */
	if (document.getElementById("database-list") != undefined) {
		dbTags = document.getElementById("database-list").getElementsByTagName("*");
		for (i = 0; i < dbTags.length; i++){
			switch (dbTags[i].className){
				case "toggle":
					dbTags[i].className = "toggle off-state";
					dbTags[i].onclick = extendContract;
					if (dbTags[i].parentNode.className == "bottom") {
						dbTags[i].style.visibility = "visible";
						dbTags[i].style.display = "block";
					} else {
						dbTags[i].style.visibility = "visible";
						dbTags[i].style.display = "inline";
					}
					toggleCounter++;
					break;
				case "expand":
					dbTags[i].onclick = extendContract;
					dbTags[i].style.visibility = "visible";
					dbTags[i].style.display = "inline";
					toggleCounter++;
					break;
				case "collapse":
					dbTags[i].onclick = extendContract;
					dbTags[i].style.visibility = "hidden";
					dbTags[i].style.display = "none";
					toggleCounter++;
					break;
				case "hidden":
					var expandCounter = dbTags[i].id.split("_")[1];
					var thisHiddenContents = document.createElement("div");
					thisHiddenContents.className = "hidden-contents";
					thisHiddenContents.innerHTML = dbTags[i].innerHTML;
					dbTags[i].innerHTML = "";
					dbTags[i].appendChild(thisHiddenContents);
					if (controls == undefined){
						var controls = document.createElement("p");
						controls.className = "expanding-controls";
						if (pageHeading == "walk-in") {
							var linkType = "resource";
						} else {
							var linkType = "database";
						}
						var expandLink = document.createElement("a");
						expandLink.id = "expand-all";
						expandLink.onclick = extendContractAll;
						expandLink.title = "Click to show all " + linkType + " summaries and help options.";
						expandLink.appendChild(document.createTextNode("Expand all " + linkType + "s"));
						var collapseLink = document.createElement("a");
						collapseLink.id = "collapse-all";
						collapseLink.onclick = extendContractAll;
						collapseLink.title = "Click to hide all " + linkType + " summaries and help options.";
						collapseLink.appendChild(document.createTextNode("Collapse all " + linkType + "s"));
						controls.appendChild(expandLink);
						controls.appendChild(document.createTextNode(" | "));
						controls.appendChild(collapseLink);
						document.getElementById("database-list").insertBefore(controls,document.getElementById("database-list").getElementsByTagName("*")[0]);
						i = i + 3;
					}
					height[expandCounter] = dbTags[i].offsetHeight;
					speed[expandCounter] = (Math.sqrt(dbTags[i].offsetHeight)) * 15;
					dbTags[i].style.height = "0px";
					dbTags[i].style.borderColor = "#fff";
					isExtended[expandCounter] = 0;
					break;
				case "date-row":
					var dateAdded = dbTags[i].getElementsByTagName("p")[0].innerHTML;
					var today = new Date();
					var then = new Date();
					var datesplit = dateAdded.split("-")
					var year = datesplit[0];
					var month = datesplit[1] - 1;
					var date = datesplit[2];
					then.setFullYear(year,month,date);
					var diff = today.getTime() - then.getTime();
					diff = Math.floor(diff / (1000 * 60 * 60 * 24));
					if (diff < 60 && listType != "full-page"){
						dbTags[i].className = "";
						dbTags[i].getElementsByTagName("td")[0].className = "new-tag";
						dbTags[i].getElementsByTagName("p")[0].innerHTML = "New resource!";
						dbTags[i].parentNode.parentNode.parentNode.className = "new-db";
					}
					break;
				default:
					break;
			}
		}
		expandCounter = toggleCounter;
	}
	
	/* This code checks to see if the page contains a help box. If it does, this code sets up the "show / hide" function for the help box.  */
	if (document.getElementById("help-box") != null) {
		dbHelpTags = document.getElementById("help-box").getElementsByTagName("*");
		for (i = 0; i < dbHelpTags.length; i++){
			switch (dbHelpTags[i].className){
				case "toggle":
					dbHelpTags[i].id = "toggle_" + toggleCounter;
					dbHelpTags[i].className = "toggle off-state";
					dbHelpTags[i].title = "Click to show information";
					dbHelpTags[i].onclick = extendContract;
					toggleCounter++;
					break;
				case "hidden":
					dbHelpTags[i].id = "hidden_" + expandCounter;
					var thisHiddenContents = document.createElement("div");
					thisHiddenContents.className = "hidden-contents";
					thisHiddenContents.innerHTML = dbHelpTags[i].innerHTML;
					dbHelpTags[i].innerHTML = "";
					dbHelpTags[i].appendChild(thisHiddenContents);
					height[expandCounter] = dbHelpTags[i].offsetHeight;
					speed[expandCounter] = (Math.sqrt(dbHelpTags[i].offsetHeight)) * 15;
					dbHelpTags[i].style.height = "0px";
					dbHelpTags[i].style.borderColor = "#fff";
					isExtended[expandCounter] = 0;
					expandCounter++;
					break;
				default:
					break;
			}
		}
	}
}
