/*
pullquote function by Roger Johansson, http://www.456bereastreet.com/
*/
var pullquote = {
	init : function() {
		// Check that the browser supports the methods used
		if (!document.getElementById || !document.createElement || !document.appendChild) return false;
		var oElement, oPullquote, oPullquoteP, oQuoteContent, i, j;
		// This is to skip any span inside the pullquote, which shouldnt be another pullquote
		var iSkip = 0;
		
		// Find all span elements with a class name of pullquote
		var arrElements = document.getElementsByTagName('span');
		var oRegExp = new RegExp("(^|\\s)pullquote");
		for (i=0; i<arrElements.length; i++) {
			
			// Save the current element
			oElement = arrElements[i];
			if (oRegExp.test(oElement.className)) {
				iSkip = 0;
				
				// Create the blockquote and p elements
				oPullquote = document.createElement('blockquote');
				oPullquote.className = oElement.className;
				oPullquoteP = document.createElement('p');
				
				oPullquoteSpanOpen = document.createElement('span');
				oPullquoteSpanOpen.className = 'pullquote-open';
				
				oPullquoteSpanClose = document.createElement('span');
				oPullquoteSpanClose.className = 'pullquote-close';
				
				// Insert the pullquote text
				for(j=0;j<oElement.childNodes.length;j++) {
					if (oElement.childNodes[j].nodeName == "SPAN") iSkip += 1;
					oPullquoteP.appendChild(oElement.childNodes[j].cloneNode(true));
				}
				
				oPullquote.appendChild(oPullquoteSpanOpen);
				oPullquote.appendChild(oPullquoteP);
				oPullquote.appendChild(oPullquoteSpanClose);
				
				// Insert the blockquote element before the span elements parent element
				oElement.parentNode.parentNode.insertBefore(oPullquote,oElement.parentNode);
				
				i += iSkip+2; // 2 is for the 2 span created for the blockquote
			}
		}
	}
};
// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
addEvent(window, 'load', pullquote.init);