/*
http://www.howtocreate.co.uk/tutorials/testMenu.html
Why?

1. IE does not have a menu where you can select styles
2. Most other browsers do not store your choice if you use their view menus, but this script can

I use a client script instead of a server script because this way, I do not have to generate every page, so your
browser is able to use its cache, saving bandwidth for both you and me.

*/

//window.onload = function () {
	function loadStyles() {
	if( document.childNodes && document.createElement && ( document.styleSheets || window.opera || ( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ) && !navigator.__ice_version ) {
		var linkList = document.getElementsByTagName('link');
		/*var oH3 = document.createElement('h3');
		oH3.appendChild(document.createTextNode('Style'));*/
		oText = document.createElement('span');
		oText.className = 'websiteStyleText';
		oText.innerHTML = 'Website Style: ';
		oForm = document.createElement('form');
		/*oForm.appendChild(document.createElement('p'));*/
		var sel = document.createElement('select');
		/*oForm.firstChild.appendChild(sel);*/
		oForm.appendChild(sel);
		var selStyle, theCurCol, selopt;
		for( var x = 0, y = document.cookie.split('; '); x < y.length; x++ ) {
			var oneCookie = y[x].split('=');
			if( oneCookie[0] == 'mainsitestyle' ) {
				theCurCol = unescape( unescape( oneCookie[1] ).replace(/ MWJ .*/,'') );
				break; 
			}
		}
		for( var n = 0, optn, oRel, oTitl; n < linkList.length; n++ ) {
			oRel = linkList[n].getAttribute('rel');
			oTitl = linkList[n].getAttribute('title');
			if( !oRel || !oTitl || ( oRel != 'stylesheet' && oRel != 'alternate stylesheet' ) || oTitl.match(/spoken/i) ) { continue; }
			optn = document.createElement('option');
			optn.text = oTitl;
			optn.value = oTitl;
			try { sel.add(optn,null); } catch(e) { try { sel.add(optn,sel.options.length); } catch(f) { sel.appendChild(optn); } }
			if( theCurCol == oTitl || ( !theCurCol && oRel == 'stylesheet' && typeof( theCurCol ) == 'undefined' ) ) { optn.selected = true; selopt = sel.options.length - 1; }
		}
		optn = document.createElement('option');
		optn.text = 'No style';
		optn.value = '';
		try { sel.add(optn,null); } catch(e) { try { sel.add(optn,sel.options.length); } catch(f) { sel.appendChild(optn); } }
		if( !theCurCol && typeof( theCurCol ) != 'undefined' ) { optn.selected = true; }

		try { sel.setAttribute('onchange','var tmpVal = this.selectedIndex;changeStyle(this.options[tmpVal].value);this.options[tmpVal].selected = true;rememberStyle(\'mainsitestyle\',1800);'); } catch(e) {}
		sel.onchange = function () {
			var tmpVal = this.selectedIndex;
			changeStyle(this.options[tmpVal].value);
			this.options[tmpVal].selected = true;
			rememberStyle('mainsitestyle',1800);
		};

		var oParent = document.getElementById('websiteStyleSelect');
		/*oParent.appendChild(oH3);*/
		oParent.appendChild(oText);
		oParent.appendChild(oForm);

		if( !selopt ) { selopt = sel.selectedIndex; }
		useStyleAgain('mainsitestyle');
		setTimeout(function () { sel.options[selopt].selected = true; },10);
		if( selopt && location.hash && location.hash.match(/^#./) ) {
			location.hash = location.hash.replace(/^#/,'');
		}
	}
};