function scanAndModifyLis() {

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

	var tiles = sidebar.getElementsByTagName("li");
	var j = 0; for (j = 0; j < tiles.length; j++) {
		var currenttile = tiles[j]
		var lis = currenttile.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;');
				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 tallPage() {
	var col1 = document.getElementById("sidebar-col1");
	var col3 = document.getElementById("sidebar-col3");
	var page = document.getElementById("page");
	var highest = 0;
	if (col1.offsetHeight > highest) highest = col1.offsetHeight;
	if (col3.offsetHeight > highest) highest = col3.offsetHeight;
	if (page.offsetHeight > highest) highest = page.offsetHeight;
	if (highest > page.offsetHeight) {
		var difference = highest;
		page.setAttribute("style", "min-height: " + difference.toString() + "px; ");
	}
	highest = highest - 40;
	//col1.setAttribute("style", "min-height: " + highest.toString() + "px; ");
	col1.setAttribute("style", "min-height: 300px; ");
	col3.setAttribute("style", "min-height: " + highest.toString() + "px; ");

}

function libOnLoad() {
	scanAndModifyLis();
	extractBlockquoteCitations();
	setBlockquotePreBackgrounds();
	tallPage();
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function themeAddLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

themeAddLoadEvent(libOnLoad);	// run the theme javascript on load