var Agent
var AgentID = "Agent";
var AgentACS = "merlin.acs";
var AgentLoaded = false;
var LoadReq;
var HideReq;
var AgentLeftX, AgentCenterX, AgentRightX;
var AgentTopY, AgentCenterY, AgentBottomY;
var revisit = 0;
 
function Window_OnLoad()
{
    AgentControl.Connected = true;
    AgentLoaded = LoadLocalAgent(AgentID, AgentACS);
    if (AgentLoaded)
    {
        SetCharObj();
    }
    CheckLoadStatus();
}
 
function GetScreenPositions()
{
    var ScreenWidth = window.screen.width;
    var ScreenHeight = window.screen.height;
    if ((ScreenWidth == 0) || (ScreenHeight == 0))
    {
        ScreenWidth = 800;
        ScreenHeight = 600;
    }
    AgentLeftX = 0;
    AgentCenterX = (parseInt(ScreenWidth / 2) - parseInt(Agent.Width / 2));
    AgentRightX = (ScreenWidth - Agent.Width);
    AgentTopY = 0;
    AgentCenterY = (parseInt(ScreenHeight / 2) - parseInt(Agent.Height / 2));
    AgentBottomY = (ScreenHeight - Agent.Height);
}
 
function LoadLocalAgent(CharID, CharACS)
{
    AgentControl.RaiseRequestErrors = false;
    LoadReq = AgentControl.Characters.Load(CharID, CharACS);
    AgentControl.RaiseRequestErrors = true;
    if (LoadReq.Status != 1)
    {
        return(true);
    }
    return(false);
}
 
function SetCharObj()
{
    Agent = AgentControl.Characters.Character(AgentID);
    Agent.LanguageID = 0x409;
}
 
function CheckLoadStatus()
{
    if (!AgentLoaded)
    {
        return(false);
    }
    window.status = "";
    GetScreenPositions();
    AgentIntro();
    return(true);
}
 
function AgentIntro()
{
    if (revisit) {
	Agent.MoveTo(AgentLeftX-Agent.Width,AgentBottomY);
	Agent.Show(1);
	Agent.MoveTo(AgentCenterX,AgentBottomY);
//	Agent.MoveTo(AgentCenterX,AgentCenterY);
//	Agent.Show(0);
	Agent.Play("Wave");
	Agent.Speak("Oh, Hi again ... welcome back!");
//	Agent.MoveTo(AgentCenterX,AgentBottomY);
	Agent.Speak("You already know your way around Steven's pages, yes?");
	Agent.Play("GestureUp"); 
	Agent.Think("( ...and clearly have great taste! )");
	Agent.Play("Greet");
	Agent.MoveTo(AgentRightX+Agent.Width,AgentBottomY);
	Agent.Hide(1);
    } else {
	Agent.MoveTo(AgentCenterX,AgentCenterY);
	Agent.Show(0);
	Agent.MoveTo(AgentRightX,AgentCenterY);
	Agent.Play("Announce");
//	Agent.MoveTo(AgentRightX,AgentTopY);
	Agent.Play("Greet");
	Agent.Speak("Greetings, visitor!");
	Agent.Play("DoMagic1"); 
	Agent.Play("DoMagic2"); 
	Agent.Play("GestureRight"); 
	Agent.Speak("Welcome to Steven's web pages.");
	Agent.Play("idle2_2"); 
	Agent.Play("Think"); 
	Agent.Think("I wonder if you already know what an all-around great guy he is?");
	Agent.MoveTo(AgentLeftX,AgentTopY);
	Agent.Play("Search"); 
	Agent.Speak("Ahhh...it looks like your future may be a bit more magical now!");
	Agent.Play("Surprised"); 
	Agent.Play("Pleased"); 
	Agent.Speak("Well, enjoy your visit!  Ta-ta!");
	Agent.Play("Wave");
	Agent.Hide(0);
    }
}

function GetCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length)))
        return null;
    if (start == -1) return '';
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

var today = new Date();
var expires = new Date();

function SetCookie(name,offset) {
    if (offset)
	    expires.setTime(today.getTime() + offset);

    document.cookie = name + "=" +escape(name) +
        ( (offset) ? ";expires=" + expires.toGMTString() : "");
}

if (!GetCookie('wizzed')) {
	if (GetCookie('seensp')=="seensp") {
		revisit = 1;
	} else {
//		SetCookie("seensp", 180000);
		SetCookie("seensp", (30 * 86400000));
	}

//	SetCookie("wizzed", 120000);
	SetCookie("wizzed");

	Window_OnLoad();
}

