/* Copyright (c)2002 ELR Software Pty Ltd. http://www.elr.com.au   */
/*                                                                 */
/* This program is free software; you may use, redistribute and/or */
/* modify it under the terms of the GNU General Public License as  */
/* published by the Free Software Foundation; either version 2 of  */
/* the License, or (at your option) any later version.             */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
/* See the GNU General Public License for more details.            */
/* http://www.fsf.org/home.html                                    */

/* Object to implement MSAgent "speak" in a web page             */
/* Requires (object tag in page head)                            */
/* eg "<object classid="clsid:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F" codebase="#version=2,0,0,0"></object>" */
/* Usage:                                                        */
/* var MSA = new MSAgent('Merlin',18,'Arial');                   */
/* MSA.agentSpeak(sWords,bVisible);                              */
/* eg <span onclick="MSA.agentSpeak('Hello',false)">Hello</span> */

function MSAgent(agentname,balloonfontsize,balloonfont) {
  this.Agent         = new Object;
  this.Control       = false;
  this.AgentCLSID    = 'clsid:d45fd31b-5c6e-11d1-9ec1-00c04fd7081f';
  this._agent        = (agentname > '')  ? agentname          : 'Default';
  this.AgentACS      = (agentname > '')  ? this._agent+'.acs' : '';
  this._bfsize       = (balloonfontsize) ? balloonfontsize    : '12';
  this._bfont        = (balloonfont)     ? balloonfont        : 'Arial';
  this.ScreenWidth   = (window.screen.width == 0)  ? window.screen.width  : 800;
  this.ScreenHeight  = (window.screen.height == 0) ? window.screen.height : 600;
  this.AgentWidth    = 128;
  this.AgentHeight   = 128;
  this.centerX       = function(aw) {return(parseInt(this.ScreenWidth / 2) - parseInt(aw / 2))}
  this.rightX        = function(aw) {return(this.ScreenWidth - aw)}
  this.centerY       = function(ah) {return(parseInt(this.ScreenHeight / 2) - parseInt(ah / 2))}
  this.bottomY       = function(ah) {return(this.ScreenHeight - ah)}
  this.UsedChars     = this._agent;
  this.AgentID       = this._agent;
  this.LoadReq;
  this.AgentLoaded   = false;
  this.objectPresent = function() {
                         for (var i = 0; i < document.applets.length; i++) {
                           if (document.applets[i].classid.toLowerCase() == this.AgentCLSID.toLowerCase()) {
                             present = true; /* Classid is present in page */
                             this.Control = document.applets[i]; /* The control becomesd the object */
                           }
                         }
                         return(typeof(this.Control) == 'object');
                       }
  this.loadAgent     = function() {
                         if ((this.objectPresent()) && (!this.AgentLoaded)) {
                           this.Control.Connected = true;
                           this.AgentLoaded = this.loadAnAgent(this.AgentID,this.AgentACS);
                           if (this.AgentLoaded) {this.setCharObj()}
                             else {this.loadError()}
                         }
                       }
  this.setCharObj    = function() {/* Sets the character reference and TTS Language ID */
                         this.Agent = this.Control.Characters.Character(this.AgentID);
                         /* this.Agent.LanguageID = 0x410; */

//                       this.Agent.TTSModeID = '{227A0E40-A92A-11d1-B17B-0020AFED142E}'; /* L&H TTS3000 Carol */
//                       this.Agent.TTSModeID = '{227A0E41-A92A-11d1-B17B-0020AFED142E}'; /* L&H TTS3000 Peter */
//                       this.Agent.TTSModeID = '{BF5EAD50-9F65-11CF-8FC8-0020AF14F271}'; /* ViaVoice Outloud 4.0 Justin */
//                       this.Agent.TTSModeID = '{BF5EAD51-9F65-11CF-8FC8-0020AF14F271}'; /* ViaVoice Outloud 4.0 Jane */
//                       this.Agent.TTSModeID = '{BF5EAD52-9F65-11CF-8FC8-0020AF14F271}'; /* ViaVoice Outloud 4.0 Nanny */
//                       this.Agent.TTSModeID = '{BF5EAD53-9F65-11CF-8FC8-0020AF14F271}'; /* ViaVoice Outloud 4.0 Gramps */
//                       this.Agent.TTSModeID = '{BF5EAD54-9F65-11CF-8FC8-0020AF14F271}'; /* ViaVoice Outloud 4.0 Nicky */
//                       this.Agent.TTSModeID = '{BF5EAD55-9F65-11CF-8FC8-0020AF14F271}'; /* ViaVoice Outloud 4.0 Justin - Tel */
//                       this.Agent.TTSModeID = '{BF5EAD56-9F65-11CF-8FC8-0020AF14F271}'; /* ViaVoice Outloud 4.0 Jane - Tel */

                       }
  this.loadError     = function() {
                         var strMsg;
                         window.status = "";
                         strMsg = "Error Loading Character: " + this.AgentID + "\n";
                         strMsg = strMsg + "This Microsoft Agent Script requires the character(s):\n";
                         strMsg = strMsg + this.UsedChars;
                         alert(strMsg);
                       }
  this.loadAnAgent   = function(CharID,CharACS) {
                         /* Purpose:  Attempts to load the specified character */
                         /* Returns:  true if successful, false if not */
                         this.Control.RaiseRequestErrors = false;
                         if (CharACS) {
                           this.LoadReq = this.Control.Characters.Load(CharID,CharACS);
                         }
                         else {
                           this.LoadReq = this.Control.Characters.Load(CharID);
                         }
                         this.Control.RaiseRequestErrors = true;
                         if (this.LoadReq.Status != 1) {
                           return(true);
                         }
                         return(false);
                       }
  this.displayAgent  = function(visible,x,y) {
                         if (!visible) { /* Places agent off screen */
                           this.Agent.height = 0;
                           this.Agent.width  = 0;
                           this.Agent.top    = -this.ScreenHeight;
                           this.Agent.left   = -this.ScreenWidth;
                         }
                         else { /* Use default size & set location */
                           this.Agent.height = this.AgentHeight;
                           this.Agent.width  = this.AgentWidth;
                           this.Agent.left = x;
                           this.Agent.top  = y;
                         }
                       }
  this.agentSpeak    = function(sText,bVisible,X,Y) {
                         var x = (!isNaN(X)) ? X : this.centerX(this.AgentWidth);
                         var y = (!isNaN(Y)) ? Y : this.centerY(this.AgentHeight);
                         if (this.AgentLoaded) {
                           this.Agent.StopAll();
                           this.Agent.Hide(true) /* False is animated hide */
                           this.displayAgent(bVisible,x,y);
                           this.Agent.Show(true) /* False is animated show */
                           this.Agent.Balloon.FontName = this._bfont;
                           this.Agent.Balloon.FontSize = this._bfsize;
                           this.Agent.Speak(sText);
                           this.Agent.Hide(true);
                         }
                       }
  this.loadAgent(); /* Instantiate object */
}
