/**
 * @requires js/libs/k/kernel/base/util.js
 */

if (typeof(m) == "undefined" || !m) {
	var m = {};
}

(function($) {
	var queue = [],
		now = false; //показывается ли сейчас сообщение

	m.message = {
		show : function (message, config) {
			queue.push({message:message, config:config});

			if (!now) {
				__showFirstFromQueue();
			}
		}
	}

	function __showFirstFromQueue()
	{
		var msg = queue.shift();
		var config = $.extend(msg.config, {onFinish : __onMessageFinish});
		var messageObj = new __message(msg.message, config);
		messageObj.show();
		now = true;
	}

	function __onMessageFinish()
	{
		now = false;

		if (queue.length) {
			__showFirstFromQueue();
		}
	}

	function __message(message, options)
	{
		var that = this,
			el = null,
			startHeight = null,
			config = {onFinish : null,
			          elSelector : '#info',
					  showDelay : 5000};

		__setup();

		this.show = function()
		{
			$('div.content', el).get(0).innerHTML = message;
			startHeight = el.height();
			var leftOffset = k.util.getWinCenterX(el.width());
			el.css('left', leftOffset + 'px')
			  .css('visibility', 'visible')
			  .css('height', '0px');


			el.animate({height: startHeight}, null, null, function() {
				if (config.showDelay) {
					setTimeout(function() {
						that.hide();
					}, config.showDelay);
				}
			});
		}

		this.hide = function()
		{
			el.animate({height: '0'}, null, null, function() {
				el.css('visibility', 'hidden')
				  .css('height', startHeight);

				if (typeof(config.onFinish) == 'function') {
					config.onFinish.call(that);
				}
			});
		}

		function __setup()
		{
			if (typeof(options) == 'object' && options) {
				config = $.extend(config, options);
			}

			el = $(config.elSelector);
		}
	}
}) (jQuery);
