(function($){
	var MegaNav = function(ele, opts) {
		var options = {
			align: 'left', 
			context: null
		}
		this.opts = $.extend({}, options, opts);
		this.ele = $(ele);
		this.lis = this.ele.find("ul.nav li");
		this.megaNavContents = this.ele.find("div.megaNavContent");
		this.formFocus = false;
		
		var _this = this;
		this.lis.each(function(i, li) {
			var con = _this.megaNavContents[i], timeout;
			if (!con || $(con).find("div.grid.empty").length) return;
			$([li, con])
			.hover(function() {
				clearTimeout(timeout);
				timeout = setTimeout(function() {
					_this._menuItemHoverOn(li, con);
				}, 10);
			},
			function() {
				clearTimeout(timeout);
				timeout = setTimeout(function() {
					_this._menuItemHoverOff(li, con);
				}, 10);
			});
		});
		this.ele.find("input,select").focus(function() {
			_this.formFocus = true;
		}).blur(function() {
			_this.formFocus = false;
		});
		this.ele.find("input[type='submit']").click(this._fakeFormSubmit);
	};
	$.extend(MegaNav.prototype, {
		_menuItemHoverOn: function(li, con) {
			if (this.formFocus) return;
			$(con).css("display", "block");
			$li = $(li);
			$li.addClass("hover");
			$li.next().addClass("adjRHover");
			$li.prev().addClass("adjLHover");
			this._sizeIframe(con);
		},
		_menuItemHoverOff: function(li, con) {
			if (this.formFocus) return;
			$(con).css("display", "none");
			$li = $(li);
			$li.removeClass("hover");
			$li.next().removeClass("adjRHover");
			$li.prev().removeClass("adjLHover");
		},
		_fakeFormSubmit: function() {
			var input = $(this),
			form = input.parents("div.fakeForm:first");
			if (form.length) {
				var formObj = form.ParseClass(true),
				formFields = form.find("input[type!='submit'], textarea, select");
				formFields.add(input);
				var newForm = $("<form action=\"" + formObj.action + "\" method=\"" + formObj.method + "\" style=\"display: none;\">").append(formFields.clone(true)).appendTo(document.body)[0].submit();
			}
		},
		_sizeIframe: function(con) {
			if ($.browser.msie && $.browser.version == '6.0') {
				if (!$('iframe', con).length) {
					var iframe = $('<iframe>').appendTo(con);
				} else {
					var iframe = $('iframe', con);
				}
				$(iframe).height($(con).outerHeight());
				$(iframe).width($(con).outerWidth());
			}
		}
	});
	
	$.fn.extend({
		MegaNav: function(opts) {
			var $this = this.not(function(i) {
				return $.data(this, "MegaNavObj");
			}).each(function() {
				$.data(this, "MegaNavObj", new MegaNav(this, opts));
			});
			return this;
		}
	});
	$.MegaNav = {
		init: function(sel) {
			var $target = $(sel || "#nav");
			var opts = $target.ParseClass();
			$target.MegaNav(opts);
		}
	};
	$(function() {
		$.MegaNav.init();
	});
})(jQuery);

