/**
 *  RDFa Clipboard
 *
 *  Ben Adida - ben@adida.net
 *
 *  2007-03-02 rolled back into main RDFa tree
 * 
 *  2006-12-11 - first version
 *
 *  licensed under GPL v2
 */

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

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

// configuration information
RDFA.url = __RDFA_BASE + 'rdfa.js';
CLIP.base = __RDFA_BASE + 'rdfa-clipboard/';

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

CLIP.makeAllLinksAbsolute = function(el) {
  // let's not do images because IE is stupid
  if (el.nodeName=='IMG')
    return;
  
  var href= RDFA.getNodeAttributeValue(el,'href');
  if (href != null) {
      RDFA.setNodeAttributeValue(el,'href',RDFA.getAbsoluteURI(href));
  }
  
  // recurse down the children
  var children = el.childNodes;
  for (var i=0; i < children.length; i++) {
    CLIP.makeAllLinksAbsolute(children[i]);
  }
};

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

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

	var copy_html = "<div ";
	if (subject.termType == 'bnode') {
	    copy_html += 'rel="rdf:item"';
	} else {
	    copy_html += 'about="' + subject.toString() + '"';
	}

  // copy the innerHTML somewhere else first so we can modify the HREFs to be absolute
  var dummy_el = document.createElement('div');
  dummy_el.innerHTML = element.innerHTML;
  CLIP.makeAllLinksAbsolute(dummy_el);

	copy_html += ' ' + xmlns_stuff + '>' + dummy_el.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(' + CLIP.base + 'liveclipboard-icon-16x16.gif)' ;

	// set up the textarea with opacity 0
	var ta_el = document.createElement('textarea');
	ta_el.value = copy_html;	  
	ta_el.rows= 1;
	ta_el.cols= 1;
	if (window.clipboardData)
	  ta_el.style.visibility = 'hidden';
	ta_el.style.opacity = 0;
	ta_el.cursor = 'pointer';
	ta_el.style.overflow = 'hidden';

	cp_div.onclick = function() {
	    if (window.clipboardData) {
	      window.clipboardData.setData("Text",ta_el.value);
	    } else {
	      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);
};


//
// 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.CALLBACK_DONE_PARSING = function() {
  var dummy = new Object();
  for (var i=0; i<RDFA.elements.length; i++) {
    var el = RDFA.elements[i];
    CLIP.setupPopup(el, el._RDFA_SUBJECT);
  }
};

RDFA.load();
