function sfHover(nav) {
	var sfEls = document.getElementById('navigation').getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover = function () {
			this.className += (this.className == '') ? "sfhover" : " sfhover";
		}
		sfEls[i].onmouseout = function () {
			this.className = (this.className.indexOf(' ') != -1) ? this.className.replace(new RegExp(/ sfhover$/), '') : "";
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

function fixIESidebar() {
	document.body.className = (document.body.clientWidth < 874) ? 'ie6' : '';
}

if (window.attachEvent) window.attachEvent("onresize", fixIESidebar);

$(document).ready(function () {
	
	// Move flags to appear beneath first image in content div
	// $('ul#flags').insertAfter('#content a[@href*=gallery]');
	
	// Fix height of fade in sidebar
	$('#fade').height(($('#content').height() - 400) + 'px');
	
	// Add class for first-child in nav
	$('ul#navigation li:first-child').addClass('first-child');
	
	// Popup external links
	$('a.popup').click(function () {
		window.open(this.href,'');
		return false;
	});
	
	// Popup calendar
	if (document.getElementById('closing_date') || document.getElementById('age')) {
		popUpCal.firstDay = 1;
		popUpCal.dateFormat = 'DMY/';
		popUpCal.autoPopUp = true;
		$('#closing_date, #age').calendar();
	}
	
	// Auto-select for pre-filled form fields
	$('form input#town, form input#country').focus(function () {
		this.select();
	});
	
	// Back to top links
	$('#content').append('<a href="' + this.location.href + '#" class="top noprint">Back to top</a>');
	
	// Add image captions where alt text exists
	$('#content img[alt]').each(function () {
		if ($(this).parents('ul').length == 0) {
			$(this).wrap('<div class="captioned"></div>').parent().append('<span>' + $(this).attr('alt') + '</span>').css('width', $(this).width() + 'px');
		}
	});
	
	// Fix image alignment
	$('img[align="right"]').removeAttr('align').addClass('right').parent('.captioned').addClass('right');
	
});
