var imgActorAr = new Array();
var actorDir;

document.write('<div id="Actor"><img name="Actor" src="l/l1.gif" width="72" height="112" border="0"></div>');
document.write('<div id="Balloon"><img name="Balloon" src="images/spacepxl.gif" width="200" height="75">');
document.write('<span id="BalloonText">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>');
document.write('<embed name="lsound" src="voice.wav" autostart="false" hidden="true" mastersound></embed>');


function imgActorObjConstr(iName, iW, iH)
{
	this.img = new Image(iW, iH);
	this.img.src = actorDir + iName + ".gif";
}

function addActorImgObj(iName, iW, iH)
{
	imgActorAr[iName] = new imgActorObjConstr(iName, iW, iH);
}

function loadActor()
{
	if (document.images)
	{
		actorDir = "l/";
		for (var i = 1; i < 15; i++)
		{
			addActorImgObj("l" + i, 72, 112);
		}
		addActorImgObj("m1", 72, 112);
		addActorImgObj("m3", 72, 112);
		addActorImgObj("m4", 72, 112);
		addActorImgObj("balloon", 200, 75);
	}
}

loadActor();

function doCred(){
	window.open('credits.htm', null, 'toolbar=no, location=no, status=no, resizable=no, scrollbars=yes, width=260, height=298');
	return true;
}

////////////////////////////////////////////////////////
var bx1, bx2, by1, by2;
var actorObj;

var actorContainer;
var sideMenu;

function StartActor() {
	actorContainer = document.getElementById("mainTable");
	sideMenu = document.getElementById("sideMenu");
	if (ie4 || ns6){
		document.images["Balloon"].src = imgActorAr["balloon"].img.src;
		bx1 = sideMenu.offsetWidth + 20;
		bx2 = bx1 + 500;
		by1 = 61; //by1 = actorContainer.offsetParent.offsetTop;
		by2 = 275; //by2 = actorContainer.offsetParent.offsetTop + actorContainer.offsetHeight - 148;
		if (ns6) by2 += 30;
		actorObj = new actorConstr(bx1, bx2, by1, by2, "actorObj", "Actor", "imgActorAr", "Balloon", "BalloonText");
		actorObj.start( 0, 0,"", null, null);
	}
//	else if (ns4){
//		document.Balloon.document.images["Balloon"].src = imgActorAr["balloon"].img.src;  //"bouncer/nsballoon.gif";
//		bx1 = 252;
//		bx2 = 721;
//		by1 = 56;
//		by2 = 270;
//		actorObj = new actorConstr(bx1, bx2, by1, by2, "actorObj", "Actor", "imgActorAr", "Balloon", "BalloonText");
//		actorObj.start( 0, 0,"", null, null);
//	}
}

////////////////////////////////////////////////////////
var div;
var balloon;

function actorConstr(left, right, top, bottom, strObj, strDiv, strImgAr, strBalloon, strBalloonText)
{
	this.behaving = false;
	this.topBound = top;
	this.bottomBound = bottom;
	this.leftBound = left;
	this.rightBound = right;
	this.obj = strObj;
	this.div = strDiv;
	this.imgAr = strImgAr;
	this.balloon = strBalloon;
	this.balloonText = strBalloonText;
	this.currentLeft = this.leftBound;
	this.currentBottom = this.bottomBound;
	this.mainLoop = mainLoop;
//	this.scowl = scowl;
//	this.crouch = crouch;
//	this.akimbo = akimbo;
//	this.stop = stop;
//	this.lookSide = lookSide;
	this.mobile = mobile;
	this.circle = circle;
	this.walkLeft = walkLeft;
	this.walkRight = walkRight;
	this.alternate = alternate;
	this.setBehaviour = setBehaviour;
	this.showBalloon = showBalloon;
	this.start = start;
	if (ie4 || ns6)
	{
		div = document.getElementById(this.div);
		balloon = document.getElementById(this.balloon);
		div.style.left = this.currentLeft;
		div.style.top = this.currentBottom;
		balloon.style.left = this.currentLeft - 25;
		balloon.style.top = this.currentBottom - 75;
	}
	else if (ns4)
	{
		document.layers[this.div].left = this.currentLeft;
		document.layers[this.div].top = this.currentBottom;
		document.layers[this.balloon].left = this.currentLeft - 25;
		document.layers[this.balloon].top = this.currentBottom - 75;
	}
	
}

function start(left, bottom, runFunc, param1, param2)
{
	var s;
	
	if (left > 0) this.currentLeft += left;
	if (bottom > 0) this.currentBottom += bottom;
	
	if (ie4 || ns6)
	{
		div = document.getElementById(this.div);
		div.style.left = this.currentLeft;
		div.style.top = this.currentBottom;
		div.style.visibility = "visible";
	}
	else if (ns4)
	{
		document.layers[this.div].left = this.currentLeft;
		document.layers[this.div].top = this.currentBottom;
		document.layers[this.div].visibility = "show";
	}
	if (runFunc.length > 0)
	{
		s = this.obj + "." + runFunc + "(";
		if (param1 != null) s = s + param1;
		if (param2 != null) s = s + ", " + param2;
		s = s + ");";
		setTimeout(s, 1000);
	}
	else
	{
		setTimeout(this.obj + ".mainLoop();", 1000);
	}
}

function mainLoop()
{
	var behaviour = Math.random() * 40;

	if (this.behaving == false)
	{
		if (behaviour < 4)
		{
			this.mobile(null);
		}
		else if (behaviour < 6)
		{
			this.alternate();
		}

		else if (behaviour < 8)
		{
			this.circle();
		}
		else if (behaviour < 16)
		{
			this.walkLeft();
		}
		else if (behaviour < 24)
		{
			this.walkRight();
		}

		else
		{
			setTimeout("actorObj.mainLoop()", 1100);
		}
	}
	else
	{
		setTimeout("actorObj.mainLoop()", 1100);
	}
}

function scowl(ms)
{
	if (ms == null) ms = 3000 + Math.ceil(Math.random() * 5000);
	this.behaving = true;
	this.setBehaviour("b2", 10, false);
	this.setBehaviour("b1", ms, true);
	setTimeout("actorObj.mainLoop()", ms + 100);
}

function crouch(iterations)
{
	var ms = 0;
	if (iterations == null) iterations = Math.ceil(Math.random() * 5);
	this.behaving = true;
	this.setBehaviour("b3", 10, false);
	for (var i = 1; i < iterations; i++)
	{
		this.setBehaviour("b1", ms += 500, true);
		this.setBehaviour("b3", ms += 500, true);
	}
	this.setBehaviour("b1", ms += 500, true);
	setTimeout("actorObj.mainLoop()", ms + 100);
}

function akimbo(ms)
{
	if (ms == null) ms = 3000 + Math.ceil(Math.random() * 10000);
	this.behaving = true;
	this.setBehaviour("b4", 10, false);
	this.setBehaviour("b1", ms, true);
	setTimeout("actorObj.mainLoop()", ms + 100);
}

function stop(ms)
{
	if (ms == null) ms = 3000 + Math.ceil(Math.random() * 3000);
	this.behaving = true;
	this.setBehaviour("b5", 10, false);
	this.setBehaviour("b1", ms, true);
	setTimeout("actorObj.mainLoop()", ms + 100);
}

function lookSide(iterations)
{
	var ms = 0;
	if (iterations == null) iterations = Math.ceil(Math.random() * 5);
	this.behaving = true;
	this.setBehaviour("b6", 10, false);
	for (var i = 0; i < iterations; i++)
	{
		this.setBehaviour("b7", ms += 100, false);
		this.setBehaviour("b6", ms += 100, false);
	}
	this.setBehaviour("b1", ms += 200, true);
	setTimeout("actorObj.mainLoop()", ms + 100);
}

function mobile(iterations)
{
	var ms = 0;
	if (iterations == null) iterations = Math.ceil(Math.random() * 5);
	this.behaving = true;
//	if (ie4)
//	{
		if (iterations < 3)
		{
			this.showBalloon("<center><font size='2'>Phone my mobile now:<br> <b>07941 230531</b></font></center>", 0, 25, iterations);
		}
		else if (iterations >= 3)
		{
			this.showBalloon("<center><font size='2'>Voice Over IP:<br> <b>0207 099 4507</b></font></center>", 0, 25, iterations);
		}

//	}
	this.setBehaviour("m1", 10, false);
	for (var i = 0; i < iterations; i++)
	{
		this.setBehaviour("m3", ms += 750, false);
		this.setBehaviour("m4", ms += 2000, false);
	}
	this.setBehaviour("m1", ms += 500, false);
	this.setBehaviour("l1", ms += 500, true);
	setTimeout("actorObj.mainLoop()", ms + 100);
}

function circle()
{
	var ms = Math.ceil(Math.random() * 10000)+ 500;
	this.behaving = true;
	this.setBehaviour("l1", 10, false);
	this.setBehaviour("l5", 250, false);
	this.setBehaviour("l9", 500, false);
	this.setBehaviour("l7", 750, false);
	this.setBehaviour("l4", 1000, false);
	ms += 1100;
	this.setBehaviour("l8", ms + 250, false);
	this.setBehaviour("l10", ms + 500, false);
	this.setBehaviour("l6", ms + 750, false);
	this.setBehaviour("l2", ms + 1000, true);
	this.setBehaviour("l1", ms + 4000, true);
	setTimeout("actorObj.mainLoop()", ms + 4100);

}

function walkLeft()
{
	var steps = Math.random() * 12;
	var infront = true;
	var time = 0;
	
	steps = Math.ceil(steps);
	
	if ((this.currentLeft - (steps * 30)) >= this.leftBound)
	{
		this.behaving = true;
		this.setBehaviour("l5", 10, false);
		this.setBehaviour("l9", 500, false);
		time = 500;
		for (var i = 0; i < steps; i++)
		{
				this.currentLeft -= 10;
				this.setBehaviour("l11", time += 100, false);
				this.currentLeft -= 10;
				this.setBehaviour("l12", time += 100, false);
		}
		this.currentLeft -= 10;
		this.setBehaviour("l9", time += 100, false);
		this.setBehaviour("l5", time += 500, false);
		this.setBehaviour("l1", time += 500, true);
	}
	else
	{
		time = 100;
	}

	setTimeout("actorObj.mainLoop()", time + 100);
}

function walkRight()
{
	var steps = Math.random() * 12;
	var infront = true;
	var time = 0;
	
	steps = Math.ceil(steps);
	
	if ((this.currentLeft + (steps * 30)) < this.rightBound)
	{
		this.behaving = true;
		this.setBehaviour("l2", 10, false);
		this.setBehaviour("l6", 500, true);
		this.setBehaviour("l10", 1000, false);
		time = 1100;
		for (var i = 0; i < steps; i++)
		{
			this.currentLeft += 10;
			this.setBehaviour("l13", time += 100, false);
			this.currentLeft += 10;
			this.setBehaviour("l14", time += 100, false);
		}
		this.currentLeft += 10;
		this.setBehaviour("l10", time += 100, false);
		this.setBehaviour("l6", time += 500, false);
		this.setBehaviour("l1", time += 500, true);
	}
	else
	{
		time = 100;
	}

	setTimeout("actorObj.mainLoop()", time + 100);
}

function alternate(){
	var ms = Math.ceil(Math.random() * 10000);
	ms += 500;
	this.setBehaviour("l2", 10, false);
	this.setBehaviour("l1", ms, false);
	setTimeout("actorObj.mainLoop()", ms + 100);
}

function setBehaviour(img, ms, clearBehaving)
{
	var s;
	
	if (ie4 || ns6)
	{
		s = "document.getElementById('" + this.div + "').style.left = " + this.currentLeft + ";";
		s = s + "document.getElementById('" + this.div + "').style.top = " + this.currentBottom + ";";
		s = s + "document.images['" + this.div + "'].src = " + this.imgAr + "['" + img + "'].img.src;";
	}
	else if (ns4)
	{
		s = "document." + this.div + ".left = " + this.currentLeft + ";";
		s = s + "document." + this.div + ".top = " + this.currentBottom + ";";
		s = s + "document." + this.div + ".document.images['" + this.div + "'].src = " + this.imgAr + "['" + img + "'].img.src;";
	}
	if (ie4 || ns4 || ns6)
	{
		if (clearBehaving == true)
		{
			s = s + this.obj + ".behaving = false;";
		}
		//alert(s);
		setTimeout(s, ms);
	}
}

function getLeft()
{
	return this.currentLeft;
}
function getBottom()
{
	return this.currentBottom;
}

function showBalloon(msg, left, top, iterations)
{
	if (ie4 || ns6)
	{
		var balloonText = document.getElementById(this.balloonText);
		var lsound = document.getElementById("lsound");
		var balloon = document.getElementById(this.balloon);
		
		balloonText.innerHTML = msg;
		if (!ns6) lsound.play();
		balloonText.style.top = top - 5;
		balloonText.style.left = left + 5;
		balloon.style.left = this.currentLeft - 25;
		balloon.style.top = this.currentBottom - 75;
		balloon.style.visibility = "visible";
		setTimeout("document.getElementById('" + this.balloon + "').style.visibility = 'hidden';", (2000 * (iterations + 1)) + 1000);
	}
	else if (ns4)
	{
		document.layers[this.balloon].document.layers[this.balloonText].document.write(msg);
		if (navigator.mimeTypes['audio/x-wav']) document.lsound.play(false);
		document.layers[this.balloon].document.layers[this.balloonText].document.close();
//		alert("" + document.layers[this.balloon].document.layers[this.balloonText].document.toString());
		document.layers[this.balloon].document.layers[this.balloonText].top = top - 5;
		document.layers[this.balloon].document.layers[this.balloonText].left = left + 5;
		document.layers[this.balloon].left = this.currentLeft - 25;
		document.layers[this.balloon].top = this.currentBottom - 75;
		document.layers[this.balloon].visibility = "show";
		setTimeout("document." + this.balloon + ".visibility = 'hide';", (2000 * (iterations + 1)) + 1000);
	}
}

