/*
  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 
	{
		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/proreader/proreader.php?cid=4750&lang=sv_se&pdfurl=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://www.nynashamn.se/imagearchive/webbgrafik/ikon-lyssna-liten.gif';	//This image can be changed to match the customers chosen language
  var theimg=document.createElement("img");
  theimg.setAttribute("src",primgsrc);
  theimg.setAttribute("border","0");
  theimg.setAttribute("alt",'Lyssna till pdf-dokumentet med proReader');	//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="window.open(this.href, this.target, 'width=190, height=120, resizable=1, scrollbars=1, screenX=0, screenY=0, left=0, top=0'); return false;";
  thebutton.setAttribute("onclick",thescript);
  thebutton.onclick=new Function(thescript); // some browsers does not parse setAttribute values
  thebutton.setAttribute("target", "prwin");
  thebutton.target=new Function("prwin"); // some browsers does not parse setAttribute values
  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.
}

var limepark_proreadersearch = 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]);
			}
		}
	}
}
addEventHandlerToChain(window, "onload", limepark_proreadersearch);