(function($) {
	var istanziati = 0
	$.jGrowl = function( m , o ) {
		if ( $('#jGrowl').size() == 0 ) $('<div id="jGrowl"></div>').addClass($.jGrowl.defaults.position).prependTo('body');
		$('#jGrowl').jGrowl(m,o);
	};

	$.fn.jGrowl = function( m , o ) {
		if ( $.isFunction(this.each) ) {
			var args = arguments;
			return this.each(function() {
				var self = this;
				if ( $(this).data('jGrowl.instance') == undefined ) {
					$(this).data('jGrowl.instance', new $.fn.jGrowl());
					$(this).data('jGrowl.instance').startup( this );
				}
				if ( $.isFunction($(this).data('jGrowl.instance')[m]) ) {
					$(this).data('jGrowl.instance')[m].apply( $(this).data('jGrowl.instance') , $.makeArray(args).slice(1) );
				} else {
					$(this).data('jGrowl.instance').notification( m , o );
				}
			});
		};
	};

	$.extend( $.fn.jGrowl.prototype , {
		defaults: {
			header: 		'',
			sticky: 		false,
			position: 		'top-right', // Is this still needed?
			glue: 			'after',
			theme: 			'default',
			corners: 		'10px',
			check: 			500,
			life: 			15000,
			speed: 			'slow',
			easing: 		'swing',
			closer: 		true,
			closerTemplate: '<div>Chiudi</div>',
			log: 			function(e,m,o) {},
			beforeOpen: 	function(e,m,o) {},
			open: 			function(e,m,o) {},
			beforeClose: 	function(e,m,o) {},
			close: 			function(e,m,o) {},
			animateOpen: 	{
				opacity: 	'show'
			},
			animateClose: 	{
				opacity: 	'hide'
			}
		},
		element: 	null,
		interval:   null,
		notification: 	function( message , o ) {
			var self = this; 
			var o = $.extend({}, this.defaults, o);
			o.log.apply( this.element , [this.element,message,o] );  
			var notification = $('<div class="jGrowl-notification"><div class="close" title="chiudi"><ul><li><a href="#" title="Chiudi" accesskey="x">&times;<span class="NavigazioneAccessibile">chiudi elenco  notifiche</span></a></li></ul></div><div class="headerNotif">' + o.header + '</div><div class="messageNotif">' + message + '</div><ul><li><a href="/en/notifiche/" class="jGrowl-mostratutte">SHOW ALL <strong>&rarr;  </strong> </a></li></ul></div>')
				.data("jGrowl", o).addClass(o.theme).children('div.close').bind("click.jGrowl", function() {
					$(this).unbind('click.jGrowl').parent().trigger('jGrowl.beforeClose').animate(o.animateClose, o.speed, o.easing, function() {
						$(this).trigger('jGrowl.close').remove();
					});
				}).parent();
				
			( o.glue == 'after' ) ? $('div.jGrowl-notification:last', this.element).after(notification) : $('div.jGrowl-notification:first', this.element).before(notification);
			$(notification).bind("mouseover.jGrowl", function() {
				$(this).data("jGrowl").pause = true;
			}).bind("mouseout.jGrowl", function() {
				$(this).data("jGrowl").pause = false;
			}).bind('jGrowl.beforeOpen', function() {
				o.beforeOpen.apply( self.element , [self.element,message,o] );
			}).bind('jGrowl.open', function() {
				o.open.apply( self.element , [self.element,message,o] );
			}).bind('jGrowl.beforeClose', function() {
				o.beforeClose.apply( self.element , [self.element,message,o] );
			}).bind('jGrowl.close', function() {
				o.close.apply( self.element , [self.element,message,o] );
			}).trigger('jGrowl.beforeOpen').animate(o.animateOpen, o.speed, o.easing, function() {
				$(this).data("jGrowl").created = new Date();
			}).trigger('jGrowl.open');
			if ( $.fn.corner != undefined ) $(notification).corner( o.corners );
			$(document).keyup(function(event){//on keyup ESC chiudo la notifica
			    if (event.keyCode == 27) {
				$(this.element).find('div.jGrowl-notification:parent:eq(0)').animate(o.animateClose, o.speed, o.easing, function() {
					$(this).remove();
				});
				clearInterval( this.interval );
			    }
			});
			
			if ( $('div.jGrowl-notification:parent', this.element).size() > 1 && $('div.jGrowl-closer', this.element).size() == 0 && this.defaults.closer != false ) {
				$(this.defaults.closerTemplate).addClass('jGrowl-closer').addClass(this.defaults.theme).appendTo(this.element).animate(this.defaults.animateOpen, this.defaults.speed, this.defaults.easing).bind("click.jGrowl", function() {
					$(this).siblings().children('div.close').trigger("click.jGrowl");

					if ( $.isFunction( self.defaults.closer ) ) self.defaults.closer.apply( $(this).parent()[0] , [$(this).parent()[0]] );
				});
			};
			if ( $(this.element).find('div.jGrowl-notification:parent').size() == 5 ) {
				$(this.element).find('div.jGrowl-notification:parent:eq(0)').animate(this.defaults.animateClose, this.defaults.speed, this.defaults.easing, function() {
					$(this).remove();
				});
				clearInterval( this.interval );
			};			
		},

		update:	 function() {
			$(this.element).find('div.jGrowl-notification:parent').each( function() {
				if ( $(this).data("jGrowl") != undefined && $(this).data("jGrowl").created != undefined && ($(this).data("jGrowl").created.getTime() + $(this).data("jGrowl").life)  < (new Date()).getTime() && $(this).data("jGrowl").sticky != true && 
					 ($(this).data("jGrowl").pause == undefined || $(this).data("jGrowl").pause != true) ) {
					$(this).children('div.close').trigger('click.jGrowl');
				} 
			});

			if ( $(this.element).find('div.jGrowl-notification:parent').size() < 2 ) {
				$(this.element).find('div.jGrowl-closer').animate(this.defaults.animateClose, this.defaults.speed, this.defaults.easing, function() {
					$(this).remove();
				});
			};
			
			
		},
		startup:	function(e) {
			this.element = $(e).addClass('jGrowl').append('<div class="jGrowl-notification"></div>');
			this.interval = setInterval( function() { jQuery(e).data('jGrowl.instance').update(); }, this.defaults.check);
			
			if ($.browser.msie && parseInt($.browser.version) < 7) $(this.element).addClass('ie6');
		},
		shutdown:   function() {
			$(this.element).removeClass('jGrowl').find('div.jGrowl-notification').remove();
			clearInterval( this.interval );
		}
	});
	$.jGrowl.defaults = $.fn.jGrowl.prototype.defaults;
	
	
			
			
			
	function emptyTime(){$(".time").remove().appendTo(".time");}
	$(document).keyup(function(event){//on keyup ESC chiudo la notifica
		if ($("jGrowl").length == 0){
			if($(".time").length == 0) {
			    if (event.keyCode == 27) {
				$("body").prepend("<div class=\"time\" style=\"display:none;\">esc</div>").ready(function() {setTimeout(emptyTime,1000);});
			    }
			}else{
			    if (event.keyCode == 27) {emptyTime(); 
					$.get("/en/notifiche/callback/notifiche.asp?vUlt=1", function(data){
						if (data != "false"){
							$.jGrowl(data,{ sticky:true, closer:false });
						};
					});			
			    }
			}
		}
	});
	
	
	
})(jQuery);



