/* jQuery Fly Menu plugin */

(function($) {
	$.fn.flymenu = function(settings) {

		settings = $.extend({
			showDelay: 0,
			switchDelay: 0,
			hideDelay: 0,
			menuSel: 'ul',
			itemSel: 'li',
			show: function() {
				this.style.visibility = 'visible';
			},
			hide: function() {
				this.style.visibility = 'hidden';
			}
		}, settings);

		timeout = function (obj, action, time) {
			$(obj).attr('pending', action);
			window.setTimeout(function() {
				if($(obj).attr('pending') == action) {
					if(action == 'show') {
						$(obj).bgiframe();
						$(obj).parent().addClass('active');
						settings.show.call(obj);
					} else {
						$('.bgiframe', obj).remove();
						$(obj).parent().removeClass('active');
						settings.hide.call(obj);
					}
				}
			}, time);
		};

		$(this).children(settings.itemSel).each(function(i) {

			var itemElem = this;

			if (i == 0) {
				$(this).addClass('first');
			}
			if (i == $(this).parent().children(settings.itemSel).length - 1) {
				$(this).addClass('last');
			}

			if ($(this).children(settings.menuSel).size() > 0) {
				$(this).addClass('parent').hover(
					function() {
						var obj = this;
						$(this).parent().children('.active').each(function() {
							if (this != obj) {
								timeout(	$(this).children(settings.menuSel).get(0),
											'hide',
											settings.switchDelay);
							}
						});
						timeout(	$(this).children(settings.menuSel).get(0),
									'show',
									$(this).parent().children('.active').size > 0 ? settings.switchDelay : settings.showDelay);
					},
					function() {
						timeout(	$(this).children(settings.menuSel).get(0),
									'hide',
									settings.hideDelay);
					}
				);
			}

			$(this).children(settings.menuSel).each(function() {
				$(this).flymenu(settings);
			});

		});

		return $(this);

	}
})(jQuery);
