(function( $ ) {
	var methods = {
		init : function( options ) { 
			return this.each(function() {
				var $this = $(this);
				var settings = $.extend( {
					'dataUrl' : '/cms/news-alerts.html'
				}, options);
				
				$this.children(".email-button").click(function(){
					if(validate($this.children(".email").val())){
						$.get(settings.dataUrl, { 
						Email: escape($this.children(".email").val()),
						Url: escape(window.location.host + window.location.pathname), 
						NT: String(Math.random()).substr(2,10) },
							function(data){
								if(parseInt( data ) < 1){
									$this.children("input").hide();
									$this.children("p.thanks").show();
								}
						});
					}
					else{
						alert("Please use a valid email.");
					}
				});
			});
		},
		destroy : function( ) {
			return this.each(function() {
				$(window).unbind('.newsAlerts');
			});
		}
	};
	$.fn.newsAlerts = function( method ) {
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.newsAlerts' );
		}    
	};
	function validate(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   if(reg.test(email) == false) {
		  return false;
	   }
	   else{
	   		return true;
	   }
	}
})( jQuery );
