// 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 window.open() calls to open the URL in a new window since "target" attr is deprecated
		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;
			});
		}

		/*
		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
		*/
		if($(this).attr('rel') == 'email') {
			var email = $(this).html();
			email = email.replace(/-at-/, '@').replace(/-dot-/, '.');
			$(this).html(email);
			$(this).attr({
				href: 'mailto:' + email
			});
			$(this).removeAttr('rel');
			
			// Enter root relative path to email form for thickbox popup
			var path_to_email_form = '/email_form.php';
			
			// 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
	*/
	$('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()
	
});
