/**
*
* ReadSpeaker Javascript Class
*
* @version		1.0
* @package		Joomla
* @copyright	Copyright (C) Elze Kool, 2009 Edu'Actief B.V.
* @license		GNU/GPL, see LICENSE.php
*/

/** 
 * Base class used for ReadSpeaker integration into Joomla 1.5
 * @param baseURL string Base URL of Joomla installation
 **/
function readSpeakerClass(baseURL, Server, customerID, onLoaded)
{
	// Store base URL
	this.baseURL = baseURL;
	
	// Store ReadSpeaker server
	this.Server = Server;
	
	// Store customer ID
	this.customerID = customerID;

	// Call onloaded
	this.onLoaded = onLoaded;
	
	// Default no selected string
	this.selectedString = "";
	
	/**
	 * Allow user to select text to read 
	 **/
	this.sayit = function()
	{
		// Get selection
		if (document.getSelection) {
			this.selectedString = document.getSelection();
		} else if (document.all) {
			this.selectedString = document.selection.createRange().text;	
		} else if (window.getSelection) {
			this.selectedString = window.getSelection();
		}
		
		// Store selected string
		document.getElementById('rstext').value = this.selectedString;

		// Set current URL if not set already
		if (!(document.getElementById('rsurl').value)) 
		{
			if (window.location.href)
				document.getElementById('rsurl').value = window.location.href;
			else if (document.location.href)
				document.getElementById('rsurl').value = document.location.href;
		}
	};

	/**
	 * Open new readspeaker window 
	 **/
	this.openandread = function()
	{
		window.open('','modReadSpeaker_rs','width=380,height=180,toolbar=0');
		setTimeout("document.getElementById('modReadSpeaker_rsform').submit();",500);
	};
	
	/** 
	 * Find all links on this page and replace PDF link 
	 **/
	this.findPDFLinks = function()
	{
		// Get all anchors
		pdfLinks = document.getElementsByTagName("A");

		cnt = pdfLinks.length;
		while (cnt)
		{
			// Decrease count
			cnt--;
			
			// Get lnk object
			lnk = pdfLinks[cnt];

			// In a try block so empty href strings and very short links 
			// don't couse the javascript system to crash
			try
			{
				// Check if link has .PDF in it's url
				if ((lnk.href.toUpperCase().indexOf('.PDF') != -1) & (lnk.href.toUpperCase().indexOf('HTTP://') != -1))
				{
					// Check if local link
					if (lnk.href.toUpperCase().indexOf(this.baseURL) != -1)
					{
						nUrl = this.Server + "/cgi-bin/eduactiefrsone?customerid=" + this.customerID + "&url=" + encodeURIComponent(lnk.href);
						lnk.href = nUrl;
					}
				}
			}
			catch(e)
			{
			}
		}
	};

}



