// Contains all Javascript to be executed onDOMready and/or onload
$(document).ready(function() {

	/*
	XPath to grab all <a> tags with the rel attribute.
	Add anymore processing code for <a> tags with rel attribute inside this each() loop
	*/
	$('/a[@rel]').each(function() {

		/*
		Converts all <a> tags with rel="external" to class="offsite" and removes rel so as to be processed by code below for handling Open in New Window links
		DEPRECATED in favor of class="offsite" to provide the option of visual styling for special links (see below)
		This is kept around for backwards compatibility
		*/
		if($(this).attr('rel') == "external") {
			// if(!$(this).attr('title') || $(this).attr('title').length == 0) $(this).attr('title', 'This link opens in a new window.');
			// $(this).click(function() {
				// window.open($(this).attr('href'));
				// return false;
			// });
			thisClass = $(this).attr('className');
			$(this).attr({
				rel: null,
				className: thisClass + ' offsite'
			})
		}

		/*
		Converts all <a> tags with rel="email" into valid, clickable mailto links. Used for screwing spam spiders
		HTML should be written as <a rel="email">username-at-domain-dot-com</a> which will default to is JS is unavailable
		*/

		// Placeholders for obfuscated email addresses
		var atPattern = '-at-';
		var dotPattern = '-dot-';
		
		// Enter root relative path to email form for thickbox popup
		var path_to_email_form = null;
		
		if($(this).attr('rel') == 'email') {
			var email = $(this).html();
			email = email.replace(new RegExp(atPattern), '@').replace(new RegExp(dotPattern), '.');
			$(this).html(email);
			$(this).attr({
				href: 'mailto:' + email
			});
			$(this).removeAttr('rel');
			
			
			// Further rewrites mailto if using thickbox popup email form.
			if(path_to_email_form) {
				$(this).attr({
					className: 'thickbox',
					href: path_to_email_form + encodeURI('?email_to=' + email + '&TB_iframe=true&height=350&width=500')
				});
			}
		}
		
	}); //end each()
	
	/*
	Converts all <a> tags with class="offsite" to window.open() calls to open the URL in a new window since "target" attr is deprecated
	Makes link still easy to copy/paste for users through right-clicking
	*/
	$('a.offsite').each(function() {
		if(!$(this).attr('title') || $(this).attr('title').length == 0) $(this).attr('title', 'This link opens in a new window.');
		$(this).click(function() {
			window.open($(this).attr('href'));
			return false;
		});
	}); //end each()
	
});
