document.write "<OBJECT id=agent WIDTH=0 HEIGHT=0 classid=CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F CODEBASE=#VERSION=2,0,0,0></OBJECT>"
document.write "<OBJECT id=lcs   WIDTH=0 HEIGHT=0 CLASSID=CLSID:C3480405-A7F8-11D1-AA75-00C04FA34D72 CODEBASE=#VERSION=2,0,0,0></OBJECT>"
document.write "<OBJECT id=len   WIDTH=0 HEIGHT=0 CLASSID=CLSID:C3480409-A7F8-11D1-AA75-00C04FA34D72 CODEBASE=#VERSION=2,0,0,0></OBJECT>"
document.write "<OBJECT Id=dss   WIDTH=0 HEIGHT=0 CLASSID=CLSID:0C7F3F20-8BAB-11d2-9432-00C04F8EF48F CODEBASE=#VERSION=4,0,0,0></OBJECT>"


public Agent, Char, Playing, Path, Macro, RestFactor, PointFactor
public sentencebreaks, loaderror, wasvisible, CharID, ScreenOrgX, ScreenOrgY, LangId

dim exitrequest

sentencebreaks = ".!?"

loaderror = "There was a problem in loading the agent control or the local character." & _
	vbCr & vbCr & "Things you can check:" & _
	vbCr & vbCr & "1." & chr(9) & "Make sure you have Microsoft Agent version 2 installed." & _
	vbCr & "2." & chr(9) & "Make sure you have downloaded and installed at least one character file."

SelectedIndex = 0

AltCharPaths  = 1

NoLocal       = False

MyFirstTime   = True

PlayedOnce    = False

sub setlang
  on error resume next
  LangId = &H409
  if cesky.checked then
  langID = &H405
  end if
end sub
setlang

'sub window_onfocus
'location.reload
'end sub
'sub window_onresize
'    ScreenOrgX = window.event.screenX - window.event.clientX
'    ScreenOrgY = window.event.screenY - window.event.clientY
'    char.moveto ScreenOrgX , ScreenOrgY
'end sub
'
'sub body_onmouseover
'  status = "X=" + window.event.ClientX + " Y=" + window.event.clientY
'end sub
'sub onr
'    set rng = document.body.createTextRange.text
'     status = """"+ document.all.lm.style.top+""""
    'offsetLeft
    'getAttribute("x")
'    ScreenOrgX = window.event.screenX - window.event.clientX
'    ScreenOrgY = window.event.screenY - window.event.clientY
'    char.moveto ScreenOrgX , ScreenOrgY
'end sub

'setTimeout "onr",1000
'onr


sub window_onload

    on error resume next

'    set callerwindow = window

    ScreenOrgX = window.event.screenX - window.event.clientX
    ScreenOrgY = window.event.screenY - window.event.clientY

'    alert(window.event.screenY)
'    alert(window.event.clientY)
'    alert(ScreenOrgY)
'    alert(ScreenOrgX)

'    if window is nothing then
'       setTimeout "window_onload",100
'       exit sub
'    end if

'    set document = window.document
    setlang
'    LangId = &H409
'    alert(document.charset)
'    Select Case document.charset
'    case "windows-1250"
'      LangId = &H405
'    case "iso-8859-2"
'      LangId = &H405
'    case "windows-1251"
'      LangId = &H419
'    End Select

'    set sel = document.selection

'    if lcase(sel.type) = "text" then
'       set rng = sel.createrange
'       initAgent
'       Char.show
'       readText rng.text
'    else
'       if document.all("SpeakIt Powertoy") is Nothing then
'	  set rng = document.body.createTextRange
'	  initAgent
'	  Char.show
'	  readText rng.text
'       else
'	  playPresentation
'       end if
'    end if

end sub

sub initAgent

    on error resume next

    err.clear
    
    'if (Aget != null) then 
    'return
    'endif

    Set Agent = CreateObject("Agent.Control.2")

    Agent.Connected = True

    Randomize Timer

    SelectedIndex = Int((5 - 1 + 1) * Rnd + 1)

    If document.all("SpeakIt Character") is Nothing then
       agent.characters.load "default"
       set Char = agent.characters("default")
       setlang
       Char.LanguageID = LangId
    else
       SelectedChar = replace(document.all("SpeakIt Character").innerHTML,vbCr,"")
       MacroStart = instr(SelectedChar, "{")
       if len(SelectedChar) > 1 then SelectedChar = mid(SelectedChar, MacroStart + 1) else SelectedChar = ""
       MacroEnd = instr(SelectedChar, "}")
       Macro = trim(left(SelectedChar, MacroEnd - 1))

       Select Case Macro
         Case "pM_Green"
              SelectedIndex = 0
         Case "PM_Green"
              SelectedIndex = 5
         Case "genie"
              SelectedIndex = 2
         Case "Genie"
              SelectedIndex = 7
         Case "peedy"
              SelectedIndex = 3
         Case "Peedy"
              SelectedIndex = 8
         Case "merlin"
              SelectedIndex = 1
         Case "Merlin"
              SelectedIndex = 6
         Case "robby"
              SelectedIndex = 4
         Case "Robby"
              SelectedIndex = 9
       End Select

    end if
    err.clear
    for x = 0 to 5
        Select Case SelectedIndex
          Case 0 :CharID = "PM_Green" : CharFile = "PM_Green.acs" : LCharFile = "PM_Green.acf"      : AltCharPaths = 0 : RestFactor = -0.12 : PointFactor = 0.333 : NextSelection = array( 1, 2, 3, 4, 5,10) : Path = "http://www.fyi-net.com/msagent/"
          Case 1 :CharID = "Merlin"   : CharFile = "Merlin.acs"   : LCharFile = "merlin/merlin.acf" : AltCharPaths = 0 : RestFactor =  0.02 : PointFactor = 0.500 : NextSelection = array( 0, 2, 3, 4, 6,10) : Path = "http://agent.microsoft.com/agent2/chars/"
          Case 2 :CharID = "Genie"    : CharFile = "Genie.acs"    : LCharFile = "genie/genie.acf"   : AltCharPaths = 0 : RestFactor =  0.01 : PointFactor = 0.133 : NextSelection = array( 0, 1, 3, 4, 7,10) : Path = "http://agent.microsoft.com/agent2/chars/"
          Case 3 :CharID = "Peedy"    : CharFile = "Peedy.acs"    : LCharFile = "peedy/peedy.acf"   : AltCharPaths = 0 : RestFactor =  0.02 : PointFactor = 0.111 : NextSelection = array( 0, 1, 2, 4, 8,10) : Path = "http://agent.microsoft.com/agent2/chars/"
          Case 4 :CharID = "Robby"    : CharFile = "Robby.acs"    : LCharFile = "robby/robby.acf"   : AltCharPaths = 0 : RestFactor =  0.00 : PointFactor = 0.111 : NextSelection = array( 0, 1, 2, 3, 9,10) : Path = "http://agent.microsoft.com/agent2/chars/"
          Case 5 :CharID = "PM_Green" : CharFile = "PM_Green.acs" : LCharFile = "PM_Green.acf"      : AltCharPaths = 1 : RestFactor = -0.12 : PointFactor = 0.333 : NextSelection = array(10,10,10,10,10,10) : Path = "http://www.fyi-net.com/msagent/"
          Case 6 :CharID = "Merlin"   : CharFile = "Merlin.acs"   : LCharFile = "merlin/merlin.acf" : AltCharPaths = 1 : RestFactor =  0.02 : PointFactor = 0.333 : NextSelection = array(10,10,10,10,10,10) : Path = "http://agent.microsoft.com/agent2/chars/"
          Case 7 :CharID = "Genie"    : CharFile = "Genie.acs"    : LCharFile = "genie/genie.acf"   : AltCharPaths = 1 : RestFactor =  0.01 : PointFactor = 0.133 : NextSelection = array(10,10,10,10,10,10) : Path = "http://agent.microsoft.com/agent2/chars/"
          Case 8 :CharID = "Peedy"    : CharFile = "Peedy.acs"    : LCharFile = "peedy/peedy.acf"   : AltCharPaths = 1 : RestFactor =  0.02 : PointFactor = 0.111 : NextSelection = array(10,10,10,10,10,10) : Path = "http://agent.microsoft.com/agent2/chars/"
          Case 9 :CharID = "Robby"    : CharFile = "Robby.acs"    : LCharFile = "robby/robby.acf"   : AltCharPaths = 1 : RestFactor =  0.00 : PointFactor = 0.111 : NextSelection = array(10,10,10,10,10,10) : Path = "http://agent.microsoft.com/agent2/chars/"
        End Select
        if x = 0 then SavedSelection = NextSelection
           for i = 0 to AltCharPaths
               err.clear
               if i < 1 then
                  Agent.Characters.Load CharID, CharFile
               else
                  Agent.Characters.Load CharID, Path & LCharFile
               end if
               if err.number = 0 then
                  If i = 1 then
                     NoLocal = True
                     Window.Status = "Loading Or Checking For Character Sequences For " & CharID & " Please Be Patient."
                  end if
                  exit for
               end if
           next
           if err.number = 0 then exit for
           if SavedSelection(x) <> 10 then SelectedIndex = SavedSelection(x)
        next

    Set Char = Agent.Characters(CharID)
    setlang
    Char.LanguageID = LangId

    DownloadButton = "center"

    if NoLocal then
       Char.Get "State", "Showing, Speaking, Hiding"
       Char.Get "Animation", "Greet, GreetReturn, Read, ReadContinued"
       Char.moveto screen.width - 138, screen.height - 148
       Char.Show
       Char.Speak "Hello, my name is " & CharID &" I am currently not stored on your system so I have placed a link in the SpeakIt Window so that you can download me or other characters so this will be much faster next time if you choose, I must now get my pieces from the Internet or at least check to see if my pieces are here from last time, I may look like I am stuck, but I am not, please be patient, I will be ready shortly. If you download me later, choose the run from current location option and I will be available without this delay in the future."
    else
       'Char.MoveTo ScreenOrgX + document.body.clientWidth - Char.width , ScreenOrgY + document.body.clientHeight - Char.height  * RestFactor
       Char.moveto screen.availwidth - 138, screen.availheight - Char.height
       Char.Show
    end if

    Char.commands.caption = "Speak It!"
    Char.commands.add "Stop","Stop","stop"
    Char.commands.add "Properties","Properties","[show] properties"

end sub

sub readText(ptext)
    on error resume next
    setlang
    Char.LanguageID = LangId
    ptext = ptext & "\mrk=1\"
    Char.play "Read" : firsttime = true
    for each paragraph in split(replace(ptext,chr(13),chr(10)),chr(10))
        if (len(paragraph) > 80) and (not firsttime) then Char.play "ReadContinued"
         firsttime = false
         for i = 1 to len(sentencebreaks)
             sbreak = mid(sentencebreaks,i,1)
	       paragraph = replace(paragraph, sbreak & " ",sbreak & "·")
         next
         for each sentence in split(paragraph,"·")
             Char.speak sentence
         next
    next
end sub

sub agent_bookmark(bookmarkID)
    on error resume next
    select case bookmarkID
      case 1
'	   if wasvisible then
'	      Char.balloon.visible = false
'	      set exitrequest = Char.play("ReadReturn")
'	   else
'	      set exitrequest = Char.hide
'	   end if
    end select

end sub

sub agent_requestcomplete(request)

    select case request
      case exitrequest
'        agent.characters.unload "default"
'	   window.close
    end select

end sub

sub read(ReadID)
    If not document.all(ReadID) is nothing then
       readText document.all(ReadID).innerText
    end if
end sub

sub agent_idlestart(characterID)
    Char.balloon.visible = false
'    window.close
end sub

sub agent_command(userInput)
    select case userinput.name
      case "Stop"
'	   window.close
      case "Properties"
	   agent.showdefaultcharacterproperties
      case else
	   on error resume next
	   callerwindow.PowertoyCallback userinput
      end select
end sub

sub agent_change(o)
  on error resume next
  agent.showdefaultcharacterproperties
  status=o.left
  char.moveto ScreenOrgX+o.style.pixelLeft, ScreenOrgY+o.style.pixelRight
  char.play "gestureright"
  char.speak "Zde mne lze vyměnit ?"
end sub

sub agent_hide(characterID, cause)
'    window.close
end sub

sub window_onerror
    msgbox "An error occured: " & vbCr & Err.Description
'    window.close
end sub

sub playPresentation
    on error resume next
    initAgent
    setlang
    Char.LanguageID = LangId
    for x = 0 to 1
        rtext = replace(document.all("SpeakIt Powertoy").innerHTML,vbCr,"{}")
        do while rtext <> ""
	   MacroStart = instr(rtext, "{")
	   select case MacroStart
	     case 0
                  if not NoLocal then Char.speak rtext
		  rtext = ""
	     case 1
		  if len(rtext) > 1 then rtext = mid(rtext, MacroStart + 1) else rtext = ""
	     case else
		  if not NoLocal Then Char.speak left(rtext, MacroStart - 1)
		  if len(rtext) > 1 then rtext = mid(rtext, MacroStart + 1) else rtext = ""
	   end select
	   MacroEnd = instr(rtext, "}")
	   select case MacroEnd
	     case 0
		  rtext = ""
	     case 1
		  if len(rtext) > 1 then rtext = mid(rtext, MacroEnd + 1) else rtext = ""
	     case else
		  Macro = trim(left(rtext, MacroEnd - 1))
                  if not NoLocal then
		     if left(Macro,1) <> "#" then
			window.execScript "on error resume next : Char." & Macro, "vbscript"
		     else
			window.execScript "on error resume next : " & mid(Macro,2), "vbscript"
                     end if
                  else
                     if left(Macro,1) = "p" then
                        window.execScript "on error resume next : Char.Get " & Chr(34) & "Animation" & Chr(34) & ", " & mid(Macro,5), "vbscript"
                     end if
		  end if
	          if len(rtext) > 1 then rtext = mid(rtext, MacroEnd + 1) else rtext = ""
	   end select
        loop
        if not NoLocal then exit for
        if NoLocal then NoLocal = False
    next
    Char.speak "\mrk=1\"
end sub

sub SpeakMacro(rtext)
    on error resume next
    'init agent removed
    'if (Agent != null) then initAgent
    setlang
    Char.LanguageID = LangId
    for x = 0 to 1
'        rtext = replace(document.all("SpeakIt Powertoy").innerHTML,vbCr,"{}")
        do while rtext <> ""
	   MacroStart = instr(rtext, "{")
	   select case MacroStart
             case 0
                  if not NoLocal then Char.speak rtext
		  rtext = ""
	     case 1
		  if len(rtext) > 1 then rtext = mid(rtext, MacroStart + 1) else rtext = ""
	     case else
		  if not NoLocal Then Char.speak left(rtext, MacroStart - 1)
		  if len(rtext) > 1 then rtext = mid(rtext, MacroStart + 1) else rtext = ""
	   end select
	   MacroEnd = instr(rtext, "}")
	   select case MacroEnd
	     case 0
		  rtext = ""
	     case 1
		  if len(rtext) > 1 then rtext = mid(rtext, MacroEnd + 1) else rtext = ""
	     case else
		  Macro = trim(left(rtext, MacroEnd - 1))
                  if not NoLocal then
		     if left(Macro,1) <> "#" then
			window.execScript "on error resume next : Char." & Macro, "vbscript"
		     else
			window.execScript "on error resume next : " & mid(Macro,2), "vbscript"
                     end if
                  else
                     if left(Macro,1) = "p" then
                        window.execScript "on error resume next : Char.Get " & Chr(34) & "Animation" & Chr(34) & ", " & mid(Macro,5), "vbscript"
                     end if
		  end if
	          if len(rtext) > 1 then rtext = mid(rtext, MacroEnd + 1) else rtext = ""
	   end select
        loop
        if not NoLocal then exit for
        if NoLocal then NoLocal = False
    next
    Char.speak "\mrk=1\"
end sub

