
	// general utilities
	
			
// event delegation

var setup = function()
{


	if (!document.getElementById ||  !document.createTextNode)
	{  return   } 
	
	
		var windowLoc = "";
		var firstPos = location.href.indexOf("#")
		if (firstPos != -1) {
			windowLoc = location.href.substring(0,firstPos);
		}
	
	// get podCatContainer and set class to dynamic
	var podCat = document.getElementById("podCatContainer");
	/*for (i=0;i<podCat.getElementsByTagName('ul').length; i++) {
		//hides ul on load and on javascript-enabled
		podCat.getElementsByTagName('ul')[i].className = "dynamic";
	}	*/
	
	
	
	var podSubcat = document.getElementById("podSubcatContainer");
	/*for (i=0;i<podSubcat.getElementsByTagName('a').length; i++) {
			//hides ul on load and on javascript-enabled
			if (podSubcat.getElementsByTagName('a')[i].innerHTML == "Show all") {
				podSubcat.getElementsByTagName('a')[i].className = "dynamicA";
			}
		}*/
		
		
	// add Show all button for browsers with javascript
	var extraHTML = '<a href="javascript:;" id="showHideAll">Show all</a>';
	podSubcat.innerHTML = extraHTML + podSubcat.innerHTML;


	
		
		var theseLiNodes = podSubcat.getElementsByTagName('li');
		var aNodesArray = new Array();
		
		// add >> to category and subcategory links
		for (i=0; i<theseLiNodes.length; i++) {
			if (theseLiNodes[i].getElementsByTagName('a')[0].innerHTML != "" && theseLiNodes[i].getElementsByTagName('a')[0].parentNode.nodeName.toLowerCase() != "em") {
				aNodesArray.push(theseLiNodes[i].getElementsByTagName('a')[0].innerHTML);
				theseLiNodes[i].getElementsByTagName('a')[0].innerHTML = theseLiNodes[i].getElementsByTagName('a')[0].innerHTML + " &raquo;";
			}
		}
		
		
		//if it's the podcast by date page (5th letter from end is "s" from "podcasts" open the first blocks and change show image
	if (location.href.charAt(location.href.length-5) == "s") { 	
		podSubcat.getElementsByTagName('ul')[0].style.display = "block";
		podSubcat.getElementsByTagName('a')[1].innerHTML = "- " + podSubcat.getElementsByTagName('a')[1].innerHTML.substring(0,podSubcat.getElementsByTagName('a')[1].innerHTML.length-2) + " -";
		podSubcat.getElementsByTagName('ul')[1].style.display = "block";
		podSubcat.getElementsByTagName('a')[2].innerHTML = "- " + podSubcat.getElementsByTagName('a')[2].innerHTML.substring(0,podSubcat.getElementsByTagName('a')[2].innerHTML.length-2) + " -";
	}
		
		
		
	function getHref(string) { //get text after #
       	var findHashPos = string.lastIndexOf("#");
        var restOfWord = string.substr(findHashPos+1);

        string =  restOfWord; 
  		return string;
	}
	
	var allLinks = document.getElementsByTagName('a');
	var incomingLink = location.href.toString();
	var incomingLinkIndex = incomingLink.lastIndexOf("#");

	if (incomingLinkIndex != -1) {
		var th = getHref(incomingLink);
		//location.href = windowLoc;
		openLinked(th);
	};

	function openLinked(th) {
		var i = 0;
				for (i=0; i<allLinks.length; i++) {
					if (allLinks[i].getAttribute('name') == th) {	// find the A that contains the href
						
							
							
							// if it is a category link
							if (allLinks[i].parentNode.parentNode.id == "podSubcatContainer") {
								closeEveryCat(allLinks[i]);
								allLinks[i].parentNode.getElementsByTagName('ul')[0].style.display = "block";
								changeShowImg (allLinks[i]);
								
								//updatePageNav(allLinks[i]);
								
								//if it is a subcategory or specific podcast link
							} else {
							
							
							changeShowImg (allLinks[i].parentNode.parentNode.parentNode.getElementsByTagName('a')[0]);
							//updatePageNav(allLinks[i].parentNode.parentNode.parentNode.getElementsByTagName('a')[0]);
						// cycle through podSubcat's ULs to find which one it's in and open them, if not, close them.
						var k=0;
						for (k=0; k<podSubcat.getElementsByTagName('ul').length; k++) {
							var thisL = podSubcat.getElementsByTagName('ul')[k];
					
							var j=0;
							for (j=0; j<thisL.getElementsByTagName('a').length;j++) {
								if (thisL.getElementsByTagName('a')[j] == allLinks[i]) {
									thisL.style.display = 'block';
									break;
								} else {
									thisL.style.display = 'none';
								}
							}
							
						}
														
						
							}
						
						location.href = location.href;
						
						break;
						//location.href = th.getAttribute('href');
					}
				}
				
				//showHideShowAllLink();
	}

	
	/*document.onclick = function(g){
		var tg = getTarget(g); // gets clicked object
			
				//alert(tg.getAttribute('href').lastIndexOf("#"));
			if (tg.nodeName == "A") {// && tg.getAttribute('href').lastIndexOf("#") == 0) { // if # is the first char			 / if a # is found in the url  - tg.getAttribute('href').lastIndexOf("#") != -1 
				var th = getHref(tg.getAttribute('href'));
					openLinked(th);
					//updatePageNav(tg);
			}
		}*/
		
	
	var allOpen = false;	
	var allSubOpen = false;



	function showHideAllSub (t) {
			if (allOpen && t.innerHTML == "Show all") {
				var e = document.getElementById('podSubcatContainer');
				var el = e.getElementsByTagName('ul');
				var k = 0;
				for (k=0;k<el.length; k++) {
					if (el[k] != t.parentNode.getElementsByTagName('ul')[0]) {
							el[k].style.display = 'none';
					}
				}
				//showHideShowAllLink();
			}
			var i=0;
			for (i=0;i<t.parentNode.getElementsByTagName('ul').length; i++) {
				var ul = t.parentNode.getElementsByTagName('ul')[i]; 
				for (j=0;j<ul.getElementsByTagName('ul').length; j++) {
					var vl = ul.getElementsByTagName('ul')[j]; 
					if (t.firstChild.nodeValue == "Show all") {
						vl.style.display = 'block';
					} else if (t.firstChild.nodeValue == "Hide all") {
						vl.style.display = 'none';
					}
				}
			}	
			if (t.firstChild.nodeValue == "Show all") {
				t.innerHTML = "Hide all";
				allSubOpen = true;
			} else if (t.firstChild.nodeValue == "Hide all") {
				t.innerHTML = "Show all";
				allSubOpen = false;
			}
		
	}
	
	function showHideAll (t) {
			var m = document.getElementById('podSubcatContainer');
			if (t.innerHTML == "Show all") {
				closeEveryCat(t);
				for (k=0;k<m.childNodes.length; k++) {
					if (m.childNodes[k].nodeName.toLowerCase() == "li") {
						var mm = m.childNodes[k].getElementsByTagName('ul')[0];
								mm.style.display = 'block';
					}
				};
			} else if (t.innerHTML == "Hide all") {
				closeEveryCat(t);
			}
			if (t.innerHTML == "Show all") {
				t.innerHTML = "Hide all";
				allOpen = true;
			} else if (t.innerHTML == "Hide all") {
				t.innerHTML = "Show all";
				allOpen = false;
			}
			changeShowImg (t)
	}
	
	function closeEverySubcat (t,m) {
	//closes every ul contained in parent ul
				var ml = m.getElementsByTagName('ul');			
				for (k=0;k<ml.length; k++) {
						ml[k].style.display = 'none';
				}
				if (allOpen) {
					closeEveryOtherCat(t);
				}
	}
	
	function closeEveryOtherCat(t) {
		//closes every ul but the one containing the clicked link
		var e = document.getElementById('podSubcatContainer');
		var el = e.getElementsByTagName('ul');
		for (k=0;k<e.getElementsByTagName('ul').length; k++) {
			if (e.getElementsByTagName('ul')[k] != t.parentNode.parentNode) {
				el[k].style.display = 'none';
			}
		}	
	}
	
	function closeEveryCat (t) {
	//closes every ul under main ul
		var m = document.getElementById('podSubcatContainer');
		var ml = m.getElementsByTagName('ul');
		for (k=0;k<m.getElementsByTagName('ul').length; k++) {
				ml[k].style.display = 'none';
		}	
		
	}
	
	function showHideCurrentClick (t) {
	//opens/closes ul referenced by click, don't hide if Show all is active
		if (!displayIsBlock){  // if Show all is not active displayIsBlock is false
			cl.style.display = 'block';
		} else if (displayIsBlock) {// if Show all is active displayIsBlock is true
			if (allSubOpen && !allOpen) {
				cl.style.display = 'block';
				allSubOpen = false; 
			} else if (!allSubOpen && !allOpen) {
				cl.style.display = 'none';
			} else if (allOpen) {
				cl.style.display = 'block';
				allOpen = false; 
			}
		}	
		
		//updatePageNav(t);
		
		changeShowImg(t);
	}
	
	
	
	
	function changeShowImg (t) {
		// add >> or - -  to category and subcategory links
	
				if (t.id != "showHideAll" && t.parentNode.nodeName.toLowerCase() != "em") {
		
		for (j=0; j<theseLiNodes.length; j++) {
			if (theseLiNodes[j].getElementsByTagName('a')[0].innerHTML != "") {
				
					// if this is the calling link...
						if (theseLiNodes[j].getElementsByTagName('a')[0].innerHTML == t.innerHTML) { 
						
		 // if Show all is showing...
					if (document.getElementById('showHideAll').innerHTML != "Hide all") {
			
								var hasRaquo = t.innerHTML.charAt(0) != "-";
								// change link text from raquo to - and vice versa
								if (hasRaquo) {
									t.innerHTML = "- " + t.innerHTML.substring(0,t.innerHTML.length-2) + " -";
								} else if (!hasRaquo && t.parentNode.getElementsByTagName('ul')[0].style.display == 'none') {
									t.innerHTML = t.innerHTML.substring(2,t.innerHTML.length - 1) + " &raquo;";
								}
								
								var tparentNode = t.parentNode.parentNode.parentNode.getElementsByTagName('a')[0];
								// if parent node is not the Show all link then change to -
								if (tparentNode.id != "showHideAll") { 
									if (tparentNode.innerHTML.charAt(0) != "-") {
										tparentNode.innerHTML ="- " + tparentNode.innerHTML.substring(0,tparentNode.innerHTML.length-2) + " -";
									}
								}
							
			// if Hide all is showing change it to Show all. If the link is a subcat, change to -
					} else if (document.getElementById('showHideAll').innerHTML == "Hide all") {
						document.getElementById('showHideAll').innerHTML = "Show all";
						if (t.parentNode.parentNode.id != "podSubcatContainer") {
							t.innerHTML ="- " + t.innerHTML.substring(0,t.innerHTML.length-2) + " -";
						}
					}
					
						 // if this is not the calling link...
						} else if (theseLiNodes[j].getElementsByTagName('a')[0].innerHTML != t.innerHTML) {
							var hasRaquo = theseLiNodes[j].getElementsByTagName('a')[0].innerHTML.charAt(0) != "-"; // has raquo if first char is not a -
							// if it currently has a dash and the parent node isn't the link's parent (ie, not the subcat's, if clicked, category) change to raquo
							if (!hasRaquo && theseLiNodes[j] != t.parentNode.parentNode.parentNode) {
							
								theseLiNodes[j].getElementsByTagName('a')[0].innerHTML = theseLiNodes[j].getElementsByTagName('a')[0].innerHTML.substring(2,theseLiNodes[j].getElementsByTagName('a')[0].innerHTML.length - 1) + " &raquo;";
							}
						}
						}
			}
		
				} else if (t.id == "showHideAll") {
					for (j=0; j<theseLiNodes.length; j++) {
					
						var thisPodA = theseLiNodes[j].getElementsByTagName('a')[0];
						// if Show all was clicked and the link is a category then change to -
						if (t.innerHTML == "Hide all" && theseLiNodes[j].parentNode.id == "podSubcatContainer") {
								if (thisPodA.innerHTML.charAt(0) != "-") {
									thisPodA.innerHTML ="- " + thisPodA.innerHTML.substring(0,thisPodA.innerHTML.length-2) + " -";
								} 
						// if Show all was clicked and the link is a sub category then change to raquo
						} else if (t.innerHTML == "Hide all" && theseLiNodes[j].parentNode.id != "podSubcatContainer" && thisPodA.innerHTML != "") {
								if (thisPodA.innerHTML.charAt(0) == "-") {
									thisPodA.innerHTML = thisPodA.innerHTML.substring(2,thisPodA.innerHTML.length - 1) + " &raquo;";
								} 
						// if Hide all was clicked change everything to a raquo
						} else if (t.innerHTML == "Show all") {
							if (thisPodA.innerHTML.charAt(0) == "-") {
								thisPodA.innerHTML = thisPodA.innerHTML.substring(2,thisPodA.innerHTML.length - 1) + " &raquo;";
							}  
						}
					}
			
				}
	}
	
	
	
	
	/*function updatePageNav(t) {
		
		THIS WORKED BUT DECIDED NOT TO USE CATEGORIES IN NAVIGATION
		
		var podNavUL = document.getElementById('podCatNav');
		var thisLinkLi = "";
		// find the clicked link's category li
		//if a category link is clicked set thisLinkLi to nav link with matching ID
		if (t.parentNode.parentNode.id == "podSubcatContainer") {
			thisLinkLi = document.getElementById(t.name).parentNode;
			
		//if a subcategory link is clicked find category and set thisLinkLi to nav link with matching ID
		} else if (t.parentNode.parentNode.id == "" && t.innerHTML != "Show all" && t.innerHTML != "Hide all") {
			var thisLinkCatName = t.parentNode.parentNode.parentNode.getElementsByTagName('a')[0].name;
			thisLinkLi = document.getElementById(thisLinkCatName).parentNode;
			
		//if a navigation link is clicked set thisLinkLi to self's ID
		} else if (t.parentNode.parentNode.id == "podCatNav") {
			thisLinkLi = t.parentNode;
		
		//if the Show all link is clicked set thisLinkLi to nothing
		} else if (t.innerHTML == "Show all" || t.innerHTML == "Hide all") {
			for (i=0; i<podNavUL.getElementsByTagName('li').length;i++) {
					podNavUL.getElementsByTagName('li')[i].className = "";
			}
		}
			
			
			
			//go through podCatNav ul and change classNames
			for (i=0; i<podNavUL.getElementsByTagName('li').length;i++) {
				if (podNavUL.getElementsByTagName('li')[i] == thisLinkLi) {
					thisLinkLi.className = "selected";
				} else {
					podNavUL.getElementsByTagName('li')[i].className = "";
				}
			}
	
		
	}*/
	
	
	
	/* function showHideShowAllLink () {
	THIS FUNCTION THROWS AN ERROR IN IE 
	this is how the code was set up <li><a href="javascript:;">21st-century Williamsburg &raquo;</a> <a href="javascript:;" style="display:none;">Show all</a>
		var thisL = podSubcat.getElementsByTagName('li');
		
		var i=0;
		for (i=0; thisL.length; i++) {
			var thisLI = thisL[i];
			
			if (thisLI.getElementsByTagName('ul')[0].parentNode.parentNode.id=="podSubcatContainer") {
			
				var thisUL = thisLI.getElementsByTagName('ul')[0].style.display;
				var thisLIA = thisLI.getElementsByTagName('a')[1];
				if (thisUL == 'block') {
					thisLIA.style.display = 'inline';
					thisLIA.innerHTML = "Show all";
				  } else if (thisUL != 'block') {
					thisLIA.style.display = 'none';
					thisLIA.innerHTML = "Show all";
				  }
			}
		}
		
	}*/
	
	/*document.onclick = function(g) {
		var gt = getTarget(g);
		if (gt.parentNode.parentNode.id == "podCatNav") {
			//updatePageNav(gt);
		}
	}*/
	
	// start event delegation
	
	
	
	podCat.onclick = function(e){
		var t = getTarget(e); // gets clicked object

	//if a Show all or Hide all link is clicked that is not the main Show all...
	/*	if (t.firstChild.nodeValue == "Show all" && t.id != "showHideAll" || t.firstChild.nodeValue == "Hide all" && t.id != "showHideAll") {
			showHideAllSub(t);
					return false;
		}*/
	
			
		
		
	// if the showHideAll link is clicked...
		if (t.id == "showHideAll") {
			showHideAll(t);
			//updatePageNav(t);
					return false;
			//showHideShowAllLink();
		}
		
	// if a category or subcategory link is clicked...	
		if (t.nodeName.toLowerCase() === 'a' 
								   && t.parentNode.getElementsByTagName('ul').length > 0 
								   && t.innerHTML != "Show all" 
								   && t.innerHTML != "Hide all" 
								   && t.innerHTML != "View transcript"
								   && t.innerHTML.substring(0,5) != "Enhan"
								   && t.innerHTML.substring(0,5) != "Regul") {
			
		//set displayIsBlock variable
			cl = t.parentNode.getElementsByTagName('ul')[0]; //the first ul tag in the parentNode
			if (cl.style.display == 'none' || cl.style.display == ''){
				 displayIsBlock = false;
			} else if (cl.style.display == 'block'){
				 displayIsBlock = true;
			}
			
			// if the clicked link is a sub category...
			if (t.parentNode.parentNode.id != "podSubcatContainer") {
				var m = t.parentNode.parentNode;
				closeEverySubcat(t,m);	
				showHideCurrentClick(t);
			}
			
			// if the clicked link is a category...
			if (t.parentNode.parentNode.id == "podSubcatContainer") {
				closeEveryCat(t);				
				showHideCurrentClick(t);				
				//location.href = windowLoc+ "#" + t.getAttribute('name');		
			}
			
				window.scrollTo(0, 450)
			
				//showHideShowAllLink();
		
					
					return false;
		};
		
			if (t.nodeName.toLowerCase() === 'a' && t.className === "innerLink") {
	
				var th = getHref(t.getAttribute('href'));
				location.href = t.getAttribute('href');
					openLinked(th);
			}
		
		
	};
	
	function makeThumbnails(numImages)
	{
	
	document.write("\r<div id=\"thumbnailContainer1\">\r");
	
			for (i = 0; i<numImages;i++)
			{//write 1st thumbnailcontainer
			document.write("<a href =\"javascript:thumbs(" + i + ",true);\" id =\"thumb" + i + "\" class=\"thumbnailBox\" style=\"background:url(images/" + (i+1) + ".jpg) 50% 35% no-repeat\;\"></a>\r");
			}
			
	document.write("\r</div>");
	
	document.getElementById('thumb0').className = "thumbnailBox selected";
	}
		
	
	function getTarget(x){ // here's the magic, really simple stuff
		x = x || window.event;
		return x.target || x.srcElement;
	}
	// end event delegation
	
	
};


window.onload = setup;
