function scanAndModifyLis() {

	var sidebar = document.getElementById("sidebar");
	if (sidebar == null) return;

	var lis = sidebar.getElementsByTagName("li");
	var i = 0;
	for (i = 0; i < lis.length; i++) {
		var currentli = lis[i];
		var lisinli = currentli.getElementsByTagName("li");
		if (lisinli.length > 0) { // we assign a class parent because there are lis within this li
			currentli.setAttribute('class','parent');
			continue;
		}
		var asinli = currentli.getElementsByTagName("a");
		if (asinli.length > 0 && asinli[0].href != null) {
			currentli.setAttribute('onclick','loadFirstInnerLink(this); return false;');
			currentli.setAttribute('style','cursor: pointer; cursor: hand;');
			currentli.setAttribute("title",asinli[0].getAttribute("title"));
			currentli.setAttribute("href",asinli[0].getAttribute("href"));
		}
	}

}

function loadFirstInnerLink(callerObject) {
	var asinli = callerObject.getElementsByTagName("a");
	if (asinli.length > 0 && asinli[0].href != null) {
	// 	alert("gato");
		window.location = asinli[0].href;
	}
}


/*
* blockquotes.js
*
* Simon Willison, 20th December 2002
*
* Explanation: 
*   http://simon.incutio.com/archive/2002/12/20/#blockquoteCitations
* Inspired by Adrian Holovaty: 
*   http://www.holovaty.com/blog/archive/2002/12/20/0454
* Alternative implementation of the same idea by Paul Hammond: 
*   http://www.paranoidfish.org/boxes/2002/12/20/
*/

function extractBlockquoteCitations() {
  quotes = document.getElementsByTagName('blockquote');
  var i = 0;
  for (i = 0; i < quotes.length; i++) {
    cite = quotes[i].getAttribute('cite');
    if (cite) {
      newlink = document.createElement('a');
      newlink.setAttribute('href', cite);
      newlink.setAttribute('title', "Source for this quote:");
      newlink.setAttribute('class', "autocite");
      newlink.appendChild(document.createTextNode('Quotation source'));
      newdiv = document.createElement('div');
      newdiv.className = 'blockquotesource';
      newdiv.appendChild(newlink);
      quotes[i].appendChild(newdiv);
    }
  }
}


function reparentChildren(tagName) {
  quotes = document.getElementsByTagName(tagName);
  var i = 0;
  for (i = 0; i < quotes.length; i++) {
	var newdiv = document.createElement('div');
	newdiv.className = "intermediary-container";
	while (quotes[i].childNodes.length > 0) {
		var childnode = quotes[i].childNodes[0];
		newdiv.appendChild(childnode);
// 		alert(childnode);
	}
	quotes[i].appendChild(newdiv);
  }
}

function setBlockquotePreBackgrounds() {
	reparentChildren("blockquote");
	reparentChildren("pre");
}



function libOnLoad() {

// 	moveSidebar();
	scanAndModifyLis();
	extractBlockquoteCitations();
	setBlockquotePreBackgrounds();

}

/* sadly this did not work as intended since block boxes do not pay attention to floats, shit... */
function moveSidebar() {
	var sidebar = document.getElementById("sidebar");
	if (sidebar == null) return;

	var content = document.getElementById("content");
	if (content == null) return;

	content.insertBefore(sidebar,content.firstChild);
// 	var contentclass = content.getAttribute("class");
// 	if (contentclass == null) { content.setAttribute("class","reparentedSidebar"); }
// 	else { content.setAttribute("class",contentclass + " reparentedSidebar"); }
// 	alert(content.getAttribute("class"));
	content.setAttribute("class","reparentedSidebar");
// 	sidebar.setAttribute("style","background: red;");
	
}

// this must run when this is loaded

libOnLoad();