$(function()
	{
		
		var SHOW_TIME = 400;
		var HIDE_TIME = 300;
		
		var activeSubnav;
		var originalNavHeight;
		
		function hideNav($subnav, instant)
		{
			$subnav.css('zIndex', 1);
			var destProperties = {
				'height': '0px',
				'paddingTop': '0em',
				'marginBottom': '0em'
			};
			if (instant) {
				$subnav.stop().css($.extend(destProperties, {display: 'none'}));
			} else {
				$subnav.parent().parent().stop().animate(
					{
						'height': originalNavHeight + 'px'
					},
					{
						queue: false,
						duration: HIDE_TIME
					}
				);
				$subnav.stop().animate(
					destProperties,
					{
						queue: false,
						duration: HIDE_TIME,
						complete: function()
						{
							if ($subnav.height() == 0) {
								$subnav.css(
									{
									display: 'none'
									}
								);
							}
						}
					}
				);
			}
		}
		
		function showNav($subnav)
		{
			var destHeight = $subnav.data('originalHeight') + originalNavHeight + 20;
			//alert($subnav.data('originalHeight') + ',' + originalNavHeight + ',' + 20);
			$subnav.parent().parent().animate(
				{
					'height': destHeight + 'px'
				},
				{
					queue: false,
					duration: SHOW_TIME
				}
			);
			$subnav.css(
				{
					'zIndex': '2'
				}
			).animate(
				{
					'height': $subnav.data('originalHeight') + 'px',
					'paddingTop': '1em',
					'marginBottom': '1em'
				},
				{
					queue: false,
					duration: SHOW_TIME
				}
			);
			activeSubnav = $subnav;
		}
		
		function hideActiveSubnav()
		{
			if (activeSubnav) {
				hideNav(activeSubnav, false);
				// clear the auto out from last menu...
				var ob = activeSubnav.parent()[0];
				ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
				ob.hoverIntent_s = 0;
			}
		}
		
		$('ul.horizontal_navigation>li').each(
			function(i)
			{
				var $topLink = $('>a', this);
				var $this = $(this);
				if ($this.is('.subnav')) {
					$topLink.bind(
						'click',
						function()
						{
							return false;
						}
					);
					var $subnav = $('>ul', this);
					
					var w = $topLink.outerWidth() + 1;
					$this.css('width', w + 'px');
					
					var maxWidth = 0;
					$subnav.css(
						{
							'width': '400px'
						}
					).data(
						'originalHeight',
						$subnav.height()
					).find('>li>a').each(
						function(i)
						{
							var $thisA = $(this);
							$thisA.html($thisA.text().split(' ').join('&nbsp;'));
							maxWidth = Math.max(maxWidth, $(this).outerWidth());
						}
					).end().css(
						{
							'width': (maxWidth + 6) + 'px',
							'height': '0px',
							'paddingTop': '0em',
							'marginBottom': '0em',
							'display': 'none'
						}
					)
					
					
					$this.hoverIntent(
						{
							sensitivity: 3,    
							interval: 0,    
							over: function()
							{
								//console.log('over', $('>a', this).text(), 'active= ', activeSubnav ? activeSubnav.parent().find('>a').text() : '');
								hideActiveSubnav();
								showNav($('>ul', this));
							},    
							timeout: 500,    
							out: function()
							{
								//console.log('out', $('>a', this).text(), 'active= ', activeSubnav ? activeSubnav.parent().find('>a').text() : '');
								var $subnav = $('>ul', this);
								hideNav($subnav);
								if (activeSubnav[0] == $subnav[0]) {
									activeSubnav = null;
								}
							}  
						}
					);
				} else {
					$topLink.bind(
						'mouseover',
						function(e)
						{
							hideActiveSubnav();
						}
					);
				}
				
			}
		);
		
		// set the height of the nav container explicitly so that the rest of the content doesn't get pushed down when the menu expands...
		var $primaryNavContainer = $('#primary_navigation_container');
		
		// FIXME: IE can't get the height correctly :(
		var primaryNavHeight = 105;//$primaryNavContainer.height();
		
		/*
		$primaryNavContainer.css(
			{
				height: primaryNavHeight + 'px'
			}
		)
		*/
		originalNavHeight = primaryNavHeight - $('ul.horizontal_navigation').offset().top;
		
		$('#primary').css(
			{
				'height': originalNavHeight + 'px'
			}
		);
		
		$('.loading').removeClass('loading');
	}
);

function doSFIR(colour)
{
	// init sIFR fonts
	$('h1, h2.replace').each(
		function()
		{
			var $h1 = $(this);
			$h1.text($h1.text().toUpperCase());
		}
	);
		
	var garda = { src: '/images/garda.swf' };
	sIFR.useStyleCheck = true;
	sIFR.activate(garda);
	sIFR.replace(garda, {
	  selector: 'h1, h2.replace',
	  css: '.sIFR-root { color: ' + colour + '; }',
	  wmode: 'transparent',
	  forceSingleLine: true
	});
}
