/**
 *	Highlight RDFa metadata in Javascript
 *  using Overlib and the RDFa Javascript parser
 *
 *	Ben Adida - ben@mit.edu
 *  2006-03-20
 *  2006-05-22 moved to W3
 *  2006-10-08 made portable
 *
 *
 *	licensed under GPL v2
 */

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

// two simple namespaces as objects
var HIGHLIGHT = new Object();
var RDFA = new Object();

// configuration information
HIGHLIGHT.overlib_url = __RDFA_BASE + 'overlib.js';
RDFA.url = __RDFA_BASE + 'rdfa.js';

// a function that is called on an element when a triple pertains to it
// with the element being the literal object
RDFA.CALLBACK_NEW_TRIPLE_WITH_LITERAL_OBJECT = function(el, triple) {
	HIGHLIGHT.setupPopup(el,triple.toString());
}

// a function that is called on an element when a triple pertains to it
// with the element being the clickable link for a URI object
RDFA.CALLBACK_NEW_TRIPLE_WITH_URI_OBJECT = function(el, triple) {
	HIGHLIGHT.setupPopup(el,triple.toString());
}

// a function that is called on an element when a triple pertains to it
// with the element being the subject of the assertions
RDFA.CALLBACK_NEW_TRIPLE_WITH_SUBJECT = function(el, triple) {
    // nothing
}

// what happens when it's done parsing
RDFA.CALLBACK_DONE_PARSING = function() {
    // alert('done parsing!');
}


// set up an overlib popup for an element
HIGHLIGHT.setupPopup = function (element, message) {
  // HTML-escape the message
  message = message.replace(/</g,'&lt;').replace(/>/g,'&gt;');
  
	// store the accumulated messages, in case there's more than one triple here.
	if (!element.rdfa_message) {
		element.rdfa_message = '';
	}
	element.rdfa_message += message + '<br />';

	// set up an overlib
	element.onmouseover = function() {return overlib(element.rdfa_message,ABOVE,CENTER,STICKY,CLOSECLICK,CAPTION,'RDFa Triples',WIDTH,400);};
	element.onmouseout = function() {return nd();};

	// mark the element with a span
	element.style.border= "3px solid red";
}

// overlib setup
var AFTER_OVERLIB = null;
HIGHLIGHT.setupOverlib = function(callback) {
	// create the overlib DIV
	overlib_div = document.createElement("div");
	overlib_div.id = 'overDiv';
	overlib_div.style.position = 'absolute';
	overlib_div.style.visibility = 'hidden';
	overlib_div.style.zIndex = 1000;
	overlib_div.innerHTML = '';

	// insert it into the DOM tree
	document.getElementsByTagName('body')[0].appendChild(overlib_div);

	// set up the overlib callback function
	AFTER_OVERLIB = function() {
		// tell overlib everything is loaded, because given that it's getting
		// dynamically loaded, it won't get the window.onload event.
		olLoaded = 1;
		callback();
	};

    var s = document.createElement("script");
    s.type = 'text/javascript';
    s.src = HIGHLIGHT.overlib_url;

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


//
// 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);
}

// setup overlib and when it's done, traverse RDFa
HIGHLIGHT.setupOverlib(function(){
    RDFA.load();
});
