$(function()
{ 
	/**
	 * IE displays tooltips for alt text
	 * so we'll dynamically add an empty title 
	 * attribute to prevent this
	 *
	 * Horrible thing to do, but the tooltips are a pain with the nav
	 */
	$('#flash')
		.find('img')
		.attr('title', '');
	
	/**
	 * Generate a hidden span with hover state, ready for the mouse over
	 */
	$('#nav a').each(
		function()
		{
			var bg = $(this).css('background-image');
			//alert(bg);
			var span = $('<span></span>')
							.appendTo($(this))
							.css({
								backgroundImage: bg,
								cursor: 'pointer'
							})
							.width($(this).width())
							.hide();
			
			if(!$(this).hasClass('active'))
			{
				$(this).addClass('js');
			}
		}
	);
	
	/**
	 * Fade span with mouseover event
	 */
	$('#nav a').hover(
		function()
		{
			if(!$(this).hasClass('active'))
			{
				$('span', this).fadeIn(500);
			}
		},
		function()
		{
			$('span', this).fadeOut(800);
		}
	);
});



/**
 *	Create the 'active/hover' line
 */
$(function()
{
	var a = $('#nav>ul>li a.active');
	
	if(!a.length)
	{
		/*
		 * If there are no active links, default the line indicator
		 * to the first navigation item
		 */
		a = $('#nav>ul>li a:first');
	}
	
	/* 5px gap between bar and anchor */
	var gap_px = 5;
	
	var pos = a.offset();
	
	var line = $('<div />')
					.attr('id', 'nav_line')
					.appendTo($('body'));

	$('<div />').appendTo(line);
	
	var lineHeightCenter = line.outerHeight() / 2;
	var anchorHeightCenter = a.parent().outerHeight() / 2;

//	console.log(Math.floor((anchorHeightCenter - lineHeightCenter) + pos.top));
	$('body').animate({borderBottom: '0'}, 1000, function() {
		pos = a.offset();
				line.css({
					top: Math.floor((anchorHeightCenter - lineHeightCenter) + pos.top),
					width: pos.left - gap_px
				});
	});
	
	$(window).resize(function() {
		pos = a.offset();
		line.css({
			top: Math.floor((anchorHeightCenter - lineHeightCenter) + pos.top),
			width: pos.left - gap_px
		});
		resizeW();
	});
	
	var mouse_safe = false;
	
	$('#nav>ul>li>a')
		.mouseenter(
			function()
			{
				if(mouse_safe === false)
				{
					var active_top = $(this).parent().outerHeight() / 2;
					var new_pos = $(this).offset();
					line.stop();
					line.animate({width:0}, 200, function() {
						line.css({
								top: Math.floor((active_top - lineHeightCenter) + new_pos.top)
							})
							.animate({width: pos.left - gap_px}, {queue: false, duration: 300}, function() {mouse_safe=true;});
		
					});
				}
				return;
			}
		)
		.parent()
		.parent()
		.parent()
		.find('ul')
		.mouseleave(
			function(e)
			{
				//if(mouse_safe===true)
				//{
				line.animate({
					top: Math.floor((anchorHeightCenter - lineHeightCenter) + pos.top)
				}, {queue: false, duration: 500}, function() {mouse_safe=false;});
				//}
				
			}
		);
});

/**
 * Image preloader function
 */
jQuery.preloadBackgrounds = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img />").attr("src", arguments[i]);
	}
}
//$.preloadImages("image1.gif", "/path/to/image2.png", "some/image3.jpg");

/**
 * Footer nav preload
 */
$(function() {
	var src = false;
	$('#footer a').not(':last')
		.each(
		function() 
		{
			var _src = $(this).css('background-image');
			var new_src = _src.replace(/_off|url\(|\)/g, '');
			$.preloadBackgrounds(new_src);
		}
	);
	
	resizeW();
});

/*
 * Bodged to page centering etc behave naturally...
 * hmmmm
 */
function resizeW() {
	//$('#outer').css({marginTop: -95});
	return false;
	var outer = $('body #outer');
	var w_height = outer.height();
	var w_margin = w_height / 2;
	var window_height = $(window).height();
	
	outer.wrap('<div id="centerPage"></div>');
//	alert(w_height);
	
	$('#centerPage').css({marginTop:-95});
	return false; // remove this, just bypassing the function
	
	if ($(window).height() > (w_height - 190))
	{
		
		/*
		 * Center the page
		 */
		$('#centerPage').css({
			position: 'absolute',
			top: '50%',
			left: 0,
			width: '100%',
			overflow: 'visible',
			height: outer.height(),
			marginTop: (w_height) / 2 * -1
		});
		
		$('html').css({
			overflow: 'hidden'
		});
	}
	else
	{
		console.log('fixed');
		/*
		 * fix contents to the top of the page
		 */
		$('#centerPage').css({
			position: 'relative',
			top: 0,
			left: 0,
			width: '100%',
			marginTop: -95
			
		});
		
		$('html').css({
			overflow: 'auto'
		});
	}
}

$(window).load(function() {
	setTimeout("resizeW()", 500);
});




/*
 * Lifestyle -> Situation gallery TV 'slideshow'
 */
$(function() {
	var billboard = $('#content.situation_gallery .billboard');
	var imageCount = $('.image img', billboard).length;
	var i = 1;
	
	billboard.find('.count').text('1 of ' + imageCount);
	
	billboard
		.find('.image img')
		.not(':first')
		.each(function() {
			$(this)
			.parent()
			.hide();
		});
		
	billboard
		.find('.next a')
		.click(function(e) 
		{
			e.preventDefault();
			var curImg = billboard.find('.image img:visible').parent();
			
			curImg.fadeOut(500, function() 
			{
				i = i < imageCount ? ++i : i=1;
				billboard.find('.count').text(i +' of ' + imageCount);
				
				if(!curImg.next().length)
				{
					return billboard.find('img:first').parent().fadeIn(500);
				}
				return curImg.next().fadeIn(500);
			});
		});
	
	billboard
		.find('.prev a')
		.click(function(e) 
		{
			e.preventDefault();
			var curImg = billboard.find('.image img:visible').parent();
			
			curImg.fadeOut(500, function() 
			{
				i = i > 1 ? --i : i=imageCount;
				billboard.find('.count').text(i +' of ' + imageCount);
				
				if(!curImg.prev().length)
				{
					return billboard.find('img:last').parent().fadeIn(500);
				}
				return curImg.prev().fadeIn(500);
			});
		});
		
	billboard
		.find('.enlarge a')
		.click(function(e)
		{
			e.preventDefault();
			$('.image a img:visible')
				.parent()
				.click();
		});
	
	//$('.image img a', billboard).attr('rel', 'prettyPhoto[gallery]');
});

$(function()
{	$('#content.conservation .content>div h2').css({cursor: 'pointer'});
	$('#content.conservation .content>div h2')
		.click(
			function()
			{
				var p = $(this).parent().parent();
				p.find('div.item').hide();
				
				p.find('h2').css({borderTop: '0'}).parent().css({paddingBottom: 0});
				p.find('div.item').css({paddingBottom:0});
				
				var c = $(this).parent().attr('class');
				//console.log(c);
				$('#content.conservation ul.nav li a').removeClass('active');
				$('#content.conservation ul.nav li.'+c+' a').addClass('active');
				
				if(!p.find('div.item').is(':visible'))
				{
					$(this).next().fadeIn(300);
					$(this).parent().next().find('h2').css({borderTop: '1px solid #ccc'});
					$(this).parent().css({paddingBottom: '16px'});
				}
				else
				{
					p.find('.item').hide();
				}
			}
		);
		
	$('#content.conservation ul.nav li a')
		.click(function(e) {
			e.preventDefault();
			var c = $(this).parent().attr('class');
			
			$('#content.conservation .content .'+c+' h2').click();
			
			$(this).parent().parent().find('a').removeClass('active');
			$(this).addClass('active');
		});
		
	$('#content.conservation ul.nav li a:first').click();
});


/*
 * Intro Message overlay stuff
 */
$(document).ready(function() {
	$('#quiver_notification').hide();
});

//trigger the overlay
/*$(function() {
	notification_message();
});

function notification_message()
{
	var notification_overlay = $('#quiver_notification');
	
	if (notification_overlay.length)
	{
		$('<img src="images/overlay/bottle.gif" />').appendTo(notification_overlay);
		var notification_underlay = $('<div></div>').attr('id', 'quiver_notification_overlay');
		
		var close = $('<div></div>').addClass('close_button').appendTo(notification_overlay);
		
		notification_underlay.css({
			position: 'absolute',
			width: '100%',
			height: $(document).height(),
			backgroundColor: '#000',
			display: 'none',
			top: '0px',
			left: '0px',
			zIndex: '10050'
		}).insertBefore(notification_overlay);
		
		notification_overlay.addClass('js');
		
		if($.browser.msie && parseInt($.browser.version) < 7)
		{
			notification_overlay.css({
				position: 'absolute'
			});
		}
		
		notification_underlay.css({opacity: 0}).show().fadeTo(500, 0.9, display_notification_overlay);
	}
	
	function display_notification_overlay()
	{
		setTimeout("$('#quiver_notification').fadeIn(400)", 100);
		
		close.click(hideOverlay);
		//notification_underlay.click(hideOverlay);
	}
	
	function hideOverlay()
	{
		notification_overlay
			.fadeOut(300, function() {
				
				//trigger restart of the flash movie
				$('#flash').height($('#flash').height());
				$('#flash object').hide();
				
				notification_underlay.fadeOut(300, function(){$('#flash object').show();});
			});
	}
}

$(window).resize(function() {
	$('notification_underlay').height($(document).height() + 'px');
});*/

$(function() {
	if($.browser.msie && parseInt($.browser.version) < 7)
	{
		$('.alphaOverlay .top, .alphaOverlay .bot').css({fontSize: '1px'});
		$('.alphaOverlay .background').css({width:'100%', height: $('.alphaOverlay .inner').height() + 'px'});
		$('.alphaOverlay .inner').css({position: 'relative'});
		
		// quick fix for conservation page
		$('#content.conservation .inner').css({top:'150px'});
		if($('#content.conservation .inner').length > 0)
		{
			$('#wrapper').css({
				height: '650px'
			});
		}
	}
});