/**
 * The RDFa Javascript template.
 *
 * Ben Adida - ben@mit.edu
 * 2006-03-21
 * 2006-05-22 moved to W3C
 *
 * licensed under GPL v2
 */


// EXPECTING __RDFA_BASE
if (typeof(__RDFA_BASE) == 'undefined')
  __RDFA_BASE = 'http://www.w3.org/2006/07/SWD/RDFa/impl/js/20070301/';

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

RDFA.CALLBACK_DONE_PARSING = function() {
    var N3_TEXT = "";

    // go through all the triples by subject
    RDFA.object_each(RDFA.triples, function(s) {
      var new_subject_p = true;
      
      // go through all the predicates
      RDFA.object_each(RDFA.triples[s], function(p) {
        var objects = RDFA.triples[s][p];
        
        // go through all the objects
        for (var i=0; i<objects.length; i++) {
          if (new_subject_p) {
            if (N3_TEXT != '')
              N3_TEXT += '.\n\n';
            
            N3_TEXT += objects[i].prettySubject() + '\n';
            new_subject_p = false;
          } else {
            N3_TEXT += ';\n';
          }
          
          N3_TEXT += '\t' + objects[i].prettyPredicate(true) + ' ' + objects[i].prettyObject() + ' ';
        }
      });
    });
    
    N3_TEXT += '.';
    
    try {
      var n3_b64 = btoa(N3_TEXT);
      var n3_uri = 'data:text/rdf+n3;base64,' + n3_b64;
    
      document.location= n3_uri;
    } catch (e) {
      alert('unable to download N3:\n\n' + N3_TEXT);
    }
}

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

// callback when the RDFa 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();