/* 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 () {
	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'));
		oForm = document.createElement('form');
		oForm.appendChild(document.createElement('p'));
		var sel = document.createElement('select');
		oForm.firstChild.appendChild(sel);
		var 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('index');
		oParent.appendChild(oH3);
		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(/^#/,'');
		}
	}
};

function wordCountArticle() {
	//doo dee doo - not hooked into anything - can be accessed using bookmarklets
	//can take a long time (several seconds) to perform the count - leave it alone and let it finish
	if( !document.selectNodes ) { alert('Use a browser that supports DOM 3 XPath and the selectNodes method.'); return; }
	//generic (counts all words, even in TOCs)
	//var allTextNodes = document.selectNodes("//text()[not(ancestor::*[name() = 'script' or name() = 'style'])]");
	//same but using document.evaluate for lesser browsers
	//var allTextNodes = document.evaluate("//text()[not(ancestor::*[name() = 'script' or name() = 'style'])]", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
	//custom for this site
	var allTextNodes = document.selectNodes("id('content')//text()[not(ancestor::*[name() = 'script' or name() = 'style' or @id = 'tocont' or @class = 'lastmod'])]");
	//same but using document.evaluate for lesser browsers
	//var allTextNodes = document.evaluate("id('content')//text()[not(ancestor::*[name() = 'script' or name() = 'style' or @id = 'tocont' or @class = 'lastmod'])]", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
	var oneTextNode, wordcount = 0;
	//using document.evaluate for lesser browsers
	//while( oneTextNode = allTextNodes.iterateNext() ) {
	//using document.selectNodes
	for( var i = 0; oneTextNode = allTextNodes[i]; i++ ) {
		oneTextNode = oneTextNode.nodeValue;
		if( oneTextNode != 'Table of contents' && oneTextNode.match(/[a-z0-9]/i) ) {
			wordcount += oneTextNode.replace(/'/g,'').replace(/^[^a-z0-9]+|[^a-z0-9]+$/ig,'').split(/[^a-z0-9]+/gi).length;
		}
	}
	alert(wordcount+' words found');
}