<reactionclass id="root">
	<bindobject id="globalobj">"nothing bound"</bindobject>		
			
	<!--  No page loaded yet -->
	<reaction>
		<guard>currentState.query("NOPAGE")</guard>
		<action>if(!env.ui.contains(env.ui.btnNext)) env.ui.addChild(env.ui.btnNext);</action>		
	</reaction>
	
	<!--  Creating the page view -->
	<reaction>
		<guard>variableUpdates.PageIndex!=undefined</guard>
		<action>env.ui.buildNewPage(currentState,currentVariables);</action>
	</reaction>
	
	<!-- stopped -> play (NO SELECTION - start from first word in page)-->
	<reaction>
		<guard><![CDATA[
			(currentState.query("NOSEL") && lastState.query("STOPPED") && currentState.query("PLAYING"))
		]]></guard>
		<action>
			if(env.ExternalInterface.available){
				env.ExternalInterface.call("pushsnippet",env.XmediaApplication.getPageAudioStart(currentVariables.PageIndex),env.XmediaApplication.getPageAudioEnd(currentVariables.PageIndex));				
				env.ExternalInterface.call("play");	
			}		
		</action>
	</reaction>	
	
		
	<!-- stopped -> play (Start from SELECTION point)-->
	<reaction>
		<guard><![CDATA[
			(currentState.query("SEL") && lastState.query("STOPPED") && currentState.query("PLAYING"))
		]]></guard>
		<action>
			var selAudioPos:int = env.XmediaApplication.txtToAudioPos(currentVariables.SelStart);
			if(env.ExternalInterface.available){
				env.ExternalInterface.call("pushsnippet",selAudioPos,env.XmediaApplication.getPageAudioEnd(currentVariables.PageIndex));								
				env.ExternalInterface.call("play");
			}		
		</action>
	</reaction>	
	
	<!-- Play selection --> 
	<reaction>
		<guard><![CDATA[
			!lastState.query("PLAYINGSEL") && currentState.query("PLAYINGSEL");
		]]></guard>
		<action>
			var selStartAudioPos:int = env.XmediaApplication.txtToAudioPos(currentVariables.SelStart);
			var selEndAudioPos:int = env.XmediaApplication.txtToAudioPos(currentVariables.SelEnd);
			if(env.ExternalInterface.available){
				env.ExternalInterface.call("pushsnippet",selStartAudioPos,selEndAudioPos);
				env.ExternalInterface.call("play");	
			}		
		</action>
	</reaction>	
	
	<!-- paused -> play --> 
	<reaction>
		<guard><![CDATA[
			(lastState.query("PAUSED") && currentState.query("PLAYING"))
		]]></guard>
		<action>if(env.ExternalInterface.available) env.ExternalInterface.call("play");</action>
	</reaction>	
	
	<!-- play -> pause --> 
	<reaction>
		<guard><![CDATA[
			(lastState.query("PLAYING") && currentState.query("PAUSED"))
		]]></guard>			
		<action>if(env.ExternalInterface.available) env.ExternalInterface.call("pause");</action>
	</reaction>	
	
	<!-- Stop!  --> 
	<reaction>
		<guard><![CDATA[
			(!lastState.query("STOPPED") && currentState.query("STOPPED"))
		]]></guard>
		<action>if(env.ExternalInterface.available) env.ExternalInterface.call("popsnippet");</action>
	</reaction>	
	
	<!-- playback buttons --> 
	<reaction>
		<guard><![CDATA[
			(currentState.query("STOPPED") || currentState.query("PAUSED"));
		]]></guard>			
		<action>
			if(env.ui.contains(env.ui.btnPause)) env.ui.removeChild(env.ui.btnPause);
			if(!env.ui.contains(env.ui.btnPlay)) env.ui.addChild(env.ui.btnPlay);		
		</action>
	</reaction>	
	
	<!--  --> 
	<reaction>
		<guard>(currentState.query("PLAYING"))</guard>
		<action>
			if(env.ui.contains(env.ui.btnPlay)) env.ui.removeChild(env.ui.btnPlay);
			if(!env.ui.contains(env.ui.btnPause)) env.ui.addChild(env.ui.btnPause);			
		</action>
	</reaction>	
	
	<!-- when stopped (with no selection) point to first character --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("STOPPED") && currentState.query("NOSEL")
		]]></guard>
		<action>env.ui.setCursorPos(0,0,0);</action>
	</reaction>	
	
	<!-- when stopped (with a selection) point to first character in selection --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("STOPPED") && currentState.query("SEL")
		]]></guard>
		<action>
			var chPos:int = currentVariables.SelStart-env.XmediaApplication.getPageTxtWindow(currentVariables.PageIndex)[0];
			env.ui.setCursorPos(chPos,chPos,0); 		
		</action>
	</reaction>	
	
	<!-- playing globally - move the cursor in sync --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("PLAYING") && (variableUpdates.SyncPointL!=undefined || variableUpdates.SmoothParam!=undefined)
		]]></guard>
		<action>
			var origin:int = env.XmediaApplication.getPageTxtWindow(currentVariables.PageIndex)[0];				
			env.ui.setCursorPos(env.XmediaApplication.audioToTxtPos(currentVariables.SyncPointL)-origin,
						 env.XmediaApplication.audioToTxtPos(currentVariables.SyncPointR)-origin,
						 currentVariables.SmoothParam); 		
		</action>
	</reaction>	
	
	<!--playing a selection - different graphic for sync  --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("PLAYINGSEL") && (variableUpdates.SyncPointL!=undefined || variableUpdates.SmoothParam!=undefined)
		]]></guard>
		<action><![CDATA[
			var origin:int = env.XmediaApplication.getPageTxtWindow(currentVariables.PageIndex)[0];									
			/* which end position to use? */
			var txtPosR:int = env.XmediaApplication.audioToTxtPos(currentVariables.SyncPointR);
			if(txtPosR>currentVariables.SelEnd) txtPosR=currentVariables.SelEnd;
			
			env.ui.setSelectionCursorPos( currentVariables.SelStart-origin,
								  env.XmediaApplication.audioToTxtPos(currentVariables.SyncPointL)-origin,
								  txtPosR-origin,
								  currentVariables.SmoothParam);		
		]]></action>
	</reaction>	
	
	<!-- when playing a selection - hide the progress graphic when not needed --> 
	<reaction>
		<guard><![CDATA[
			(lastState.query("PLAYINGSEL") && !currentState.query("PLAYINGSEL")) 
				|| (currentState.query("PLAYINGSEL") && currentState.query("NOSYNC"))
		]]></guard>
		<action><![CDATA[
			if(env.ui.selectionPlayCursor!=null && env.ui.glyphContainer.contains(env.ui.selectionPlayCursor)) {
				env.ui.glyphContainer.removeChild(env.ui.selectionPlayCursor);
			}
		]]></action>
	</reaction>		
	
	<!--playing an investigation - syncs go to pronunciation dialog  --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("SELINVESTIGATE") && (variableUpdates.SyncPointL!=undefined || variableUpdates.SmoothParam!=undefined)
		]]></guard>
		<action><![CDATA[
			var origin:int = env.XmediaApplication.getPageTxtWindow(currentVariables.PageIndex)[0];									
			env.ui.pronunciationDialog.sync(currentVariables.SyncPointL,currentVariables.SyncPointR,currentVariables.SelStart,origin);		
		]]></action>
	</reaction>	
		
	<!-- when playing an investigation - hide the progress graphic when not needed --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("SELINVESTIGATE") && lastState.query("PLAYINGSEL") && currentState.query("STOPPED")
		]]></guard>
		<action><![CDATA[
			env.ui.pronunciationDialog.nosync();
		]]></action>
	</reaction>	
	
	<!-- hover --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("HOVER") && variableUpdates.HoverStart!=undefined && variableUpdates.HoverEnd!=undefined;
		]]></guard>
		<action><![CDATA[
			var origin:int = env.XmediaApplication.getPageTxtWindow(currentVariables.PageIndex)[0];					
			env.ui.textWidget.setTextFormat(env.ui.textFormat);
			env.ui.textWidget.setTextFormat(env.ui.textFormatHover,currentVariables.HoverStart-origin,currentVariables.HoverEnd-origin);					
			env.ui.mouseCursorLink();			
		]]></action>
	</reaction>	
	
	<!-- unhover --> 
	<reaction>
		<guard><![CDATA[
			lastState.query("HOVER") && currentState.query("NOHOVER");
		]]></guard>
		<action>
			env.ui.textWidget.setTextFormat(env.ui.textFormat);
			env.ui.mouseCursorNormal();
		</action>
	</reaction>	
	
	<!-- sel --> 
	<reaction>
		<guard><![CDATA[
			currentState.query("SEL") && variableUpdates.SelStart!=undefined && variableUpdates.SelEnd!=undefined;
		]]></guard>
		<action>
			<![CDATA[
			var origin:int = env.XmediaApplication.getPageTxtWindow(currentVariables.PageIndex)[0];					
			while (env.ui.glyphContainer.numChildren) env.ui.glyphContainer.removeChildAt(0);
			for (var ch:int=currentVariables.SelStart-origin; ch<currentVariables.SelEnd-origin; ch++) {
				var r:Rectangle = env.ui.textWidget.getCharBoundaries(ch);
				r.x=r.x+env.ui.textWidget.x; 
				r.y=r.y+env.ui.textWidget.y;
				env.ui.addTextGlyph( env.ui.glyphContainer,r );							
			}		
			]]>
		</action>
	</reaction>	
	
	<!-- unsel --> 
	<reaction>
		<guard><![CDATA[
			lastState.query("SEL") && currentState.query("NOSEL")
		]]></guard>
		<action>while (env.ui.glyphContainer.numChildren) env.ui.glyphContainer.removeChildAt(0);	</action>
	</reaction>			
	
					
					
	<!-- 	/****** Selection Actions Dialog control ******/	 --> 
	
	<!-- 	show	 --> 
	<reaction>
		<guard><![CDATA[
			!lastState.query("SELACTIONS") && currentState.query("SELACTIONS")
		]]></guard>
		<action>
			var origin:int = env.XmediaApplication.getPageTxtWindow(currentVariables.PageIndex)[0];					
			var r:Rectangle = env.ui.textWidget.getCharBoundaries(currentVariables.SelStart-origin);
			r.x=r.x+env.ui.textWidget.x;
			r.y=r.y+env.ui.textWidget.y;				
			env.ui.selectionActionsWidget.x=r.x;
			env.ui.selectionActionsWidget.y=r.y-30;							
			env.ui.addChild(env.ui.selectionActionsWidget);			
		</action>
	</reaction>			
	
	<!-- 	hide	 --> 
	<reaction>
		<guard><![CDATA[
			lastState.query("SELACTIONS") && !currentState.query("SELACTIONS")
		]]></guard>
		<action>
			if(env.ui.contains(env.ui.selectionActionsWidget)) {
				env.ui.removeChild(env.ui.selectionActionsWidget);
			}
		</action>
	</reaction>		
	
	<!-- 	/****** Selection Actions ******/	 --> 
	
	<!-- 	investigate	 --> 
	<reaction>
		<guard><![CDATA[
			lastState.query("SELACTIONS") && currentState.query("SELINVESTIGATE");
		]]></guard>
		<action><![CDATA[
			if(env.ui.pronunciationDialog!=null && env.ui.contains(env.ui.pronunciationDialog)) {
				env.ui.removeChild(env.ui.pronunciationDialog);
			}
			
			/*extract window of xmedia for selection*/
			var audioStart:int = env.XmediaApplication.txtToAudioWordPos(currentVariables.SelStart);
			var audioEnd:int = env.XmediaApplication.txtToAudioWordPos(currentVariables.SelEnd);
			
			var txtContentWindow:String = env.XmediaApplication.extractTxtWindow(env.XmediaApplication.getTxtContent(),currentVariables.SelStart,currentVariables.SelEnd);
			var txtXmediaWindow:XML = env.XmediaApplication.extractWindow(env.XmediaApplication.getTxtXmedia(),currentVariables.SelStart,currentVariables.SelEnd);					
			var audioXmediaWindow:XML = env.XmediaApplication.extractWordWindow(env.XmediaApplication.getAudioXmedia(),audioStart,audioEnd);
			
			/* create the dialog */
			env.ui.pronunciationDialog = new PronunciationDialog(txtContentWindow,txtXmediaWindow,audioXmediaWindow);
			env.ui.addEventProxy(env.ui.pronunciationDialog, DialogEvent.OK, "SELECTION.UNINVESTIGATE");
			env.ui.addEventProxy(env.ui.pronunciationDialog, DialogEvent.PLAYSEL, "SELECTION.PLAY");
			env.ui.addChild(env.ui.pronunciationDialog);			
		]]></action>
	</reaction>	
	
	<!-- 	hide investigate dialog	 --> 
	<reaction>
		<guard><![CDATA[
			lastState.query("SELINVESTIGATE") && !currentState.query("SELINVESTIGATE");
		]]></guard>
		<action><![CDATA[
			if(env.ui.pronunciationDialog!=null && env.ui.contains(env.ui.pronunciationDialog)) {
				env.ui.removeChild(env.ui.pronunciationDialog);
				env.ui.pronunciationDialog=null;
			}					
		]]></action>
	</reaction>					
</reactionclass>