/*
# $Id$
*/

window.addEvent('load', function() {

	// Setup smooth scrolling for anchors
	new SmoothScroll({ duration: 200 });

	// Fix footer to the bottom of the page
	var wss = window.getScrollSize();
	var ws = window.getSize();
	if(wss.y<=ws.y) {
		if($('footer')) {
			var fs = $('footer').getSize();
			var fp = $('footer').getPosition();
			$('footer').setStyles({
				marginTop: (ws.y-fs.y)-fp.y
			});
		}
	}

	// Set id on all anchors
	var anchors = $$('a:empty');
	if(anchors.length>0) {
		anchors.each(function(e) {
			e.id = e.get('name');
		});
	}

	// Make all "external" hyperlinks open a new window
	$$('a.external').each(function(e) {
		e.addEvent('click', function() {
			window.open(this.href);
			return false;
		});
	});

	// Make all "print-page" links print the current page
	$$('a[rel="print-page"]').addEvent('click', function() {
		window.focus();
		window.print();
		return false;
	});

	// Convert any input with "rel="calendar"" into calendar instances
	$$('input[rel=calendar]').each(function(el, i) {
		var options = {};
		options[el.get('id')] = 'd/m/Y';
		var myCal = new Calendar(options);
	});

	// Quick search: When new city is chosen, erase postcode
	$('quick-search-qtown').addEvent('change', function() {
		$('quick-search-locp').value = '';
	});

	// Quick search: When postcode is entered that doesn't match city, erase city
	$('quick-search-locp').addEvent('change', function() {
		var city = $('quick-search-qtown').value.toUpperCase().substring(0, 2).replace(/[0-9]/, '');
		var postcode = $('quick-search-locp').value.toUpperCase().substring(0, 2).replace(/[0-9]/, '');
		if(postcode!='' && city!='' && postcode!=city) {
			$('quick-search-qtown').value = '';
		}
	});
});