// DOM LOAD - ANYTHING THAT IS DEPENDENT ON DOM AND MUST WAIT FOR THE PAGE TO LOAD GOES HERE
$(document).ready(function() {
	// INIT EXTERNAL LINKS
	externalLinks();
	
	$('#slideshow').cycle({
		timeout: 10000
	});
	$('#slideshowSubPage').cycle({
		timeout: 4000
	});
	
	
	/* 	TODO: OPTIONAL: make event titles appear when hovering over day in calendar.
			- Currently this doesn't work when the user clicks on the next/prev arrows.
	
	$('td').hover(function(){
		$(this).children('ul').show();
		console.log('test');
	},
	function(){
		$(this).children('ul').hide();
		console.log('test');
	}
	);
	*/
	
	// Fire Shadowbox
	Shadowbox.init({
		overlayOpacity		: 0.6
	});
	
	// OPTIONAL: Check if we're on the dev or staging site and add the appropriate notification
	var location = (window.location.hostname);
	switch(location) {
		case "staging.carislifesciences.com":
			var whatserver = "staging";
			var color1 = "#EEA98A";
			var color2 = "#EF5940";
			break;
		case "carislifesciences.2edev.com":
			var whatserver = "development";
			var color1 = "#75B7E5";
			var color2 = "#173959";
			break;
		case "localhost":
			var whatserver = "local";
			var color1 = "#7FB860";
			var color2 = "#59631E";
			break;
		default:
			var whatserver = "";
	}

	if(whatserver == "staging" || whatserver == "development" || whatserver == "local"){
		$("body").prepend(function(){
			return notify(whatserver);
		});
	}
	function notify(whatserver){
		var notification = '<div style="width:100%; font-size:13px; background-color:' + color1 + '; border-bottom:1px solid ' + color2 + '; color:#fff; padding-top:8px; padding-bottom:8px;" id="notification"><span style="display:block;">You are currently viewing the <strong>' + whatserver + '</strong> site.</span></div>';
		return notification;
	}
	
});


// EXTERNAL LINKS - LOAD EXTERNAL LINKS IN A NEW WINDOW
function externalLinks() {
	 $('a[rel*="external"]').attr('target','_blank');
}

// LOAD ADDITIONAL JS LIBRARIES
/* FIXME: Is this needed? 
function $import(src){
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',src);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
*/
// FORM VALIDATION
var regexpZip = /^[0-9]{5}$/;
function checkLocatorForm()	{
	if (document.getElementById("postalCode").value.match(regexpZip)) {
		return true;
	} else {
		window.alert("Please enter a valid 5-digit Postal Code.");
		
		/* FIXME: properly position this error message and remove the alert above. 
		$('body').append('<div class="PostalCodeError"><div>Please enter a valid 5-digit Postal Code.</div></div>');
		$('.PostalCodeError').offset({
			top		: 10,
			left	: 0
		});
		$('.PostalCodeError').fadeIn('fast');
		$('html').click(function(){
			$('.PostalCodeError').hide();
		});
		*/
		
		document.getElementById("postalCode").focus();
		return false;
	}
}

