/*
 * Fix IE background image flicker (via http://www.mister-pixel.com/)
 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

/*
 * methods to run on DOM ready
 */
$(document).ready(function() {
	
	if ($("h3#enterLink").length > 0) {
		$("h3#enterLink").bind("click", function() {
			document.location.href = '/neo-pangea.php?#';
		});
	}
	
	// add sIFR support for news archive, news article, and mailing list pages
	if (($("body.news-archive") || $("body.news-article") || $("body.mailing-list")) && (typeof sIFR == "function"))
	{
		//sIFR();
	}
	
	// hover functions for image links
	/*
	$("body.landing h3 a").hover(function() {
		$(this).parent().css("background-position", "50% 100%");
	}, function() {
		$(this).parent().css("background-position", "50% 0");
	});
	*/
	$("p.return > a").hover(function() {
		$(this).parent().css("background-position", "0 100%");
	}, function() {
		$(this).parent().css("background-position", "0 0");
	});
	
	$("p.learn-more > a").hover(function() {
		$(this).parent().css("background-position", "0 100%");
	}, function() {
		$(this).parent().css("background-position", "0 0");
	});
	
	$("dd#email a").hover(function() {
		$(this).parent().parent().css("background-position", "0 100%");
	}, function() {
		$(this).parent().parent().css("background-position", "0 0");
	});
	
	$("form#subscription button").hover(function() {
		$(this).css("background-position", "0 100%");
	}, function() {
		$(this).css("background-position", "0 0");
	});
	
	$("form#login button").hover(function() {
		$(this).css("background-position", "0 100%");
	}, function() {
		$(this).css("background-position", "0 0");
	});
	
	// override IE google toolbar changing field color to yellow
	$("ol#subscription-fields input").bind("propertychange", function(e) {
		$(this).css("background-color", "transparent");
	});
	
	// mailing list functionality
	$("form#subscription").submit(function() {
		// Test for a valid e-mail address
		var validEmail = checkEmail($("input#emailaddress").val());
		if (!validEmail) {
			// Remove previously show erros
			$("div#messages").remove();
			// Show new error
			$("ol#subscription-fields").before("<div id=\"messages\"><h4>Please enter a valid e-mail address.</h4></div>");
			
			return false;
		}
		else
		{
			return true;
		}
	});
	
	// check email-address validity
	var checkEmail = function(email)
	{
		var test = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
		return (!test) ? false : true;
	};
	
	// detach all handlers
	$(window).unload(function() {
		$("body.landing h3 a").unbind();
		$("p.return > a").unbind();
		$("p.learn-more > a").unbind();
		$("dd#email a").unbind();
		$("form#subscription button").unbind();
		$("form#login button").unbind();
		$("ol#subscription-form input").unbind();
		$("form#subscription").unbind();
	});
});