// addLoadEvent function
// Originally written by Simon Willison (http://simon.incutio.com)
// Allows the attachment of multiple events to the window.onload event

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// highlightPages function
// Originally written by Jeremy Keith (http://domscripting.com)
// This function takes the value of the last child of the current link (within the navigation), which is the link text, and applies it to the id attribute of the body element. It also applies a class of "here" to that link

function highlightPage() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("navigation")) return false;
  var nav = document.getElementById("navigation");
  var links = nav.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    var currenturl = window.location.href;
    if (currenturl.indexOf(linkurl) != -1) {
      links[i].className = "here";
      var linktext = links[i].lastChild.nodeValue.toLowerCase();
      document.body.setAttribute("id",linktext);
    }
  }
}

addLoadEvent(highlightPage);

// showTags function
// Written by Patrick Beeson (http://patrickbeeson.com)
/*This function builds the unordered list controling the display of the tabbed "tag" container. It hides and reveals the tags based on what list item is clicked. For instance, if you click"Top 10," it shows the list of the top-ten tags.*/

function showTags(){
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById("ten_tags")) return false;
	if (!document.getElementById("hundred_tags")) return false;
	if (!document.getElementById("cdt_tags")) return false;
	// Hide all tags but the top ten at load
	var ten = document.getElementById("ten_tags");
	ten.className = "show";	
	var hundred = document.getElementById("hundred_tags");
	hundred.className = "hide";
	var cdt = document.getElementById("cdt_tags");
	cdt.className = "hide"
	// Create an unordered list with tab options
	var optionlist = document.createElement("ul");
	optionlist.setAttribute("id","tag_options");
	// Ten tags list item
	var optionone = document.createElement("li");
	optionone.setAttribute("id","ten");
	optionone.className = "active"; //active status
	// Ten tags anchor with text
	var optiononelink = document.createElement("a");
	optiononelink.setAttribute("href","#ten_tags")
	var optiononetext = document.createTextNode("Hot 10");
	optiononelink.appendChild(optiononetext);
	// Append option to list
	optionone.appendChild(optiononelink);
	optionlist.appendChild(optionone);
	// Hundred tags list item
	var optiontwo = document.createElement("li");
	optiontwo.setAttribute("id","hundred");
	// Hundred tags anchor with text
	var optiontwolink = document.createElement("a");
	optiontwolink.setAttribute("href","#hundred_tags");
	var optiontwotext = document.createTextNode("Top 50");
	optiontwolink.appendChild(optiontwotext);
	// Append option to list
	optiontwo.appendChild(optiontwolink);
	optionlist.appendChild(optiontwo);
	// CDT tags list item
	var optionthree = document.createElement("li");
	optionthree.setAttribute("id","cdt");
	// CDT tags anchor with text
	var optionthreelink = document.createElement("a");
	optionthreelink.setAttribute("href","#cdt_tags");
	var optionthreetext = document.createTextNode("CDT Picks");
	optionthreelink.appendChild(optionthreetext);
	// Append option to list
	optionthree.appendChild(optionthreelink);
	optionlist.appendChild(optionthree);
	// Insert the unordered list to the document
	var tags = document.getElementById("tags");
	tags.insertBefore(optionlist,ten);

	// onClick actions for tags and tag options	
	optiontwo.onclick = function() {
		ten.className = "hide";
		hundred.className = "show";
		cdt.className = "hide";
		optionone.className = "inactive";
		optiontwo.className = "active";
		optionthree.className = "inactive";
		return false;
	}
	optionthree.onclick = function() {
		ten.className = "hide";
		hundred.className = "hide";
		cdt.className = "show";
		optionone.className = "inactive";
		optiontwo.className = "inactive";
		optionthree.className = "active";
		return false;
	} 
	optionone.onclick = function() {
		ten.className = "show";
		hundred.className = "hide";
		cdt.className = "hide";
		optionone.className = "active";
		optiontwo.className = "inactive";
		optionthree.className = "inactive";
		return false;
	}
}

addLoadEvent(showTags);
