function addEvent(elm, evType, fn, useCapture){if(elm.addEventListener){elm.addEventListener(evType, fn, useCapture);return true;}else if (elm.attachEvent){var r = elm.attachEvent('on' + evType, fn);return r;}else{elm['on' + evType] = fn;}}

/* START =Concertina */
/*
We may want to migrate this to jQuery for smoother 
transitions. For now it's hand-rolled/custom.
*/

//global constants
var collapsed = false;
var useShowAllLink = false;
var hideAllTitle = "Hide all the topics in this list";
var showAllTitle = "Show all the topics in this list";
var hideAllText = "Hide all";
var showAllText = "Show all";
var plusImageURL = "url(concertinaPlus.gif)";
var minusImageURL = "url(concertinaMinus.gif)";
var dontShowAll = "";
//check to see if fragment identifier in URL
var u = location.href;
var anch = u.split("#")[1];

function togglePlusMinus(whichLink, plusOrMinus){
	if (plusOrMinus=="plus")	{
		whichLink.style.backgroundImage = plusImageURL;
		whichLink.style.backgroundRepeat = "no-repeat";
		whichLink.style.backgroundPosition = "4px 4px";
	}
	if (plusOrMinus=="minus")	{
		whichLink.style.backgroundImage = minusImageURL;
		whichLink.style.backgroundRepeat = "no-repeat";
		whichLink.style.backgroundPosition = "4px 4px";
	}
}
function concertinaOpenCloseAll(source,displayState){
if (displayState=="open"){
	displayState = "block";
 collapsed = false;
	}
if (displayState=="close"){
	displayState = "none";
 collapsed = true;
	}
if (source.tagName!=null) {
	//only handling this individual concerina, not all of them
	var ul = source.parentNode;
	}
else {
	var ul = document.getElementsByTagName("ul");
	}
if (ul.length>0)
	{
for (i=0; i<ul.length; i++){
	if (ul[i].className=="concertina"){
		var ul_divs = ul[i].getElementsByTagName("div");
		for (j=0; j<ul_divs.length; j++){
			if (ul_divs[j].className=="ListHead"){
				if (collapsed) {
					togglePlusMinus(ul_divs[j].getElementsByTagName("a")[0],"plus");
					}
				else {
					togglePlusMinus(ul_divs[j].getElementsByTagName("a")[0],"minus");
					}
				}
			if (ul_divs[j].className=="ListBody"){
				ul_divs[j].style.display=displayState;
				}
			}
		} // end of if ul concertina class
	} //end of ul for loop
	}
else
	{
	var ul_divs = ul.getElementsByTagName("div");
	for (k=0; k<ul_divs.length; k++){
		if (ul_divs[k].className=="ListHead"){
			if (collapsed) {
				togglePlusMinus(ul_divs[k].getElementsByTagName("a")[0],"plus");
				}
			else {
				togglePlusMinus(ul_divs[k].getElementsByTagName("a")[0],"minus");
				}
			}
		if (ul_divs[k].className=="ListBody"){
			ul_divs[k].style.display=displayState;
			}
		}
	}
}
function showHideSingleLink(thisLink){
var listbody = thisLink.parentNode.parentNode.getElementsByTagName("div")[1]	;
if (listbody.style.display =="none"){
	listbody.style.display = "block";
	togglePlusMinus(thisLink,"minus");
	}
else{
	listbody.style.display = "none";
	togglePlusMinus(thisLink,"plus");
	}
}
function addHoverBehaviourtoTab()
{
	$(".ListHead a.toggleSwitch, a.concertina_showall").focus(function(){
	$(this).addClass("hover");
	}).blur(function(){
	$(this).removeClass("hover");
	});
}
function initConcertina(){
concertinaOpenCloseAll(this,'close');
var ul = document.getElementsByTagName("ul");
for (i=0; i<ul.length; i++){
	if (ul[i].className=="concertina"){
		//add show/hide all link after list if there is more than one list item contained
		var listCount = ul[i].getElementsByTagName("li").length;
		useShowAllLink = ((listCount > 1)&&(!dontShowAll));
		if (useShowAllLink){
			var showAllLink = document.createElement("a");
			var showAllLinkText = document.createTextNode(showAllText);
			showAllLink.setAttribute("href","#");
			showAllLink.setAttribute("class","concertina_showall");
			showAllLink.setAttribute("className","concertina_showall");
			showAllLink.setAttribute("title", showAllTitle);
			showAllLink.appendChild(showAllLinkText);
			ul[i].appendChild(showAllLink, ul[i].parentNode);
			showAllLink.onclick = function(e){
				if (collapsed){
					concertinaOpenCloseAll(this,'open');
					this.setAttribute("title", hideAllTitle);
					this.innerHTML=hideAllText;
					}
				else{
					concertinaOpenCloseAll(this,'close');
					this.setAttribute("title", showAllTitle);
					this.innerHTML=showAllText;
					}
				return false;
				}
			}
		//find the ListHead divs
		var ul_divs = ul[i].getElementsByTagName("div");
		for (j=0; j<ul_divs.length; j++){
			if (ul_divs[j].className=="ListHead"){
				//now find the link that's inside that listHead
				var this_a = ul_divs[j].getElementsByTagName("a")[0];
				this_a.onclick = function(e){
						showHideSingleLink(this);
						return false;
					}
				// check to see if this link is the target of an external link 
				// (with a fragment identifier, e.g. a href="QandA.htm#q5
				if ((anch)&&(anch.length>0)){
					if (this_a.name == anch){
						showHideSingleLink(this_a);
						}
					}
				}
			}
		} // end of if ul concertina class
	} //end of ul for loop
}
/* END =Concertina */
addEvent(window, 'load', initConcertina, false);

