/*

accite - Making the cite attribute useful!
 ~ now cranked to the fucking max with Prototype!
by Seth Thomas Rasmussen
http://sethrasmussen.com/

COPYRIGHT: Liscensed under the same terms as all content 
on http://sethrasmussen.com/

DESCRIPTION: Auto-links <blockquote /> and <q /> 
elements using their cite attributes.

PARAMETERS:

- bqatitle
	Optional: specifies the title attribute for the source link inserted into <blockquote /> elements;
	Defaults to "View this blockquote's source";
- bqatxt
	Optional: specifies custom text for the source link inserted for each <blockquote /> element;
	Defaults to the URI specified in the cite attribute;
- bqtrimlen
	Optional: specifies a character length at which to trim the source link text;
	Makes most sense if you're not using the previous parameter, and the URI cited is very long;
- bqpclass
	Optional: specifies a class to apply to the inserted <p /> element which holds the <blockquote /> source link;

- qatitle
	Optional: specifies the title attribute of the <a /> element created to wrap the quote;
	Defaults to "View this quote's source";
- qclass
	Optional: specifies a class to apply to the <q /> element;

*/

var d = document

var Accite = {
  bq_link_text: '[ ~ ]',
	bq_link_title: "View this blockquote's source",
  q_link_title:  "View this quote's source",
  
  bq_source_class: null,
  q_class: null,
  
  process_quotes: function(quotes) {
    quotes.each(function(q,i){
      var cite = q.getAttribute('cite')
      if (cite) {
        var source_a = d.createElement('a')
        source_a.setAttribute('href', cite)
        source_a.setAttribute('rel', 'external')
        source_a.setAttribute('title', Accite.q_link_title)
        if (Accite.q_class) {source_a.setAttribute('class', Accite.q_class)}
        
        var q2 = q.cloneNode(true)
        
        for (var c=0; c<q.childNodes.length; c++) {q.removeChild(q.childNodes[c])}
        for (var c=0; c<q2.childNodes.length; c++) {source_a.appendChild(q2.childNodes[c])}
        
        q.appendChild(source_a)
      }
    })
  },
  
  process_blockquotes: function(blockquotes) {
    blockquotes.each(function(bq,i){        
      var cite = bq.getAttribute('cite')
      if (cite) {
        var source = d.createElement('p')
        if (Accite.bq_source_class) {
          source.setAttribute('class', Accite.bq_source_class)
        }
        
        var source_a = d.createElement('a')
        source_a.setAttribute('href', cite)
        source_a.setAttribute('rel', 'external')
        source_a.setAttribute('title', Accite.bq_link_title)
        
        var source_a_txt = d.createTextNode(Accite.bq_link_text)
        source_a.appendChild(source_a_txt)
        
        source.appendChild(source_a)
        bq.appendChild(source)
      }
    })
  },
  
  // init
  get_accited: function() {
    var blockquotes = $$('blockquote')
    if (blockquotes) Accite.process_blockquotes(blockquotes)
    
    var quotes = $$('q')
    if (quotes) Accite.process_quotes(quotes)
  }
}

Event.observe(window, 'load', Accite.get_accited)

