/**
 * globalSubscribe.js
 *
 * triggers the overlay and subscribe to mailing list modal pop-up
 */

/* use this function wrapper so we can use jQuery on the few ProtoType pages
	left on the truth site. It's like noconflict with $ */
(function($) { 	$(document).ready(function() {
	$('#globalSubscribe').wrap('<div id="globalSubscribe_overlay">').show();

	var globalSubscribe_set_position = function() {
		var d = $(document);
		var w = $(window);
		var o = $('#globalSubscribe_overlay');
		var p = $('#globalSubscribe');
		var new_y = (w.height() - 200) / 2;
		var new_x = (w.width() - 400) / 2;

		p.css({
			'top' 	: new_y
		,	'left' 	: new_x
		});
		o.height(d.height()).width(w.width());
	};
	$(window).resize(globalSubscribe_set_position);		

	$('#globalSubscribe_close').click(function() {
		$('#globalSubscribe_overlay').fadeOut('fast', function() {
			// reset the form!
			$('#globalSubscribe_thanks, .globalSubscribe_error').hide();
			$('#globalSubscribe_name, #globalSubscribe_email').val('');
			$('#globalSubscribe_body').show();
		});
	});

	$('#globalSubscribe_name').keyup(function() {
		$('#globalSubscribe_name_error').hide();
	});
	$('#globalSubscribe_email').keyup(function() {
		$('#globalSubscribe_email_error').hide();
	});
	
	$('#globalSubscribe_submit').click(function() {
		var name = $('#globalSubscribe_name').val();
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
		var email = $('#globalSubscribe_email').val();
		var errors = 0;
		if (!email.match(re)) {
			$('#globalSubscribe_email_error').show();
			errors = 1;
		}
		if (name.length == 0) {
			$('#globalSubscribe_name_error').show();
			errors = 1;
		}
		if (errors == 0) {		
			$.post('/global/functions/_globalSubscribe.cfm', { email : email, name : name });
			$('#globalSubscribe_body').hide();
			$('#globalSubscribe_thanks').show();
		}
		return false;
	});
});	})(jQuery)

function globalSubscribe() {(function($) {
	$(window).trigger('resize');// trigger globalSubscribe_set_position
	$('#globalSubscribe_overlay').fadeIn('fast');
})(jQuery)}
