var backgrounds = new Array(
	'BKG-DDW2.jpg',
	'BKG-DSC00307.jpg',
	'BKG-DSC_0046.jpg',
	'BKG-DSC_0249.jpg',
	'BKG-DSC_0395.jpg',
	'BKG-IMG_0395.jpg',
	'BKG-IMG_0529.jpg',
	'BKG-IMG_0546.jpg',
	'BKG-Ian-Kellett-Imagery-7713.jpg',
	'BKG-M0011091.jpg',
	'BKG-M0011112.jpg',
	'BKG-M0011113.jpg',
	'layered-BKG.jpg'
);
var secondaryNavOpen = false;
// This code sets a random background image:
$(document).ready(function() {
	$.extend({
		cookie: function(name,value) {
			if(value==null) {
				if (document.cookie.length>0) {
					var results = document.cookie.match('(^|;) ?'+name+'=([^;]*)(;|$)');
					if(results) return (unescape(results[2]));
  					else return null;
				}
			} else {
				//var exdate=new Date();
				//exdate.setDate(exdate.getDate()+1000); // Set the cookie for 1000 days!
				//document.cookie=name+'='+escape(value)+';path=/;expires='+exdate.toUTCString();
				// Thisis only for the session:
				document.cookie=name+'='+escape(value)+';path=/';
			}
		}
	});
	// They changed their minds - always load new images now:
	//var noCookie = (/nocookie/.test(window.location.search.substring(1)));
	var noCookie = true;
	var img = $.cookie('background-image');
	if(img==null || noCookie) {
		var r = Math.floor(Math.random()*backgrounds.length);
		$.cookie('background-image',backgrounds[r]);
		img = backgrounds[r];
	}
	// This code is if we use the background-size:cover method:
	//var url = 'url('+theme_directory+'/interface/gfx/backgrounds/'+img+')';
	//$('body').css('background-image',url);
	// This approach supports more browsers:
	var viewPortWidth = $(window).width();
	var viewPortHeight = $(window).height();
	if(viewPortWidth>1900) $('.bg img').attr('width',viewPortWidth);
	if(viewPortHeight>1200) $('.bg img').attr('height',viewPortHeight);
	var url = theme_directory+'/interface/gfx/backgrounds/'+img;
	$('.bg img').attr('src',url);
});
// This code sets up the navigation states:
$(document).ready(function() {
	// Slide down any secondary nav (if any):
	var skipSlide = false;
	var prev_page_item = $.cookie('prev_page_item');
	if(prev_page_item!=null) {
		var curr_page_item = $('#navigation ul li.current_page_item').attr('id');
		if(prev_page_item==curr_page_item) {
			skipSlide = true;
		} else if(prev_page_item!=null) {
			skipSlide = $('#navigation ul li.current_page_item').hasClass('parentOf'+prev_page_item);
		}
	}
	if(!skipSlide && $('#navigation ul li.parent').hasClass('current_page_item')) {
		$('#navigation ul li.parent_current').slideDown('slow', function() {});
		var id = $('#navigation ul li.current_page_item').attr('id');
		$('#navigation').attr('class',id);
	} else {
		var children = $('#navigation ul li.parent_current');
		if(children!=null && children.length>0) {
			children.css('display','block');
			var id = $('#navigation ul li.current_page_item').attr('id');
			$('#navigation').attr('class',id);
		}
	}
	// This code sets the current navigation connector location:
	var p = $('#navigation ul li.current_page_item').offset();
	if(p!=null) {
		$('#connectorCurrent').css('top',p.top);
		$('#connectorCurrent').css('display','block');
	}
	// This code deals with navigation hover states:
	$('#navigation ul li').each(function(index) {
		$(this).hover(
			function () {
				$(this).addClass("hover");
				// Only show connector if there is content:
				var e = $('#content');
				if(e!=null && e.length>0) {
					var p = $(this).offset();
					$('#connectorHover').css('top',p.top);
					$('#connectorHover').css('display','block');
				}
			},
			function () {
				$(this).removeClass("hover");
				$('#connectorHover').css('display','none');
			}
		);
		$(this).click(
			function () {
				var link = $('a',this);
				goto(link);
			}
		);
	});
});
// This adjusts the content area size to fit:
$(document).ready(function() {
	var viewPortHeight = $(window).height();
	var ch = viewPortHeight - 120;
	$('#content').css('height',ch+'px');
	var csh = viewPortHeight - 151;
	$('#contentScroll').css('height',csh+'px');
	$('#contentScroll').fadeIn('slow');
});
// This resize content area when window resized:
$(window).resize(function() {
	var viewPortWidth = $(window).width();
	var viewPortHeight = $(window).height();
	// Resize content area is required:
	var ch = viewPortHeight - 120;
	$('#content').css('height',ch+'px');
	var csh = viewPortHeight - 151;
	$('#contentScroll').css('height',csh+'px');
	// Resize background if required:
	if(viewPortWidth>1900) $('.bg img').attr('width',viewPortWidth);
	if(viewPortHeight>1200) $('.bg img').attr('height',viewPortHeight);
});
// Animate closing menu if required and load URL:
function goto(link) {
	var url = $(link).attr('id');
	var curr_id = $('#navigation ul li.current_page_item').attr('id');
	$.cookie('prev_page_item',curr_id);
	var id = $('#navigation').attr('class');
	var skipSlide = $(link).parent().hasClass('parent_current') || $(link).parent().hasClass('current_section');
	if(!skipSlide && id!=null && id.length>0) {
		$('#connectorCurrent').css('display','none');
		$('#navigation ul li.parent_current').slideUp('slow', function() {
			window.location = url;
		});
	} else {
		window.location = url;
	}
	return false;
}
