// The following utility function is used to check whether a 
// value exists in the particular array.
Array.prototype.href_in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i]['link'].href == p_val) {
			return true;
		}
	}
	return false;
}

/* #### linkGrabber() ####
 * -Author-
 * Will Rosenberg (Digital Surgeons) 2008
 * -Description-
 * This function will run through the document.links array, add each unique link to a 
 * seperate array, write the new array of links to a hidden div, and add a hide/show 
 * button. The purpose of this functionality is to get a list of all the links on the 
 * page so that they may be opened all at once on Firefox using the Snap Links add-on.
 * -Versions-
 * 1.0 - Release
 */
function linkGrabber() {
	// Declare variables
	var i = 0;
	var count = 0;
	var allLinks = new Array();
	var allTotal = 0;
	var uniqueLinks = new Array();
	var uniqueTotal = 0;
	var uniqueHTML = "";
	
	// Get link information
	allLinks = document.links;
	allTotal = document.links.length;
	
	// Put all unique links into uniqueLinks array
	// count is used to properly add items to the array, so that it can be iterated in the future.
	for (i=0; i<allTotal; i++) {
		// If the href of the current allLinks items is not in the array
		//  And if the link does not go to the blog
		//  And if the link does not go to the glossary
		if (!uniqueLinks.href_in_array(allLinks[i].href) && 
			allLinks[i].href.indexOf("/blog/") == -1 &&
			allLinks[i].href.indexOf("?term=") == -1) {
			uniqueLinks[count] = new Array();                   // Instantiate a new subarray
			uniqueLinks[count]['link'] = allLinks[i];           // Store the actual link in the array.
			uniqueLinks[count]['text'] = allLinks[i].innerHTML; // And store the text (innerHTML) of the link.
			count++;
		}
	}
	
	// Create the new HTML elements for the links.
	linkList = document.createElement('ul');          // ul containing li's
	linkListItem = document.createElement('li');      // li containing link (a)
	linkListItem.innerHTML = "<b style=\"color:#000\">Unique Non-Blog, Non-Glossary Links</b>"; // content of that link
	linkList.appendChild(linkListItem);               // add it to the ul



	uniqueTotal = uniqueLinks.length;
	for (i=0; i<uniqueTotal; i++) {
		linkListItem = document.createElement('li'); // li containing link (a)
		listItemHTML = "<a href=\"" + uniqueLinks[i]['link'].href + "\">" + uniqueLinks[i]['text'] + "</a>";
		linkListItem.innerHTML = listItemHTML;       // content of that link
		linkList.appendChild(linkListItem);          // add it to the ul
	}
	
	linkSpan = document.createElement('span');       // Create span to contain hide/show link.
	linkSpanLink = document.createElement('a');      // Create hide/show link.
	
	linkSpanLink.setAttribute('id', 'linkAnchor');
	linkSpanLink.setAttribute('href', 'javascript:hideshowlinkdiv()');
	linkSpanLinkHTML = "Show Unique Links";
	linkSpanLink.innerHTML = linkSpanLinkHTML;
	linkSpan.appendChild(linkSpanLink);
	
	// Write the three main elements to the document.
	document.write(unescape("%3Cp id=\"linkPara\">%3C/p>"));
	document.write(unescape("%3Cimg id=\"linkImg\" src=\"linkgrabbg.gif\" style=\"display:none;visibility:hidden;\">%3C/div>"));
	document.write(unescape("%3Cdiv id=\"linkDiv\" style=\"display:none;visibility:hidden;\">%3C/div>"));
	linkDiv = document.getElementById('linkDiv');
	linkPara = document.getElementById('linkPara');
	linkImg = document.getElementById('linkImg');
	
	// Add the objects we created to these elements.
	linkDiv.appendChild(linkList);
	linkPara.appendChild(linkSpan);
	
	// Then dynamically resize the bg image.
	divWidth = linkDiv.offsetWidth;
	divHeight = linkDiv.offsetHeight;
	linkImg.setAttribute('width', divWidth);
	linkImg.setAttribute('height', divHeight);
}

// The following function works with the previous function to hide or show the link div.
function hideshowlinkdiv() {
	theImg = document.getElementById('linkImg');
	theDiv = document.getElementById('linkDiv');
	theAnchor = document.getElementById('linkAnchor');
	if (theDiv.style.visibility == "visible") {
		theDiv.style.visibility = "hidden";
		theDiv.style.display = "none";
		theImg.style.visibility = "hidden";
		theImg.style.display = "none";
		theAnchor.innerHTML = "Show Unique Links";
	} else {
		theDiv.style.visibility = "visible";
		theDiv.style.display = "block";
		theImg.style.visibility = "visible";
		theImg.style.display = "block";
		theAnchor.innerHTML = "Hide Unique Links";

		/*/ If DIV expands off the page, move it's left boundary.
		winW = getWindowWidth();
		if (winW < 920 && winW >= 450) {
			// Adjust so right edge stays on the page.
			theDiv.style.left = (winW-460);
			theImg.style.left = (winW-460);
		} else if (winW < 450){
			// Adjust so left edge stays on the page, regardless of whether the right side is off the page.
			theDiv.style.left = -10; // includes 10px margin
			theImg.style.left = -10;			
		} else {
			// Place in its normal position.
			theDiv.style.left = 460; //920-460
			theImg.style.left = 460;
		}*/
	}
}