/*jslint browser: true, eqeqeq: true, undef: true */
/*global jQuery */
/******************************************************************************
Lines above are for jslint, the JavaScript verifier.
http://www.jslint.com/
******************************************************************************/

/* based on http://www.seoadsensethemes.com/wordpress-multi-level-drop-down-menu-using-jquery/
   and modified.
   Tested in FF2, FF3, IE6, IE7, IE8, Safari, Opera, and Chrome. */
jQuery(document).ready(function () {
	var $ = jQuery;
	if (top.document.location !== document.location) {
		// If the page is in a frameset, don't show flyouts.
		return;
	}
	var queue = {
		/* This object implements a hide delay on the menus. */
		timeouts: [],
		/* Basically, we timeout any calls to hide a flyout by
		using this wrapper method: */
		add: function (f, delay) {
			var o = {};
			o.f = function () {
				f();
				o.f = null;
			};
			o.timeout = window.setTimeout(function () {
				if (o.f) {
					o.f();
				}
				o.timeout = null;
			}, delay);
			this.timeouts.push(o);
		},
		/* ...but any time we open a flyout, we need to hide
		those other other flyouts immediately by calling this
		method: */
		run: function () {
			$.each(this.timeouts, function (i, o) {
				if (o.timeout) {
					window.clearTimeout(o.timeout);
					o.timeout = null;
				}
				if (o.f) {
					o.f();
				}
			});
			this.timeouts = [];
		}
	};
	var hover = function () {
		queue.run();
		$(this).addClass("hover");
		var flyout = $(this).find('div.flyout:first').get(0);
		if (!flyout) {
			return;
		}
		var inner = $(flyout).find(".flyout_inner").get(0);
		if (!inner) {
			return;
		}
		if (!flyout.iframe) {
			flyout.iframe = document.createElement("iframe");
			flyout.iframe.style.borderWidth = "0";
			flyout.iframe.style.margin = "0";
			flyout.iframe.style.padding = "0";
			flyout.iframe.style.position = "absolute";
			$(inner).before(flyout.iframe);
		}
		$(flyout).show();
		flyout.iframe.style.top    = inner.offsetTop + "px";
		flyout.iframe.style.left   = inner.offsetLeft + "px";
		flyout.iframe.style.width  = inner.offsetWidth + "px";
		flyout.iframe.style.height = inner.offsetHeight + "px";
		flyout.iframe.style.visibility = "visible";
	};
	var unhover = function () {
		var that = this;
		queue.add(function () {
			$(that).removeClass("hover");
			var flyout = $(that).find('div.flyout:first').get(0);
			if (!flyout) {
				return;
			}
			$(flyout).hide();
			if (flyout.iframe) {
				flyout.iframe.style.visibility = "hidden";
			}
		}, 500);
	};
	$("#nav1 .navbar .hasFlyout > a").parent().hover(hover, unhover);
	$("#nav1 .navbar div.flyout").each(function () {
		$(this).prepend($("<div class='flyout_top'></div>"));
	});
	
	// FOR TESTING PURPOSES: activate a dropdown.
	// hover.apply($("#nav1 .navbar .hasFlyout > a").parent().get(1));
});

