﻿(function($) 
{	
	jQuery.fn.delay = function(time,func){
		this.each(function(){
			setTimeout(func,time);
		});
		return this;
	};
	
	$.fn.notification = function(userDefinedSettings) 
	{
		var settings = $.extend({}, $.fn.notification.defaultSettings, userDefinedSettings);
		var $container = ($('#' + settings.containerId).length)? $('#' + settings.containerId) : $('<div />').attr('id',settings.containerId).css(settings.position,'20px').appendTo('body');
		
		function showNotification()
		{
			var $this = this;
			var $notification = $('<div />')
					.attr('class',settings.notificationClass)
					.hide()
					.appendTo($container);
				
			var $title = $('<h3 />')
					.appendTo($notification);
				
			var $message = $('<p>')
					.appendTo($notification);
					
			settings.onShow.call($this);
			$title.html(settings.title);
			$message.html(settings.message);
			
			$notification.slideDown(settings.speed)
						.delay(settings.visibleTime,function()
						{
							settings.onHide.call($this);
							$notification.slideUp(settings.speed,function(){$(this).remove();});
						});
			
			return false;
		}
		
		if(settings.triggerEvent == 'instant')
			showNotification();
		else
			$(this).bind(settings.triggerEvent,showNotification);
		
		return this;
	};

	$.fn.notification.defaultSettings = 
	{
		triggerEvent: 'click',
		visibleTime: 4000,
		position: 'left',
		speed: 200,
		onShow: function(){},
		onHide: function(){},
		containerId: 'notifications',
		notificationClass: 'notification',
		title: '',
		message: ''
	};

})(jQuery);


// $('.submit').notification('click');
