(function(f){window.setTimeout = f(window.setTimeout);window.setInterval = f(window.setInterval);})
(function(f){return function(c,t){var a = Array.prototype.slice.call(arguments,2);
if(typeof c != "function")
c = new Function(c);
return f(function(){
c.apply(this, a)
}, t)
}
});

$.notif = function(message, options) {
	$.notif.counter++;
	var defaults = {classname:'notify',horz:'right',vert:'bottom',fadeSpeed:'fast',timeout:'3000',sticky:false,debug:false};
	var options = $.extend(defaults, options);
	var elem = document.createElement('div');
	elem.id = 'notif_' + $.notif.counter;
	var notifier = $(elem);
	notifier.addClass(options.classname);
	notifier.css({'position':'fixed'});
	if (message) {
		$('body').append(notifier);
		if (options.debug) {
			message += '|' + elem.id;
		}
		notifier.hide().text(message);
		var vertOffset = 0;
		var horzOffset = 0;
		if ($.notif.placeholder.length)
		{
			newOffset = $.notif.placeholder.shift();
			horzOffset = newOffset.horz;
			vertOffset = newOffset.vert;
		} else {
			$.each($.notif.messages[$.notif.column], function (idx, val) {
				if (val.shown) {
					vertOffset += $('#' + val.id).outerHeight(true);
					if ((vertOffset + notifier.outerHeight(true)) > $(window).height()) {
						$.notif.column++;
						$.notif.messages[$.notif.column] = {};
						vertOffset = 0;
					}
					horzOffset = $.notif.column * notifier.outerWidth(true);
				}
			});
		}
		$.notif.ids[elem.id] = $.notif.column;
		notifier.css(options.vert, vertOffset);
		notifier.css(options.horz, horzOffset);
		notifier.fadeIn(options.fadeSpeed);
		notifier.click(function() {
			//$.notif.hideRemove(elem.id, $(this), options);
			el.fadeOut(options.fadeSpeed, function() {
				$.notif.placeholder.push({'horz': $(el).css(options.horz), 'vert': $(el).css(options.vert)});
				$.notif.messages[$.notif.ids[id]][id].shown = false;
				el.remove();
			})
		});
		if (!options.sticky) {
			//setTimeout($.notif.hideRemove, options.timeout, elem.id, notifier, options);
			setTimeout(function () {
				notifier.fadeOut(options.fadeSpeed, function() {
					$.notif.placeholder.push({'horz': $(notifier).css(options.horz), 'vert': $(notifier).css(options.vert)});
					$.notif.messages[$.notif.ids[elem.id]][elem.id].shown = false;
					notifier.remove();
				})
			}, options.timeout);
		}
		$.notif.messages[$.notif.column][elem.id] = {
			'id':	   elem.id,
			'shown':	true,
			'horz':	 notifier.css(options.horz),
			'vert':	 notifier.css(options.vert)
		};
	}
};
$.notif.hideRemove = function(id, el, options) {
	el.fadeOut(options.fadeSpeed, function() {
		$.notif.placeholder.push({'horz': $(el).css(options.horz), 'vert': $(el).css(options.vert)});
		$.notif.messages[$.notif.ids[id]][id].shown = false;
		el.remove();
	})
};
$.notif.column = 0;
$.notif.counter = 0;
$.notif.ids = { };
$.notif.messages = { };
$.notif.messages[$.notif.column] = {};
$.notif.placeholder = [];

