/*
Find all blockquotes on the page and 
add citation sections if appropriate.

Citations will look like:

<div>Source: <a>TITLE</a></div>
or
<div><a>Source</a></div>
*/
function populateCitations( ) {

	// Check for browser funcitonality
	if ( !document.getElementsByTagName ) { return false; }
	if ( !document.createElement ) { return false; }
	if ( !document.createTextNode ) { return false; }

	var quotes = document.getElementsByTagName( 'blockquote' );
	for ( var i = 0; i < quotes.length; i++ ) {
		var currentQuote = quotes[ i ];
		if ( currentQuote.getAttribute( 'cite' ) ) {
		
			// Harvest values from the blockquote
			var theHref = currentQuote.getAttribute( 'cite' );
			var theTitle = ( currentQuote.getAttribute( 'title' ) ) ? currentQuote.getAttribute( 'title' ) : 'Source';
			
			// Create citation structure
			var citationHolder = document.createElement( 'div' );
			citationHolder.setAttribute( 'class', 'citation' );
			// This next line solves a boneheaded IE bug, and doesn't seem to break other browsers
			citationHolder.setAttribute( 'className', 'citation' );
			currentQuote.appendChild( citationHolder );
			
			if ( 'Source' != theTitle ) {
				var sourceText = document.createTextNode( 'Source:' );
				citationHolder.appendChild( sourceText );
			}
			
			var citationAnchor = document.createElement( 'a' );
			citationAnchor.setAttribute( 'href', theHref );
			citationAnchor.setAttribute( 'title', 'Citation' );
			citationHolder.appendChild( citationAnchor );
			
			var titleText = document.createTextNode( theTitle );
			citationAnchor.appendChild( titleText );
		
		}
	}
	

}
addLoadEvent( populateCitations );
