$j(function(){
	
	// get page elements
	var contentAreaHead = $j('.sra-head');
	var totalAreaHeads = contentAreaHead.length;
	var contentAreaBody = $j('.sra-contents');
	var theContainer = $j('#search-results-lnav')[0];
	
	// create local nav
	var topImg = document.createElement('img');
	var btmImg = document.createElement('img');
	topImg.src = "../assets/images/search/nav_landing_top.gif";
	btmImg.src = "../assets/images/search/nav_landing_bottom.gif";
	topImg.className = "display-block";
	btmImg.className = "display-block";
	topImg.alt = "";
	btmImg.alt = "";
	// for pages that don't have the local nav
	if(document.getElementById("search-results-lnav")) { 
		theContainer.appendChild(topImg);
		var theList = document.createElement('ul');
		var titleItem = document.createElement('li');
		titleItem.className="header";
		theContainer.appendChild(theList);
		theList.appendChild(titleItem).appendChild(document.createTextNode('Search Results Categories'));
		
		// create show all categories link
		var catItem = document.createElement('li');
		var catLink = document.createElement('a');
		catLink.href = "#"
		catLink.className = "viewall";
		catLink.onclick = showAll;
		var catText = document.createTextNode("View All");
		theList.appendChild(catItem).appendChild(catLink).appendChild(catText);
	
		for(var i = 0; i < totalAreaHeads; i++) {
		
			var theItem = document.createElement('li');
			var theLink = document.createElement('a');
			
			// add onclick evenet to all new links
			theLink.onclick = limitResult;
			theLink.href = "#";
			theLink.id = i;
			
			// used to link together the  new local nav links and the content areas
			contentAreaHead[i].id = i + "-h";
			contentAreaBody[i].id = i + "-b";
			
			var sectionTitle = $j('.sra-title',contentAreaHead[i]);
			var numPages = $j('.num-pages',contentAreaHead[i]);
			var numText = numPages[0].firstChild.nodeValue;
			var numArray = numText.split(" ");
			var numPageText = document.createTextNode(" (" + numArray[2] + ")");
			var theImage = sectionTitle[0].getElementsByTagName('img')[0];
			var linkText = document.createTextNode(theImage.getAttribute('alt'));
			
			theList.appendChild(theItem).appendChild(theLink).appendChild(linkText);
			theItem.appendChild(numPageText);
		}
	
		// append the bottom image to the list area
		theContainer.appendChild(btmImg);
	}
	
	function limitResult() {
	
		var currentContent = this.getAttribute('id');
		
		
		for (var x = 0; x < totalAreaHeads; x++) {
			
			var areaHeadId = contentAreaHead[x].id;
			var idArray = areaHeadId.split("-");
			
			if(idArray[0] == currentContent) {
				$j(contentAreaHead[x]).slideDown("slow");
				$j(contentAreaBody[x]).slideDown("slow");
			}
			else {
				
				$j(contentAreaHead[x]).hide();
				$j(contentAreaBody[x]).hide();
			}
		}
		
		return false;
	
	}
	
	function showAll() {
	
		for (var x = 0; x < totalAreaHeads; x++) {
			
			$j(contentAreaHead[x]).slideDown("slow");
			$j(contentAreaBody[x]).slideDown("slow");
		}
		
		return false;
	}
	
});
