// =============================================
//                Auto Cite Link
// =============================================
//         Version "Alpha"  (2008-02-09)
//
//  http://www.6times9.com/javascript/citelink/
//
//        Copyright 2008 Richard Winskill
// =============================================

// This version of the script is designed for the Greasmoney add-on for Firefox.
// If you just want to add it to a website, then go back to the webpage above and choose the "Normal" download link.

// This script is a slightly modified version of the Normal script:
// * It will check to make sure citelink.js isn't included by the HTML before running
// * The CSS to right-align the <blockquote> URL in hardcoded in the STYLE attribute

// ==UserScript==
// @name          Auto Cite Link
// @namespace     http://www.6times9.com/javascript/citelink/
// @description   Takes the CITE attribute of <q> and creates a superscript link to the referenced page, and takes the CITE attribute of <blockquote> and creates a normal link at the end of the block.
// @include       *
// ==/UserScript==

function citelink(){
	//Get all SCRIPTs
	scripts=document.getElementsByTagName("script")
	hardcoded=0
	for (script=0; script<scripts.length; script++){
		if(scripts[script].src.match("citelink.js")){hardcoded=1;}
	}
	if(hardcoded==0){
		//Get all Qs
		iqs=document.getElementsByTagName("q")
		icount=1
		//Extract CITE and place it in a SUP
		for (iq=0; iq<iqs.length; iq++){
			cite=iqs[iq].getAttribute("cite")
			if(cite){iqs[iq].innerHTML+="<sup><a href=\""+cite+"\">"+icount+"<\/a><\/sup>"; icount++;}
		}
		//Get all BLOCKQUOTEs
		bqs=document.getElementsByTagName("blockquote")
		//Extract CITE and place it in a new paragraph
		for (bq=0; bq<bqs.length; bq++){
			cite=bqs[bq].getAttribute("cite")
			if(cite){bqs[bq].innerHTML+="<p class=\"cite\" style=\"font-size: small; text-align: right;\"><a href=\""+cite+"\">"+cite+"<\/a><\/p>"}
		}
	}
}

window.setTimeout(function() { citelink() }, 60);
