/* 
	JavaScript List Markers
	Author: Piotr 'Riddle' Petrus
	Last modified: 22nd May 2007
	
	===================================================================
	
	Adds XHTML invalid attributes "start" and "value".
	Bases on classes. For example:
		<ol class="start:100"> becomes <ol class="start:100" start="100">
		<li class="value:-1"> becomes <li class="value:-1" value="1">
		
	Why? Because browsers don't support advanced CSS (generated 
	content with complex styling) and sometimes we need to show 
	our boss 100% validated site.
	
	Licence: Attribution-ShareAlike 3.0 Unported
	http://creativecommons.org/licenses/by-sa/3.0/
	
	You can use it in your project and even remove this commentary, 
	just include my name and URL somewhere. Thanks.
	
	DOM Content Loaded code credits:
		http://dean.edwards.name/weblog/2006/06/again/
		http://www.outofhanwell.com/blog/index.php
			?title=the_window_onload_problem_revisited
		http://code.jquery.com/jquery-latest.js
	
	===================================================================
	
	Known bugs:
		* IE - it might not work on https:// connection due to
			src="javascript:;"
		* Safari - it seems to ignore "start" attribute added by JS
	
*/

var Markers = {
	replace: function() {
		this.replaceClass('ol', 'start');
		this.replaceClass('li', 'value');
	},
	
	replaceClass: function(element, attribute) {
		var elems = document.getElementsByTagName(element);
		for (var i = 0; i < elems.length; i++) {
			var className = elems[i].className.replace(/^\s+|\s+$/g, '')
				.replace(/\s\s+/g, ' ').replace(/\t+/g, '');
			var classes = className.split(' ');
			for (var j = 0; j < classes.length; j++) {
				var r = new RegExp(attribute + '\:-?\\d+');
				if (r.test(classes[j])) {
					var val = classes[j].replace(attribute + ':', '');
					elems[i].setAttribute(attribute, val);
				}
			}
		}
	}
	
}

/* ================================================================ */
/* if you don't know what code below does and if it scares you ;), 
   you can always call Markers.replace() on window.onload event.   */

if (/*@cc_on!@*/false) {
	document.write('<script id="__init_script" defer="true" src="javascript:;"><\/script>');
}

function registerInit(callback) {
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", callback, false);
	}
	if (/*@cc_on!@*/false) {
		var deferScript = document.getElementById('__init_script');
		if (deferScript) {
			deferScript.onreadystatechange = function() {
				if (this.readyState == 'complete') {
					callback();
				}
			};
			deferScript.onreadystatechange();
			deferScript = null;
		}
	}
	if (/WebKit/i.test(navigator.userAgent)) {
		var _timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				clearInterval(_timer);
				callback();
			}
		}, 10);
	}
	window.onload = callback;	
}

function init() {
	if (arguments.callee.done) { return; }
	arguments.callee.done = true;
	Markers.replace();
};

if (document.getElementById && document.getElementsByTagName) {
	registerInit(init);
}