function IsSupported()
{
	if (document.getElementById
	&&  document.getElementsByTagName)
	{
		return true;
	}
	
	return false;
}

// Borrowed from http://www.scottandrew.com/weblog/articles/cbs-events
function AddEvent(obj, evType, fn, useCapture)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, useCapture);
    	return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
}

// Add an escape() function to the RegExp class.
// From http://simonwillison.net/2006/Jan/20/escape/
RegExp.escape = function(text)
{
	if (!arguments.callee.sRE)
	{
    	var specials = [
			'/', '.', '*', '+', '?', '|',
			'(', ')', '[', ']', '{', '}', '\\'
		];
    	arguments.callee.sRE = new RegExp(
      		'(\\' + specials.join('|\\') + ')', 'g'
    	);
  	}
	return text.replace(arguments.callee.sRE, '\\$1');
}

// Main utility class.
var AQbase = {
	
	// Domains to be considered part of the website
	localDomains: [ document.domain ],
	

	// DO NOT MODIFY anything below this line
	init: function()
	{
		AQbase.obfuscateEmailLinks();
		//AQbase.addLinkIcons();
	},
	
	addLinkIcons: function()
	{
		var anchors = document.getElementsByTagName('a');
		
        for (var i = 0; i < anchors.length; i++)
        {
        	var fileLink = anchors[i];
        	
        	// If the <a> element doesn't have a link, ignore it.
        	if (!fileLink.href)
        	{
        		continue;
        	}
        	
        	// Check for matching filenames.
            if (fileLink.href.match(/\.pdf$/))
            {
                fileLink.className += ' pdf';
            }
            else if (fileLink.href.match(/\.doc$/))
            {
                fileLink.className += ' doc';
            }
            else if (fileLink.href.match(/\.(wmv|wma|wvx|wax|asf|asx|wms|wmz|wmd)$/))
            {
                fileLink.className += ' wmv';
            }
            else if (fileLink.href.match(/\.(mov|qt)$/))
			{
                fileLink.className += ' mov';
            }
            // This must be the last "else if" clause to be the default.
            else if (fileLink.href.match(/^http(s?):\/\//))
            {
            	var external = true;
				for (var j = 0; j < AQbase.localDomains.length; j++)
				{
					var regex = new RegExp('http(s?):\/\/' + RegExp.escape(AQbase.localDomains[j]) + '(/.*)?', 'i');
					if (regex.test(fileLink.href))
					{
						external = false;
					}
				}
				
				if (external)
				{
					fileLink.className += ' website';
				}
			}
        }
	},
	
	obfuscateEmailLinks: function()
	{
		var anchors = document.getElementsByTagName('a');
		for (var i = 0; i < anchors.length; i++)
		{
			if (anchors[i].className == 'email-link')
			{
				// Extract the text.
				var address = anchors[i].firstChild.nodeValue;
				
				// Convert to standard email address.
				address = address.replace(/\s+\[?at\]?\s+/ig, '@');
				address = address.replace(/\s+\[?d\-?o\-?t\]?\s+/ig, '.');
				
				// Replace the node with a new one.
				// (A new node has to be created to work around a Safari href+mailto bug.)
				var link = document.createElement('a');
				link.setAttribute('href', 'mailto:' + address);
				link.appendChild(document.createTextNode(address));
				if (document.replaceNode) // IE5 doesn't support replaceChild, so use replaceNode instead
				{
					anchors[i].replaceNode(link);
				}
				else
				{
					anchors[i].parentNode.replaceChild(link, anchors[i]);
				}
			}
		}
	}
}

// Load the utility.
if (IsSupported())
{
	AddEvent(window, 'load', AQbase.init, false);
}
