/* ```````````` NATIVE SEARCH ``````````````` */

/* <license rdf:resource="http://creativecommons.org/licenses/by-nc-sa/2.5/" /> */
/* Native Blog Search by Aditya Mukherjee (www.aditya-mukherjee.com) */

/* Modified to do Recursive Backlinks by Greg Hill (http://www.greg-hill.id.au) 25/5/2006 */
/* search query becomes "link:<this_URL>" */

function backlinker(targURL, targID)
{
if (typeof(bc)=='undefined')
	bc=0;

msg=document.createTextNode('   ... loading ... ');

if (!targID)
	targID = 'backlinks';
else
	orig = document.createTextNode('');

targNode = document.getElementById(targID);

if (targID.search('backlinks')==0)
	orig = targNode.cloneNode(true);

targNode.replaceChild(msg, targNode.firstChild);

if (!targURL)
	targURL=window.location.href;

re=new RegExp("[#?].+","ig");

thisURL = encodeURIComponent(targURL.replace(re,''));

if (thisURL.search('http')==0)			// add link for http queries
	thisURL='link:'+thisURL;
else								// otherwise, restrict to domain
{
	thisURL=thisURL.replace(/ /ig,'+')+'+blogurl:'+window.location.hostname;
	orig=document.createTextNode('No results.');
}

searchURL = encodeURIComponent('http://blogsearch.google.com/blogsearch_feeds?hl=en&q='+thisURL+'&ie=utf-8&num=10&output=rss');

var launchURL='http://xoxotools.ning.com/outlineconvert.php?output=json&classes=item&url='+searchURL+'&xn_auth=no&callback=ProcQuery&submit=Convert';

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = launchURL;
document.getElementsByTagName("head")[0].appendChild(script);
}

function ProcQuery(obj)
{
var i=0;
var results = new Array();

if(!obj.item)						// no items
{
	targNode.replaceChild(orig, msg);
	return;
}

if (typeof(obj.title)=='undefined')			// no items
{
	targNode.replaceChild(orig, msg);
	return;
}

results=obj.item;						// more than zero items

var ul=document.createElement('ul');

targNode.replaceChild(ul, msg);

for(var i=0; i<results.length; i++)
{
	bc++;

	var li=document.createElement('li');
	var app=document.createElement('a');
	var rec=document.createElement('div');
	var btn=document.createElement('a');

	rec.id='list'+bc;
	rec.style.display='inline';

	btn.href='javascript: backlinker("'+results[i].link+'", "list'+ bc+'");';
	btn.innerHTML='&nbsp;&nbsp;~&gt;';
	btn.title='See what links to this post';

	app.href=results[i].link;
	app.innerHTML=results[i].title;
	app.title=results[i].description.replace(/<[^>]+>/ig,'');

	li.appendChild(app);
	li.appendChild(rec);
	rec.appendChild(btn);
	ul.appendChild(li);
}

}
