// Scripts for the Steven Scott web site
// Copyright (c)2002-2003 ReGo. All rights reserved.
// Must be included after lib.js


var gridOffsetX   = 100;
var gridOffsetY   =  30;
var gridLine      =   3;	// Border-width
var pictureWidth  = 178;
var pictureHeight = 100;
var textHeight    =  20;	// Height of text line
var textPartW     =  78;	// Width of the first text part

var rowNames = new Array('A', 'B', 'C', 'D', 'E');
var xyzNames = new Array('X', 'Y', 'Z');
var vwNames  = new Array('V', 'W');
var colNames = new Array('1', '2', '3', '4');
var thColNames = new Array('L', 'M', 'R');
var mosaik = new Array(23, 47, 57);
var gridRows = rowNames.length;
var gridCols = colNames.length;

var gridWidth  = pictureWidth + 2*gridLine;
var gridHeight = pictureHeight + 2*gridLine;

var gridFullW = gridCols*pictureWidth + (gridCols+1)*gridLine;
var gridFullH = gridRows*pictureHeight + (gridRows+1)*gridLine;

var gridThirdH = Math.floor(gridFullH / 3) + gridLine;	// 168 169
var gridThirdH2 = Math.floor(2*gridFullH / 3) + gridLine;

var gridHHalfW = 2*pictureWidth + 3*gridLine;

var gridHThirdW = Math.floor((gridFullW + 2*gridLine) / 3);

var gridHalfH = Math.floor((gridFullH + gridLine) / 2);

// Create object grid
var script1=null, script2=null, script3=null, scrip4=null, scriptMontana=null, scriptHide=null, sceneHide=null;
var currentIdx = -1;
var currentShow = -1;
var currentLength = 0;
var nextIdx = 0;
var playTimer = 0;
var showIsPaused = false;

var theGrid = new Array(rowNames.length);
for(var i=0 ; i<rowNames.length ; i++)
	theGrid[i] = new Array(colNames.length);
var theRows = new Array(rowNames.length);
var theRowsG = new Array(rowNames.length);
var theThirds = new Array(3);
var theThirdsG = new Array(3);
var theHHalf = new Array(2);
var theHHalfG = new Array(2);
var theHThirds = new Array(3);
var theHThirdsG = new Array(3);

var theXYZG = new Array(xyzNames.length);
for(i=0 ; i<xyzNames.length ; i++)
	theXYZG[i] = new Array(colNames.length);
var theXYZ = new Array(xyzNames.length);
for(i=0 ; i<xyzNames.length ; i++)
	theXYZ[i] = new Array(colNames.length);

var theVWG = new Array(vwNames.length);
for(i=0 ; i<vwNames.length ; i++)
	theVWG[i] = new Array(colNames.length);
var theVW = new Array(vwNames.length);
for(i=0 ; i<vwNames.length ; i++)
	theVW[i] = new Array(colNames.length);


function CleanUp() {

	clearInterval(window.lowli);
	lowli = 0;
	clearInterval(window.txtli);
	txtli = 0;


	for(i=0 ; i<theGrid.length ; i++)
		for(j=0 ; j<theGrid[i].length ; j++)
			if(theGrid[i][j])
				delete theGrid[i][j];

	for(i=0 ; i<theXYZG.length ; i++)
		for(j=0 ; j<theXYZG[i].length ; j++)
			if(theXYZG[i][j])
				delete theXYZG[i][j];
	for(i=0 ; i<theXYZ.length ; i++)
		for(j=0 ; j<theXYZ[i].length ; j++)
			if(theXYZ[i][j])
				delete theXYZ[i][j];

	for(i=0 ; i<theVWG.length ; i++)
		for(j=0 ; j<theVWG[i].length ; j++)
			if(theVWG[i][j])
				delete theVWG[i][j];
	for(i=0 ; i<theVW.length ; i++)
		for(j=0 ; j<theVW[i].length ; j++)
			if(theVW[i][j])
				delete theVW[i][j];
}

var theC23, theC23G;
var theTwoThird, theTwoThirdG;
var theFull, theFullG;
var theABCD, theABCDG;
var theABCDhl, theABCDhr, theABCDhlG, theABCDhrG;

// Text objects
var theTopText, theTopText1, theTopText2;
var theBottomText;
var theCV, theCreds, theInfo1, theInfo2, theInfo3, theInfo4, theInfo5/*MontanaLight*/, theInfo6/*Weinberger*/;

var message = "Hermetic Light by Steven Scott\n\nWeb coding ©2002-2003 ReGo - www.rego.dk";
var msgPause1 = '<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD WIDTH="50%"><A CLASS="dark" HREF="front.html">Home</A></TD><TD WIDTH="50%" ALIGN="right"><A CLASS="dark" HREF="javascript:GoTo(\'prev\')"><IMG SRC="gfx/arrow_back.jpg" WIDTH="12" HEIGHT="10" ALT="Previous" BORDER="0"></A>&nbsp;&nbsp;&nbsp;';
var msgPauseA = '<A CLASS="dark" HREF="javascript:Pause()"><IMG SRC="gfx/arrow_pause.jpg" WIDTH="9" HEIGHT="10" ALT="Pause" BORDER="0"></A>';
var msgPauseB = '<A CLASS="dark" HREF="javascript:Pause()"><IMG SRC="gfx/arrow_play.jpg" WIDTH="9" HEIGHT="10" ALT="Play" BORDER="0"></A>';
var msgPause2 = '&nbsp;&nbsp;&nbsp;<A CLASS="dark" HREF="javascript:GoTo(\'next\')"><IMG SRC="gfx/arrow_forward.jpg" WIDTH="12" HEIGHT="10" ALT="Next" BORDER="0"></A></TD></TR></TABLE>';

function clickIE() {
	if(document.all) {
		alert(message);
		return false;
	}
}
function clickNS(e) {
	if(document.layers || (document.getElementById && !document.all)) {
		if(e.which==2 || e.which==3) {
			alert(message);
			return false;
		}
	}
}
if(document.layers) {
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown = clickNS;
} else {
	document.onmouseup = clickNS;
	document.oncontextmenu = clickIE;
}

document.oncontextmenu = new Function("return false")


function TheShowMustGoOn(what) {

	currentShow = what;

	switch(what) {
		case 1:
			PlayShow1();
			break;
		case 2:
			PlayShow2();
			break;
		case 3:
			PlayShow3();
			break;
		case 4:
			PlayShow4();
			break;
		case 5:
			PlayShowMontana();
			break;
		case 6:
			PlayShowWeinberger();
			break;
	}
}


var timerArr = new Array();
var timerObj = new Array();
var timerImg = new Array();
var timerCnt = 0;
var timerMod = 0;
var txtMod1  = 0;
var txtMod2  = 0;

function PlayScene(scene) {

	if(window.lowli) {
		clearInterval(lowli);
		lowli = 0;
	}
	clearInterval(window.txtli);
	txtli = 0;
	
	scene.PlayScene();

	var cnt = scene.actions.length;
	timerCnt = 0;
	timerMod = 0;
	txtMod1  = 0;
	txtMod2  = 0;
	for(var i=0 ; i<cnt ; i++) {
		if(scene.actions[i].transition > 0) {
			timerObj[timerCnt] = scene.actions[i];
			timerImg[timerCnt] = eval('document.p' + scene.actions[i].grid);
			timerCnt++;
			timerMod = scene.actions[i].transition;
		}
		if(scene.actions[i].txtTrans1 > 0) {
			txtMod1 = scene.actions[i].txtTrans1;
		}
		if(scene.actions[i].txtTrans2 > 0) {
			txtMod2 = scene.actions[i].txtTrans2;
		}
	}
	if(timerCnt > 0) {
		if(timerMod == 1 || timerMod == 2)
			FadeOut();
		else if(timerMod == 3)
			FadeIn();
	}
	if(txtMod1 > 0 || txtMod2 > 0) {
		TextEffect();
	}
}

function FadeOut() {
	lowli = setInterval("listfadeout()", 50);
}
function listfadeout() {
	var val1 = (timerMod == 2 ? 50 : 0);
	var val2 = val1 / 10;
	for(var i=0 ; i<timerCnt ; i++) {
		var obj = eval(timerImg[i]);
		if(parseFloat(obj.style.MozOpacity) > val2)
			obj.style.MozOpacity = parseFloat(obj.style.MozOpacity) - 0.02;
		else if(obj.filters && obj.filters.alpha.opacity > val1)
			obj.filters.alpha.opacity -= 2;
		else if(lowli) {
			clearInterval(lowli);
			lowli = 0;
		}
	}
}

function FadeIn() {
	lowli = setInterval("listfadein()", 50);
}
function listfadein() {
	for(var ii=0 ; ii<timerCnt ; ii++) {
		var obj = eval(timerImg[ii]);
		if(parseFloat(obj.style.MozOpacity) < 1)
			obj.style.MozOpacity = parseFloat(obj.style.MozOpacity) + 0.02;
		else if(obj.filters && obj.filters.alpha.opacity < 100)
//			obj.filters.alpha.opacity += 2;
			obj.filters.alpha.opacity = Math.min(obj.filters.alpha.opacity+2, 100);
		else if(lowli) {
			clearInterval(lowli);
			lowli = 0;
		}
	}
}

function TextEffect() {
	txtli = setInterval("dotexteffect()", 50);
}
function dotexteffect() {
	if(txtMod1 > 0) {
		var obj1 = eval('document.ptxt1');
		if(parseFloat(obj1.style.MozOpacity) > 0)
			obj1.style.MozOpacity = parseFloat(obj1.style.MozOpacity) - 0.02;
		else if(obj1.filters && obj1.filters.alpha.opacity > 0)
			obj1.filters.alpha.opacity -= 2;
//		else if(txtli) {
//			clearInterval(txtli);
//			txtli = 0;
//		}
	}

	if(txtMod2 > 0) {
		var obj2 = eval('document.ptxt2');
		if(parseFloat(obj2.style.MozOpacity) > 0)
			obj2.style.MozOpacity = parseFloat(obj2.style.MozOpacity) - 0.02;
		else if(obj2.filters && obj2.filters.alpha.opacity > 0)
			obj2.filters.alpha.opacity -= 2;
//		else if(txtli) {
//			clearInterval(txtli);
//			txtli = 0;
//		}
	}
}


function SSScript(name, loopFrame) {
	this.name = name;
	this.scenes  = new Array();
	this.sceneTimer = 0;
	this.loopAtEnd  = (arguments.length > 1 ? loopFrame : 0);	// -1=stop playing, positive numbers=frame index


	this.AddScene =	function(scene, index) {
						if(arguments.length <= 1 || index < 0 || index > this.scenes.length)
							index = this.scenes.length;
						this.scenes[index] = scene;

						scene.startAt = this.sceneTimer;
						this.sceneTimer += scene.totalTime;
					}
}

function SSScene() {
	this.actions = new Array();
	this.allowLink = 0;
	this.totalTime = 0;
	this.startAt = 0;


	this.AddAction =	function(action, linkTo) {
							var aidx = this.actions.length;
							this.actions[aidx] = action;

							if(arguments.length >= 2) {
//								alert(linkTo);
								this.actions[aidx].linkTo = linkTo;
							}

							this.totalTime += action.totalTime;
						}

	this.PlayScene = 	function() {
							var cnt = this.actions.length;
							for(var i=0 ; i<cnt ; i++) {
								this.actions[i].PlayAction();
							}

						// DEBUG
//							theBottomText.writeIt('Scene ' + currentIdx + ': Start at: ' + this.startAt + ' (' + this.totalTime + ')');
						}

}

function SSAction(grid, picture, duration, transition, textTop1, txtTransition1, textTop2, txtTransition2) {
	this.grid       = new String(grid);
	this.picture    = picture;
	this.duration   = (arguments.length >= 3 ? (duration*1000) : 5000);
	this.transition = (transition ? transition : 0);

	this.textTop1   = (textTop1 ? textTop1 : '');
	this.textTop2   = (textTop2 ? textTop2 : '');
	this.txtTrans1  = (txtTransition1 ? txtTransition1 : 0);
	this.txtTrans2  = (txtTransition2 ? txtTransition2 : 0);

	this.linkTo     = -1;
	this.totalTime  = this.duration;

	this.obj = null;
	this.gobj = null;


	if(grid && this.grid.substr(0, 4) != 'hide') {
		this.obj = new lib_obj(grid);
		this.gobj = new lib_obj('g'+grid);
	}


// METHODS
	this.PlayAction =	function() {
							if(this.grid.substr(0, 4) == 'hide') {
								var gn = this.grid.substr(4);
								if(gn == 'Full') {
									theFull.hideIt();
									theFullG.hideIt();
								} else if(gn == 'A2t') {
									theTwoThird.hideIt();
									theTwoThirdG.hideIt();
								} else if(gn == 'C23') {
									theC23.hideIt();
									theC23G.hideIt();
								} else if(gn == 'ABCD') {
									theABCD.hideIt();
									theABCDG.hideIt();
								} else if(gn == 'ABCDhl') {
									theABCDhl.hideIt();
									theABCDhlG.hideIt();
								} else if(gn == 'ABCDhr') {
									theABCDhr.hideIt();
									theABCDhrG.hideIt();
								} else if(gn == 'ssinfo1') {
									theInfo1.hideIt();
								} else if(gn == 'ssinfo2') {
									theInfo2.hideIt();
								} else if(gn == 'ssinfo3') {
									theInfo3.hideIt();
								} else if(gn == 'ssinfo4') {
									theInfo4.hideIt();
								} else if(gn == 'ssinfo5') {
									theInfo5.hideIt();
								} else if(gn == 'ssinfo6') {
									theInfo6.hideIt();
								} else if(gn == 'sscreds') {
									theCreds.hideIt();
								} else {
									var n = gn.substr(0, 1);
									var t = gn.substr(1, 1);
									if(t == 'r') {
								// Rows
										for(var k=0 ; k<gridRows ; k++)
											if(n == rowNames[k]) {
												theRows[k].hideIt();
												theRowsG[k].hideIt();
												break;
											}
									} else if(t == 't') {
								// Third rows
										if(n == 'A') {
											theThirds[0].hideIt();
											theThirdsG[0].hideIt();
										} else if(n == 'C') {
											theThirds[1].hideIt();
											theThirdsG[1].hideIt();
										} else if(n == 'E') {
											theThirds[2].hideIt();
											theThirdsG[2].hideIt();
								// Third cols
										} else if(n == 'L') {
											theHThirds[0].hideIt();
											theHThirdsG[0].hideIt();
										} else if(n == 'M') {
											theHThirds[1].hideIt();
											theHThirdsG[1].hideIt();
										} else if(n == 'R') {
											theHThirds[2].hideIt();
											theHThirdsG[2].hideIt();
										}
									} else if(t == 'h') {
								// Half cols
										if(n == 'L') {
											theHHalf[0].hideIt();
											theHHalfG[0].hideIt();
										} else if(n == 'R') {
											theHHalf[1].hideIt();
											theHHalfG[1].hideIt();
										}
									} else {
										// Should check for XYZ and VW grids
										var xyzFound = false;
					//		alert(n + ' - ' + t + ' ... ' + xyzNames.length);
										for(var k=0 ; k<xyzNames.length ; k++) {
											if(n == xyzNames[k]) {
												theXYZ[k][parseInt(t)-1].hideIt();
												theXYZG[k][parseInt(t)-1].hideIt();
												xyzFound = true;
												break;
											}
										}
										if(!xyzFound) {
											for(var k=0 ; k<vwNames.length ; k++) {
												if(n == vwNames[k]) {
													theVW[k][parseInt(t)-1].hideIt();
													theVWG[k][parseInt(t)-1].hideIt();
//													xyzFound = true;
													break;
												}
											}
										}
									}
								}
							} else if(this.picture) {

								if(this.transition == 1) {
									this.obj.bgimage(this.picture);
								} else { // if(this.transition == 3) {
									var opacity = (this.transition != 3 ? 100 : 50);

									var obj = eval('document.p' + this.grid);
									obj.src = this.picture;
									if(bw.ns6)		obj.style.MozOpacity = opacity/10; //0.5;
									else if(bw.ie)	obj.filters.alpha.opacity = opacity;
									this.obj.bgimage('gfx/blank.jpg');
									this.obj.showIt();
									this.gobj.showIt();
								}

							} else {
								this.obj.showIt();
							}

							if(this.textTop1 != '') {
								if(this.txtTrans1 == 1) {
									theTopText1.bgimage(this.textTop1);
								} else {
									var obj1 = eval('document.ptxt1');
									obj1.src = this.textTop1;
									if(bw.ns6)		obj1.style.MozOpacity = 1.0;
									else if(bw.ie)	obj1.filters.alpha.opacity = 100;
									theTopText1.bgimage('gfx/txt_blanka.jpg');
								}
							}
							if(this.textTop2 != '') {
								if(this.txtTrans2 == 1) {
									theTopText2.bgimage(this.textTop2);
								} else {
									var obj2 = eval('document.ptxt2');
									obj2.src = this.textTop2;
									if(bw.ns6)		obj2.style.MozOpacity = 1.0;
									else if(bw.ie)	obj2.filters.alpha.opacity = 100;
									theTopText2.bgimage('gfx/txt_blankb.jpg');
								}
							}
//							theTopText.writeIt(this.textTop);
//							theBottomText.writeIt(this.textBottom);
						}

}


function TestClick(grid) {
	var allow = 0, cnt, linkTo;

	switch(currentShow) {
		case 1:
			allow = script1.scenes[currentIdx].allowLink;
			cnt = script1.scenes[currentIdx].actions.length;
			linkTo = script1.scenes[currentIdx].actions[grid].linkTo;
			break;
		case 2:
			allow = script2.scenes[currentIdx].allowLink;
			cnt = script2.scenes[currentIdx].actions.length;
			linkTo = script2.scenes[currentIdx].actions[grid].linkTo;
			break;
		case 3:
			allow = script3.scenes[currentIdx].allowLink;
			cnt = script3.scenes[currentIdx].actions.length;
			linkTo = script3.scenes[currentIdx].actions[grid].linkTo;
			break;
		case 4:
			allow = script4.scenes[currentIdx].allowLink;
			cnt = script4.scenes[currentIdx].actions.length;
			linkTo = script4.scenes[currentIdx].actions[grid].linkTo;
			break;
		case 5:
			allow = scriptMontana.scenes[currentIdx].allowLink;
			cnt = scriptMontana.scenes[currentIdx].actions.length;
			linkTo = scriptMontana.scenes[currentIdx].actions[grid].linkTo;
			break;
		case 6:
			allow = scriptWeinberger.scenes[currentIdx].allowLink;
			cnt = scriptWeinberger.scenes[currentIdx].actions.length;
			linkTo = scriptWeinberger.scenes[currentIdx].actions[grid].linkTo;
			break;
	}

alert('Test click grid ' + grid + ' (current show ' + currentShow + ')\nAllow = ' + allow + '\nCount = ' + cnt + '\nLinkTo = ' + linkTo);
	if(currentIdx >= 0 && allow) {
		if(grid <= cnt) {
			if(linkTo != -1) {
				nextIdx = linkTo;
				if(playTimer > 0)
					clearTimeout(playTimer);
				switch(currentShow) {
					case 1:
						PlayShow21();
						break;
					case 2:
						PlayShow22();
						break;
					case 3:
						PlayShow23();
						break;
					case 4:
						PlayShow24();
						break;
					case 5:
						PlayShowMontana2();
						break;
					case 6:
						PlayShowWeinberger2();
						break;
				}
			}
		}
	}
}

function GoTo(where) {
	var scn = parseInt(where);
	if(isNaN(scn)) {
		if(where == 'prev') {
			if(nextIdx < 0)
				nextIdx = currentLength-2;
			else if(nextIdx < 2)
				nextIdx = currentLength-1;
			else
				nextIdx = Math.max(nextIdx-2, 0);
		}
//		else if(where == 'next')
//			nextIdx++;
	} else {
		nextIdx = scn;
	}

	if(playTimer > 0)
		clearTimeout(playTimer);
	PlayScene(scriptHide.scenes[0]);

	switch(currentShow) {
		case 1:
			PlayShow21();
			break;
		case 2:
			PlayShow22();
			break;
		case 3:
			PlayShow23();
			break;
		case 4:
			PlayShow24();
			break;
		case 5:
			PlayShowMontana2();
			break;
		case 6:
			PlayShowWeinberger2();
			break;
	}

	showIsPaused = false;
	WriteControl();
}

function Pause() {

	if(showIsPaused) {
		if(playTimer > 0)
			clearTimeout(playTimer);

		switch(currentShow) {
			case 1:
				PlayShow21();
				break;
			case 2:
				PlayShow22();
				break;
			case 3:
				PlayShow23();
				break;
			case 4:
				PlayShow24();
				break;
			case 5:
				PlayShowMontana2();
				break;
			case 6:
				PlayShowWeinberger2();
				break;
		}
	} else {
		if(playTimer > 0)
			clearTimeout(playTimer);

		switch(currentShow) {
			case 1:
				playTimer = setTimeout('PlayShow21()', 6000000);
				break;
			case 2:
				playTimer = setTimeout('PlayShow22()', 6000000);
				break;
			case 3:
				playTimer = setTimeout('PlayShow23()', 6000000);
				break;
			case 4:
				playTimer = setTimeout('PlayShow24()', 6000000);
				break;
			case 5:
				playTimer = setTimeout('PlayShowMontana2()', 6000000);
				break;
			case 6:
				playTimer = setTimeout('PlayShowWeinberger2()', 6000000);
				break;
		}
	}

	showIsPaused = !showIsPaused;
	WriteControl();
}

function WriteControl() {
	var msg = msgPause1;
	msg += (showIsPaused ? msgPauseB : msgPauseA);
	msg += msgPause2;
	theBottomText.writeIt(msg);
}

