/**************************************************************
Based on work by James Nylen

USING

(1)----Make a menu like this:
<ul id="nav-menu">
  <li><a href="#">Item 1</a></li>
  <li><a href="#">Item 2</a>
    <ul>
      <li><a href="#">Item 2.1</a></li>
      <li><a href="#">Item 2.2</a></li>
    </ul>
  </li>
  <li><a href="#">Item 3</a></li>
</ul>

You can also use different tag names than ul and li for the
menu items and submenus, but submenus still have to be
*direct children of their parent menu items*.

CSS CLASS NAMES

.first - the first item in a menu or submenu
.last - the last item in a menu or submenu
.parent - a menu item that contains a submenu
.active - a parent item with an open submenu

INVOKING

$('#nav-menu').flymenu(options);
where options has one or more of the following properties:
{
  showDelay: [milliseconds to wait before showing a menu],
  switchDelay: [milliseconds to wait before switching to a
                different submenu on the same level],
  hideDelay: [milliseconds to wait before hiding a menu],
  itemSel: [selector that matches a menu item (default is li)],
  menuSel: [selector that matches a submenu (default is ul)],
  show: function() {
    //code to show a submenu
    //called with this == a submenu (ul or menuSel element)
    //by default, just sets this.visibility to visible
  }
  hide: function() {
    //code to hide a submenu
    //called with this == a submenu (ul or menuSel element)
    //by default, just sets this.visibility to hidden
  }
}

All properties are optional.  Delays default to 0ms.  You
should not use child or descendant selectors with itemSel and
menuSel, since itemSel and menuSel are always passed to the
jQuery children() function.

**************************************************************/

(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);
