window.addEvent('domready', function()	{
	
	// =============================================
	// Text resizing
	// =============================================
	var body = $$('body');
	var accessBar = $('text_resize');
	
	function addSelected(){
		// Remove "selected" class from LI
		accessBar.getElement('li[class=selected]').removeClass('selected');
		// Now add to parent LI of the clicked anchor (whose class = cookie value) 
		accessBar.getElement('a[class='+Cookie.read('fontSize')+']').getParent().addClass('selected');
	}

	// Check if cookie is set
	if(Cookie.read('fontSize')) {
		// Add associated clicked class to wrapper
		body.addClass(Cookie.read('fontSize'));
		
		addSelected();
	}

	if($defined(accessBar)) {
		accessBar.getElements('a').addEvent('click', function(el) {
			// Prevent default behaviour
			el.stop();

			// Get the current item class
			var sizeClass = this.getProperty('class');

			// Remove existing classes from wrapper and add the one clicked
			body.removeProperty('class').setProperty('class', sizeClass);

			// Set cookie
			Cookie.write('fontSize', sizeClass, {
				path: '/',
				duration: 30
			});
			
			addSelected();
		});
	}
	
	
	// =============================================
	// Setup labelled text boxes
	// =============================================
	$$('.make.labelled').setLabel();
	
	
	// =============================================
	// Inject back & print buttons into page tools
	// =============================================
	var pagetools = $('pagetools');
	if ($defined(pagetools)) {
		var backLink = new Element('li', {
			'class' : 'backlink',
			'html': '<a href="javascript:history.back();">Back to the previous page</a>'
		}).inject($('pagetools'));
	
		var printLink = new Element('li', {
			'class' : 'printlink',
			'html': '<a href="javascript:window.print()">Print page</a>'
		}).inject($('pagetools'));
	}

});
