// Main notifications placeholder object
var Notifications = {
	'init' : function() {
		this.container = $('<div id="notifications">');

		$(function() {
			$(document.body).append(this.container);
		}.bind(this));
	}
};

var Notification = function(options) {
	// Set options
	this.options = $.extend({}, {
		'text' : 'This is a notification',	// The notification text (can be HTML)
		'showDuration' : 2 * 1000,		// The display duration for this notification. Only applies when showCloseButton is false. Default: 2 seconds
		'showCloseButton' : false,		// Flag to select whether close button is shown.
		'class' : '',				// Additional class to add to the notification element.
		'fadeDuration' : 500			// The duration of the fadeIn and fadeOut animation.
	}, options || {});

	// Create notification element.
	this.element = 
		$('<div class="notification">')
			.addClass(this.options['class'])
			.html(this.options.text)
			.hide();

	// Close button
	if(this.options.showCloseButton) {
		this.element.append(
			$("<span>")
				.text('×')
				.addClass("close-button")
				.click(function() {
					this.hide();
				}.bind(this))
		);
	}
};

Notification.prototype = {
	'show' : function() {
		Notifications.container.prepend(this.element);

		this.element.fadeIn(this.options.fadeDuration);

		if(!this.options.showCloseButton) {
			setTimeout(function() {
				this.hide();
			}.bind(this), this.options.showDuration);
		}
	},
	'hide' : function() {
		this.element.fadeOut(this.options.fadeDuration, function() {
			this.element.remove();
		}.bind(this));
	}
};

Notifications.init();

