Dim MyAgentRunnable
Dim TestAgent
Dim MyCharacter
Dim LoadRequestUNC
Dim LoadRequestURL
Dim GetShowAnimation
Dim Hiding
Dim currText
Dim selText
Dim MyCharName
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  FIRST DETERMINE WHETHER THE AGENT SUPPORT FILES ARE ALL PRESENT
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
MyCharName = "blueshark2.acs"

Function HaveAgent()
' This procedure attempts to create an Agent Control object.
' If it succeeds, it returns True.
'    This means the control is available on the client.
' If it fails, it returns False.
'    This means the control hasn't been installed on the client.
   HaveAgent = False
   On Error Resume Next
   Set TestAgent = CreateObject("Agent.Control.2")
   HaveAgent = IsObject(TestAgent)
End Function

Function HaveCharacter(charFileName)
'  This procedure attempts to load the named character
'  It returns TRUE if successful, FALSE if not
   HaveCharacter = False
   On Error Resume Next
   
   TestAgent.Connected = TRUE
   TestAgent.Characters.Load "MyCharacter", charFileName
   Set MyCharacter = TestAgent.Characters("MyCharacter")
   HaveCharacter = IsObject(MyCharacter)
End Function

Function HaveTTS()
'  This procedure tests for TTS support in the character
   HaveTTS = False
   On Error Resume Next
   HaveTTS = (MyCharacter.TTSModeId <> "")
End Function

MyAgentRunnable = False

If HaveAgent Then
	If HaveCharacter(MyCharName) Then
		If HaveTTS Then
'  ALL REQUIRED FILES ARE PRESENT, SET FLAG TO ALLOW INITIALIZATION
		MyAgentRunnable = True
		Else
			'MsgBox "Missing TTS Support"
		End If
	Else
		'MsgBox "Missing Character"
	End If
Else
	'MsgBox "Missing Core Agent Files"
End If

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IF ALL NECESSARY FILES ARE PRESENT THAN CREATE THE OBJECT TAGS
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If MyAgentRunnable Then
	document.write ("<OBJECT classid=""clsid:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F"" id=""Agent""></OBJECT>")
	document.write ("<OBJECT classid=""clsid:B8F2846E-CE36-11D0-AC83-00C04FD97575"" id=""TruVoice""></OBJECT>")
End If

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  RUN AUTOMATICALLY WHEN PAGE LOAD COMPLETE
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Window_OnLoad
'  IF TEXT-TO-SPEECH IS ENABLED THEN LOAD THE CHARACTER
	If readCookie("TTS") = "yes" Then 
		If MyAgentRunnable Then
			LoadCharacter
		Else
			DoSetupOptions
		End If
	End If
End Sub

Sub DoSetupOptions
	msgStr = "You do not have all of the necessary components for the text reader currently installed on your computer.  "
	msgStr = msgStr & "If you would like to use text-to-speech then please click YES below and follow the instructions on the installation page.  "
	msgStr = msgStr & "Would you like to go to the installation page now?"
	choice = MsgBox (msgStr, 4132, "Download Text-to-Speech Support")
	If choice = 6 Then location.href = "http://kids-learn.org/sharkbytes/instructions.htm" 
End Sub

Sub startMyCharacter
'  LOAD AND START CHARACTER IF ALL SUPPORT COMPONENTS ARE PRESENT
	If MyAgentRunnable Then
		LoadCharacter
	Else
		DoSetupOptions
	End If
End Sub

Sub stopMyCharacter
'  STOP CHARACTER AND UNLOAD IT WHEN DONE HIDING
On Error Resume Next
	MyCharacter.Stop
	Set Hiding = MyCharacter.Hide
End Sub

Sub LoadCharacter
On Error Resume Next
'  SEE WHETHER THE CHARACTER IS ALREADY LOADED
   If not isObject(Agent.Characters("MyCharacter")) Then
	'  LOAD THE CHARACTER FROM THE LOCAL FILE SYSTEM
   	Set LoadRequestUNC = Agent.Characters.Load ("MyCharacter", MyCharName)  
   End If

'  ASSIGN THE CHARACTER
    Set MyCharacter = Agent.Characters("MyCharacter")
'  AND PLAY THE INTRODUCTION
    DoIntro
End Sub

Sub DoIntro
  InitCommands
  ' Set the character's language ID
  MyCharacter.LanguageID = &H0409
'  PLACE THE AGENT ON THE TOP LEFT CORNER OF THE WINDOW
  'On Error Resume Next
  MyCharacter.MoveTo window.screenLeft+10, window.screenTop+175
  MyCharacter.Show
  MyCharacter.Speak "Hello, my name is Mr Ocean Clean, your friendly text reader."
  MyCharacter.Speak "Highlight any words you would like me to read, and click on me."
  MyCharacter.Play "Explain"
  MyCharacter.Speak "If you want me to stop talking, click on me again."
  MyCharacter.Play "Explain"
  MyCharacter.Speak "I will go away if you right mouse click on me and select hide."
End Sub


Sub InitCommands
  ' Give user access to the Advanced Character Options dialog
  MyCharacter.Commands.RemoveAll
  'MyCharacter.Commands.Add "ACO", "Advanced Character Options"
End Sub


Sub Agent_RequestComplete(ByVal Request)
If Request = Hiding Then
	Agent.Characters.Unload("MyCharacter")
End If
End Sub

Sub Agent_Click (ByVal CharacterID, ByVal Button, ByVal Shift, ByVal X, ByVal Y)
'  START READING THE SELECTED TEXT
	selText = document.selection.createRange().text
	if selText <> currText then
		currText = selText
		MyCharacter.Speak currText
	else
		MyCharacter.Stop
	end if
End Sub

Sub Agent_DragComplete (ByVal CharacterID, ByVal Button, ByVal Shift, ByVal X, ByVal Y)
'  STOP THE SPEAKING AND PLAY A RANDOM COMMENT
  MyCharacter.Stop
  MyCharacter.Speak "Well, \emp\the ocean is nice over here!|I like riding on these waves!|I like it much better over here. Thank you!|Now why did you move me? I liked it over there!|Hey, be gentle!|\emp\Please don't put my fins in your soup!"
End Sub


Sub Agent_Command (ByVal UserInput)
  If UserInput.Name = "ACO" Then
    Agent.PropertySheet.Visible = True
  End If
End Sub
