// Add links to citation source (cite attribute) to blockquote elements

/*
Script source:
  Dunstan Orchard http://www.1976design.com/blog/
  http://dealmeida.net/js/46_blockquotes.js
  Altered from original idea by Simon Willison at:
  http://simon.incutio.com/archive/2002/12/20/blockquoteCitations


Changes:
  Made the alert conditional, only shows when there is something to report,
  and when variable 'showalert' is 1.
  Removed the 'onsubmit' removal.
  Simplified alert message.
  This adaption made by liorean, 2005-03-20. Repackaged by Rijk.
*/

function addCitationLinks(){
//    if(confirm('Cancel addCitationLinks?'))
//        return;
    var
        i=0,
        elm,
        cite,
        coll=document.getElementsByTagName('blockquote'),
        oP=document.createElement('p');
    oP.className='source';
//    alert(coll.length);
    while(elm=coll.item(i++))
        if(cite=elm.getAttribute('cite')){
//            alert(cite);
            if(/http:\/\/|ftp:\/\//i.test(cite))
                appendCite(
                    cite.replace(/^(?:http|ftp):\/\/(.{32}).+/,'$1\u2026'),
                    cite,
                    'Go to '+cite);
            else if(/urn:quote:/i.test(cite))
                appendCite(
                    (cite=cite.replace(/urn:quote:/g,'').replace(/\s/g,'_')),
                    'http://www.quotationspage.com/quotes/'+cite);
            else if(/urn:author:/i.test(cite))
                appendCite(
                    cite.replace(/%20/g,' ').replace(/urn:author:/g,''));
            else
                appendCite(
                    cite.replace(/%20/g,' '));
        }
    function appendCite(text,href,title){ // Doesn't that sound nasty?
        var
            p=oP.cloneNode(false);
        p.appendChild(document.createTextNode('\u2014'+(href!=undefined?'':text)));
        if(href!=undefined){
            (tmp=document.createElement('a'))
                .setAttribute('href',href);
            tmp.appendChild(document.createTextNode(text));
            if(title!=undefined)
                tmp.setAttribute('title',title);
            p.appendChild(tmp);
        }
        elm.appendChild(p);
    }
} 

