document.write ("<OBJECT classid=""clsid:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F"" id=""Agent"" CODEBASE=""http://activex.microsoft.com/activex/controls/agent2/MSagent.exe#VERSION=2,0,0,0""></OBJECT>")
document.write ("<OBJECT classid=""clsid:B8F2846E-CE36-11D0-AC83-00C04FD97575"" id=""TruVoice"" CODEBASE=""http://activex.microsoft.com/activex/controls/agent2/tv_enua.exe#VERSION=6,0,0,0""></OBJECT>")


Dim Peedy
Dim LoadRequestUNC
Dim LoadRequestURL
Dim GetShowAnimation
Dim currText
Dim selText

Sub Window_OnLoad


  ' Load the character
  LoadCharacter
End Sub

Sub InitCommands
  ' Give user access to the Advanced Character Options dialog
  Peedy.Commands.RemoveAll
  Peedy.Commands.Add "ACO", "Advanced Character Options"
End Sub

Sub DoIntro
  InitCommands

  ' Set the character's language ID
  Peedy.LanguageID = &H0409

  ' Make the character appear
  On Error Resume Next
  Peedy.MoveTo window.screenLeft+10, window.screenTop+50
  Peedy.Show

  Peedy.Speak "Highlight any words you would like me to read, and click on me."
Peedy.Play "Explain"
  Peedy.Speak "If you want me to stop talking, click on me again."
Peedy.Play "Explain"
Peedy.Speak "I will go away if you right mouse click on me and select hide."

End Sub

Sub Agent_RequestComplete(ByVal Request)
  ' The Request to load the ACF character fails 
  If Request = LoadRequestURL Then
	
    ' If it failed
    If Request.Status = 1 Then
      Msgbox "Peedy could not be loaded. The server may be busy. Try refreshing this page."
      Exit Sub

    ' If it succeeded
    ElseIf Request.Status = 0 Then
      ' Assign Peedy to the loaded character
      Set Peedy = Agent.Characters("Peedy")
      ' Get the preliminary animations necessary to show Peedy
      Set GetShowAnimation = Peedy.Get ("state", "showing, speaking")
      Peedy.Get "animation", "Blink, Greet, Pleased, Explain, Think, GestureRight,Idle1_1,Idle2_2, Announce, Uncertain", False
    End If
  ' The request to get the animation 
  ElseIf Request = GetShowAnimation Then
    ' If it failed 
    If Request.Status = 1 Then
		Msgbox "Request to load the Show animation failed. The server may be busy."
		Exit Sub
	' If it succeeded
    ElseIf Request.Status = 0 Then
      DoIntro
    End If
  End If
End Sub

Sub LoadCharacter
  On Error Resume Next

  ' Attempt to load the character from the Microsoft Agent Chars directory
  Set LoadRequestUNC = Agent.Characters.Load ("Peedy", "Peedy.acs")

  ' If it fails...
  If LoadRequestUNC.Status <> 0 Then
    ' Attempt to load the character from the Microsoft Agent site
    Set LoadRequestURL = Agent.Characters.Load ("Peedy", "http://www.microsoft.com/msagent/chars/Peedy/Peedy.acf") 

    ' It didn't fail so assign Peedy to loaded character file
  Else 
    ' Assign Peedy to the loaded character
    Set Peedy = Agent.Characters("Peedy")
    DoIntro
  End If
End Sub

Sub Agent_Click (ByVal CharacterID, ByVal Button, ByVal Shift, ByVal X, ByVal Y)
  ' Start the monologue
'  for each obj in document.all.tags("BODY")
' 	if (obj.innerText <> "") then  Peedy.Speak obj.innerText
'  next 
	selText = document.selection.createRange().text
	if selText <> currText then
		currText = selText
		Peedy.Speak currText
	else
		Peedy.Stop
	end if
'  Peedy.Stop
'  Peedy.Play "Alert"
'  Peedy.Speak "Hey, watch it with that pointer!|Oh that tickles!|\pit=170\Hee hee hee!"
'  Peedy.Play "Blink"
End Sub

Sub Agent_DragComplete (ByVal CharacterID, ByVal Button, ByVal Shift, ByVal X, ByVal Y)
  Peedy.Stop
  Peedy.Play "Blink"
  Peedy.Speak "Well, \emp\this gives me a new perspective!|I like it much better over here.|Now why did you move me?|Hey, be careful!"
  Peedy.Play "Blink"
End Sub


Sub Agent_Command (ByVal UserInput)
  If UserInput.Name = "ACO" Then
    Agent.PropertySheet.Visible = True
  End If
End Sub
