// ==UserScript==
// @name          Friend Link
// @namespace     http://www.ts0.com/
// @description   Automatically turns names of people you know into inline microformat links to their sites
// @include       *
// ==/UserScript==


/*
sorry code is kinda messy and hacky, I promise I'll rewrite it at some point!
*/

if (window==top)
{
	GM_registerMenuCommand ("Edit Friends Names",editor);
}

function editor()
{
	//owin = window.open("about:blank");
	document.open();
	document.write('<html><head><title>FriendLink Configuration</title>');
	document.write('<script>function change(e){item=e.target}\n');
	document.write('function addItem(e){btm=e.target; newItem=document.createElement("div"); newItem.innerHTML = "<input onchange=\\"change(event)\\"/><input onchange=\\"change(event)\\"/><input onchange=\\"change(event)\\"/><input onchange=\\"change(event)\\" value=\\"http://\\"/>"; btm.parentNode.insertBefore(newItem, btm);}\n');
	document.write('</script>');
	document.write('</head><body style="font:normal 12px verdana;"><b>FriendLink Configuration</b><br>Just enter short name, full name, <a href=\"http://www.gmpg.org/xfn/background#friendship\">XFN values</a> and urls below then click save.<br><br><div id="names">');
	
	aryNames = GM_getValue("names","Thom").split("|");
	aryFullNames = GM_getValue("fullnames","Thom Shannon").split("|");
    aryXFN = GM_getValue("XFN","").split("|");
	aryUrls = GM_getValue("urls","http://www.ts0.com/").split("|");
	for (x=0;x<aryNames.length;x++)
	{
		document.write('<div id="item"><input onchange="change(event)" value="'+aryNames[x]+'"/><input value="'+aryFullNames[x]+'" onchange="change(event)"/><input value="'+aryXFN[x]+'" onchange="change(event)"/><input value="'+aryUrls[x]+'" onchange="change(event)"/></div>');
	}
	document.write('<a href="#" onclick="addItem(event);return false;">Add</a></div><br><button id="svBtn">Save</buttton></body></html>');
	document.close();
	document.getElementById('svBtn').addEventListener('click', function(e) {
		aryNames = new Array();
		aryFullNames = new Array();
		aryXFN = new Array();
		aryUrls = new Array();
  	for(x=0;x<e.target.ownerDocument.getElementById("names").childNodes.length;x++)
  	{
  		dItem=e.target.ownerDocument.getElementById("names").childNodes[x];
  		if(dItem.tagName=="DIV")
  		{
  			if (dItem.firstChild.value!='' && dItem.firstChild.nextSibling.value!='')
  			{
  				aryNames.push(dItem.firstChild.value);
  				aryFullNames.push(dItem.firstChild.nextSibling.value);
  				aryXFN.push(dItem.firstChild.nextSibling.nextSibling.value);
  				aryUrls.push(dItem.firstChild.nextSibling.nextSibling.nextSibling.value);
  			}
  		}
  	}
  	GM_setValue("names",aryNames.join('|'));
  	GM_setValue("fullnames",aryFullNames.join('|'));
  	GM_setValue("XFN",aryXFN.join('|'));
  	GM_setValue("urls",aryUrls.join('|'));
  	alert('Saved');
	}, true);
}

document.addEventListener('keyup', function(e) {
	if(document.getElementById('friendsLinkUndo'))
	{
		document.getElementById('friendsLinkUndo').parentNode.removeChild(document.getElementById('friendsLinkUndo'));
	}
	if(e.target.tagName=="TEXTAREA")
	{
		aryNames = GM_getValue("names","Thom").split("|");
		aryFullNames = GM_getValue("fullnames","Thom Shannon").split("|");
		aryXFN = GM_getValue("XFN","").split("|");
		aryUrls = GM_getValue("urls","http://www.ts0.com/").split("|");
		for (x=0;x<aryNames.length;x++)
		{
			strFind=aryNames[x];
			if (e.target.value.substr(e.target.selectionStart-strFind.length,strFind.length).toLowerCase()==strFind.toLowerCase() && String.fromCharCode(e.keyCode).toLowerCase() == strFind.substr(strFind.length-1).toLowerCase())
			{
				startLink = "<span class=\"vcard\"><abbr class=\"fn\" title=\""+aryFullNames[x]+"\"><a class=\"url\" rel=\""+aryXFN[x]+"\" href=\""+aryUrls[x]+"\">";
				endLink = "</a></abbr></span>";
				previousText = e.target.value;
				previousPos = e.target.selectionStart;
				pos = e.target.selectionStart+startLink.length+endLink.length;
				e.target.value = e.target.value.substr(0, e.target.selectionStart-strFind.length) + 
				startLink + e.target.value.substr(e.target.selectionStart-strFind.length,strFind.length) + 
				endLink + e.target.value.substr(e.target.selectionStart);
				e.target.selectionStart = pos;
				e.target.selectionEnd = pos;
				
				break;
			}
		}
	}

}, true);

