/*
  Description:Adds a proReader button to all liks where pdf is found, either in the link text or in the link istelf.
*/

function filter(links)
{
	var lnk_url = links.href;
	var lnk_text = links.innerHTML.toLowerCase();
	var txt = ("pdf");	//The text that the script should look for, will work with doc, xls, txt and more
	if((lnk_url.indexOf(txt)-3) > -1) //If the url ends with the string given in txt, return true
	{
		return(true);
	}
	else if(lnk_text.indexOf(txt) > -1) //If the url contains the string txt, return true
	{
		return(true);
	}
	else if(lnk_url.indexOf("doc_download") > -1 && lnk_url.indexOf("app.readspeaker") == -1)
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

/*
If an url isn't complete this function will try to use repair the pdf link
*/
function returnUrl(pdf_url)
{
	full_url = "";
	if(pdf_url.indexOf("http") > -1 || pdf_url.indexOf("https") > -1)	//If the url to the pdf contains http or https the link should be complete
	{
		full_url = pdf_url;
	}
	else if(pdf_url.indexOf("/") == 0)	//If the link starts with a / , adding the domain should complete most of the urls
	{
		full_url = document.loaction.protocol + "://" + document.location.hostname + pdf_url;
	}
	else if(pdf_url.indexOf("/") == -1)	//If the url doens't contain a / the script completes the url by adding the domain and the path to the pdf link
	{
		full_url = document.loaction.protocol + "://" + document.location.hostname + document.location.pathname + pdf_url;
	}
	else
	{
		full_url = pdf_url;
	}
}
/*
Creates the proReader button and add it after the link

*/
function addButton(thelink) {
  // build proReader href
  var prhref="http://app.readspeaker.com/cgi-bin/rsent?customerid=4627&lang=nl_nl&output=template&url=REPLACE_URL";	//The parameters cid, lang can be changed to match the specific customer. The parameter voice can also be added.
  prhref=prhref.replace('REPLACE_URL', escape(full_url));
  // build proReader image
  var primgsrc='http://media.readspeaker.com/images/buttons/pdf_listen.gif';	//This image can be changed to match the customers chosen language
  var theimg=document.createElement("img");
  theimg.setAttribute("src",primgsrc);
  theimg.setAttribute("style","border: none;");
  theimg.setAttribute("alt",'Lees Voor');	//The alt-text can be changed to match the customers language

  // build proReader button
  var tmp = document.createTextNode(" ");
  var thebutton=document.createElement("a");
  thebutton.setAttribute("href",prhref);
  thescript="var rs=window.open('http://app.readspeaker.com/cgi-bin/rsent?customerid=4627&lang=nl_nl&output=template&url=REPLACE_URL', 'readspeaker', 'height=110, width=350, top=0, left=0, screenX=0, screenY=0, innerHeight=110, innerWidth=350'); rs.focus(); return false;";
  thescript=thescript.replace('REPLACE_URL', escape(full_url));
  thebutton.setAttribute("onclick",thescript);
  thebutton.onclick=new Function(thescript); // some browsers does not parse setAttribute values
  thebutton.setAttribute("target", "rs");
  thebutton.appendChild(theimg);

  // build proReader html conde fragment
  var thecode=document.createDocumentFragment();
  thecode.appendChild(tmp);
  thecode.appendChild(thebutton);
  thelink.parentNode.insertBefore(thecode, thelink.nextSibling);	//Insert the proReader button before the next object in the html.
}

window.onload = function()
{
	var tmp_all_links = document.getElementsByTagName("A");	//Search for all elements that is a link
	var all_links = new Array();
	for (var j=0;j<tmp_all_links.length;j++)
		all_links[j] = tmp_all_links[j];	//Copy the result into an Array since the getElementsByTagName will update when a new a is added by this script.
	for(var i=0;i<all_links.length;i++)
	{
		if(filter(all_links[i]))
		{
			returnUrl(all_links[i].href);

			if(full_url != '')
			{
				addButton(all_links[i]);
			}
		}
	}
}
