/**
 *  RDFa Clipboard
 *
 *  Ben Adida - ben@adida.net
 *
 *  2006-12-25 - 
 * 
 *  2006-12-11 - first version
 *
 *  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 = "";

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

// configuration information
CLIP.overlib_url = __RDFA_BASE + __RDFA_VERSION_SUBDIR + 'overlib.js';
RDFA.url = __RDFA_BASE + __RDFA_VERSION_SUBDIR + '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) {
};

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

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

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

CLIP.quoteHTML = function(html) {
  var div = document.createElement("div");
  var text = document.createTextNode(html);
  div.appendChild(text);
  return div.innerHTML.replace(/\n/g,'<br />');
};

// set up an overlib popup for an element
CLIP.setupPopup = function (element, subject) {
	var xmlns_stuff = '';

	element._RDFA_NAMESPACES.each(function(one_ns) {
	  if (one_ns == '')
		return;
	  xmlns_stuff += ' xmlns:' + one_ns + '="' + element._RDFA_NAMESPACES[one_ns].uri + '"';
	});

	var copy_html = "<div ";
	if (subject.substring(0,3) == "[_:") {
	    copy_html += 'rel="rdf:item"';
	} else {
	    var subject_target = document.createElement('a');
	    subject_target.href = subject;
	    copy_html += 'about="' + subject_target.href + '"';
	}

	copy_html += ' ' + xmlns_stuff + '>' + element.innerHTML + '</div>';

	// set up a place to copy and paste
	var cp_div = document.createElement('div');
	cp_div.style.cssFloat = 'right';
	cp_div.style.width = '16px';
	cp_div.style.height = '16px';
	cp_div.style.margin = 0;
	cp_div.style.padding= 0;
	cp_div.style.zIndex= 100000;
	cp_div.style.backgroundImage = 'url(' + __RDFA_BASE + 'liveclipboard-icon/default/images/gif-light/liveclipboard-icon-16x16.gif)' ;

	// set up the textarea with opacity 0
	var ta_el = document.createElement('textarea');
	if (document.baseURI)
	ta_el.textContent = copy_html;
	else
	ta_el.innerText = copy_html;
	ta_el.rows= 1;
	ta_el.cols= 1;
	ta_el.style.opacity = 0;
	ta_el.cursor = 'pointer';
	ta_el.style.overflow = 'hidden';

	cp_div.onclick = function() {
	    ta_el.selectionStart = 0;
	    ta_el.selectionEnd = ta_el.value.length;
	    ta_el.focus();
	};

	cp_div.appendChild(ta_el);

	element.insertBefore(cp_div, element.firstChild);

	// set up an overlib
	/*
element.onmouseover = function() {
		var f= overlib('<form><textarea id="rdfa_clipboard" rows="0" cols="0" style="float: left; font-size: 0pt; height:0px; width: 0px;">' + CLIP.quoteHTML(copy_html) + '</textarea><font size="+2">Press Ctrl-C to Copy</font></form>',ABOVE,CENTER,STICKY,CAPTION,'Copy &amp; Paste',WIDTH,300,HEIGHT,40,CLOSECLICK);
		var textarea = document.getElementById('rdfa_clipboard');
		textarea.selectionStart = 0;
		textarea.selectionEnd = textarea.textContent.length;
		textarea.focus();
		return f;
	};
	element.onmouseout = function() {return nd();};
	*/

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

// overlib setup
var AFTER_OVERLIB = null;
CLIP.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 = CLIP.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
CLIP.setupOverlib(function(){
    RDFA.load();
});

RDFA.CALLBACK_DONE_PARSING = function() {
  var dummy = new Object();
  for (var subj in RDFA.elements) {
    if (subj in dummy)
	continue;

    var el = RDFA.elements[subj];
    CLIP.setupPopup(el, subj);
  }
};
