document.write("<style type='text/css'> ");

document.write("#rspopup {margin: 3px; font-size: 12px; font-family: Arial; width: 160px; border-top: 1px solid #a4cbff; }");

document.write("#rspopup a { display:block; width:161px;color: black; text-decoration: none; } ");

document.write("#rspopup ul { margin: 0px;  } #rspopup li { list-style-type:none; margin:  0px; padding: 0px; padding-left: 2px; padding-top: 2px; } ");

document.write("#rspopup li.head {  width:160px;font-weight: 600;  text-align:left;  text-decoration:none;  background:#ffffff;  color:#000;  padding:0.25em;  margin-left:1px;  } ");

document.write("#rspopup .act { font-weight:bold; color:#000; } ");

document.write("#rspopup ul { margin: 0px; padding: 0px; } #rspopup a, #rspopup a:visited { background:#ffffff; color:#000; display:block; width:160px;  border:2px solid #a4cbff;  text-align:left;  text-decoration:none;  padding:0.25em;  } #rspopup a:hover { background-color: #a4cbff; }");

document.write("#rspopup a.actlink {  color:#000; background-image: url('http://media.readspeaker.com/images/enterprise/default/check.png'); background-repeat: no-repeat; background-position: 98% 3px; } ");

document.write("#bottomlinks { font-family:Arial;color:#333;font-size:11px; } #bottomlinks a {color:black;text-decoration : none;padding : 2px;} #bottomlinks a:hover {color:black;text-decoration : none;padding : 2px;background-color: #a4cbff; }");

document.write("</style>");



var defaultvalue="wordsent";

var defaultsurvive=360000000;



document.write("<style type='text/css'>");

document.write(" .sync_word_highlighted { background-color: #a4cbff; }");

document.write(" .sync_sent_highlighted { background-color: #beffd6; }");

document.write("</style>");



var readid=null;

var restorehtml=null;

var newhtml="";

var oldwordhl=null;

var oldsenthl=null;



function rshlsetContent(thecontent) {

  newhtml+=thecontent;

}



function rshlsetId(theid) {

  readid=theid;

}



function rshlinit() {

  var x=null;

  if (readid!=null) {

    x=document.getElementById(readid);

  }

  if (x!=null) {

    restorehtml=x.innerHTML;

    x.innerHTML=newhtml;

    newhtml="";

  }

}



function rshltidy() {

  var x=null;

  if (readid!=null) {

    x=document.getElementById(readid);

  }

  if (x!=null && restorehtml!=null) {

    x.innerHTML=restorehtml;

    restorehtml=null;

    readid=null;

  }

}



function rshlexit() {

  closepage('xpl');

}



function rshlsync(type,id) {

  var newEl = document.getElementById("sync"+id);

  if (newEl && newEl.className=="sync_sent") {

    if (oldsenthl) {

      oldsenthl.className = 'sync_sent';

    }

    oldsenthl=newEl;

    newEl.className = 'sync_sent_highlighted';

  }

  else if (newEl && newEl.className=="sync_word") {

    if (oldwordhl) {

      oldwordhl.className = 'sync_word';

    }

    oldwordhl=newEl;

    newEl.className = 'sync_word_highlighted';

  }

}



function readpage(rscall,playerid,rs_type) {

try {
  pageTracker._trackPageview('/readSpeaker' + Url.decode(rscall).match(/url\=https?\:\/\/www\.niagarafalls\.ca([^\?&]+)/i)[1]);
} catch (e) {}


  var thesync=loadSettings("ReadSpeakerHL");

  if (thesync=="")

    thesync=defaultvalue;

  var audioformat="mp3";

  if (thesync!='none')

    audioformat="swf";

  origrscall=rscall;

  rscall=rscall+"&audioformat="+audioformat+"&sync="+thesync;

  rs_readel=null;

  if (rs_type==1) {

    tmp_array=rscall.match(/readid=[^&]+/gi);

    rs_read_div = tmp_array[0].replace("readid=","");

    rs_readel=document.getElementById(rs_read_div);

  }

  if (rs_readel && rs_readel.innerHTML.length>0) {

	var selectedString = rs_readel.innerHTML;

	selectedString = "<div id='"+rs_read_div+"'>"+selectedString+"</div>";

	rscall += "&selectedhtml="+escape(selectedString);

	rscall=rscall.replace("/cgi-bin/rsent?","/enterprise/rsent_wrapper.php?");

  }

  newrscall=escape(rscall);



  the_player="<table style='border:1px solid #aeaeae; font-size: 10px;'><tr><td>";

  the_player+="<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='250' height='20'>";

  the_player+="<param name='movie' value='http://media.readspeaker.com/flash/readspeaker20.swf?"+audioformat+"="+newrscall+"&rskin=simple&autoplay=1&tips=1'>";

  the_player+="<param name='quality' value='high'><param name='autostart' value='true'>";

  the_player+="<param name='allowScriptAccess' value='always'><param name='bgcolor' value='#FFFFFF'>";

  the_player+="<param name='loop' value='false'>";

  the_player+="<EMBED src='http://media.readspeaker.com/flash/readspeaker20.swf?"+audioformat+"="+newrscall+"&rskin=simple&autoplay=1&tips=1'";

  the_player+=" allowScriptAccess='always' quality='high' autostart='true' bgcolor='#FFFFFF' width='250' height='20' loop='false' type='application/x-shockwave-flash'";

  the_player+=" pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' swliveconnect='true'>";

  the_player+="</EMBED></OBJECT>";

  the_html="<div id='bottomlinks'><a href='#' onclick='showcontrols(\""+origrscall+"\",\""+playerid+"\");return false'>Settings</a> | <a href='"+origrscall+"&save=1' target='rs'>Download mp3</a></div>";

  the_html+="<div id='controls'></div>";

  the_html+="</td><td style='vertical-align: top;'><a href='#' onclick='closepage(\""+playerid+"\");return false'><img id='closebr' src='http://graphics.rspeak.com/images/wr/close.gif' border='0' alt='close'></a></td></tr></table>";

  var x=document.getElementById(playerid);

  if (x) {

    x.innerHTML=the_player+the_html;

  }

}


function readpageSmall(rscall,playerid,rs_type) {

try {
  pageTracker._trackPageview('/readSpeaker' + Url.decode(rscall).match(/url\=https?\:\/\/www\.niagarafalls\.ca([^\?&]+)/i)[1]);
} catch (e) {}

  var thesync=loadSettings("ReadSpeakerHL");

  if (thesync=="")

    thesync=defaultvalue;

  var audioformat="mp3";

  if (thesync!='none')

    audioformat="swf";

  origrscall=rscall;

  rscall=rscall+"&audioformat="+audioformat+"&sync="+thesync;

  rs_readel=null;

  if (rs_type==1) {

    tmp_array=rscall.match(/readid=[^&]+/gi);

    rs_read_div = tmp_array[0].replace("readid=","");

    rs_readel=document.getElementById(rs_read_div);

  }

  if (rs_readel && rs_readel.innerHTML.length>0) {

	var selectedString = rs_readel.innerHTML;

	selectedString = "<div id='"+rs_read_div+"'>"+selectedString+"</div>";

	rscall += "&selectedhtml="+escape(selectedString);

	rscall=rscall.replace("/cgi-bin/rsent?","/enterprise/rsent_wrapper.php?");

  }

  newrscall=escape(rscall);



/*  the_player="<table style='border:1px solid #aeaeae; font-size: 10px;'><tr><td>";*/
	the_player = "";
  the_player+="<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='50' height='20'>";

  the_player+="<param name='movie' value='http://media.readspeaker.com/flash/readspeaker20.swf?"+audioformat+"="+newrscall+"&rplayer=mini&rskin=rounded&autoplay=1&tips=1'>";

  the_player+="<param name='quality' value='high'><param name='autostart' value='true'>";

  the_player+="<param name='allowScriptAccess' value='always'><param name='bgcolor' value='#FFFFFF'>";

  the_player+="<param name='loop' value='false'>";

  the_player+="<EMBED src='http://media.readspeaker.com/flash/readspeaker20.swf?"+audioformat+"="+newrscall+"&rplayer=mini&rskin=rounded&autoplay=1&tips=1'";

  the_player+=" allowScriptAccess='always' quality='high' autostart='true' bgcolor='#FFFFFF' width='50' height='20' loop='false' type='application/x-shockwave-flash'";

  the_player+=" pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' swliveconnect='true'>";

  the_player+="</EMBED></OBJECT>";

/*  the_html="<div id='bottomlinks'><a href='#' onclick='showcontrols(\""+origrscall+"\",\""+playerid+"\");return false'>Settings</a> | <a href='"+origrscall+"&save=1' target='rs'>Download mp3</a></div>";

  the_html+="<div id='controls'></div>";*/

 /* the_html+="</td><td style='vertical-align: top;'><a href='#' onclick='closepage(\""+playerid+"\");return false'><img id='closebr' src='http://graphics.rspeak.com/images/wr/close.gif' border='0' alt='close'></a></td></tr></table>";*/

  var x=document.getElementById(playerid);

  if (x) {

    x.innerHTML=the_player;//+the_html;

  }

}



function closepage(playerid) {

  var x=document.getElementById(playerid);

  if (x) {

    x.innerHTML='';

  }

  rshltidy();

}



function saveSettings(name, content, lifetime) {

  lifetime=parseInt(eval(lifetime));  

  if (lifetime+""=="NaN") {

    tmpdate="";

  }

  else {

    var thedate = new Date();

    thedate.setTime(thedate.getTime() + lifetime);

    thedate=thedate.toGMTString();

    tmpdate="; expires="+thedate;

  }

  document.cookie=name+"="+escape(content)+tmpdate;

}



function loadSettings(ckname) {

  ckarr=document.cookie;

  cks=ckarr.split("; ");

  for (i=0;i<cks.length;i++) {

    cknameval=cks[i].split("=");

    if (cknameval[0]==ckname) {

      return unescape(cknameval[1]);

    }

  }

  return "";          

}



function setstyle(style) {

  saveSettings("ReadSpeakerHL",style,defaultsurvive);

  var x=document.getElementById('controls');

  if (x!=null)

    x.innerHTML="";

}



function showcontrols(rscall,playerid) {

  var x=document.getElementById('controls');

  if (x!=null && x.innerHTML!="") {

    x.innerHTML="";

    return false;

  }

  var thevalue=loadSettings("ReadSpeakerHL");

  if (thevalue=="")

    thevalue=defaultvalue;

  thestring="<div id='rspopup'><ul><li class='head'>Highlighting Options:</li>";

  thestring+="<li class="; if (thevalue=="wordsent") thestring+="'act'";

  thestring+="><a href='#' onclick='setstyle(\"wordsent\");closepage(\""+playerid+"\");readpage(\""+rscall+"\",\""+playerid+"\");return false;'"; if (thevalue=="wordsent") thestring+="class='actlink'"; thestring+=">Word and Sentence</a></li>";

  thestring+="<li class="; if (thevalue=="sent") thestring+="'act'";

  thestring+="><a href='#' onclick='setstyle(\"sent\");closepage(\""+playerid+"\");readpage(\""+rscall+"\",\""+playerid+"\");return false;'"; if (thevalue=="sent") thestring+="class='actlink'"; thestring+=">Sentence only</a></li>";

  thestring+="<li class="; if (thevalue=="word") thestring+="act";

  thestring+="><a href='#' onclick='setstyle(\"word\");closepage(\""+playerid+"\");readpage(\""+rscall+"\",\""+playerid+"\");return false;'"; if (thevalue=="word") thestring+="class='actlink'"; thestring+=">Word only</a></li>";

  thestring+="<li class="; if (thevalue=="none") thestring+="act";

  thestring+="><a href='#' onclick='setstyle(\"none\");closepage(\""+playerid+"\");readpage(\""+rscall+"\",\""+playerid+"\");return false;'"; if (thevalue=="none") thestring+="class='actlink'"; thestring+=">No Highlighting</a></li></ul></div>";

  var x=document.getElementById('controls');

  if (x!=null)

    x.innerHTML=thestring;

}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
};