/*!
 * jquery.notifications. The jQuery notification plugin
 *
 * Copyright (c) 2011 Thorn-Welf Walli
 * http://lammpee.com
 *
 * Launch  : August 2011
 * Version : 0.1.0
 */

var NOTIFICATION_DEFAULT_CLASS_TRUE = 'ui-notification-true';
var NOTIFICATION_DEFAULT_CLASS_FALSE = 'ui-notification-false';

(function($) {

	
	// Implementation
	$.fn.nfications = function(config) {
		var interfaces, id, sender;
		sender = this;

		if ($(sender).data('nfications') != null) {

			var obj = null;
			$.each($.fn.nfications.interfaces, function(i, v) {

				if (v.id == $(sender).data('nfications')) {
					obj = v;
					return;
				}

			});

			if (obj != null)
				return obj;

		} else {

			config = $.extend(true, $.fn.nfications.defaults, {
				"sender" : sender
			}, config);

			if (true) {

				var obj = null;

				id = $.fn.nfications.interfaces.length;
				for (i = 0; i < id; i++) {
					if (typeof $.fn.nfications.interfaces[i] == 'undefined') {
						id = i;
						break;
					}
				}

				obj = new notification($.extend(true, {
					"id" : id
				}, config));
				$.fn.nfications.interfaces[id] = obj;
				$(this).data('nfications', id);

			}

		}

	}

	function notification(config) {

		var self = $.extend(true, this, config);

		$
				.extend(
						true,
						self,
						{

							// Lässt die Meldung erscheinen
							add : function(index, attributes) {

								attributes = $.extend(true,
										self.DEFAULT_CONDITION, attributes);

								self.conditions = self.conditions[index] = attributes;

							
							},

							show : function(index) {

								if ($('[notification="' + self.id + '_' + index
										+ '"]').length < 1) {

									var noti = $('<div/>', {
										"notification" : self.id + '_' + index,
										class : 'ui-notification '
												+ self.conditions[index].class,
										html : $('<div/>', {
											class : 'ui-notification-icon',
											text : self.conditions[index].text
										})
									});
									noti.css("display", "none");
									$(noti).oneTime(
											self.conditions[index].time,
											function() {
												self.close(index);
											});

									if (self.position == "top")
										$(self.sender).prepend(noti);
									else
										$(self.sender).append(noti);
								}

								if ($(
										'[notification="' + self.id + '_'
												+ index + '"]').css('display') != "block")
									$(
											'[notification="' + self.id + '_'
													+ index + '"]').fadeIn(
											"slow");

							},

							close : function(index) {

								$(
										$('[notification="' + self.id + '_'
												+ index + '"]')).fadeOut(
										"slow",
										function() {
											$(
													$('[notification="'
															+ self.id + '_'
															+ index + '"]'))
													.remove();
											
											if(self.conditions[index].closed != null)
												if (typeof self.conditions[index].closed != 'object')
													self.conditions[index].closed(self.conditions[index].closed);
												else
													$.each(self.conditions[index].closed,function(i, v){
													
													v(self.conditions[index]);
															
													});

										});
							}

						});
	}

	// ########################################################

	$.fn.nfications.interfaces = [];


	
	$.fn.nfications.defaults = {
		DEFAULT_CONDITION : {
			icon : null,
			class : null,
			url : null,
			text : null,
			time : 1500,
			closed : null
		}
	}

	$.fn.nfications.defaults = $.extend(true, {

		sender : null,
		position : "top", // top or bottom

		conditions : {

			"true" : $.extend(true, {},
					$.fn.nfications.defaults.DEFAULT_CONDITION, {
						class : NOTIFICATION_DEFAULT_CLASS_TRUE
					}),

			"false" : $.extend(true, {},
					$.fn.nfications.defaults.DEFAULT_CONDITION, {
						class : NOTIFICATION_DEFAULT_CLASS_FALSE
					})
		}

	}, $.fn.nfications.defaults);

})(jQuery);
