/**
 * WordPress + CC Attribution Bookmarklet backend
 *
 *	Ben Adida - ben@mit.edu
 *  2006-03-21
 *      Nathan R. Yergler - nathan@yergler.net
 *  2006-03-25
 *  2006-05-22 moved to W3
 *
 *	licensed under GPL v2
 */


// EXPECTING __RDFA_BASE
if (typeof(__RDFA_BASE) == 'undefined')
  __RDFA_BASE = 'http://www.w3.org/2001/sw/BestPractices/HTML/rdfa-bookmarklet/';

var __RDFA_VERSION_SUBDIR = '2006-05-22/';

// configuration information
var RDFA = new Object();
RDFA.url = __RDFA_BASE + __RDFA_VERSION_SUBDIR + 'rdfa.js';

RDFA.getObject = function(subject, predicate_ns, predicate_name) {
  // Return the object (uri or literal text) for the subject and predicate;
  // The actual predicate is constructed from the RDFA.Namespace object
  // (predicate_ns) and the predicate_name string.
  //
  // If no matching triple is found, returns null.

  var curie = new RDFA.CURIE(predicate_ns, predicate_name);
  var triples = RDFA.getTriples(subject, curie);

  if (triples) {
     return triples[0].object;
  }

  return null;

} // getObject

RDFA.CALLBACK_DONE_PARSING = function() {
    var xhtml = new RDFA.Namespace('xhtml', 'http://www.w3.org/1999/xhtml');
    var cc = new RDFA.Namespace('cc','http://web.resource.org/cc/');
    var dc = new RDFA.Namespace('dc', 'http://purl.org/dc/elements/1.0/');

    var license = (RDFA.getObject('', cc, 'license') || RDFA.getObject('', xhtml, 'license'));

    blogText  = RDFA.getObject('', dc, 'title') + ' (';
    blogText += '<a href="' + RDFA.getObject('', cc, 'citeURL') + '">';
    blogText += RDFA.getObject('', cc, 'attributedTo') + '</a>';

    if (RDFA.getObject('',cc,'derivativeDescription'))
        blogText += ', ' + RDFA.getObject('', cc, 'derivativeDescription');

    blogText += ') ';

    if(navigator.userAgent.indexOf('Safari') >= 0){
         Q=getSelection();
    } else {
         Q=document.selection?document.selection.createRange().text:document.getSelection();
    }
    Q = Q + '\n\n' + blogText;

    wp_admin = window.wp_admin_url;

    location.href= wp_admin + '/post.php?text='+encodeURIComponent(Q)+'&popupurl='+encodeURIComponent(location.href)+'&popuptitle='+encodeURIComponent(document.title);

} // RDFA.CALLBACK_DONE_PARSING

//
// Everything below is used only for loading the RDF/A javascript.
// You probably don't need to look at it.
//

// callback when the RDF/A parsing is done.
RDFA.CALLBACK_DONE_LOADING = function() {
    RDFA.parse();
}

RDFA.load = function()
{
    var s = document.createElement("script");
    s.type = 'text/javascript';
    s.src = RDFA.url;

    // add it to the document tree, load it up!
    document.getElementsByTagName('head')[0].appendChild(s);
}

RDFA.load();
