	var clips = new Object;
	var currentClip = null;
// 	var highlightInterval=null;
	var iconRegistry = new Object;
	var baseImageDirCES = '/images/';
	var clipsToRegister = new Array;
	//var sentencefound = false;
	var jumpto = '';
	audiosystem_ready = false;
	var delayedread = null
	audiotestname = null;
	audiotesttiming = null;	
	audiotesttimingoutput = null;

	// add trim function to strings if it doesn't already exist
	if (typeof String.prototype.trim == "undefined") {
		String.prototype.trim = function () {
			var s = this.replace(/^\s*/, "");
			return s.replace(/\s*$/, "");
		}
	}

	function defineClip(paramObject){
		/*********************************************************************************************************
			Defines an individual audio clip to be used on this page.

		 *********************************************************************************************************/
			
	
		paramObject.id = paramObject.icon;

		if (paramObject.read == undefined || paramObject.read == ''){
			paramObject.read = paramObject.icon;
		}
		
		clips[paramObject.read] = new soundClip(paramObject);
	
	}

	function soundClip(paramObject){
		var id = paramObject.read;
		var url = paramObject.mp3;
		var sentencetimes = paramObject.timings;
		var preload = paramObject.preload;
		
		tempArray = new Array;

		this.id = id;
		this.files = new Array;

		if (url != '') {
			
			tempArray = url.split('|');
			
			for(var i=0;i<tempArray.length;i++){
				this.files[i] = new Object;
				this.files[i].src = tempArray[i];
				this.files[i].ready = false;
				this.files[i].obj = createEmptySoundObj();
				this.files[i].obj.addEventHandler("onLoad", "readyToPlayClip('" + id + "','"+i+"')");
				this.files[i].obj.addEventHandler("onSoundComplete", "donePlayingClip('" + id + "')");
			}
			
		}

		if (sentencetimes != undefined && sentencetimes != '') {
			tempArray = sentencetimes.split('|');
			for(var i=0;i<tempArray.length;i++){
				if(this.files[i] != undefined){
					timeline = new Array;
					timeline = tempArray[i].split(',');
					this.files[i].time = timeline;
				}
			}
		} 
		
		this.activefile = 0;
		this.activesentence = 0;
		this.preload = preload;

	}
	
	function readyToPlayClip(itemToPlay,fileNumberToPlay){
		
		var fileToPlay = parseInt(fileNumberToPlay);
		var item = itemToPlay;

		if (clipOK(item,fileToPlay)){
			if(clips[item].files[fileToPlay].obj.getDuration() > 0){
				clips[item].files[fileToPlay].ready = true;
				clips[item].activefile = fileToPlay;
				clips[item].activesentence = 0;
				clips[item].files[fileToPlay].obj.stop();
				clips[item].files[fileToPlay].obj.start(0);
				
				updateIcons(item,'play');
			}else{
				//alert("Audio file "+clips[item].files[fileToPlay].src+ " was not found. Unable to play it.");
			}
		}else{
			//alert("Audio file "+item+" #"+fileToPlay+ " was not properly initialized. Unable to play it.");
		}
	}
	
	function playClip(item,filenum){
		
			if (filenum == undefined){
				filenum = 0;
			}
			pauseClip();
			updateIcons(item,'loading');
			if (clipOK(item,filenum)){
				currentClip = item;
				if (!clips[item].files[filenum].ready){
					loadClip(item,filenum);
				}			
					readyToPlayClip(item,filenum);
			}
	}	

	function loadClip(item,filenum){
		filenum = parseInt(filenum);
		if (clipOK(item,filenum)){
			clips[item].files[filenum].obj.loadSound(clips[item].files[filenum].src, false);
			if (clips[item].files[filenum].find == undefined && document.getElementById(item) != null){
				loadHighlights(item);
			}
		}
	}

	function loadHighlights(item){
		var allText = extractText(document.getElementById(item));
		var allSegments = makeSegments(allText,0);
		//allSegments.each(function(seg){alert(seg);});
		//for (var i=0;i< clips[item].files.length;i++){
			//alert(allSegments.length);
			//if (clips[item].files[i].time != undefined){
			//	clips[item].files[i].find = allSegments.splice(0,clips[item].files[i].time.length-1);
			//	alert(clips[item].files[i].find.length)
		//	}
		//}
		return true;
	}

	function donePlayingClip(item){

		if (clips[item].activefile < clips[item].files.length && (clipOK(item,clips[item].activefile + 1)) ){
			clips[item].activefile = clips[item].activefile + 1;
			clips[item].activesentence = 0;
			playClip(item,clips[item].activefile);	
		}else{
			updateIcons(currentClip,"reset");
			currentClip = null;
		}

	}


	function makeSegments(content,num){
		var character = new Array ('.','?','!',':');
		var result = new Array;
		if (num >= character.length){
			if (content != ''){
				result[0] = content;
			}
			return result;
		}else{
			var tempresult = content.trim().split(character[num]);
			for (var i=0;i<tempresult.length;i++){
				result = result.concat(makeSegments(tempresult[i],num+1));
			}
			return result;
		}
	}

	function currentFile(){
		var c = getClipsActiveFile(currentClip);
		if (c!=null){
			return c;
		}else{
			return null;
		}
	}

	function getClipsActiveFile(theItem){
		var item = theItem;
		if(clipOK(item)){
			var filenum = parseInt(clips[item].activefile);
			if (clipOK(item,filenum)){
				return clips[item].files[filenum]
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	function clipOK(theitem,thefilenumber){
		var item = theitem;
		var filenum = thefilenumber;
		if (clips[item] != null && clips[item] != undefined){
			if (filenum != undefined){
				filenum = parseInt(filenum);
				
				if (clips[item].files[filenum] != null && clips[item].files[filenum] != undefined){
					
					return true;
				}else{
					
					return false;
				}
			}else{
				
				return true;
			}
		}else{
		
			return false;
		}
	}

	function stopClip(item){
		clearTimeout(delayedread);
		if (item == undefined || item == currentClip ) {
			var cFile = currentFile();
			if (cFile != null){
				cFile.obj.stop();
			}
// 			clearInterval(highlightInterval);
		}
	}

	function resumeClip(item){
		
		if (clipOK(item)){
			var clipFile = getClipsActiveFile(item);
			stopClip();
			clipFile.obj.start(clipFile.obj.getPosition()/1000);
			updateIcons(currentClip,'pause');
			updateIcons(item,'play');

			currentClip=item;

			if (clipFile.time != undefined && clipFile.time.length > 0){
			//	search(document.forms[0], self);
			//	highlightInterval = setInterval("highlight('"+item+"')", 500);
			}
		}
	
	}


	function extractText(item){
		if (document.createRange) {	
			// "Standard" way to do it
			range = document.createRange();
			range.setStartBefore(item)
			range.setEndAfter(item);
			return range.toString();
		}else if (document.body.createTextRange){
			// IE code
			range = document.body.createTextRange();
			range.moveToElementText(item);
			return range.text;
		}else{
			return '';
		}
	}
	
	function getFirstSentence(thetext){
		start = 0;
		periodend = thetext.indexOf('.')+1;
		questionend = thetext.indexOf('?')+1;
		if (periodend == 0){
			periodend = 1000000;
		}
		if (questionend == 0){
			questionend = 1000000;
		}
		if (periodend < questionend){
			end = periodend;
		}else{
			end = questionend;
		}
		//alert('end:'+end+' returning:'+thetext.substring(start,end));
		return thetext.substring(start,end)
	
	}

	function readSection(paramObject){
		if (audiosystem_ready) {
			if (clips[paramObject.read] == undefined) {
				defineClip(paramObject);
			}
			if (paramObject.debug == 'true'){
				alert("Debug info");
				alert(paramObject.read);
	// 			paramObject.each(function(item,avalue){alert("i:"+item+" v:"+avalue)});
			}
			playClip(paramObject.read);
			//allText = extractText(document.getElementById(item));
			//currentText = allText;
			//alert(currentText);
			//sentenceNumber = 0;
			//var secClip = getClipsActiveFile(item);
			//if (secClip != null){
			//	if (clips[item].files[clips[item].activefile].time != undefined && clips[item].files[clips[item].activefile].time.length > 0){	
					//highlightInterval = setInterval("highlight('"+item+"')", 500);
			//	}
			//}
		}else{
			window.status="Please wait while audio system prepares. ";
		}
	}


	function highlight(item)
	{
		var theClip = clips[item];
		var playing = getClipsActiveFile(item);
		if (playing != null){
			var obj = playing.obj;
			if (obj != null){
				position = obj.getPosition()/1000;
				duration = obj.getDuration()/1000;
			
				$('pos').value = " " + position + "/" + duration;
				if (position >= playing.time[theClip.activesentence]){
					sentence = getFirstSentence(currentText);
					document.forms[0].findthis.value = sentence.trim();
					search(document.forms[0], self);
					currentText = currentText.substring(sentence.length).trim();
					theClip.activesentence = theClip.activesentence +1;
				}
				if (theClip.activesentence > playing.time.length){
					theClip.activesentence = theClip.activesentence -1;
// 					clearInterval(highlightInterval);
				}
			}
		}
	}		

	var aflax = new AFLAX("../../lib/AFLAX/aflax.swf");
	
		
	function createEmptySoundObj(){
		/*********************************************************************************************************
			Returns an empty AFLAX created sound object.

		 *********************************************************************************************************/
// 		alert('creating sound object');
		soundObj = new AFLAX.FlashObject(aflax, "Sound");
		soundObj.exposeFunction("loadSound", soundObj);
		soundObj.exposeFunction("start", soundObj);
		soundObj.exposeFunction("stop", soundObj);
		soundObj.exposeFunction("getBytesTotal", soundObj);
		soundObj.exposeFunction("getBytesLoaded", soundObj);
		soundObj.exposeFunction("getDuration", soundObj);
		soundObj.exposeFunction("getPosition", soundObj);

		soundObj.exposeProperty("position", soundObj);
		soundObj.exposeProperty("duration", soundObj);
		
		soundObj.mapFunction("addEventHandler");
		return soundObj;
	}
function pauseClip(item){
	if (item == undefined) {item = currentClip}
	stopClip(item);
	updateIcons(item,'pause');
}
function stopAndRewind(item){
	if (item == undefined) {item = currentClip}
	stopClip(item);
	updateIcons(item,'rewind');
}

function addToIconRegistry(paramObject){
if (iconRegistry[paramObject.read] == undefined) {
		iconRegistry[paramObject.read] = new Array;
	}
		iconRegistry[paramObject.read].push(paramObject);
}

function createAudioIcon(paramObject){
	addToIconRegistry(paramObject);
	
	switch (paramObject.position){
		case 'none' :
			$(paramObject.id).className = 'audioDiv';
			break
		case 'left':
			$(paramObject.id).className = 'audioDiv floatleft';
			break
		case 'right':
		default:
			$(paramObject.id).className = 'audioDiv floatright';
			break
	}
	updateIcons(paramObject.read,'new');
}

	function updateIcons (clipToBeRead,newmode){
		if (iconRegistry[clipToBeRead] != undefined){
			iconRegistry[clipToBeRead].each(function(paramObject){updateIcon(paramObject,newmode);})
		}
	}
	
	function updateIcon(paramObject,newmode){
		var container = paramObject.id;
		
		if (container != undefined && $(container) != null){
			container = $(container);	
			
			if (newmode == 'pause' && (paramObject.state == 'default' || paramObject.state == 'loading')){
				// do not change state
			}else{
				removeChildren(container.id);
				paramObject.state = newmode;
				switch (newmode){
					case 'play':
						
						switch (paramObject.size){
							case 'large':
								var thewidth = '45';
								var theheight = '90';
								break
							case 'small':
								var thewidth = '23';
								var theheight = '46';
								break
							default:
								var thewidth = '30';
								var theheight = '60';
								break
						}
						
						icon = document.createElement('img');
						icon.setAttribute('src', baseImageDirCES +'small-pause.gif');
						icon.setAttribute('id',paramObject.id + "_pauseicon");
						icon.setAttribute('alt','pause' + ' "' + paramObject.read + '"');
						icon.setAttribute('title','pause' + ' "' + paramObject.read + '"');
						icon.setAttribute('width',thewidth);
						icon.setAttribute('height',theheight);
						
						container.appendChild(icon);
						Event.observe(paramObject.id + "_pauseicon", 'click', function (e) {pauseClip();});
	
						icon = document.createElement('img');
						icon.setAttribute('src', baseImageDirCES +'small-stop.gif');
						icon.setAttribute('alt','stop and rewind'  + ' "' + paramObject.read + '"');
						icon.setAttribute('title','stop and rewind' + ' "' + paramObject.read + '"');
						icon.setAttribute('id',paramObject.id + "_stopicon");
						icon.setAttribute('width',thewidth);
						icon.setAttribute('height',theheight);
											
						container.appendChild(icon);
						
						Event.observe(paramObject.id + "_stopicon", 'click', function (e) {stopAndRewind();});
						break
					case 'pause':
						switch (paramObject.size){
							case 'large':
								var thewidth = '45';
								var theheight = '90';
								break
							case 'small':
								var thewidth = '23';
								var theheight = '46';
								break
							default:
								var thewidth = '30';
								var theheight = '60';
								break
						}
								
						icon = document.createElement('img');
						icon.setAttribute('src', baseImageDirCES +'small-play.gif');
						icon.setAttribute('id',paramObject.id + "_playicon");
						icon.setAttribute('alt','resume reading' + ' "' + paramObject.read + '"');
						icon.setAttribute('title','resume reading' + ' "' + paramObject.read + '"');
						icon.setAttribute('width',thewidth);
						icon.setAttribute('height',theheight);
										
						container.appendChild(icon);
						
						Event.observe(paramObject.id + "_playicon", 'click', function (e) {resumeClip(paramObject.read);});
	
						icon = document.createElement('img');
						icon.setAttribute('src', baseImageDirCES +'small-stop.gif');
						icon.setAttribute('alt','stop and rewind' + ' "' + paramObject.read + '"');
						icon.setAttribute('title','stop and rewind' + ' "' + paramObject.read + '"');
						icon.setAttribute('id',paramObject.id + "_stopicon");
						icon.setAttribute('width',thewidth);
						icon.setAttribute('height',theheight);
						
						container.appendChild(icon);
						
						Event.observe(paramObject.id + "_stopicon", 'click', function (e) {stopAndRewind(paramObject.read);});
						break
					case 'loading':
						switch (paramObject.size){
							case 'large':
								var thewidth = '90';
								var theheight = '90';
								break
							case 'small':
								var thewidth = '46';
								var theheight = '46';
								break
							default:
								var thewidth = '60';
								var theheight = '60';
								break
						}
						
						
						icon = document.createElement('img');
						icon.setAttribute('src', baseImageDirCES +'small-loading.gif');
						icon.setAttribute('id',paramObject.id + "_readicon");
						icon.setAttribute('width',thewidth);
						icon.setAttribute('height',theheight);
						
						container.appendChild(icon);
						break;
					case 'rewind':
					case 'new':
					default:
						
						paramObject.state = 'default';
						switch (paramObject.size){
							case 'large':
								var thewidth = '90';
								var theheight = '90';
								break
							case 'small':
								var thewidth = '46';
								var theheight = '46';
								break
							default:
								var thewidth = '60';
								var theheight = '60';
								break
						}

						$(paramObject.id).style.width = thewidth;
						if (paramObject.img == "")
							{paramObject.img = 'small-readingguy1.gif';}
					
						icon = document.createElement('img');
						icon.setAttribute('src', baseImageDirCES + paramObject.img );
						icon.setAttribute('alt','read' + ' "' + paramObject.read + '"');
						icon.setAttribute('title','read' + ' "' + paramObject.read + '"');
						icon.setAttribute('id',paramObject.id + "_readicon");
						icon.setAttribute('width',thewidth);
						icon.setAttribute('height',theheight);

						container.appendChild(icon);

						Event.observe(paramObject.id + "_readicon", 'click', function (e) {readSection(paramObject);});
					break
				}
			}

			
			
		}
	}

	function removeChildren(container){
		
		if (container != undefined && $(container) != null){
			var node = $(container);
			while (node.hasChildNodes()){
				node.removeChild(node.firstChild);
			}
		}	
	}

	function registerClip(paramObject){

		paramObject.id = paramObject.icon;

		if (paramObject.read == undefined || paramObject.read == ''){
			paramObject.read = paramObject.icon;
		}
		
		clipsToRegister[paramObject.read]= (paramObject);
	}

	function defineRegisteredClips(){
		clipsToRegister.each(
				function(paramObject){
					if (paramObject.loadatstart == true){
						defineClip(paramObject);
					}
				}
		);
	}

	function createRegisteredIcons(){

		clipsToRegister.each(
			function (paramObject){

				paramObject.id = paramObject.icon;
				
				if (paramObject.read == undefined || paramObject.read == ''){
					paramObject.read = paramObject.icon;
				}
				if (paramObject.id != undefined && $(paramObject.id) != undefined){
					createAudioIcon(paramObject);
				}
			}
		)
	}

	function aflaxready()
		{	
 			$('pleasewait').style.display = "block";

 			defineRegisteredClips();
			createRegisteredIcons();
			
			debugCES();
			audiosystem_ready = true;
			window.status="Audio system ready.";
			$('pleasewait').style.display = "none"; 
			if (clipsToRegister['autoplayer'] != null && clipsToRegister['autoplayer'] != undefined){		
				defineClip(clipsToRegister['autoplayer']);
				playClip('autoplayer');
			}
			if (jumpto != ''){
				window.location.href= jumpto;
			}
			
		}
	
	function registerAndRead(paramObject){
		addToIconRegistry(paramObject);
		readSection(paramObject);
	}

	function  showHidden(id){
			
			var showlinks = document.getElementsByClassName('show_' + id);
			
			showlinks.each(function(showlink){
						showlink.toggle();	
					})
			
			Effect.SlideDown(id);
		}
	function hideHidden(id){
			Effect.SlideUp(id);

			var showlinks = document.getElementsByClassName('show_' + id);
			
			showlinks.each(function(showlink){
						showlink.toggle();	
					})
		}

function delayread(item){
	
	if (audiosystem_ready && clips[item] == undefined){
	
		defineClip(clipsToRegister[item]);
	}
		delayedread = setTimeout("playClip('"+item+"');",500);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function debugCES(){
	if ($("audiotest")!= undefined){
		audiotestname = $("audiotestname");	
		audiotesttiming = $("audiotesttiming");		
		audiotesttimingoutput = $("audiotesttimingoutput");
		audiotesttimingoutput.value = "0";
		audiotestInterval = setInterval(audiotester, 500);
	}
}

function audiotester (){
	
	if (currentClip != null){
		cf = currentFile();
		audiotestname.value = currentClip + ":" + cf.src;
		audiotesttiming.value = cf.obj.getPosition()/1000;
	}
}

function resetAudioTester(){
	if (currentClip != null){
		stopAndRewind(currentClip);
		audiotesttimingoutput.value = "0";
	}
}

function playFromStartAudioTester(){
	if (currentClip != null){
		cr= currentFile();
		cr.obj.start(0);
	}
}

function pauseAudioTesting(){
	pauseClip();
	audiotesttimingoutput.value = audiotesttimingoutput.value + "," + audiotesttiming.value;
}