/*
 * flashMsg jQuery plugin (http://enrichmentkit.com)
 * Copyright (c) 2007 Pink Hominid
 * Licensed under terms of the MIT License
 * (http://enrichmentkit.googlecode.com/svn/trunk/LICENSE.txt)
 * 
 * IE 6.0+, FF 1.5+, jQuery 1.2+ Tested
 * 
 * TODO:
 * - check coordinates on show to cancel hide if we are over msg display (mouseover doesn't fire unless you move)
 */
(function ($) {	

	$.flashMsg = function (msg, type, options) {
		if (msg) {
			if (typeof type == 'object') {
				options = type;
				type = 'info';
			} else {
				type = type || 'info';
			}
			options = $.extend({}, $.flashMsg.defaults, options);
			var meta = {msg: msg, type: type, options: options};
			$.flashMsg.queue.push(meta);
			return meta;
		}
	};

	$.flashMsg.defaults = {
		init: { opacity: '0', display: 'none' },
		show: { opacity: '.7', height: 'show' },
		hide: { opacity: '0' },
		reshow: { opacity: '.7' },
		speed: 'slow',
		easing: 'swing',
		duration: 3
	};

	// if you'd like to control this from a style sheet set '$.flashMsg.defaults.style = {}'
	// for global or pass 'style:{}' as a local option
	$.flashMsg.defaults.style = {
		display: {
			position: ($.browser.msie && $.browser.version < 7) ? 'absolute' : 'fixed',
			zIndex: '9999',
			padding: '6px 20px 6px 30px',
			fontFamily: 'arial, helvetica, sans-serif',
			fontSize: '.9em',
			color: '#aaa',
			backgroundColor: '#000',
			backgroundRepeat: 'no-repeat',
			backgroundPosition: '8px center',
			width: '16%',
			bottom: '1px',
			left: '42%',
			textAlign: 'center'
		},
		close: {
			position: 'absolute',
			top: '3px',
			right: '3px',
			border: '1px solid #444',
			backgroundColor: '#111',
			fontSize: '9px',
			lineHeight: '9px',
			fontWeight: 'bold',
			fontFamily: 'Arial, Helvetica, sans-serif',
			color: '#777',
			padding: '0 2px 1px 3px',
			cursor: 'pointer',
			textDecoration: 'none'
		}
	};

	$.flashMsg.queue = [];
	
	var queueIdx = 0, timer, msgDisplayed = false, msgFocused = false;

	function show(display, meta) {
		if (meta.options.duration !== -1) {
			display.bind('mouseover', function(){
				msgFocused = true;
				stop();
				if (msgDisplayed) {
					display.stop();
					display.animate(meta.options.reshow, 'fast', meta.options.easing);
				}
			}).bind('mouseleave', function(){
				msgFocused = false;
				hide(display, meta);
			}).animate(meta.options.show, meta.options.speed, meta.options.easing, function(){
				msgDisplayed = true;
				hide(display, meta);
			});
		} else {
			display.animate(meta.options.show, meta.options.speed, meta.options.easing, function(){
				msgDisplayed = true;
			});
		}
	}
	
	function hide(display, meta) {
		timer = setTimeout(function(){
			if (!msgFocused) {
				display.animate(meta.options.hide, meta.options.duration * 1000, meta.options.easing, function(){
					$(this).unbind('mouseleave').unbind('mouseover').remove();
					msgDisplayed = msgFocused = false;
					start();
				});
			}
		}, meta.options.duration * 1000);
	}

	function process() {
		if (queueIdx < $.flashMsg.queue.length) {
			var meta = $.flashMsg.queue[queueIdx];
            var display = $('<div class="flash-msg"/>')
				.html(meta.msg)
				.addClass(meta.type)
				.css(meta.options.style.display || {})
				.css(meta.options.init)
				.appendTo('body');
			if ($.fn.corner) { display.corner('top 5px'); }	
			var close = $('<a class="flash-msg-close">x</a>')
				.css(meta.options.style.close || {})
				.click(function () {
					display.unbind('mouseleave').unbind('mouseover')
						.animate(meta.options.hide, 'fast', meta.options.easing, function(){
							$(this).remove();
							msgDisplayed = msgFocused = false;
							start();
						});
				})
				.appendTo(display);
			if ($.fn.corner && !($.browser.msie && parseFloat($.browser.version) < 7 )) {
                close.corner('2px');
            }	
			show(display, meta);
			queueIdx++;
		} else {
			start();
		}
	}
		
	function stop() {
		clearTimeout(timer);
	}

	function start() {
		timer = setTimeout(process, 500);
	}
	
	start();
	
})(jQuery);

