/*
 * File: links.js
 *
 * Copyright (c) 2008, Paulo Avila (apaulodesign.com)
 *
 * Description: This script checks the content of the "href" attribute of
 * every "a" tag inside the content of the document. Depending on its value,
 * the "rel" attribute is set so that the style sheet places an image next
 * to the link to label the type of document it links to.
 *
 * Modification Log:
 *	2005.05.23	Paulo	- Initial setup of functions
 *	2008.08.09	Paulo	- Added logic to exclude links that contains an image
 *	2008.30.09	Paulo	- Accounts for an onclick attribute having 'window.open'
 *	          	     	  and an absolute link (with 'http') to internal site
 */

function links()
{
	var i = 0;
	var extension = "";
	var hrefText = "";
	var onclickText = "";
	var contentLinks = "";		// an array of all the 'a' tags inside the 'content' of the document

//$('a[href^="http://"]').attr({ target: "_blank" });

	// if the browser supports the DOM
	if ( document.getElementById && document.getElementsByTagName )
	{
		contentLinks = document.getElementById("content").getElementsByTagName('a');

		for (i = 0; i < contentLinks.length; i++)
		{
			// if the link has a href attribute
			if ((hrefText = contentLinks[i].getAttribute('href')) && hrefText.length > 0)
			{
				onclickText = "";
				if (contentLinks[i].getAttribute('onclick'))
					onclickText = contentLinks[i].getAttribute('onclick');

				// if the link isn't for an image
				if (contentLinks[i].getElementsByTagName('img')[0] == null)
				{
					extension = getExtension(hrefText).toLowerCase();

					if (contentLinks[i].target == "_blank" || onclickText.indexOf("window.open") >= 0)
						contentLinks[i].rel = "new";

					else if (hrefText.substr(0, 4) == "http" && hrefText.indexOf("apaulodesign") < 0)
						contentLinks[i].rel = "external";

					else if (hrefText.charAt(0) == "#" || hrefText.indexOf(".php#") != -1)
						contentLinks[i].rel = "anchor";

					else if (hrefText.substr(0, 7) == "mailto:")
						contentLinks[i].rel = "email";

					else if (extension == ".pdf")
						contentLinks[i].rel = "pdf";

					else if (extension == ".jpg" || extension == ".gif" || extension == ".png" || extension == ".bmp")
						contentLinks[i].rel = "image";

					else if (extension == ".doc")
						contentLinks[i].rel = "doc";

					else if (extension == ".xls")
						contentLinks[i].rel = "xls";

					else if (extension == ".ppt")
						contentLinks[i].rel = "ppt";
				}

				// manual override allowed
				//if (contentLinks[i].getAttribute('class') == "no-decoration")
				//	contentLinks[i].rel = "";
			}
		}
	}
}


function getExtension(string)
{
	var ext = null;
	var theLength = string.length;
	var i = theLength - 1;

	for ( i ; i > 0; i--)
		if (string.charAt(i) == '.')
			break;

	ext = string.substr(i, theLength-i);

	return ext;
}