document.observe('dom:loaded', function () {
	$$('#quicklinks select').invoke('observe', 'change', function () {
		var value = $F(this);
		if (!value.blank()) { window.location.href = value; }
	});
	
	$$('#content .column.main dt', '#content .column.main dl').each(function (term) {
		var spacer = new Element('div', { className: 'fixIEclearing' });
		term.insert({ before: spacer });
	});
	
	$$('a[href=#print]').invoke('observe', 'click', function (event) {
		event.stop();
		window.open("/_pdf/pdf.aspx?page=" + window.location.href, 'DEV_print', 'width=700,height=500,scrollbars=yes,menubar=1');
	});
	
	var body_class = $w($(document.body).className).first();
	if (body_class === 'profile') { body_class = 'barristers'; }
	var menu_items = $$('#header .mainmenu ul li');
	
	var Timer = {
		counter: null,
		start: function (fn, time) { this.counter = setTimeout(fn, time); },
		stop: function () {
			if (this.counter !== null) clearInterval(this.counter);
			this.counter = null;
		}
	};
	
	menu_items.each(function (li) {
		if (li.className === body_class) {
			li.addClassName('hover');
			li.addClassName('active');
		}
		
		li.observe('mouseover', function () {
			Timer.stop();
			menu_items.invoke('removeClassName', 'hover');
			this.addClassName('hover');
		});
		
		li.observe('mouseout', function () {
			Timer.stop();
			Timer.start(function () {
				li.removeClassName('hover');
				menu_items.select(function (item) { return item.hasClassName('active'); }).invoke('addClassName', 'hover');
			}, 80);
		});
	});
	
	$('content').observe('mouseover', function () {
		menu_items.select(function (li) { return li.hasClassName('active'); }).invoke('addClassName', 'hover');
	});
	
	if (document.getElementById('root_folder-all')) {
		var method = (Prototype.Browser.IE) ? 'click' : 'change';
		
		var main_tickbox = $('root_folder-all'),
			form = main_tickbox.up('form'),
			other_boxes = form.select('input[type=checkbox]:not(#root_folder-all)');
		
		main_tickbox.observe(method, function () {
			other_boxes.each(function (box) {
				box.checked = this.checked;
			}, this);
		});
		
		other_boxes.invoke('observe', method, function () {
			(function () {
				main_tickbox.checked = (!other_boxes.pluck('checked').include(false));
			}).defer();
		});
		
		form.observe('submit', function () {
			// if the ‘all’ box is checked then we do not need the other values
			// if nothing is selected then we should assume ‘all’
			if (main_tickbox.checked) {
				other_boxes.invoke('disable');
				(function () { other_boxes.invoke('enable'); }).defer(); // reset
			} else if (!other_boxes.pluck('checked').include(true)) {
				main_tickbox.checked = true;
			}
		});
	}	
});
