function nwaMenus()
{
	this.headerBlock = 'nwaHeader';
	this.menus = [];
	this.activeMenu = null;
	this.subMenuOffset = 0;
	this.observed = [];
	this.observedCallback = null;
	this.openedSubMenu = null;
};
nwaMenus.prototype.add = function(url, menuElementId, menuId, rl)
{
	this.menus.push(
		{
			"url": url,
			"menuElementId": menuElementId,
			"menuId": menuId,
			"side": rl,
			"moved": false

		});
};
nwaMenus.prototype.observe = function(items)
{
	var menus = this;
	for (var i=0; i < items.length; i++)
	{
		$('#'+items[i]).bind("mouseover", function()
		{
			//menus.hideActiveMenu(700);	
			menus.hideActiveMenu(0);
		});
	}
};
nwaMenus.prototype.load = function()
{
	//Need to load bgiframe.js if is IE...
	if ($.browser.msie)
	{
		$.getScript("/includes/js/jquery.bgiframe.js");
	}
	var menus = this;
	for (var i=0; i<this.menus.length; i++)
	{
		this.call(i);
	}
	$(window).bind("resize",
		function() {
			menus.hideActiveMenu();
		}
	);
};
nwaMenus.prototype.call = function(i)
{
		var menuUrl = this.menus[i].url;
		var menuElementId = '#'+this.menus[i].menuElementId;
		var menuId = '#'+this.menus[i].menuId;
		var side = this.menus[i].side;
		var menus = this;
		$.get(menuUrl,
		function(menuDoc){
			$('html > body').append(menuDoc);
			$(menuElementId).hover(
			function(){
				if (menus.activeMenu != null)
				{
					var mId = menus.getMenuElementID(menus.activeMenu);
					//var mDiv = this.activeMenu.substring(0, this.activeMenu.indexOf("Menu"));
					//console.log("mId on: "+$(mId).attr('timeOut'));
					//console.log("menuId, menuElementId: "+menuId, menuElementId);
					if ($(mId).attr('timeOut') != null)
					{
						clearTimeout($(mId).attr('timeOut'));
					}
				}
				menus.showMenu(menuId, menuElementId, side);
				menus.closeSubMenu();
				
				/*
				menus.hideActiveMenu();
				menus.activeMenu = menuId;
				var res = menus.getElementPosition(menuElementId);
				$(menus.activeMenu).css({  position: "absolute", left: res.x, top: res.y + $(this).height(), visibility: "visible", display: 'block' });
				*/
			},
			function() {
				//var mId = menus.getMenuElementID(menus.activeMenu);

				//var mDiv = this.activeMenu.substring(0, this.activeMenu.indexOf("Menu"));
				//console.log("mId out: "+$(mId).attr('timeOut'));
				/*
				$(menuElementId).attr({ timeOut: setTimeout( function () {$('.nwaMenu').css({ visibility: 'hidden'})}, 1000) } );
				*/
			//	menus.hideActiveMenu(700);
				menus.hideActiveMenu(0);
			});

			$(menuId +" > li").each(function(i)
				{
					var jObj = $(this);
					var url = this.getAttribute('url');
					var target = this.getAttribute('target');
					var subMenu = this.getAttribute('submenu');
					jObj.bind('click', function()
					 {
						alert("subMenu = " + submenu);
						 menus.menuClick(url, target);
					 });
 					var kids = jObj.children();
					var hasSubMenu = menus.hasSubMenu(kids);
					jObj.bind('mouseover', function()
					  {
						if (menus.activeMenu != null)
						{
							var mDiv = menus.activeMenu.substring(0, menus.activeMenu.indexOf("Menu"));
							var mId = menus.getMenuElementID(menus.activeMenu);
							//var className = mId.getAttribute('class', 2);
							//console.log(menus.activeMenu+", "+mDiv+", "+mId);
                                                        //fix to make tab stay highlighted while using the menu
                                                        //$("div:eq(12)").attr("class","tab_hover");
							//$("div[class='tab_hover']").attr("class","tab");
							if (mId == '#reservations') {
								$("div[id='reservations']").attr("class","tab_hover");
							}
							if (mId == '#reservations_active') {
                                                                $("div[id='reservations_active']").attr("class","tab_hover");
                                                        }
	                                                if (mId == '#travel') {
                                                                $("div[id='travel']").attr("class","tab_hover");
                                                        }
							if (mId == '#travel_active') {
								$("div[id='travel_active']").attr("class","tab_hover");
							}
							if (mId == '#promo') {
								$("div[id='promo']").attr("class","tab_hover");
							}
							if (mId == '#promo_active') {
                                                                $("div[id='promo_active']").attr("class","tab_hover");
                                                        }
							if (mId == '#wperks') {
								$("div[id='wperks']").attr("class","tab_hover");
							}
							if (mId == '#wperks_active') {
                                                                $("div[id='wperks_active']").attr("class","tab_hover");
                                                        }
							if (mId == '#nwainfo') {
                                                                $("div[id='nwainfo']").attr("class","tab_hover");
                                                        }
							if (mId == '#nwainfo_active') {
                                                                $("div[id='nwainfo_active']").attr("class","tab_hover");
                                                        } 
							//end of fix
							clearTimeout($(mId).attr('timeOut'));
						//	if (!hasSubMenu)
						//	{
								//console.log(menus.openedSubMenu);
								if (menus.openedSubMenu != null)
								{
									var isMySubMenu = false;	
									var openedSubMenuId = menus.openedSubMenu.getAttribute('id');
									for (var i=0; i<kids.length; i++)
									{
										if (kids[i].nodeName == 'ul' || kids[i].nodeName == 'UL')
										{
											//console.log("menus.openedSubMenu: "+menus.openedSubMenu.getAttribute('id'));
											//console.log(subMenu + " == " + kids[i].getAttribute('id'));
											if(subMenu == openedSubMenuId)
											{
												isMySubMenu = true;
												break;
											}
										}
									}
									//console.log("isMySubMenu: "+isMySubMenu);
									if (!isMySubMenu)
									{									
										//console.log(menus.openedSubMenu.getAttribute('id'));
										menus.closeSubMenu();
									}
								}
//							}
						}

					  });
					if (hasSubMenu)
					{
						jObj.bind('mouseover', function()
						{							
							  	menus.moveSubMenuTo(this, menuElementId, subMenu, side);
						});
					}
				});
		}
		);

};
nwaMenus.prototype.getMenuElementID = function(menuId)
{
	var ele = null;
	if (menuId.indexOf('#') != -1)
		menuId = menuId.substr(1, menuId.length);
	for (var i=0; i<this.menus.length; i++)
	{
		////console.log(menuId+" = "+this.menus[i].menuId);
		if (this.menus[i].menuId == menuId)
		{
			ele = '#'+this.menus[i].menuElementId;
			break;
		}
	}
	return ele;
};
nwaMenus.prototype.hasSubMenu = function(kids)
{
	var hasMenu = false;
	for (var i=0; i<kids.length; i++)
	{
		if (kids[i].nodeName == 'ul' || kids[i].nodeName == 'UL')
		{
			hasMenu = true;
			break;
		}
	}
	return hasMenu;
};
nwaMenus.prototype.showMenu = function(menuId, menuElementId, side)
{	
	if (this.activeMenu != menuId)
	{
		this.hideActiveMenu();
		this.activeMenu = menuId;
		if ($.browser.msie)
		{
			$(this.activeMenu).bgIframe();
		}
		var res = this.getElementPosition(menuElementId);
		//$(this.activeMenu).css({  position: "absolute", left: res.x, top: res.y + $(menuElementId).height(), visibility: "visible", display: 'block' });
		//for drop shadows...
		//$(this.activeMenu).wrap('<div class="wrap1" id="foobar"><div class="wrap2"><div class="wrap3"><div class="box">');
		//$('#foobar').css({  position: "absolute", left: res.x, top: res.y + $(menuElementId).height(), width: $(this.activeMenu).css("width"), height: $(this.activeMenu).height() });
		//alert($(this.activeMenu).height());
		var mIdHeight = $(menuElementId).height();
		if (side == "left") {
			$(this.activeMenu).css({  position: "absolute", left: (res.x - "10") + "px", top: (res.y + mIdHeight)+"px" });
		}
		else { 
			$(this.activeMenu).css({  position: "absolute", left: (res.x - "55" ) + "px", top: (res.y + mIdHeight)+"px" });
		}
		//$(this.activeMenu).css({  position: "absolute", left: res.x, top: "10px" });
		$(this.activeMenu).fadeIn("fast");
	}

};
nwaMenus.prototype.moveSubMenuTo = function(ele, what, who, left)
{
	if (typeof who == "string")
	{
		who = this.getElement(who);
	}
	if (typeof what == "string")
	{
		what = this.getElement(what);
	}
	//left =  left != null ?  (-1 * ($(ele).width()-this.subMenuOffset)) : ($(ele).width()-this.subMenuOffset);
	if (left != null)
	{	
		left =  -1 * ($(ele).width()-(this.subMenuOffset - "2"));	
	}
	else
	{
		left =  $(ele).width()-this.subMenuOffset;	
	}
	var cssLeft = parseInt($(who).css("left"));
	//var cssHash = { visibility: 'visible', display: 'none', filter: 'alpha(opacity=100)', opacity: 1.0};
	var cssHash = { visibility: 'visible', display: 'none'};
	var shouldShow = false;
	if (left != cssLeft)
	{
		cssHash.left = left;
		shouldShow = true;
	}
	else
	{
		if ($(who).css("visibility") == "hidden")
		{
			shouldShow = true;		
		}
	}
	if (shouldShow)
	{
		//fadeIn in IE is slow with bgIframe :(
		if ($.browser.msie)
		{
			$(who).bgIframe().css(cssHash).show();
		}
		else
		{
			//nudge for non IE...
			var eHeight = $(who).parent().height();	
			var t = $(who).css("top");
			$(who).css({ 'margin-top': -1 * eHeight});			
			$(who).css(cssHash).fadeIn("Fast");
		}
	
	
	}
	this.openedSubMenu = who;
};
nwaMenus.prototype.closeSubMenu = function()
{
	if (this.openedSubMenu != null)
	{
		$(this.openedSubMenu).css({visibility: 'hidden'});
		 //$(this.openedSubMenu).fadeOut("fast");
		 this.openedSubMenu = null;
	}
};
nwaMenus.prototype.activateMenu = function(menu)
{
	for (var i=0; i<this.menus.length; i++)
	{
		if (this.menus[i].menuElementId == menu)
		{
			this.showMenu('#'+this.menus[i].menuId, '#'+this.menus[i].menuElementId);
		}
		break;
	}
};
nwaMenus.prototype.hasActiveMenu = function()
{
	var active = false;
	if (this.activeMenu != null)
	{
		active = true;
	}
	return active;
};
nwaMenus.prototype.hideActiveMenu = function(timer)
{
	//console.log("timer: "+timer);
	if (this.activeMenu != null)
	{
		var mId = this.getMenuElementID(this.activeMenu);

		//var mDiv = this.activeMenu.substring(0, this.activeMenu.indexOf("Menu"));
		clearTimeout($(mId).attr('timeOut'));
		$(this.activeMenu).find("ul").each(function()
		{
			var thisJQobj = $(this).css({ visibility: 'hidden' });
			if ($.browser.msie)
			{
				/* removes bgiframe if present */
				thisJQobj.find("iframe", this).remove();
			}
		});
		if (timer == null)
		{
			//$(this.activeMenu).css({ visibility: 'hidden' });
			var thActiveMenu = this.activeMenu;
			this.activeMenu = null;			
			$(thActiveMenu).fadeOut(100, function() {
				if ($.browser.msie)
				{
					$('iframe.bgiframe', thActiveMenu).remove();
				}			
			});
			
		}
		else
		{
			var menus = this;
			//$(mId).attr({ timeOut: setTimeout( function () {$('.nwaMenu').css({ visibility: 'hidden'})}, timer) } );
			$(mId).attr({ timeOut: setTimeout( function () {menus.hideActiveMenu()}, timer) } );
			//$("div[id='"+mId.replace(/#/,"")+"']").attr("class","tab");
			//$(mId).attr({ timeOut: setTimeout( function () {$('.nwaMenu').fadeOut('normal') }, timer) } );
		}
		if (mId.match("active") >=0) {
			$("div[class='tab_hover']").attr("class","tab");
		}
		else {
			$("div[class='tab_hover']").attr("class","tab_active");
		}
		
		//$("div[class='tab_hover']").toggleClass("tab_hover");
		//$("div[class='tab_hover']").attr("class","tab");
		//$("div:has()").addClass("test");
		//if (typeof(this.observedCallback) == 'function') {
		//   this.observedCallback;
		//}
	}
};
nwaMenus.prototype.menuClick = function(href, target)
{
    if (href != null)
    {
        if (target != null)
        {
            window.open(href, target);
        }
        else
        {
            document.location = href;
        }
    }
	return false;
};
// this function returns the x, y position
// works in IE and Mozilla.
nwaMenus.prototype.getElementPosition = function(element){
	var x=0,y=0;

	if (typeof element == "string")
		element = this.getElement(element);

	while (element!=null){
		x+=element.offsetLeft-element.scrollLeft;
		y+=element.offsetTop-element.scrollTop;
		element=element.offsetParent;
	}
	//todo:  need to adjust x with how many pixles they 
	// have scrolled.
	var a = this.getScrollXY();
	x+=a[0];
	y+=a[1];
	return {x:x,y:y};
};
nwaMenus.prototype.getScrollXY = function() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
};
// Browser compatible element getter
nwaMenus.prototype.getElement = function(id){
	if (id.indexOf('#') != -1)
		id = id.substr(1, id.length);
	if (document.getElementById(id))
		return document.getElementById(id);
	if (document.all)
		return document.all[id];
	return null;
};
