<?xml version="1.0"?>
<!--
very much based on the XBL from http://www.squarefree.com/userstyles/xbl.html
I did it myself because I wanted to play with XBL a bit, because I
wanted to change the behavior a bit, and because the original was rather
poorly commented.
-->
<bindings xmlns="http://www.mozilla.org/xbl">
 <binding id="clicktoview">
  <implementation>
   <constructor>
    <![CDATA[
// copy this and this.parentNode for the setTimeout hack, below
var applet = this;
var appParent = this.parentNode;

// prevent replacement the second time around (i.e. when we explicitly
// request it by clicking on the div.)  we check parentNode in case we
// have a nested element (e.g. nested <object>s)
if (applet.clickToViewProcessed || applet.parentNode.clickToViewProcessed)
	return;
applet.clickToViewProcessed = true;

// FIXME: I've seen this end up with negative numbers O_o
var w = parseInt(applet.width || applet.style.width || applet.getAttribute('width'));
var h = parseInt(applet.height || applet.style.height || applet.getAttribute('height'));

// ! catches NaN
if (!w || w < 32) w = 32;
if (!h || h < 32) h = 32;

// create the replacement element
var replacement = document.createElement('div');

replacement.style.setProperty('width', w + 'px', '');
replacement.style.setProperty('height', h + 'px', '');
replacement.style.setProperty('background-color', 'ThreeDFace', '');
replacement.style.setProperty('text-align', 'center', '');
replacement.style.setProperty('cursor', 'pointer', '');
replacement.style.setProperty('overflow', 'hidden', '');
replacement.style.setProperty('-moz-outline', '1px dashed red', '');

// create an inner span so that we can vertically align the inner text
var message = document.createElement('span');

// this feels like a total hack.  I don't know of a better way, though.
message.style.setProperty('vertical-align', 'middle', '');
message.style.setProperty('line-height', h + 'px', '');
message.style.setProperty('color', 'ThreeDText', '');

message.innerHTML = '&#x261b; Click to load';
if (applet.type)
	message.innerHTML += ' (<i>' + applet.type + '</i>)';
else if (applet.id)
	message.innerHTML += ' (<i>id: ' + applet.id + '</i>)';
else if (applet.classid)
	message.innerHTML += ' (<i>' + applet.classid + '</i>)';
else if (applet.tagName)
	message.innerHTML += ' (<i>element: ' + applet.tagName + '</i>)';

replacement.appendChild(message);

// without this 
setTimeout(function ()
{
	appParent.insertBefore(replacement, applet);
	appParent.removeChild(applet);
}, 0);

replacement.onclick = function ()
{
	appParent.insertBefore(applet, replacement);
	appParent.removeChild(replacement);
}
    ]]>
   </constructor>
  </implementation>
 </binding>
</bindings>
