// 
//
// (c) 2009 iSpeech Inc.
// No portion of this material can be redistributed 
// without the explicit permission of iSpeech Inc.
//
// 
// Version 1.0
//
var idCount =0;
var curId=-1;
var allTags="";
function trace(t){alert(t);}
function scanNode(n,f)
{
	var h3nodes = n.getElementsByTagName("h3");	
	for(var x=0;x<h3nodes.length;x++)
    {
		if(h3nodes[x].className.toLowerCase().indexOf(f.toLowerCase())>-1)
		{
			
			var perma = getPermaLink(h3nodes[x]);
			var nodes = h3nodes[x].parentNode.getElementsByTagName("div");		
			nodes[1].insertBefore(createiSpeechNode(perma),nodes[1].firstChild);
		}
	}	
} 
function getPermaLink(n)
{
	if (n.firstChild.nodeName=="A")
		return n.firstChild.href;
	else
	{
		var anodes = n.parentNode.getElementsByTagName("a");
		for(var x=0;x<anodes.length;x++)
		{
			if (anodes[x].className.toLowerCase().indexOf('permalink')>-1)
				return anodes[x].href;
		}
		return n.firstChild.nextSibling.href;
	}
}

function createiSpeechNode(p)
{
	var s= document.createElement("span");
	var im=document.createElement("img");
	im.src="http://www.ispeech.org/images/listen.gif";	
	im.alt="Listen to this post. Powered by iSpeech.org";
	im.title="Listen to this post. Powered by iSpeech.org";
	idCount+=1;
	var id=idCount;
	im.onclick=function(){showPlayer(im,p,id);};
	im.style.border="none";
	im.style.margin="0px";
	im.style.cursor="pointer";
	im.width="77";
	im.height="18";	

	var iframe= document.createElement("iframe");
	iframe.style.border="none";
	iframe.frameBorder="0";
	iframe.width="0";
	iframe.height="0";
	iframe.id = "ispeech_iframe_" + id.toString();
	
	s.appendChild(im);
	s.appendChild(document.createElement("br"));
	s.appendChild(iframe);
	return s;
}
function ge(e){return document.getElementById(e);}
function showPlayer(im,p,id)
{
	if(curId!=id)
	{
		if(curId!=-1)
			closeAllPlayers();
		curId=id;
		var ifr = ge("ispeech_iframe_" + id.toString());
		ifr.src="http://www.ispeech.org/redirect.php?u=" + escape(p);
		//ifr.style.width="235px";
		//ifr.style.height="120px";
		ifr.style.width="204px";
		ifr.style.height="56px";
	}
	else
		closeAllPlayers();
}
function closeAllPlayers()
{
	for(var i = 1;i<=idCount;i++)
	{
		var ifr = ge("ispeech_iframe_" + i.toString());
		ifr.src="about:blank";
		ifr.style.width="0px";
		ifr.style.height="0px";
	}
	curId=-1;
}
function startUp()
{	
	scanNode( ge('pagebody'),'entry-header');	
}
function init()
{
	window.onload=startUp;
	document.write('<a href="http://www.ispeech.org/podcasts/?url='+ escape(location.href) +'">View Podcast</a>');
}
init();

/*
<form action="https://www.typepad.com/t/app/weblog/design/widgets" method="post">
  <input type="hidden" name="service_key" value="52616e646f6d4956fcb18a0458c1292c8871fb2ce397e7cb02622795de772eb42add78636c637a8e9bed516ea2b0001c " />
  <input type="hidden" name="service_name" value="iSpeech Listen Button" />
  <input type="hidden" name="service_url" value="http://www.ispeech.org/" />
  <input type="hidden" name="long_name" value="iSpeech Listen Button" />
  <input type="hidden" name="short_name" value="ispeech_listen" />
  <input type="hidden" name="content" value="<!-- iSpeech Listen Button --><script type=&quot;text/javascript&quot; src=&quot;http://www.ispeech.org/scripts/scripts.php?src=tpe&amp;ver=1.0&quot;></script><!-- End iSpeech Listen Button -->"
  <input type="hidden" name="return_url" value="http://www.ispeech.org/affiliate.php" />
  <input type="submit" name="submit" value="Install Widget on TypePad" />
</form>
//*/