/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 *
 * Title : 		Preview Your Links with Unobtrusive JavaScript
 * Original Author : 	Chris Campbell
 * URL : 		http://particletree.com/features/preview-your-links
 *
 * DK 12/16/2005: modified to use object literal notation, use prototype's 
 * event listener, and remove amazon link icons. 
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

var LinkPreview = {
	/*
	 * Summary:	Grabs all non-image links from the page and calls checkLinks() 
	 */
	show: function(){
		var links = document.getElementsByTagName("a");

		for (i=0; i<links.length; i++){
			var currentLink = links[i];
			var	images = currentLink.getElementsByTagName("img");

			// Check if the link is an image. We don't want icons next to images.
			if (images.length == 0){
				var linkHref = currentLink.href;

				this.checkLinks(linkHref, currentLink)
			}
		}
	},

	/*
	 * Summary:	Checks if the link goes to an external file (ie. .doc, .pdf) and calls append()
	 * Parameters: 	The href of the link | the <a> object 
	 */
	checkLinks: function(linkHref, currentLink) {
		var linkHrefParts = linkHref.split(".");

		// extension is the last element in the LinkSplit array
		var extension = linkHrefParts[linkHrefParts.length - 1];

		// In some browsers there is a "/" placed after the link. removes the "/"
		extension = extension.replace("/","");

		if( extension in { doc:1, pdf:1, ppt:1, txt:1, xls:1, zip:1 } ){
			this.append(currentLink, extension );
		}
	},

	/*
	 * Summary:	Creates a span after the object passed in and sets the class to the link type
	 * Parameters: 	<a> object | external link type
	 */
	append: function(currentLink, extension){
		var span = document.createElement('span');
		span.innerHTML = "&nbsp;";
		currentLink.parentNode.insertBefore(span,currentLink.nextSibling);
		span.className = extension;
	}
}

Event.observe(window, "load", LinkPreview.show.bind(LinkPreview), false);