$(function(){	// shorthand for $(document).ready() BTW
	initialiseCorners();
	initialiseSearch();
	/*initialiseColumns();*/
	resizeNavBoxes();
	initialiseMainNavHover();
});

function initialiseCorners() {
	$('div.rounded').corner({
		tl: { radius: 10 },
		tr: { radius: 10 },
		bl: { radius: 10 },
		br: { radius: 10 },
		antiAlias: true,
		autoPad: true
	});
}

function initialiseMainNavHover() {
	/*
	   Slightly ugly image flip; take navigation.*.norm.gif and flip with navigation.*.hover.gif
	*/
	hoverOn = function() {
		$(this).children('a').children('img').each(function() {
			$(this).attr('src', $(this).attr('src').replace('norm.gif', 'hover.gif'));
		});
	}
	hoverOff = function() {
		$(this).children('a').children('img').each(function() {
			$(this).attr('src', $(this).attr('src').replace('hover.gif', 'norm.gif'));
		});
	}
	$('#topnav li.flyout').hover(hoverOn, hoverOff);
}

function initialiseSearch() {
	$('#search-box').focus(function() {
		if (this.value == 'Search') {
			this.value = '';
			this.style.color = '#5F676D';
		}
	});
	$('#search-box').blur(function() {
		if (this.value == '') {
			this.value = 'Search';
			this.style.color = '#A0ADB6';
		}
	});
}

function initialiseColumns() {
	$('div.columns').columnize();
}

function resizeNavBoxes() {
	$('div.menu').each(function() {
		var columns = $(this).children('ul.sub_nav').size();
		if(columns > 1) {
			var new_width = parseInt($(this).css('min-width')) * columns;
			$(this).css('min-width', new_width);
		}
	});
}


/* Hack for IE 6 and below.
*  Inline colours are horrendous, but the stylesheet is being ignored.
*/
function ieMainNav() {
	/* TODO: Background colour that doesn't make the nav hard to read;
	   We can't use the image flips because of the transparency business.
	   OR TODO: Used .gifs already.
	   The -trans business is for the IE7-js transparency bugfix.
	   ("IE7" was a cute name for a javascript library before IE7 was released. Now it's just confusing.)
	*/
	$("#topnav li.flyout").hover(function(){
    	$(this).className += "_over over";
		$(this).css('background-color', '#527300');
	}, function(){
		$(this).className = this.className.replace("_over over", "");
		$(this).css('background-color', '#cadb2a');
	});
}
