/**
 * LaPrème Newsletter
 *
 * @copyright LaPrème Nijmegen
 * @author Leon van der Veen
 */
var lpNewsletter = new lpClass({
	/**
	 * Options
	 */
	'defaultOptions': {
		'windowOptions': {},
		'subscribeUrl': null,
		'unsubscribeUrl': null,
		'hashUrl': null,
		'codes': {
			'SUBSCRIBE_OK': 0,
			'UNSUBSCRIBE_OK': 1,
			'ERROR': 2,
			'TIMEOUT': 3,
			'HASH': 4,
			'INVALID_EMAIL': 5
		},
		'hash': null,
		'subscribeButtonText': 'Subscribe',
		'unsubscribeButtonText': 'Unsubscribe',
		'subscribeLabelText': 'Email',
		'unsubscribeLabelText': 'Email',
		'messageAreaClass': 'message',
		'bodyClass': 'body'
	},
	
	/**
	 * Init
	 *
	 * @param bool Unsubscribe (false = subscribe; true = unsubscribe)
	 * @param object Options
	 */
	'initialize': function(unsubscribe, options)
	{
		// Set options
		this.setOptions(options);

		// Make window
		this.win = new lpWindow(this.options.windowOptions);
		
		// Hash
		this.hash = this.options.hash;
		if (!this.hash)
		{
			this.getHash();
		}
		
		// Message area
		this.messageArea = jQuery('<div></div>')
			.addClass(this.options.messageAreaClass)
			.hide();
		
		// Subscribe of unsubscribe?
		if (unsubscribe)
		{
			this.unsubscribeBody();
		}
		else
		{
			this.subscribeBody();
		}
	},
	
	/**
	 * Show
	 */
	'show': function()
	{
		this.win.show();
	},
	
	/**
	 * Hide
	 */
	'hide': function()
	{
		this.win.hide();
	},
	
	/**
	 * Get hash
	 * 
	 * @param function On complete
	 */
	'getHash': function(onComplete)
	{
		jQuery.ajax(
			this.options.hashUrl,
			{
				'dataType': 'json',
				'success': jQuery.proxy(function(data)
				{
					if (data && data.hash && data.code == this.options.codes.HASH)
					{
						this.hash = data.hash;
						
						if (onComplete)
						{
							onComplete();
						}
					}
				}, this)
			}
		);
	},
	
	/**
	 * Set message
	 * 
	 * @param string Message
	 */
	'setMessage': function(message)
	{
		if (!message)
		{
			this.hideMessage();
		}
		else
		{
			this.messageArea
				.text(message)
				.show();
		}
	},
	
	/**
	 * Hide message
	 */
	'hideMessage': function()
	{
		this.messageArea
			.hide();
	},
	
	/**
	 * Subscribe body
	 */
	'subscribeBody': function()
	{
		// Field
		var fieldID = 'email_' + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
		var field = jQuery('<input type="text" />')
			.attr('id', fieldID);
		
		// Label
		var label = jQuery('<label></label>')
			.text(this.options.subscribeLabelText)
			.attr('for', fieldID);
		
		// Button
		var button = jQuery('<input type="submit" />')
			.val(this.options.subscribeButtonText);
		
		// Form
		var form = jQuery('<form></form>')
			.submit(jQuery.proxy(function()
			{
				this.subscribe(field.val());
				return false;
			}, this));
		
		// Body
		var body = jQuery('<div></div>')
			.addClass(this.options.bodyClass)
			.append(this.messageArea)
			.append(
				form
					.append(label)
					.append(field)
					.append(button)
			);
		
		// Set body
		this.win.setBody(body);
	},
	
	/**
	 * Message body
	 * 
	 * @param string Message
	 */
	'messageBody': function(message)
	{
		// Body
		var body = jQuery('<div></div>')
			.addClass(this.options.bodyClass)
			.append(this.messageArea);
		
		// Set body
		this.win.setBody(body);
		
		// Set message
		this.setMessage(message);
	},
	
	/**
	 * Unsubscribe body
	 */
	'unsubscribeBody': function()
	{
		// Field
		var fieldID = 'email_' + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
		var field = jQuery('<input type="text" />')
			.attr('id', fieldID);
		
		// Label
		var label = jQuery('<label></label>')
			.text(this.options.unsubscribeLabelText)
			.attr('for', fieldID);
		
		// Button
		var button = jQuery('<input type="submit" />')
			.val(this.options.unsubscribeButtonText);
		
		// Form
		var form = jQuery('<form></form>')
			.submit(jQuery.proxy(function()
			{
				this.unsubscribe(field.val());
				return false;
			}, this));
		
		// Body
		var body = jQuery('<div></div>')
			.addClass(this.options.bodyClass)
			.append(this.messageArea)
			.append(
				form
					.append(label)
					.append(field)
					.append(button)
			);
		
		// Set body
		this.win.setBody(body);
	},
	
	/**
	 * Subscribe
	 *
	 * @param string Email
	 */
	'subscribe': function(email)
	{
		if (!this.hash)
		{
			this.getHash(jQuery.proxy(function()
			{
				this.subscribe(email);
			}, this));
			return;
		}
		jQuery.ajax(
			this.options.subscribeUrl,
			{
				'dataType': 'json',
				'data': {
					'email': email,
					'hash': this.hash
				},
				'type': 'POST',
				'success': jQuery.proxy(function(data)
				{
					if (data && data.hash)
					{
						this.hash = data.hash;
						if (data.code == this.options.codes.SUBSCRIBE_OK)
						{
							this.messageBody(data.message);
						}
						else
						{
							this.setMessage(data.message);
						}
					}
					else
					{
						alert('Invalid newsletter result format');
					}
				}, this),
				'error': jQuery.proxy(function()
				{
					alert('Invalid newsletter result format');
				}, this)
			}
		);
	},
	
	/**
	 * Unsubscribe
	 *
	 * @param string Email
	 */
	'unsubscribe': function(email)
	{
		if (!this.hash)
		{
			this.getHash(jQuery.proxy(function()
			{
				this.unsubscribe(email);
			}, this));
			return;
		}
		jQuery.ajax(
			this.options.unsubscribeUrl,
			{
				'dataType': 'json',
				'data': {
					'email': email,
					'hash': this.hash
				},
				'type': 'POST',
				'success': jQuery.proxy(function(data)
				{
					if (data && data.hash)
					{
						this.hash = data.hash;
						
						if (data.code == this.options.codes.UNSUBSCRIBE_OK)
						{
							this.messageBody(data.message);
						}
						else
						{
							this.setMessage(data.message);
						}
					}
					else
					{
						alert('Invalid newsletter result format');
					}
				}, this),
				'error': jQuery.proxy(function()
				{
					alert('Invalid newsletter result format');
				}, this)
			}
		);
	}
}, [lpWindow]);
