/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  *
  * Title : 		Preview Your Links with Unobtrusive JavaScript
  * Author : 		Chris Campbell
  * URL : 		http://particletree.com/features/preview-your-links
  *
  * Description :	Inspired by the TargetAlert Firefox extension that “provides visual cues for
  *			the destinations of hyperlinks” and Christian Heilmann’s Image previews with 
  *			DOM JavaScript, we wrote a simple set of unobtrusive JavaScript functions to 
  *			find links on a page that go to amazon product pages, pdfs, word documents or
  *			whatever destination that might slow down the browsing process and adds an 
  *			icon next to them to let you know what you might expect to find behind a link.
  *
  * Created: 	7/24/2005
  * Modified:	7/27/2005
  * Modified:  2007/02/10 R L McNish - bug fix, case-insesitive, optimized, support for older browsers, more file types, email, http
  *
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

// global code executed at page load
addEvent(window, 'load', linkPreview);

/*
  * Summary:	Attaches an event to the object passed in
  *			Script written by Christian Heilmann at http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
  * Parameters: 	Object to attach event to | type of event to attach | function call
  * Return: 		Boolean indicating success or failure
  */
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	}
	else { 
		return false; 
	} 
}

/*
  * Summary: Called once at page load.
  * Grabs all non-image links from the page and adds an icon depending on link
  * special handling for amazon.com, mailto, http:// 
  */
function linkPreview(){
	var links = document.getElementsByTagName("a");
   var currentLink;
   var	images;
   var linkHref;
   var linkHrefParts;
   var extension;
   var ftypes = /avi|bmp|csv|dat|doc|mdb|mov|mp3|mpeg|mpg|pdf|pps|ppt|qt|rtf|txt|vsd|wmv|xls|zip/; // define the CSS-supported file types (file extensions) here, alphabetically
   var loc='left';
   var emloc='left';

	for (i=0; i<links.length; i++){
		currentLink = links[i];
		images = currentLink.getElementsByTagName("img");
		
	 	// Check if the link is an image. We don't want icons next to images.
		if (images.length == 0){
			linkHref = currentLink.href.toLowerCase(); // find the link's href string - case insensitive
			
			if (linkHref.match(/amazon\.com/)){ // Find all links directed to amazon.com
				append(currentLink, "amazon", loc);
			}
           else if (linkHref.match(/mailto/)){ // find all mailto links
           	append(currentLink, "em", emloc);
           }
			else{
				linkHrefParts = linkHref.split(".");	// embedded checkLinks(linkHref, currentLink);
	
				// extension is the last element in the LinkSplit array
				extension = linkHrefParts[linkHrefParts.length - 1];
	
				// In some browsers there is a "/" placed after the link. removes the "/"
				extension = extension.replace("/","");
	
               //The "in" operator returns true if the specified property is in the specified object.
               //Implemented in: JavaScript 1.4
               //Not supported in Internet Explorer 5.0 and below. 
               //if( extension in { doc:1, pdf:1, ppt:1, rtf:1, txt:1, xls:1, zip:1 } ) { // alphabetical order
               if (ftypes.test(extension)) {		// fix for older browsers 
                   append(currentLink, extension, loc );
				}
			}
		}
	}
}


/*
  * Summary:	Creates a span *before* (was after) the object passed in and sets the class
  *            of the span to the link type. The CSS file does the rest - adding the icon in the span
  *            and aligning it.
  * Parameters: 	<a> object | external link type
  */
function append(currentLink, extension, location){
	var span = document.createElement('span');
	span.innerHTML = "&nbsp;";
   if (location=='left') {
   	currentLink.parentNode.insertBefore(span,currentLink); // inserts span *before*
   }
   else {
		currentLink.parentNode.insertBefore(span,currentLink.nextSibling); // inserts span *after*
   }
	span.className = 'LinkPreview_'+extension; // span definition includes icon
}
