window.addEvent('domready', classchange);
var TB_doneOnce = 0;
var toolHeight = 0;
var topPos = 0;
function classchange(){
	if ($('tools')){
		toolHeight = $('tools').getSize().y
		topPos = -1 - toolHeight;
		$('tools').setStyles({
			visibility: 'visible',
			top: topPos
		});
	}
	$('accessopt').addEvent('click', function(event){
		// stop default behaviour
		event.preventDefault();
		// remove click border
		$('accessopt').blur();
		toggle();
	});
	$('closetools').addEvent('click', function(event){
		// stop default behaviour
		event.preventDefault();
		// remove click border
		$('closetools').blur();
		toggle();
	});
	var myCookie = Cookie.read('txtsize');
	if (myCookie) {
			$$("body").each(function(el){
				el.setStyle('font-size', myCookie);
			});
	}
	myCookie = Cookie.read('stylesheet');
	if (myCookie) {
		if (!$("swapstyle")) {
			new Element('link').setProperties({
				'id': 'swapstyle',
				 'rel': 'stylesheet',
				 'type': 'text/css',
				'href': myCookie
			}).injectInside(document.head);
		} else {
			$("swapstyle").setProperty('href', myCookie);
		}
	}
	$$("li.button").each(function(el){
		el.addEvent('click', function(event){
			var linkdetails = this.getElement('a');
			// stop default behaviour
			event.preventDefault();
			var value = linkdetails.get('rel');
			switch(value){
				case 'smaller':
					changetxt(value);
					break;
				case 'bigger':
					changetxt(value);
					break;
				case 'default':
					changecolour('master');
					break;
				case 'bow':
					changecolour(value);
					break;
				case 'boy':
					changecolour(value);
					break;
				case 'wob':
					changecolour(value);
					break;
				case 'yob':
					changecolour(value);
					break;
			}
			// remove click border
			this.blur();
			linkdetails.blur();
		});
	});
}
function changetxt(what){
	var inc = (what =='smaller') ? -1 : 1;
	$$("body").each(function(el){
		var txtsize = el.getStyle('font-size').toInt();
		if (txtsize < 40 ) { // For IE
			if (((txtsize + inc) >= 9) && ((txtsize + inc) <= 12)) {
				var newtxtsize = (txtsize + inc) + "px";
				el.setStyle('font-size', newtxtsize);
				myCookie  = Cookie.write('txtsize', newtxtsize, {"duration": 0, "path": "/"});// duration 0 is session cookie
			}
		} else {
			inc = inc * 10;
			if (((txtsize + inc) >= 50) && ((txtsize + inc) <= 90)) {
				var newtxtsize = (txtsize + inc) + "%";
				el.setStyle('font-size', newtxtsize);
				myCookie  = Cookie.write('txtsize', newtxtsize, {"duration": 0, "path": "/"});// duration 0 is session cookie
			}
		}
	});
}
function changecolour(what){
	var newstylesheet = '/css/'+what+'.css';
	if (!$("swapstyle")) {
		new Element('link').setProperties({
			'id': 'swapstyle',
			 'rel': 'stylesheet',
			 'type': 'text/css',
			'href': newstylesheet
		}).injectInside(document.head);
	} else {
		$("swapstyle").setProperty('href', newstylesheet);
	}
	myCookie  = Cookie.write('stylesheet', newstylesheet, {"duration": 0, "path": "/"});// duration 0 is session cookie
} 
function toggle(){
		if (TB_doneOnce == 0) {
			$('tools').tween('top', -1);
			$('page').tween('top', toolHeight);
			TB_doneOnce = 1;
		} else {
			$('tools').tween('top', topPos);
			$('page').tween('top', 0);
			TB_doneOnce = 0;
		}
}