jQuery(function($) {
	$.Newsletter = {
		$unsubscribe: $('#legal #nlUnsubscribe'),
		$copy: $('#nlcopy'),
		$submit: $('#nlSubmit'),
		$content: $('#nlContent'),
		$emailField: $('#emailAddress'),
		action: "add",
		msgUnsubscribe: Config.Msgs.NEWSLETTER_TO_UNSUBSCRIBE,
		msgnvalid: Config.Msgs.NEWSLETTER_EMAIL_INVALID,

		init: function() {
			this.$unsubscribe.click({self:this}, this.onClick);
			this.$submit.click({self:this}, this.doSubmit);
			this.$emailField.focus({self:this}, this.onFocus);
			this.$emailField.blur({self:this}, this.onBlur);
			this.$emailField.val(Config.Msgs.NEWSLETTER_EMAILFIELD_DEFAULT);
		},

		onBlur: function(event) {
			var self = event.data.self;
			var $email = self.$emailField;
			if ($email.val() == "") {
				$email.val(Config.Msgs.NEWSLETTER_EMAILFIELD_DEFAULT);
			}
		},

		onFocus: function(event) {
			var self = event.data.self;
			var $email = self.$emailField;
			if ($email.val() == Config.Msgs.NEWSLETTER_EMAILFIELD_DEFAULT) {
				$email.val("");
			} else {
				$email.select();
			}
		},

		onClick: function(event) {
			var self = event.data.self;
			self.updateCopy(self.msgUnsubscribe);
			self.$content.fadeOut("slow", function() {
				$(this).parent().addClass('unsubscribe');
				$(this).fadeIn("slow");
				self.action = "remove";
			});
			event.preventDefault();
		},
		updateCopy: function (msg) {
			this.$copy.hide("slow", function() {
				$(this).text(msg).show("slow");
			});
		},

		doSubmit: function(event) {
			var self = event.data.self;
			if (!self.validateEmail())
				return false;
			switch(self.action) {
				case "remove":
					self.doQuery(self);
					break;
				case "add":
				default:
					self.doQuery(self);
					break;
			}
		},

		doQuery: function(context) {
			var path = Config.Paths.apiURL + 'newsletter/' + this.action + '/' + this.$emailField.val();
			$.ajax({
				type: 'GET',
				context: context,
				url: path,
				contentType: "application/json; charset=utf-8",
				cache: false,
				success: this.onSuccess,
				error: this.onError
			});
		},

		onSuccess: function(msg, status, xhr) {
			var message = jQuery.parseJSON(msg).response;
			switch(message) {
				case "subscribed":
					this.updateCopy(Config.Msgs.NEWSLETTER_ALREADY_SUBSCRIBED);
					break;
				case "exists":
					this.updateCopy(Config.Msgs.NEWSLETTER_RE_SUBSCRIBED);
					break;
				case "added":
					this.updateCopy(Config.Msgs.NEWSLETTER_CONFIRMED);
					break;
				case "removed":
					this.updateCopy(Config.Msgs.NEWSLETTER_REMOVED);
					break;
			}
		},

		onError: function(xhr, status, error) {
			if( error === undefined ) {
				var message = jQuery.parseJSON(xhr.responseText).error.message;
				this.updateCopy(message);
			}
		},

		validateEmail: function() {
			var result = false;
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var email = this.$emailField.val();
			if (!reg.test(email)) {
				this.updateCopy(this.msgnvalid);
			} else {
				result = true;
			}
			return result;
		}
	};
	
	$.Newsletter.init();
});

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};
