/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  *
  * 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
  *
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/


//code enters here
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:	Grabs all non-image links from the page and calls checkLinks() if amazon.com is not located in the link 
  */
function linkPreview(){
	var links = document.getElementsByTagName("a");

	for (i=0; i<links.length; i++){
		var currentLink = links[i];
		if (currentLink.getAttribute('href')) {
			var linkHref = currentLink.href;
			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 
  */
function checkLinks(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 } ){
		currentLink.className = extension;
		if (extension == 'pdf') {
				currentLink.onclick = function() {
					return getPDF(currentLink.href);		
				}
		}
	}
}

function getPDF(url) {
  window.open(url,'documento','status=yes,scrollbars=yes,resizable=yes,width=500,height=400');
	return false;
}

function showPDF(theURL) {
  window.open(theURL,'documento','status=yes,scrollbars=yes,resizable=yes,width=500,height=400');
}


