// ==UserScript==
// @name Enhance blockquote display
// @author Arve Bersvendsen 
// @namespace http://virtuelvis.com/ 
// @version 2.0
// @description  Enhance blockquotes by adding clickable links when a
//			blockquote uses the cite attribute, and the
//			blockquote doesn't already contain a clickable
//			link to the cited work.
// @ujs:category browser: enhancements
// @ujs:published 2005-11-13 10:26
// @ujs:modified 2005-11-13 11:41
// @ujs:documentation http://userjs.org/scripts/browser/enhancements/enhance-blockquotes 
// @ujs:download http://userjs.org/scripts/download/browser/enhancements/enhanceblockquotes.js
// ==/UserScript==

/* 
 * Copyright © 2005 by Arve Bersvendsen
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


document.addEventListener('load', function(ev){
  	var quotes;
    Array.prototype.hasValue = function(needle){
      for (var i = 0; i < this.length; i++){
        if (this[i] == needle){
          return true;
        }
      }
      return false;
    }   
    
	if (document.evaluate){
		var seek = "//blockquote[starts-with(@cite,'http:') or starts-with(@cite,'ftp:') or starts-with(@cite,'https:')]";
		quotes = document.evaluate( seek, document, null, 0, null );
		var el;
		while (el = quotes.iterateNext()){
		  var cite = el.getAttribute("cite");
		  var title = el.getAttribute("title");
	     var bqlinks = el.getElementsByTagName('a');
	     var bqhrefs = new Array();
	     for (var c = 0; c < bqlinks.length; c++) {
	       if (bqlinks[c].href) bqhrefs.push(bqlinks[c].href);
	     }
	     if (!bqhrefs.hasValue(cite)) {
	       newlink = document.createElement('a');
	       newlink.setAttribute('href', cite);
	       newlink.setAttribute('title', ('Go to ' + title?title:cite));
	       if (cite.length > 42) {
	         cite = cite.substring(0,21) + '\u2026' + cite.substring((cite.length-21));
	       }
	       newlink.appendChild(document.createTextNode(cite));
	       newdiv = document.createElement('p');
	       newdiv.className = 'bqsource';
	       newdiv.appendChild(document.createTextNode('\u2014 '));
	       newdiv.appendChild(newlink);
	       el.appendChild(newdiv);
       }
		}
	} else {
		quotes = document.getElementsByTagName('blockquote');
		for (var i = 0; i < quotes.length; i++) {
		  cite = quotes[i].getAttribute('cite');
		  title = quotes[i].getAttribute('title');
		  if ((cite) && (cite != "")) {
		   if ( (cite.match('http://', 'i')) || (cite.match('https://', 'i')) || (cite.match('ftp://', 'i')) ){
		     var bqlinks = quotes[i].getElementsByTagName('a');
		     var bqhrefs = new Array();
		     for (var c = 0; c < bqlinks.length; c++) {
		       if (bqlinks[c].href) bqhrefs.push(bqlinks[c].href);
		     }
		     if (!bqhrefs.hasValue(cite)) {
		       newlink = document.createElement('a');
		       newlink.setAttribute('href', cite);
		       newlink.setAttribute('title', ('Go to ' + title?title:cite));
		       if (cite.length > 42) {
		         cite = cite.substring(0,21) + '\u2026' + cite.substring((cite.length-21));
		       }
		       newlink.appendChild(document.createTextNode(cite));
		       newdiv = document.createElement('p');
		       newdiv.className = 'bqsource';
		       newdiv.appendChild(document.createTextNode('\u2014 '));
		       newdiv.appendChild(newlink);
		       quotes[i].appendChild(newdiv);
		     }
		   } else {
		     newdiv = document.createElement('div');
		     newdiv.className = 'bqsource';
		     var exp = new RegExp('%20','g');
		     cite = cite.replace(exp, ' ');
		     newdiv.appendChild(document.createTextNode('\u2014 ' + cite));
		     quotes[i].appendChild(newdiv);
		   }
		 }
	  }
  }
}, false); 
