//support existing object...
function nwaMenus(options)
{
	//return the jquery plug-in
	var m = jQuery.fn.nwaMenus(options);
	return m.nwaMenus;
};
//
// create closure
//
(function($) {
	//
	// plugin definition
	//
	// Note: Plugn functions are in alpha order - please retain.
	//
	$.fn.nwaMenus = function(options)
	{
		// build main options
		secret.options = jQuery.extend({
			headerBlock: 'nwaHeader',
			subMenuOffset: 0,
			preLoadAll: false,
			hideDelay: 550
		}, options);
		
		//secret.mySelf = this.nwaMenus;
		return this;	
	}
	//add method
	$.fn.nwaMenus.add = function(url, menuElementId, menuId, rl)
	{
		//secret.debug(url);
		//secret.debug(secret.menus);
		secret.menus.push(
			{
				"url": url,
				"menuElementId": menuElementId,
				"menuId": menuId,
				"side": rl,
				"moved": false
			});
	};  
	//load method
	$.fn.nwaMenus.load = function()
	{
		//secret.debug("options: "+this.preLoadAll);
		//Need to load bgiframe.js if is IE...
		if (jQuery.browser.msie)
		{
			jQuery.getScript("/includes/js/jquery.bgiframe.js");
		}
	    for (var i=0; i<secret.menus.length; i++)
	    {
	        var menuId = '#'+secret.menus[i].menuId;
	        //add hover event for menu...
	        secret.addMenuHoverEvent(menuId, i);
	        if (secret.options.preLoadAll)
	        {
	            secret.call(i, secret.options.preLoadAll);
	        }
	    }
		jQuery(window).bind("resize",
			function() {
				secret.hideActiveMenu();
			}
		);
	};
	//observe method
	$.fn.nwaMenus.observe = function(items)
	{
		var nwaMenus = this;
		for (var i=0; i < items.length; i++)
		{
			jQuery('#'+items[i]).bind("mouseover", function()
			{
				secret.hideActiveMenu(secret.options.hideDelay, true);
				//secret.debug("nwaMenus.observedCallback: "+nwaMenus.observedCallback);
				if (nwaMenus.ovservedCallback != null)
				{
					if (typeof(nwaMenus.observedCallback == "function")){
						nwaMenus.observedCallback();				
					}
				}
			});
		}
	};
	//call-back for observe methods
	$.fn.nwaMenus.observedCallback = null;	
	//
  	// private variables & functions
  	//
	// Note: fuction are in alpha order - please retain.
	//
	var secret = {
		//current active menu
    	activeMenu: null,
    	//array of menus that we have fetched
	    menus: [],
	    //what elements to observe
    	observed: [],
    	//options that were passed in
		options: null,
		//what sub-menu is open
    	openedSubMenu: null,

		//
		// function for adding menu hover event
		//
		addMenuHoverEvent: function(menuId, i)
		{
		            var menuElementId = '#'+secret.menus[i].menuElementId;
		            var side = secret.menus[i].side;
		            $(menuElementId).hover(
			            function(){
			                if (secret.activeMenu != null)
			                {
			                    var mId = secret.getMenuElementID(secret.menus.activeMenu);
			                    if ($(mId).attr('timeOut') != null)
			                    {
			                        clearTimeout($(mId).attr('timeOut'));
			                    }
			                }
			                var menuObj = $(menuId);
			                if (menuObj.get(0) == null)
			                {
			                    secret.call(i);
			                }
			                else
			                {
			                    if (secret.activeMenu != menuId)
			                    {
			                        secret.showMenu(menuId, menuElementId, side);
			                    }
			                }
			                secret.closeSubMenu();
			            },
			            function() {
			                secret.hideActiveMenu(0);
			            }
		            ); //end hover
		},
		// 
		// function to call via ajax the menu
		//
		call: function(i, isPreLoad)
		{
				var menuUrl = secret.menus[i].url;
				var menuElementId = '#'+secret.menus[i].menuElementId;
				var menuId = '#'+secret.menus[i].menuId;
				var side = secret.menus[i].side;
				if (isPreLoad == null)
				{
					isPreLoad = false;
				}
		        $.get(menuUrl,
		        function(menuDoc){
		            $('html > body').append(menuDoc);	
					if (isPreLoad != true)
					{
		            	secret.showMenu(menuId, menuElementId, side);
					}
		            $(menuId +" > li").each(function(i)
		                {
		                    var jObj = $(this);
		                    var url = this.getAttribute('url');
		                    var target = this.getAttribute('target');
		                    var subMenu = this.getAttribute('submenu');
							var dealsMenu = this.getAttribute('dealsMenu');
							var dealsSubMenu = this.getAttribute('dealsSubMenu');
		                    jObj.bind('click', function()
		                     {
		                         secret.menuClick(url, target, dealsMenu, dealsSubMenu);
		                     });
		                    var kids = jObj.children();
		                    var hasSubMenu = secret.hasSubMenu(kids);
		                    jObj.bind('mouseover', function()
		                      {
		                        if (secret.activeMenu != null)
		                        {
		                            var mId = secret.getMenuElementID(secret.activeMenu);
		                            var cleanedmId = mId.replace(/#/, "");
		                            $("div[@id='" + cleanedmId + "']").attr("class","tab_hover");
		                            clearTimeout($(mId).attr('timeOut'));
		                            if (secret.openedSubMenu != null)
		                            {
		                                var isMySubMenu = false;
		                                var openedSubMenuId = secret.openedSubMenu.getAttribute('id');
		                                for (var i=0; i<kids.length; i++)
		                                {
		                                    if (kids[i].nodeName == 'ul' || kids[i].nodeName == 'UL')
		                                    {
		                                                if(subMenu == openedSubMenuId)
		                                        {
		                                            isMySubMenu = true;
		                                            break;
		                                        }
		                                    }
		                                }
		                                if (!isMySubMenu)
		                                {
		                                    secret.closeSubMenu();
		                                }
		                            }
		                        }
		                      }); //end bind
		                    if (hasSubMenu)
		                    {
		                        jObj.bind('mouseover', function()
		                        {
		                                secret.moveSubMenuTo(this, menuElementId, subMenu, side);
		                        });
		                    }
		                });
		        }); //end .get(menuUrl)		
		},				
		//
		// function to close subMenu
		//
		closeSubMenu: function()
		{
			if (secret.openedSubMenu != null)
			{
				jQuery(secret.openedSubMenu).css({visibility: 'hidden'});
				 secret.openedSubMenu = null;
			}
		},
		//
		// function for debugging
		//
		debug: function debug($obj) {
		  if (window.console && window.console.log)
		    window.console.log('debug: ' + $obj);
		},
		//
		// function to return browser compatible element
		//
		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;
		},						
		//
		// function to get the element position and
		// return the x, y position
		// tested with IE and Mozilla.
		//
		getElementPosition: function(element){
			var x=0,y=0;		
			if (typeof element == "string")
				element = secret.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 = secret.getScrollXY();
			x+=a[0];
			y+=a[1];
			return {x:x,y:y};
		},
		// 
		// function to return array of [x,y] scroll
		//
		getScrollXY: function() {
		  var scrOfX = 0, scrOfY = 0;
		  if($.browser.safari) {
		     scrOfY = window.pageYOffset;
		     scrOfX = window.pageXOffset;
		  }
		  else 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 ];
		},		
		//
		// function to get the menu element ID
		//
		getMenuElementID: function(menuId)
		{
			var ele = null;
			if (menuId != null)
			{
				if (menuId.indexOf('#') != -1)
					menuId = menuId.substr(1, menuId.length);
				for (var i=0; i<secret.menus.length; i++)
				{
					if (secret.menus[i].menuId == menuId)
					{
						ele = '#'+secret.menus[i].menuElementId;
						break;
					}
				}
			}
			return ele;
		},			
		//
		// function to determine if there is a sub-menu
		//
		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;
		},		
		//
		// function to hide active menu
		//
		hideActiveMenu: function(timer)
		{
			//secret.debug("timer: "+timer+", activeMenu: "+secret.activeMenu);
			if (secret.activeMenu != null)
			{
				var mId = secret.getMenuElementID(secret.activeMenu);
				if (mId != null)
				{
					clearTimeout($(mId).attr('timeOut'));
				}
				jQuery(secret.activeMenu).find("ul").each(function()
				{
					var thisJQobj = jQuery(this).css({ visibility: 'hidden' });
					if (jQuery.browser.msie)
					{
						/* removes bgiframe if present */
						thisJQobj.find("iframe", this).remove();
					}
				});
				if (timer == null)
				{
					//$j(this.activeMenu).css({ visibility: 'hidden' });
					var theActiveMenu = secret.activeMenu;
					secret.activeMenu = null;
					jQuery(theActiveMenu).fadeOut(100, function() {
						if (jQuery.browser.msie)
						{
							jQuery('iframe.bgiframe', theActiveMenu).remove();
						}
					});
					//reset tab to initial state
					if (mId.match("active") >=0) {
						jQuery("div[@class='tab_hover']").attr("class","tab");
					}
					else {
						jQuery("div[@class='tab_hover']").attr("class","tab_active");
					}
		
				}
				else
				{
					jQuery(mId).attr({ timeOut: setTimeout( 
						function () 
						{
							secret.hideActiveMenu()
							//reset tab to initial state
							if (mId.match("active") >=0) {
								jQuery("div[@class='tab_hover']").attr("class","tab");
							}
							else {
								jQuery("div[@class='tab_hover']").attr("class","tab_active");
							}
						}, timer) } );
				}
			}
		},		
		//
		// function for click handler
		//
		menuClick: function(href, target, dealsMenu, dealsSubMenu)
		{
			//try { console.log("Entering menuClick function...");} catch(err) {}
		    if (href != null)
		    {
				//try { console.log("href is not equal to null");} catch(err) {}
		        if (target != null)
		        {
					//try { console.log("target is not equal to null");} catch(err) {}
		            window.open(href, target);
		        }
		        else
		        {
					if (isPromoLandingPage) {
						//try { console.log("isPromoLandingPage is true");} catch(err) {}
						if (dealsMenu == 'true') {
							//try { console.log("dealsMenu is true");} catch(err) {}
							if (dealsSubMenu == 'true') {
							//try { console.log("dealsSubMenu is true");} catch(err) {}
							}
							else {
								//try { console.log("dealsSubMenu is false");} catch(err) {}
								document.location = '/deals/'; 
							}
						}						
						else {
							//try { console.log("dealsMenu is false");} catch(err) {}
							document.location = href; 
						}
					}
					else {
					//try { console.log("isPromoLandingPage is false");} catch(err) {}
		            document.location = href;
					}
		        }
		    }
			//try { console.log("href is equal to null");} catch(err) {}
					if (isPromoLandingPage) {
						//try { console.log("isPromoLandingPage is true");} catch(err) {}
						if (dealsMenu == 'true') {
							//try { console.log("dealsMenu is true");} catch(err) {}
							if (dealsSubMenu == 'true') {
							//try { console.log("dealsSubMenu is true");} catch(err) {}
							}
							else {
								//try { console.log("dealsSubMenu is false");} catch(err) {}
							}
						}						
						else {
							//try { console.log("dealsMenu is false");} catch(err) {}							 
						}
					}			
			//return false;
		},
		//
		// function to move sub menu
		//
		moveSubMenuTo: function(ele, what, who, left)
		{
			if (typeof who == "string")
			{
				who = secret.getElement(who);
			}
			if (typeof what == "string")
			{
				what = secret.getElement(what);
			}
			if (left == "left")
			{
				left =  -1 * ($(ele).width()-(secret.options.subMenuOffset - "8"));
			}
			else if (left == "WPleft")
			{
				left =  -1 * ($(ele).width()-(secret.options.subMenuOffset + "5"));
			}	
			else if (left == "WPleftFrSp")
			{
				left =  -1 * ($(ele).width()-(secret.options.subMenuOffset + "65"));
			}			
			else
			{
				left =  $(ele).width()-(secret.options.subMenuOffset - "7");
			}
			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();
					$(who).css({ 'margin-top': -1 * eHeight});
		            //other effect cause text to jump ;*
		            $(who).css(cssHash).show();
				}
			}
			secret.openedSubMenu = who;
		},		
		//
		// function to show sub menu
		//
		showMenu: function(menuId, menuElementId, side)
		{
			//secret.debug("this.activeMenu: "+this.activeMenu+", menuId: "+menuId);
			if (secret.activeMenu != menuId)
			{
				secret.hideActiveMenu();
				secret.activeMenu = menuId;
				if (jQuery.browser.msie)
				{
					jQuery(secret.activeMenu).bgIframe();
				}
				var res = secret.getElementPosition(menuElementId);
				var mIdHeight = jQuery(menuElementId).height();
		                if (side == "left") {
		                        $(secret.activeMenu).css({  position: "absolute", left: res.x + "px", top: (res.y + mIdHeight)+"px" });
		                }
		                else if (side == "WPleft") {
		                        $(secret.activeMenu).css({  position: "absolute", left: res.x + "px", top: (res.y + mIdHeight)+"px" });
		                }
		                else if (side == "WPleftFrSp") {
		                        $(secret.activeMenu).css({  position: "absolute", left: res.x + "px", top: (res.y + mIdHeight)+"px" });
		                }						
		                else if (side == "right_ll") {
		                        $(secret.activeMenu).css({  position: "absolute", left: (res.x - "41" ) + "px", top: (res.y + mIdHeight)+"px" });
		                }
		                else {
		                        $(secret.activeMenu).css({  position: "absolute", left: (res.x - "55" ) + "px", top: (res.y + mIdHeight)+"px" });
		                }
		                //other effect cause text to jump ;*
		                $(secret.activeMenu).show();
			}
		}		
	}; //end secret  
//
// end of closure
//
})(jQuery);

// ORBITZ Edit - will never be on landing page, hard-code the following 2 methods to the correct www.nwa.com URL
var isPromoLandingPage = false;

function switchPromoTab(val) {
	if (val != null) {
		url = "http://www.nwa.com/deals/#tab-" + val;
		document.location = url;
	}
	else {
		document.location = "http://www.nwa.com/deals/";
	}
}

//for JpJp
function switchPromoTabJp(val) {
	if (val != null) {
			url = "http://www.nwa.com/asia/jp/deals/#tab-" + val;
			document.location = url;
	}
	else {
		document.location = "http://www.nwa.com/asia/jp/deals/";
	}
}
