function sfHover()
{
	 var timeout = 800;
	 var cssClass = "hover";
	 var queue = [];
	 var reCSS = new RegExp("\\b" + cssClass + "\\b");
	 var sfEls = $j('div').filter('.menu-a').find('td');
	 for (var i=0; i<sfEls.length; i++)
	 {

		 
			// mouseover and mouseout handlers for regular mouse based interface.
			sfEls[i].onmouseover = function()
			{			
				queueFlush();
				if (!($j(this).hasClass('active'))) this.className += " " + cssClass;
			}

			sfEls[i].onmouseout = function()
			{
				 queue.push([setTimeout(queueTimeout, timeout), this]);
			}

			// focus and blur handlers for keyboard based navigation.
			sfEls[i].onfocus = function()
			{
				queueFlush();
				if (!($j(this).hasClass('active')))this.className += " " + cssClass;
			}

			 sfEls[i].onblur = function()
			 {
				 queue.push([setTimeout(queueTimeout, timeout), this]);
			 }

			 // click event handler needed for tablet type interfaces (e.g. Apple iPhone).
			 sfEls[i].onclick = function(e)
			 {
				if (this.className.search(reCSS) == -1)
				{
					// CSS not set, so clear all sibling (and decendants) menus, and then set CSS on this menu…
					var elems = this.parentNode.getElementsByTagName("td");

					for (var i=0; i<elems.length; i++)
					{
						elems[i].className = elems[i].className.replace(reCSS, "");
					}

					if (!($j(this).hasClass('active'))) this.className += " " + cssClass;
				}
				else
				{
					// CSS already set, so clear all decendant menus and then this menu…
					var elems = this.getElementsByTagName("td");
					for (var i=0; i<elems.length; i++)
					{
						elems[i].className = elems[i].className.replace(reCSS, "");
					}
					this.className = this.className.replace(reCSS, "");
				}

				if (e && e.stopPropagation)
				e.stopPropagation();
				else
				window.event.cancelBubble = true;
			 }
	 }

	 queueFlush = function ()
	 {
		 while (queue.length)
		 {
			clearTimeout(queue[0][0]);
			 queueTimeout();
		 }
	 }

	 queueTimeout = function()
	 {
		 if (queue.length)
		 {
			 var el = queue.shift()[1];
			 el.className = el.className.replace(reCSS, "");
		 }
	 }
}

function toggleMenuD()
{
	if ($j(this).attr('class').indexOf('active') >= 0)
	{
		$j(this).removeClass('active');
		$j(this).next('ul').hide();
	}
	else
	{
		$j(this).addClass('active');
		$j(this).next('ul').show();
	}
}


function qInit()
{
	/* Init Navigation */
	$j('dl').filter('.menu-c').children('dd').filter('.haschild').children('ul').hide();
	sfHover();
	$j('dl').filter('.menu-c').children('dd').filter('.haschild').children('a').attr('href','javascript:void(0)');
	$j('dl').filter('.menu-c').children('dd').filter('.haschild').children('a').click(toggleMenuD);
	$j('dl').filter('.menu-c').children('dd').filter('.haschild').children('a').filter('.active').unbind('click');
	$j('dl').filter('.menu-c').children('dd').filter('.haschild').children('a').filter('.active').next().show();
	
	layoutTweaks();
}


/* Handles the inclusion external stylesheets into the head */
/* -------------------------------------------------------- */
function includeCSS(file)
{
	$j('head').append('<link href="'+file+'" type="text/css" rel="stylesheet" media="screen" />');
}
/* -------------------------------------------------------- */

/* Used to remove the margin-top of the first child in a parent container */
/* ---------------------------------------------------------------------- */
function resetMargin()
{
	$j($j(this).children()[0]).css('margin-top','0');
}
/* ---------------------------------------------------------------------- */

/* Used to handle the even distribution of widths across sibling elements */
/* ---------------------------------------------------------------------- */
function setItemWidth()
{
	var setWidth = Math.floor(100/($j(this).siblings().length+1));
	$j(this).width(setWidth+'%')
}
/* ---------------------------------------------------------------------- */


/* Used to handle match class elements to handle the matching of heights */
/* --------------------------------------------------------------------- */
var noOfMatchHeightTypes = [];

function initResizeHeight()
{
	$j("*[@class^='matchH']").each(setNoMatchGroups);
	$j.each(noOfMatchHeightTypes, function()
	{
		var tallest = 0;
		$j('.matchH'+this).each(function(i)
		{
			if ($j(this).height() > tallest) tallest = $j(this).height();
		});
		
		$j('.matchH'+this).height(tallest);
	});
}

function setNoMatchGroups()
{
	var num = $j(this).attr('class').split('matchH')[1];
	if ( noOfMatchHeightTypes.toString().match(num) == null)
		noOfMatchHeightTypes.push(num);
}
/* -------------------------------------------------------------------------------- */


function layoutTweaks()
{
	/* Adjust navigation to content spacing based on B level active state */
	if (!($j('.menu-b').parent().hasClass('active')))
	{
		$j('.navigation').css('margin','0 0 0 0');
	}
	
	/* Ensure that first element withing padded containers do not contain a margin top */
	$j('.content').each(resetMargin);
	$j('.form fieldset').each(resetMargin);
	$j('.panel').each(resetMargin);
	$j('.panelsTable').each(resetMargin);
	$j('.important').each(resetMargin);
	
	/* Ensure that the first panel within a panel group does not contain a border-top */
	$j('.panels').each(function(i)
	{
		$j($j(this).find('.panel')[0]).css('border-top','0px none');
	});
	
	/* Adjust Link Collections width in accordance to the column numbers */
	$j('.linkCollection').children().each(setItemWidth);
	
	/* Table Caption */	
	$j('table').children('caption').parent().css('margin-top','0');
	
	/* IE6/7 has issues with ABBR so this is the work around */
	if($j.browser.version.substr(0,3) <= '7.0' && $j.browser.msie)
	{
		$j('span').each(function(i)
		{
			if ($j(this).html().indexOf('*') > -1)
			{
				$j(this).css({'font-weight':'normal','color':'#c10000','font-size':'11px'});
			}
		});
	}
	
	/* Insert Error style */
	$j('.inputError').wrapInner('<span></span>');
	
	/* Matches the tallest height of a collection of items that require a matched height */
	initResizeHeight();
	
	/* Adjust Plan, Book, Fly */
	$j('.menu-a td').children('a').each(function(i)
	 {
		if ( $j(this).html().indexOf('Book') > -1 || $j(this).html().indexOf('Plan') > -1 || $j(this).html().indexOf('Home') > -1 )
		{
			$j(this).addClass('highlight4');
		}
		else if ( $j(this).html().indexOf('Fly') > -1 )
		{
			$j(this).addClass('highlight3');
		}
	 });
	
	/* Reveal RegionSelector */
	$j('#regionSelectionHomePage').removeClass('hidden');
}

$j(document).ready(qInit);