// TagOverlay v0.1
// http://www.greg-hill.id.au (CC - 2.1a)
// Imports delicious tags, highlights current text and provides pop-up box
// See http://blogfresh.blogspot.com

// Constants

var origText, bText="", hiCol="30", boxCol="180", maxCount=0;

hiStart="<span title='!tagCount!' onMouseOver='tagOver(this);' class='Tag-!tagLabel!'>";

hiEnd="</span><layer style='display: none; z-index: 101; position: absolute;' onClick='this.style.display=\"none\";'></layer>";

// Functions

function doHiRexp(tagLabel, tagCount)
{
var hiStartRep = hiStart.replace(/!tagCount!/ig, tagCount).replace(/!tagLabel!/ig, tagLabel);
var first = tagLabel.charAt(0);
var rest = tagLabel.substr(1);
var cap = first.toUpperCase();

var re = new RegExp('(>[^<>]*\\s+|>)(['+first+cap+']'+rest+')([^<>]*<)','g'); 
bText=bText.replace(re, '$1'+hiStartRep+'$2'+hiEnd+'$3');

return;
}

function procTagOverlay( obj )
{
if (origText)
{
	alert('Reverting to original text');
	document.body.innerHTML=origText;
	origText="";
	return;
}

if (!document.body || typeof(document.body.innerHTML) == "undefined")
{
	alert("TagOverlay has problems with this webpage. No results returned.");
	return false;
}

if (maxCount==0)
	for(var o in obj)
		if (obj[o]>maxCount)
			maxCount=obj[o];

bText = document.body.innerHTML;

for(var o in obj)
	doHiRexp(o, obj[o]);

origText=document.body.innerHTML;  
document.body.innerHTML = bText;

// build layers
var d = new Array();
x=document.getElementsByTagName('layer'); 
for (var i=0; i<x.length;i++)
	if(!x[i].innerHTML)
		x[i]=boxBuild(x[i]);

return true;
}

function tagOver( d )
{
e=d.nextSibling;  // span, layer

if (e)
{	if (e.style.display=="none")
	{	e.style.display="inline";
		setTimeout('e.style.display="none";',15000);
	}
	else
		e.style.display="none";
}
else
	alert('tagOver: no sibling! d.innerHTML='+d.innerHTML);

return;
}

function boxBuild( l )
{
var d = l.previousSibling;  // span, layer
var tags = d.className.toString().substring(4);
var count = d.title.toString();

var h="<p align='justify'><em>"+tags+":</em><br/><br/>";
for(var i=0; i<actions.length; i++)
	h+="<a target='_blank' title='"+actions[i].title+"' href='"+actions[i].url+"'><img src='"+actions[i].img+"'/></a><br/><br/>";
h+="</p><br/>";

//d.style.backgroundColor="hsla("+hiCol+",100%,50%, "+count/maxCount+")";
//d.style.backgroundColor="hsla("+hiCol+", 100%, 50%, 1.0)";
d.style.backgroundColor="hsl("+hiCol+",100%,50%)";
//d.style.backgroundColor="hsl("+hiCol+",100%,"+count/maxCount*100+"%)";
d.style.color="black";

l.width="15em";
l.style.padding="5px";
l.style.margin="10px";
l.style.backgroundColor="hsl("+boxCol+",50%,75%)";
l.style.color="hsl("+boxCol+",50%,10%)";
l.style.border="solid hsl("+boxCol+",50%,25%)";
l.style.fontSize="x-small";

l.innerHTML=h.replace(/!tags!/ig, tags);

return l;
}

// Code to Run on Button Push

if (typeof(delusr)=='undefined')
{
	delusr="jrjf44";
	anch="blogger-hack";
}

if (typeof(anch)=='undefined')
{
	anch="";
}

if (typeof(actions)=='undefined')
	actions = [{"title":"See !delusr! on delicious", "url":"http://del.icio.us/!delusr!/!anch!+!tags!", "img": "http://del.icio.us/static/img/delicious.small.gif"}, {"title": "Search on Technorati", "url":"http://www.technorati.com/tag/!tags!", "img":"http://static.technorati.com/pix/icn-talkbubble.gif"},{"title":"Lookup on Wikipedia", "url":"http://en.wikipedia.org/wiki/!tags!", "img":"http://en.wikipedia.org/favicon.ico"}];

for(var i=0; i<actions.length; i++)
{
	actions[i].url=actions[i].url.replace(/!delusr!/ig, delusr).replace(/!anch!/ig, anch);
	actions[i].title=actions[i].title.replace(/!delusr!/ig, delusr).replace(/!anch!/ig, anch);
}

var s=document.createElement('script');
s.type='text/javascript';s.src="http://del.icio.us/feeds/json/tags/"+delusr+"/"+anch+"?callback=procTagOverlay";
document.getElementsByTagName("head")[0].appendChild(s);
