/* SHOW/HIDE CONTENT

*** NOTES ***
[2005-09-06 bsweeney]
Please refer to the documentation at http://www.project2061.org/includes/CodeLibrary/ShowHideContent/shContent.manual.htm
*/

function sh_lib_bwcheck(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.safari=this.agent.indexOf("Safari")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6||this.ie7
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}

function sh_addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function sh_highlightContent (sh_countHighlight) {
	/* If the sh_countHighlight was not passed then set it to zero (begin of highlight). */
	sh_countHighlight == null ? sh_countHighlight = 0 : null;
	
	/* If sh_countHighlight is less than the length of the array sh_borderColor then modify 
	the show/hide content border according to the value in the array with an index matching 
	sh_countHighlight, then increment sh_countHighlight and call the function again. */
	if (sh_countHighlight < sh_borderColor.length) {
		sh_contentTag.style.borderLeft = '3px solid ' + sh_borderColor[sh_countHighlight];
		sh_contentTag.style.paddingLeft = '3px';
		sh_contentTag.style.marginLeft = '-6px';
		sh_countHighlight++;
		setTimeout('sh_highlightContent('+sh_countHighlight+');', 100);
	
	/* If sh_countHighlight is greater than the length of the array clear the border values */
	} else {
		sh_contentTag.style.borderLeft = '';
		sh_contentTag.style.paddingLeft = '';
		sh_contentTag.style.marginLeft = '';
	}
}

function sh_zoomContent (sh_countHighlight) {
	/* If the sh_countHighlight was not passed then set it to zero (begin of highlight). */
	sh_countHighlight == null ? sh_countHighlight = 0 : null;
	
	/* If sh_countHighlight is less than 10 then modify the text size of the show/hide content 
	according to the value of sh_countHighlight, then increment sh_countHighlight and call the 
	function again. */
	if (sh_countHighlight < 10) {
		sh_contentTag.style.fontSize = '.'+sh_countHighlight+'em';
		sh_countHighlight++;
		setTimeout('sh_highlightContent('+sh_countHighlight+');', 10);
	
	/* If sh_countHighlight is greater than 10 set the size back to it's default value; */
	} else {
		sh_contentTag.style.fontSize = '';
	}
}

function sh_changeDisplay (e) {
	if (!e) { var e = window.event; }
	if (!sh_followLink) { 
		if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; }
	}
	if (e.target) { sh_target = e.target; } else if (e.srcElement) { sh_target = e.srcElement; }
	
	/* If an image is used with the show/hide action link then we may need to go up the DOM tree in IE 
	which view the target to be the image, not the <a> tag where the event is attached. */
	while (sh_target.nodeName.toUpperCase() != 'A' || sh_target.nodeName.toUpperCase() == 'BODY') {
		sh_target = sh_target.parentNode;
	}
	
	/* Set the global variable sh_contentTag to the show/hide content object then change it's display.
	The global variable is used in the highlighting functions. */
	sh_contentTag = document.getElementById(sh_target.href.substring(sh_target.href.indexOf('#')+1)).parentNode;
	sh_changeDisplayAction(sh_contentTag, sh_target);
}

function sh_toggle (sh_toggleMode) {
	var araLinkTags = document.getElementsByTagName('a');
	var objAnchorTag = null;
	var i, j, k;
	for (i = 0; i < araLinkTags.length; i++) {
		/* If a link is a show/hide action link (i.e. has a class of shLInk) then grab the target show/hide 
		content. Determine, based on the toggle mode (show or hide) and the class on the show/hide content, 
		whether or not to call the sh_changeDisplayAction function. */
		if (araLinkTags[i].className.indexOf('shLink') != -1) {
			objAnchorTag = document.getElementById(araLinkTags[i].href.substring(araLinkTags[i].href.indexOf('#')+1)).parentNode;
			if (sh_toggleMode == 'SHOW' && objAnchorTag.className.indexOf('shHidden') != -1) {
				sh_changeDisplayAction(objAnchorTag, araLinkTags[i]);
			} else if (sh_toggleMode == 'HIDE' && objAnchorTag.className.indexOf('shHidden') == -1) {
				sh_changeDisplayAction(objAnchorTag, araLinkTags[i]);
			}
		}
	}
}

function sh_changeDisplayAction (targetObj, sourceObj) {
	if (sh_bw.dom) {
		
		/* If the show/hide content object has a class of shHidden remove that class, highlight the 
		content if the sh_displayHighlight parameter is true, and change the image if a value was set 
		for sh_openImage. */
		if (targetObj.className.indexOf('shHidden') != -1) {
			targetObj.className = targetObj.className.replace('shHidden', '');
			sh_displayHighlight ? sh_highlightContent() : null;
			sh_openImage.length > 0 ? sh_changeImage(sourceObj, sh_closedImage, sh_openImage) : null;
		
		/* If the show/hide content object does not have a class of shHidden add that class and change 
		the image if a value was set for sh_closedImage. */
		} else {
			targetObj.className += ' shHidden';
			sh_closedImage.length > 0 ? sh_changeImage(sourceObj, sh_openImage, sh_closedImage) : null;
		}
	}
}

function sh_changeImage (targetObj, targetImg, replaceImg) {
	if (sh_bw.dom) {
		var i, j, k;
		var araImages = targetObj.getElementsByTagName('img');
		for (i = 0; i < araImages.length; i++) {
			araImages[i].src.indexOf(targetImg) > -1 ? araImages[i].src = araImages[i].src.replace(targetImg, replaceImg) : null;
		}
	}
}

function sh_init (e) {
	if (document.getElementsByTagName) {
		var i, j, k;
		var bolSHChkAnchor = false;
		var objDOMNode = null;
		var objAnchorTag = null;
		var sh_init_tags = document.getElementsByTagName('a');
		
		/* Get the URL hash for later reference. */
		if (location.hash.length > 0) {
			objID = String(location.href);
			objID = objID.substring(objID.indexOf('#')+1, objID.length)
			bolSHChkAnchor = true;
		}
		
		/* Parse all anchors in the document looking for show/hide content. */
		for (i = 0; i < sh_init_tags.length; i++) {
			if (sh_init_tags[i].className == 'shContent') {
				/* Ensure that the anchor has an id attribute so that we can find it when we're performing 
				the show/hide action. */
				sh_init_tags[i].id != sh_init_tags[i].name ? sh_init_tags[i].id = sh_init_tags[i].name : null;
				
				/* If a named anchor has a class of shContent, does not match the value of objID, and 
				objID is not set to 'shToggle' (a catchall for not hide show/hide content areas) then 
				give it's parent object (the show/hide content container) a class of shHidden */
				if (bolSHChkAnchor && sh_init_tags[i].name != objID && objID != 'shToggleOn') {
					sh_init_tags[i].parentNode.className += ' shHidden';
				} else if (!bolSHChkAnchor) {
					sh_init_tags[i].parentNode.className += ' shHidden';
				}
				
				/* Parse all anchors again, this time seeking links to the show/hide content. */
				for (j = 0; j < sh_init_tags.length; j++) {
					/* Conditional to determine if an anchor points to the show/hide content. First 
					check to make sure the anchor has an href attribute. Next, check to see if the 
					hash in the href attribute matches the id of the show/hide content anchor. Finally, 
					check to see if the page referenced in the href attribute matches the current page 
					(assuming that the browser translates the href into a full URL). The presence of a 
					querystring, a link to a page with the same name in a different directory, or a 
					link to an off-site page will cause problems with this last test.  The last check 
					is necessary so that links to other pages aren't turned into show/hide action 
					links. */
					if (sh_init_tags[j].href && sh_init_tags[j].href.substring(sh_init_tags[j].href.indexOf('#')+1) == sh_init_tags[i].id && sh_init_tags[j].href.substring(sh_init_tags[j].href.lastIndexOf('/')+1, sh_init_tags[j].href.indexOf('#')) == location.href.substring(location.href.lastIndexOf('/')+1, location.href.indexOf('#')>0?location.href.indexOf('#'):location.href.length)) {
						sh_init_tags[j].className += " shLink";
						sh_addEvent(sh_init_tags[j], 'click', sh_changeDisplay, false);
					}
					
					/* Conditional to determine if the anchor matches the URL hash. Similar to the 
					above conditional. */
					if (bolSHChkAnchor && sh_init_tags[j].href.substring(sh_init_tags[j].href.indexOf('#')+1) == objID && sh_init_tags[j].href.substring(sh_init_tags[j].href.lastIndexOf('/')+1, sh_init_tags[j].href.indexOf('#')) == location.href.substring(location.href.lastIndexOf('/')+1, location.href.indexOf('#')>0?location.href.indexOf('#'):location.href.length)) {
						objAnchorTag = sh_init_tags[j]
					}				
				}
			}
		}
		
		/* If a DOM node is present with an id attribute of 'shToggle' then add the show all/hide all 
		toggle. */
		if (objDOMNode = document.getElementById('shToggle')) {
			var toggleLink, toggleText;
			toggleLink = document.createElement("a");
			toggleLink.href = "#shToggleOn";
			toggleLink.name = toggleLink.id = "shToggleOn";
			toggleLink.onclick = function () { sh_toggle('SHOW'); return sh_followLink; }
			toggleText = document.createTextNode("SHOW ALL");
			toggleLink.appendChild(toggleText);
			objDOMNode.appendChild(toggleLink);
			
			toggleText = document.createTextNode(" / ");
			objDOMNode.appendChild(toggleText);
			
			toggleLink = document.createElement("a");
			toggleLink.href = "#shToggleOff";
			toggleLink.name = toggleLink.id = "shToggleOff";
			toggleLink.onclick = function () { sh_toggle('HIDE'); return sh_followLink; }
			toggleText = document.createTextNode("HIDE ALL");
			toggleLink.appendChild(toggleText);
			objDOMNode.appendChild(toggleLink);
		}
		
		/* If there is a URL hash, find the position of the anchor and scroll to it. This is necessary 
		due to page shifting as content is hidden. If the hash points to a show/hide content anchor 
		we'll use the show/hide action link as the target object instead of the show/hide content. */
		if (bolSHChkAnchor) {
			objAnchorTag == null ? objAnchorTag = document.getElementById(location.hash.substring(1)) : null;
			var posY = findPosY(objAnchorTag);
			window.scroll(0, posY);
		}
	}
}

// Global variable initialization
var sh_closedImage = "";
var sh_openImage = "";
var sh_displayHighlight = false;
var sh_followLink = false;
var sh_contentTag;
var sh_borderColor = Array('#f0f0f0','#e0e0e0','#d0d0d0','#c0c0c0','#b0b0b0','#a0a0a0','#909090','#808080','#707070','#707070','#808080','#909090','#a0a0a0','#b0b0b0','#c0c0c0','#d0d0d0','#e0e0e0','#f0f0f0');
var sh_bw=new sh_lib_bwcheck()

// Attach the initializing function so that it runs when the page finishes loading
sh_addEvent(window, 'load', sh_init, false);
