// This file holds variables & functions for expandable leftnav and abstracts.

var strStyle = "<style type='text\/css'>";
strStyle += "#leftnav dd.navLevel2off {display:none;}";
strStyle += ".displayOff {display:none !important;}";
strStyle += ".catWrap {display:none;}";
strStyle += ".abstract {padding:5px;display:none;}";
strStyle += "<\/style>";
document.write(strStyle);

// GLOBAL VARIABLES

// preload some images
// abstract toggle icons:
var toggleShow = new Image();
var toggleHide = new Image();
toggleShow.src = "http://www.sas.com/images/whitepapers/toggle_show.gif";
toggleHide.src = "http://www.sas.com/images/whitepapers/toggle_hide.gif";
// left nav elbows:
var elbowOn = new Image();
var elbowOff = new Image();
elbowOn.src = "http://www.sas.com/images/elbow_on.gif";
elbowOff.src = "http://www.sas.com/images/elbow_off.gif";
// left nav toggle icons:
var leftnavShow = new Image();
var leftnavHide = new Image();
var leftnavHideInactive = new Image();
leftnavShow.src = "http://www.sas.com/images/navgraphics/plus.gif";
leftnavHide.src = "http://www.sas.com/images/navgraphics/minus.gif";
leftnavHideInactive.src = "http://www.sas.com/images/navgraphics/minus_inactive.gif";
// left nav bullets:
var leftnavBulletOn = new Image();
var leftnavBulletOff = new Image();
leftnavBulletOn.src = "http://www.sas.com/images/dot_on.gif";
leftnavBulletOff.src = "http://www.sas.com/images/dot_off.gif";

// activeid is set when a 2nd level left navlink (next to an elbow icon) is clicked
var activeid;

// array to keep track of which abstracts are in an expanded state.
var expandedArray = [];

// vars for leftnav level 1
var selectedLeftNavTargetPage;
var selectedLevel1State = 0;
var selectedLevel1, selectedLevel1Toggle, selectedLevel1Link;

// vars for leftnav links that don't toggle
var nonToggleState = 0;
var nonToggleBullet, nonToggleLink;

// vars for leftnav level 2 & content
var selectedLevel2, selectedLevel2Link, selectedLevel2Elbow, selectedContent;

// GLOBAL FUNCTIONS

// loadpage fires during body onload and when level 2 left nav link is clicked. 
function loadpage(id) {
	//convert id to string
	var strId = id.toString();
	if (strId.indexOf('-') > -1) {
		//sub categories are present in the hash so trim off subcats to right of dash
		id = strId.substring(0, strId.indexOf('-'));
	}
	try {
		//deactivate level 1 icon & link
		if (selectedLevel1){
			selectedLevel1Toggle.src = leftnavHideInactive.src;
			selectedLevel1Link.className = "navLevel1off";
		}
		//deactivate & hide the previous content
		if (activeid){
			selectedLevel2Elbow.src = elbowOff.src;
			selectedLevel2Link.className = "inactive";
			if (selectedContent) {selectedContent.className = "displayOff";}	
		}	
		//activate the elbow
		selectedLevel2Elbow = document.getElementById('elbow'+currentMetaAlias+id);
		selectedLevel2Elbow.src = elbowOn.src;
		//activate the level 2 link
		selectedLevel2Link = document.getElementById('nav'+currentMetaAlias+id);
		selectedLevel2Link.className = "active";
		//hide help message
		document.getElementById('pageHelp').className = "displayOff";
	}catch(e){}
	try {
		//show the selected content
		selectedContent = document.getElementById('list'+id);
		selectedContent.className = "displayOn"; 
	}catch(e){}
	activeid = id;
}

// showdiv fires when leftnav level 2 links are clicked. 
function showdiv(id) {
	if (currentMetaAlias == selectedLevel1){
		//refresh hash in address bar
		window.location.hash = id;
		//load the content
		loadpage(id);
	} else {
		window.location.href = selectedLeftNavTargetPage + '#' + id;
	}
}

// loadhash fires at body onload when body is finished loading.
function loadhash() {
	if (!document.getElementsByTagName) return false;
	if (window.location.hash){
		var id = window.location.hash.substr(1);
		//strip id from hash if present
		if (id.indexOf('id') > -1) {
			id = id.substr(2);
		}
		loadpage(id);
		//scroll to subcategory if present
		if (id.indexOf('-') > -1) {
			var obj = document.getElementById("id" + id);
			var top = findPos(obj);
			window.scrollTo(0,top);
		}	
	} else {
		document.getElementById('pageHelp').className = "displayOn";
	}
	toggleLeftNav(currentMetaAlias, window.location);
}

// toggle controls which abstracts are hidden & displayed. 
function toggle(id) {
	if (!document.getElementsByTagName) return false;
	if (expandedArray[id]){
		//this id was expanded so collapse it
		hideAbstract(id);
		//remove this id from array
		expandedArray[id] = "";		
	} else {
		//not yet in the array yet so just expand it & add to array
		showAbstract(id);
		expandedArray[id] = id;
	}
}

// showAbstract & hideAbstract control visual display of abstracts. 
function showAbstract(id) {
	document.getElementById('abstract'+id).className = "abstractOn";
	document.getElementById('toggleTab'+id).style.background = "#C8E2F9";
	document.getElementById('showLink'+id).innerHTML = "Hide Abstract";
	document.getElementById('toggle'+id).src = toggleHide.src;
}
function hideAbstract(id) {	
	document.getElementById('abstract'+id).className = "abstractOff";
	document.getElementById('toggleTab'+id).style.background = "#fff";
	document.getElementById('showLink'+id).innerHTML = "Show Abstract";
	document.getElementById('toggle'+id).src = toggleShow.src;
}

/* toggleLeftNav fires when top level of leftnav is clicked  
   & controls visual display and user interaction of the leftnav. */
function toggleLeftNav(parent, pageURL){
	if (!document.getElementsByTagName) return false;

	var parentNode = document.getElementById(parent);
	var childNode = document.getElementById(parent+'Level2');
	var toggleNode = document.getElementById(parent+'toggle');
	
	if (nonToggleState == 1){
		// user is currently on a page whose leftnav bullet doesn't toggle.
		// user is clicking on a toggle so deactivate the nontoggling bullet. 
		nonToggleBullet.src = leftnavBulletOff.src;
		nonToggleLink.className = "noToggleOff";
		nonToggleState = 0;
	}
	
	if (selectedLevel1State == 0 && parent == currentMetaAlias){
		if (!activeid) {
			//show the orange minus icon by section we are expanding
			toggleNode.src = leftnavHide.src;
			//activate the level 1 link
			parentNode.className = "navLevel1on";
		} else {
			//show the blue minus icon by section we are expanding
			toggleNode.src = leftnavHideInactive.src;
		}
		//show the selected section
		childNode.className = "navLevel2on";
		selectedLevel1State = 1;
		
	} else if (selectedLevel1State == 0 && parent != currentMetaAlias) {
	
		//show the orange minus icon by section we are expanding
		toggleNode.src = leftnavHide.src;
		
		//activate the level 1 link
		parentNode.className = "navLevel1on";
		
		//show the selected section
		childNode.className = "navLevel2on";
		selectedLevel1State = 1;
		
	} else if (parent == selectedLevel1 && selectedLevel1State == 1){  
		
		//user wants to collapse the currently expanded selection
		
		//show the blue plus icon by section we are hiding
		toggleNode.src = leftnavShow.src;
		
		//deactivate the level 1 link
		parentNode.className = "navLevel1off";
		
		//hide the section
		childNode.className = "displayOff";
			
		//set state to 0 (closed)
		selectedLevel1State = 0;
	
	} else {
		
		//menu is currently expanded and the user wants to expand a different part of the menu
		
		//show the blue plus icon next to the section we need hide
		selectedLevel1Toggle.src = leftnavShow.src;
		
		//deactivate the level 1 link
		if (selectedLevel1Link) {
			selectedLevel1Link.className = "navLevel1off";
		}
		
		//hide the previous section
		if (selectedLevel2){
			document.getElementById(selectedLevel2).className = "displayOff";
		}

		//menu is now collapsed, so now expand menu
		
		if (activeid && currentMetaAlias == parent){		

			//show the blue minus icon
			toggleNode.src = leftnavHideInactive.src;
			
		} else {
			//show the orange minus icon
			toggleNode.src = leftnavHide.src;
			
			//activate the level 1 link
			parentNode.className = "navLevel1on";
		}

		//expand the selected section
		childNode.className = "navLevel2on";
		selectedLevel1State = 1;	
	}
	selectedLevel1 = parent;
	selectedLevel1Toggle = toggleNode;
	selectedLevel1Link = parentNode;
	selectedLevel2 = parent+'Level2';
	selectedLeftNavTargetPage = pageURL;
}

/* setLink function is used by leftnav links that don't toggle. When the browser loads the
   page that is associated with a non-toggling link, setLink will highlight the link. */
function setLink(bullet,link){
	if (!document.getElementsByTagName) return false;
	try {
		nonToggleBullet = document.getElementById(bullet);
		nonToggleBullet.src = leftnavBulletOn.src;
		nonToggleLink = document.getElementById(link);
		nonToggleLink.className = "noToggleOn";
		nonToggleState = 1;
	}catch(e){}
}

// scroll to top & change hash
function scroll(id) {
	window.scrollTo(0,0);
	window.location.hash = id;
}

// findPos returns height from top for a given element
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}