/*
Automated unbtrusive Dropcaps for wordpress
Copyright 2005 silver-lizard
You are free to use this in any way you like, as long
as you retain this copyright notice. If you're feeling
generous, a link back to www.silver-lizard.co.uk would
be appreciated as well.

Version 1.0

Known Issues
If the first paragraph of a post in starts with another
tag (<a>, <ul>, etc) then this will fail with an error.
This should be relatively easy to fix, I just haven't
got around to it yet.
*/

addEvent(window, "load", makeDropCaps);

function makeDropCaps(){
	var storys = 	document.getElementsByTagName('div');
	for (var i=0; i< storys.length; i++){
		if (storys[i].className == 'entrytext') {
//			alert ("numentries: " + storys[i].className.length);
			for (var j=0; j <	storys[i].childNodes.length; j++){
				if (storys[i].childNodes[j].nodeName ==	 'P' || storys[i].childNodes[j].nodeName ==	 'p'){
//					alert (storys[i].childNodes[j].firstChild.nodeValue);

					var capspan = document.createElement('span');
					var captext = document.createTextNode(storys[i].childNodes[j].firstChild.nodeValue.substr(0,1));
					capspan.appendChild(captext);
					capspan.className = 'dropcap';

					storys[i].childNodes[j].firstChild.nodeValue = storys[i].childNodes[j].firstChild.nodeValue.substr(1);
					
					storys[i].childNodes[j].insertBefore(capspan, storys[i].childNodes[j].firstChild);
					break;	
				}
			}
		}
	}
}

// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}
