/**** Blockquote Eye Candy JS ****/
/*  You are free to use & share  */
/* so long as this heading stays */
/*            in tact            */
/*       http://kachii.com       */
/********(C) K Ereira 2006********/

/* Introduction
* This Javascript is designed to extract the 'cite' and 'title'
* attributes of your HTML code to create a div with the class
* 'cite' at the end of your blockquote tags, and appropriate
* title attributes - without having to write out extra HTML, and
* always remaining semantically correct.
*/

function getCitations() {
        /* searches for all blockquote tags */
	var blockQuote = document.getElementsByTagName('blockquote');
	for(i=0;i<blockQuote.length;i++) {
	        /* checks each blockquote and generates tags for them */
		var citeUrl = blockQuote[i].getAttribute('cite');
		var citeTitle = blockQuote[i].getAttribute('title');
		
		/* what if cite and/or title aren't specified? */
		if(citeUrl != null && citeTitle != null) {
			var newTitle = citeUrl + " - " + citeTitle;
			var visLink = document.createElement('a');
			visLink.setAttribute('href', citeUrl);
			visLink.setAttribute('title', "Source: " + citeTitle);
			visLink.appendChild(document.createTextNode(citeTitle));
		} else if(citeUrl == null && citeTitle != null) {
			var newTitle = citeTitle;
			var visLink = document.createTextNode(citeTitle);
		} else if(citeUrl != null && citeTitle == null) {
			var newTitle = citeUrl;
			var visLink = document.createElement('a');
			visLink.setAttribute('href', citeUrl);
			visLink.setAttribute('title', "Source: " + citeTitle);
			visLink.appendChild(document.createTextNode("Source"));
		} else if(citeUrl == null && citeTitle == null) {
			var newTitle = "Unknown Source";
			var visLink = document.createTextNode("Unknown Source");
		}
		
		/* re-write the title attribute */
		blockQuote[i].setAttribute('title', newTitle);
		
		/* creates the cite div at the end of the blockquote */
		var visDiv = document.createElement('div');
		visDiv.className = 'cite';
		visDiv.appendChild(visLink);
		
		blockQuote[i].appendChild(visDiv);
	}
}


/* Because Firefox and IE use different onload functions */
if(window.addEventListener) {
	addEventListener("load", getCitations, false);
} else {
	window.attachEvent("onload", getCitations);
}