$('html').addClass('js');



$(document).delegate('#fancybox-img', 'dblclick', function(){
	img_source = $(this).attr('src');
	img_caption = $(this).attr('alt');
	var new_caption = prompt('Edit caption for ' + img_source, img_caption);
	if (new_caption!=null && new_caption!=img_caption) {
		$.post('/cgi-bin/captioneditor.py', {source: img_source, caption: new_caption}, function(data) {
				location.reload(true);});
		};
	});



$(document).ready(function(){

// Initialize Backgound Stretcher
$(document).bgStretcher({
	images: shuffle(['/images/bg01.jpg', '/images/bg02.jpg', '/images/bg03.jpg', '/images/bg04.jpg', '/images/bg05.jpg', '/images/bg06.jpg', '/images/bg07.jpg', '/images/bg08.jpg', '/images/bg09.jpg']),
	imageWidth: 1024,
	imageHeight: 768,
	nextSlideDelay: 4000
	});

//See if section page heading exists in the section links and
//if it does, we don't need it (we will have a highlighted link instead).
//Also, add detail to page's html title tag
var pagename = $('h1:first').html();
if ($('li a.wikilink:containsExact(' + pagename + ')').length > 0) {
	$('h1:first').hide();
	//$('title').append(': ' + pagename); //IE doesn't like this version up it!
	document.title += (': ' + pagename);
	};

$("a.fancy_gallery").fancybox({
	'transitionIn':	'elastic',
	'transitionOut':	'elastic',
	'titlePosition':	'over',
	'overlayOpacity':	0.9,
	'overlayColor':	'#333'
	});

$("a.iframe").fancybox({
	'transitionIn':	'elastic',
	'transitionOut':	'elastic',
	'width':		600,
	'height':		600,
	'overlayColor':	'#333'
	});	

$('#nav img').click(function() {
	$('#centrebox').fadeToggle('slow');
	});

$('#centrebox').dblclick(function() {
	$('#footer').slideToggle();
	});

if (/\.html$/.test(window.location.pathname))
	{
		$('a.wikilink[href="' + window.location.pathname + '"]').replaceWith(function() {
			return '<span class="current">' + $(this).html() + '</span>';
			});
	}
else
	{
		$('a.wikilink:first').replaceWith(function() {
			return '<span class="current">' + $(this).html() + '</span>';
			});
	};

var section=$('h2:first').html()
$('#nav a:containsExact(' + section + ')').replaceWith(function() {
	return '<span class="current">' + $(this).html() + '</span>';
	});

});



$(window).load(function(){
	$('#centrebox').fadeIn(1500);
	vertical_centre();
});



$(window).resize(vertical_centre);



function vertical_centre() {
	var docheight = $(window).height();
	var divheight = $('#centrebox').outerHeight();
	var divtop = Math.max(20, docheight/2 - divheight/2);
	$('#centrebox').css('top', divtop);
};

function shuffle(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
};

$.extend($.expr[':'],{
	containsExact: function(a,i,m){
		return $.trim(a.innerHTML.toLowerCase()) === m[3].toLowerCase();
	}
});




