// CSU Library Website
// Common JavaScript file
// Joshua Gross, August 2009

// This function opens a link in a new, specially resized window.
function openWindow(id, height, width){
	url = document.getElementById(id).href;
	target = document.getElementById(id).target;
	newwindow = window.open(url,target,'height=' + height + ', width=' + width + ', scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
}

// This function is used on pages that have show / hide capabilities (except the database pages, which use a different show / hide feature).
function addTriggers(){
	
	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);
	}
	
	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;
			if (document.getElementById("expanding-box_" + num) != undefined) {
				document.getElementById("expanding-box_" + num).style.height = "auto";
				document.getElementById("toggle_" + num).className = "toggle on-state";
				document.getElementById("toggle_" + num).title = "Click to show less information.";
			}
			if (this.innerHTML == "Show more...") {
				this.innerHTML = "Show less...";
				this.title = "Click to show less information.";
				this.className = "toggle on-state";
			}
		} 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;
			if (document.getElementById("expanding-box_" + num) != undefined) {
				document.getElementById("toggle_" + num).className = "toggle off-state";
				document.getElementById("toggle_" + num).title = "Click to show more information.";
			}
			if (this.innerHTML == "Show less...") {
				this.innerHTML = "Show more...";
				this.title = "Click to show more information.";
				this.className = "toggle off-state";
			}
		}
	}
	
	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();
				}
			}
		}
	}
	
	function sideBarSlide(id, fromHeight, toHeight, thisDuration){
		var myEffects = new Fx.Styles(id, {duration: thisDuration, transition: Fx.Transitions.linear});
		myEffects.custom({
			 'height': [fromHeight, toHeight]
		});
	}

	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 expandContainerCounter = 0;
	var expandCounter = 0;
	var isExtended = new Array();
	var height = new Array();
	var toggleCounter = 0;
	var closeCounter = 0;

	allTags = document.getElementById("subcontent").getElementsByTagName("*");
	for (i = 0; i < allTags.length; i++){
		switch (allTags[i].className){
			case "expanding-box":
				if (controls == undefined){
					var controls = document.createElement("p");
					controls.className = "expanding-controls";
					var expandLink = document.createElement("a");
					expandLink.id = "expand-all";
					expandLink.onclick = extendContractAll;
					expandLink.title = "Click to expand the headings on this page.";
					expandLink.appendChild(document.createTextNode("Expand all headings"));
					var collapseLink = document.createElement("a");
					collapseLink.id = "collapse-all";
					collapseLink.onclick = extendContractAll;
					collapseLink.title = "Click to hide the headings on this page.";
					collapseLink.appendChild(document.createTextNode("Collapse all headings"));
					controls.appendChild(expandLink);
					controls.appendChild(document.createTextNode(" | "));
					controls.appendChild(collapseLink);
					document.getElementById("subcontent").insertBefore(controls,allTags[i]);
					i = i + 3;
				}
				allTags[i].id = "expanding-box_" + expandContainerCounter;
				allTags[i].style.height = "2.2em";
				expandContainerCounter++;
				break;
			case "toggle":
				allTags[i].className = "toggle off-state";
				allTags[i].onclick = extendContract;
				allTags[i].title = "Click to show more information.";
				allTags[i].id = "toggle_" + toggleCounter;
				if (allTags[i].style.visibility == "hidden") {
					allTags[i].style.visibility = "visible";
				}
				toggleCounter++;
				break;
			case "hidden":
				allTags[i].id = "hidden_" + expandCounter;
				var thisHiddenContents = document.createElement("div");
				thisHiddenContents.className = "hidden-contents";
				thisHiddenContents.innerHTML = allTags[i].innerHTML;
				allTags[i].innerHTML = "";
				allTags[i].appendChild(thisHiddenContents);
				if (allTags[i].parentNode.className == "expanding-box"){
					if (hideBar == undefined){
						var hideBar = document.createElement("p");
						hideBar.className = "close";
						hideBar.appendChild(document.createTextNode("Close"));
						hideBar.title = "Click to show less information.";
					}
					var thisHideBar = hideBar.cloneNode(true);
					thisHideBar.onclick = extendContract;
					thisHideBar.id = "close_" + expandCounter;
					allTags[i].appendChild(thisHideBar);
				}
				height[expandCounter] = allTags[i].offsetHeight;
				speed[expandCounter] = (Math.sqrt(allTags[i].offsetHeight)) * 15;
				allTags[i].style.height = "0px";
				isExtended[expandCounter] = 0;
				expandCounter++;
				break;
			default:
				break;
		}
	}
	if (document.location.hash !== ""){
		var anch = document.location.hash.split("#")[1];
		if (document.getElementById(anch) != undefined){
			anchNum = document.getElementById(anch).parentNode.id.split("_")[1];
			document.location = "#" + anch;
			document.getElementById("toggle_" + anchNum).onclick();
		}
	}
}

function changePicture(){
	switch((document.location.pathname.split("/")[4])){
		case "subject":
			var subject = document.location.pathname.split("/")[5];
			var image = document.createElement("img");
			image.src = "/division/library/images/subject/big/" + subject + ".jpg";
			image.alt = "Header image";
			image.title = "Header image";
			document.getElementById("subject-support-image").appendChild(image);
			break;
		case "links":
			var subject = (document.location.pathname.split("/")[5]).split(".html")[0];
			var image = document.createElement("img");
			image.src = "/division/library/images/reference/big/" + subject + ".jpg";
			image.alt = "Header image";
			image.title = "Header image";
			document.getElementById("subject-support-image").appendChild(image);
			break;
		case "endnote":
			var image = document.createElement("img");
			image.src = "/division/library/images/research/endnote/endnote-box.jpg";
			image.alt = "Header image";
			image.title = "Header image";
			document.getElementById("subject-support-image").appendChild(image);
			break;
		case "databases":
			var subject = (document.location.pathname.split("/")[6]).split(".html")[0];
			var image = document.createElement("img");
			image.src = "/division/library/images/subject/big/" + subject + ".jpg";
			image.alt = "Header image";
			image.title = "Header image";
			document.getElementById("subject-support-image").appendChild(image);
			break;
		default:
			break;
	}
}

function closeWindow(){
	window.close()
}

function showFeedbackBox(){
	if(document.getElementById("web-feedback-box").style.visibility == "hidden"){
		document.getElementById("web-feedback-box").style.visibility = "visible";
		document.getElementById("web-feedback-box").style.display = "block";
		document.getElementById("web-feedback-tab").className = "on-state";
	} else {
		document.getElementById("web-feedback-box").style.visibility = "hidden";
		document.getElementById("web-feedback-box").style.display = "none";
		document.getElementById("web-feedback-tab").className = "off-state";
	}
}

function setupWebFeedback(){
	document.getElementById("web-feedback-link").attributes.removeNamedItem("href");
	document.getElementById("web-request-link").href = "/division/library/restricted/web-content/email.html?title=" + document.title + "&URL=" + document.location.href;
}
