/*

Unobtrusive Sidenotes v1.2
(c) Arc90, Inc.

http://www.arc90.com
http://lab.arc90.com

Licensed under : Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/

*/

/* Globals */
var arc90_isIE = document.all? true: false;
var arc90_sideCLRs = 2; // total colours declared in the CSS for sidenote usage

function arc90_sidenote() {
	O = document.getElementsByTagName('SPAN');
	for (var i = 0, l = O.length, c = 0; i < l; i++) {
		var o = O[i];
		if (o != null && o.className && o.className.indexOf('sidenote') >= 0) {
			try {
				var s = arc90_newNode('div', '', 'arc90_sidenoteTXT arc90_sidenoteCLR' +c);
				var a = arc90_newNode('div', '', 'arc90_inline');
				a.innerHTML = arc90_gtlt(o.title);
				s.appendChild(a);
				o.parentNode.parentNode.insertBefore(s, o.parentNode);

				o.className = 'arc90_sidenoteLNK arc90_sidenoteCLR' +c;
				c = c + 1 < arc90_sideCLRs? c+1: 0;
			} catch (err) {
				o = null;
			}
		}
	}
}

function arc90_gtlt(s) {
	s = s.replace(/&gt;/g, '>');
	s = s.replace(/&lt;/g, '<');
	return s;
}

/* Events */
function isString(o) { return (typeof(o) == "string"); }

function arc90_addEvent(e, meth, func, cap) {
	if (isString(e))	e = document.getElementById(e);

	// this helps problems with IE 5 on Mac, not verified yet!!!
	if (e == window && meth == "load") {
		if (typeof window.onload != 'function') {
	    	window.onload = func;
		} else {
			var oldonload = window.onload;
	    	window.onload = function() {
				oldonload();
				func();
			}
	    }
		return true;
	} else if (e.addEventListener){
		e.addEventListener(meth, func, cap);
    	return true;
	}	else if (e.attachEvent)
		return e.attachEvent("on"+ meth, func);
	return false;
}

/* Nodes */
function arc90_newNode(t, i, s, x, c) {
	var node = document.createElement(t);
	if (x != null && x != '') {
		var n = document.createTextNode(x);
		node.appendChild(n);
	}
	if (i != null && i != '')
		node.id = i;
	if (s != null && s != '')
		node.className = s;
	if (c != null && c != '')
		node.appendChild(c);
	return node;
}

/* Add Events */
arc90_addEvent(window, 'load', arc90_sidenote);


