/**
* Main navigation functionality
*
* @author          Taryn Stieger
* @modifiedby      $LastChangedBy: blundenr $
* @copyright       Copyright Flight Centre Ltd. All rights reserved.
* @version         $Revision: 1692 $
* @lastmodified    $Date: 2010-09-22 15:36:32 +1000 (Wed, 22 Sep 2010) $
* @requires        
*/

;FCCN.NAVIGATION =
{
		timer: 0,
		
		/**
		* prepare FCHK navigation script for use
		*/
		init: function() 
		{
				var self;
				var $thisElement;
				
				self = this;     
				this.setDefaultNav(); // set the nav based on the URL
				
				$('#topNav > ul > li > a').bind('mouseenter',function()
				{ 
						// only get anchors top level list items
						clearTimeout(self.timer);
						self.mainNavFocus(this);
				});
				
				$('#topNav > ul > li > a').bind('mouseleave',function()
				{ 
						// only get anchors top level list items
						$thisElement = $('#' + $(this).parent().attr('id') + ' > a'); // get the anchor corresponding to the current top level nav item
						self.timer = setTimeout(function()
						{ 
								self.dehighlightNavItem($thisElement);self.setDefaultNav(); 
						}, 2000); //wait two seconds before removing navigation highlight and returning to the default
				});
				
				$('#topNav > ul > li > ul').bind('mouseenter',function()
				{ 
						clearTimeout(self.timer);
						self.mainNavFocus(this);
				});
				
				$('#topNav > ul > li > ul').bind('mouseleave',function()
				{ 
						$thisElement = $('#' + $(this).parent().attr('id') + ' > a'); // get the anchor corresponding to the current top level nav item
						self.timer = setTimeout(function()
						{ 
								self.dehighlightNavItem($thisElement);
								self.setDefaultNav();
						}, 2000) ; //wait two seconds before removing navigation highlight and returning to the default
				});
				
				this.sectionNavFocus('#section-nav', window.location.pathname);
		},        

		/**
		* Removes the highlight from a top level navigation tab and hides the corresponding the sub navigation
		*/
		dehighlightNavItem: function($element) 
		{
				var mainNavId = $element.parent().attr('id');
				var $subNavUl = $('#' + mainNavId + ' > ul');
				$element.removeClass('main-nav-over');
				$subNavUl.hide();
		},

		/**
		* removes all navigation highlights and clears the navigation timer
		*/
		resetNav: function() 
		{
				var self, $elements, $element, currentElement;
				
				$elements = $('#topNav > ul > li'); // top level list items only
				
				if(typeof timer != 'undefined') //check timer has been set before clearing
				{
						clearTimeout(this.timer);
				}
				
				// deselect all links
				for (var i = 0, ii = $elements.length; i < ii; i++) 
				{
						currentElement = $elements[i];
						if((currentElement.nodeType == 1) && (currentElement.nodeName == 'LI')) 
						{
								$element = $('#' + currentElement.id + ' > a');
								this.dehighlightNavItem($element);
						}
				}
		},

		/**
		* highlights a top level navigation tab and shows the corresponding sub navigation
		*/
		highlightNavItem :function($element)
		{
				var mainNavId= $element.parent().attr('id');
				var $subNavUl = $('#' + mainNavId + ' > ul');
				
				$element.addClass('main-nav-over');
				$subNavUl.show();
		},
		
		highlightSubNavItem :function($element)
		{
				$element.addClass('main-sub-nav-over');
		},

		/**
		* selects which tab should be highlighted by default, depending on the URL
		*/
		highlightDefaultNav: function(elementId)
		{
				var $element;

				if (elementId.length > 0)
				{
						$element = $('#'+elementId+'>a');
						this.highlightNavItem($element);
				}
		},

		/**
		* prepares the clicked element for  navigation styling
		*/
		mainNavFocus: function(element) 
		{
				var parent, $element;
		
				parent = $(element).parent().attr('id');

				if (element === null) 
				{
						return;
				}
				$element = $('#' + parent + ' > a');
				this.resetNav();
				this.highlightNavItem($element);
		},

		/**
		* sets the highlight on the main navigation bar depending on the current URL
		*/
		setDefaultNav: function() 
		{
				// remember to fill this in with remaining urls
				var self, urlStr, $anchors, $topNav, $subNav, anchor, numMatches;
				
				self = this;
				urlStr = window.location.pathname;
				$anchors = $('#topNav a');
				numMatches = 0;
				
				$.each($anchors, function(i, anchor)
				{
						// match URL to navigation anchors and ignore all matches after the first
						if ((urlStr === anchor.pathname) && (numMatches === 0))
						{
								self.resetNav();
								$topNav = $('#'+$(anchor).closest('#topNav>ul>li').attr('id')+'>a'); // get the top level element to highlight
								$subNav = $(anchor.parentNode); // get the sub nav element to highlight
								self.highlightNavItem($topNav);
								self.highlightSubNavItem($subNav);
								numMatches++;
						}
				});
		},
				
				
		/**
		* highlights the current item in the section-specific navigation
		*/
		sectionNavFocus: function(element, path)
		{
				
				var self;
				var $anchors;
				var $parentElement; 
				var pathName;
				var regex;
			
				self = this;

				$anchors = $(element+' a');

				$.each($anchors, function(i,anchor)
				{
						pathName = anchor.pathname;
						
						//IE omits leading slash so we need to add it manually for absolute equality comparison
						regex = /^[\/]/;
						if (!(regex.test(pathName))) 
						{
								pathName = "\/"+pathName; 
						}	
						
						if (path === pathName)  
						{
								$parentElement = $(anchor.parentNode);
								$parentElement.addClass('current');
								self.openSubNav($parentElement);
						}
				});
		},
		
		/**
		* highlights the current sub-navigation item in the section-specific navigation
		*/
		openSubNav: function($element)
		{
				var currentElement;
				var children;
				for (var i=0, ii=$element.length; i<ii; i++)
				{
						currentElement = $element[i];
						children = currentElement.childNodes;
								
						// opens the selected element's sub-list if it is hidden
						for (var j=0, jj=children.length; j<jj; j++) 
						{
					
								if ((children[j].nodeType == 1) && (children[j].nodeName == 'UL'))
								{
										$(children[j]).addClass('open');
								}
						}
						
						// opens the selected element's parent list if it's hidden
						if ($(currentElement).parents('ul').length === 2)
						{
								$(currentElement.parentNode).addClass('open');
						}
				}
		}
};
