var numNotes = 0;

(function(jQuery)
{
	jQuery.extend({			
		noticeAdd: function(options)
		{	
			++numNotes;
			
			var defaults = {
				inEffect: 			{opacity: 'show'},	// in effect
				stayTime: 			6000,				// time in miliseconds before the item has to disappear
				title: 				'',					// content of the item
				text: 				'',					// content of the item
				stay: 				true,				// should the notice item stay or not?
				type: 				'notice', 			// could also be error, succes,
				username:  '',
				effects: false
			}

			var options, noticeWrapAll, noticeItemInner, noticeItemClose;
								
			options 		= jQuery.extend({}, defaults, options);
			noticeWrapAll	= (!jQuery('.notice-wrap').length) ? jQuery('<div></div>').addClass('notice-wrap').appendTo('body') : jQuery('.notice-wrap');
			
			if( !window.ActiveXObject && !jQuery.browser.opera  )
				noticeItemInner	= jQuery('<div></div>').css('border','2px solid #999').hide().addClass('notice-item').appendTo(noticeWrapAll).html( '<div class="notice-title-wrap"><h1 class="notice-title">'+options.title+'</h1></div><div class="notice-text">'+options.text+'<br /><span class="userFF-Webkit">par '+options.username+'</span></div>');
			else
			{
				noticeItemInner	= jQuery('<div id="note'+numNotes+'"></div>').hide().addClass('notice-item').appendTo(noticeWrapAll);
				noticeItemInner.html( '<div class="notice-text">'+options.text+'</div>');
				Nifty('div#note'+numNotes,"big");
				$('#note'+numNotes+' .notice-text').append('<br /><span class="userIE-Opera">par '+options.username+'</span>');
				jQuery('<div class="notice-title-wrap" id="note'+numNotes+'Title" style="padding: 3px 0 6px 6px"><h1 class="notice-title">'+options.title+'</h1></div>').hide().appendTo(noticeWrapAll);
				Nifty('div#note'+numNotes+'Title',"top big");
				$('#note'+numNotes+'Title').insertBefore('#note'+numNotes+' .notice-text').show();
				noticeItemInner.show();
			}
			
			noticeItemInner.find('[percent]').each( function(){
			var percent = $(this).attr('percent');
			var val = ( Math.floor( (100 - percent) * 120 / 100 ) - 1 ) * (-1);
			$(this).css('background-position', val + 'px 0').next().html( percent + '%' );
			} );
			
			if( options.effects )
				noticeItemInner.animate(options.inEffect, 800);
			else
				noticeItemInner.show();
			noticeItemClose	= jQuery('<div></div>').addClass('notice-item-close').prependTo(noticeItemInner).html('x').click(function() { jQuery.noticeRemove(noticeItemInner,options.effects) });
			
			if(!options.stay)
			{
				setTimeout(function()
				{
					jQuery.noticeRemove(noticeItemInner,options.effects);
				},
				options.stayTime);
			}
		},
		
		noticeRemove: function(obj,effects)
		{
			if( effects )
			{
				obj.animate({opacity: '0'}, 800, function()
				{
					obj.remove();
				});
			}
			else
				obj.remove();
		}
	});
})(jQuery);