/**
 * Sitewide scripts
 */
var Emails = {
	/* Main setup method */
	setup : function () {
		/* Locate all of the mail candidates */
		var elems = $('.insert-email');
		
		/* Loop through the elements */
		$.each(elems, function (idx, elem) {
			/* Tear apart the dom */
			var dom = $(elem),
				dom_usr = dom.children('.usr').text(),
				dom_dmn = dom.children('.dom').text(),
				dom_tld = dom.children('.tld').text(),
				eml = dom_usr + '@' + dom_dmn + '.' + dom_tld,
				url_text = eml;
			
			/* Create the url to place into the elem */
			if (dom.hasClass('insert-email-plain')) {
				url_text = 'Email';
			}
			var a_tag = '<a href="mailto:'+eml+'?subject=Contact from structuredhealth.com">'+url_text+'</a>';
			$(elem)
				.removeClass('insert-email')
				.html(a_tag);
		});
	}
};


/**
 * URL setup object
 */
var URLs = {
	/* Setup method for the page urls */
	setup : function () {
		/* Get any a tags in the main wrapper */
		var a = $('div#main-wrapper').find('a'),
			regex1 = /^http:\/\/www.structuredhealth.com/,
			regex2 = /^http:\/\/structuredhealth.com/,
			regex3 = /^\//,
			regex4 = /^http:\/\/s97643.gridserver.com/,
			regex5 = /(pdf|doc|docx|ppt|pptx|xls|xlsx)$/,
			regex6 = /^(javascript:|#)/;
		$.each(a, function (idx, elem) {
			var a_item = $(elem),
				href = a_item.attr('href'),
				test1 = regex1.test(href),
				test2 = regex2.test(href),
				test3 = regex3.test(href),
				test4 = regex4.test(href),
				test5 = regex5.test(href),
				test6 = regex6.test(href);
			if ((!test1 && !test2 && !test3 && !test4 && !test6) || test5) {
				a_item.attr('target', '_blank');
			}
		});
	}
};


/**
 * Object to set up fancybox
 */
var Images = {
	/* Sets up the fancybox stuff */
	setup : function () {
		/* Find the urls and candidate images */
		var imgs = $('a.do-fancybox');
		imgs.fancybox();
	}
};


/* Hook form load */
$(function () {
	/* Call up the setup method on the main object */
	Emails.setup();
	Images.setup();
	URLs.setup();
});

