//note: should check to see if playing the flash sound causes problems if flash is not installed

//Browsercheck (needed)
function lib_bwcheck() {
	this.ver = navigator.appVersion;
	this.agent = navigator.userAgent
	this.dom = document.getElementById ? 1 : 0;
	this.opera5 = this.agent.indexOf("Opera 5") > -1;
	this.ie5 = (this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5) ? 1 : 0; 
	this.ie6 = (this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5) ? 1 : 0;
	this.ie4 = (document.all && !this.dom && !this.opera5) ? 1 : 0;
	this.ie = this.ie4 || this.ie5 || this.ie6
	this.mac = this.agent.indexOf("Mac")>-1
	this.ns6 = (this.dom && parseInt(this.ver) >= 5) ? 1 : 0; 
	this.ns4 = (document.layers && !this.dom) ? 1 : 0;
	this.bw = (this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
	return this;
}
var bw = new lib_bwcheck();

var xLeftClosed = 0; //initial position of left door
var xLeftOpen = -90; //position of left door when it's open
var xRightClosed = 90; //initial position of right door
var xRightOpen = 180; //position of right door when it's open

//object constructor for doors
function makeDoor(obj,nest) {
	nest=(!nest) ? "" : 'document.'+nest+'.' //nest refers to the parent layer (needed for ns4)
	this.element =
		bw.dom ? document.getElementById(obj) :
		bw.ie4 ? document.all[obj] :
		bw.ns4 ? eval(nest+'document.'+obj) : 0;
  	this.css =
		bw.dom ? document.getElementById(obj).style :
		bw.ie4 ? document.all[obj].style :
		bw.ns4 ? eval(nest+'document.'+obj) : 0;
	this.xLeft = xLeftClosed;
	this.xRight = xRightClosed;
	this.openDoorLeft = obj_openDoorLeft;
	this.openDoorRight = obj_openDoorRight;
	this.closeDoorLeft = obj_closeDoorLeft;
	this.closeDoorRight = obj_closeDoorRight;
	this.obj = obj + "Object"
    eval(this.obj + "= this")
	return this
}
var px = bw.ns4 || window.opera ? "" : "px";
var doorStep = 15; //incremental movement
var doorSpeed = 50; //animation speed in milliseconds
var openleftTimer = null;
var openRightTimer = null;
var closeleftTimer = null;
var closeRightTimer = null;
var whichOpen = null; //tracks which doors are open

function openDoors(index) {
	if(reopenTimer) {
		clearTimeout(reopenTimer);
		reopenTimer = null;
	}
	if (whichOpen != index) { //check to see if requested doors are already open
		if (whichOpen != null) { //close any doors that are open
			closeDoors(whichOpen);
		}
		if (!openleftTimer && !openRightTimer) { //check to see if doors are already in the process of opening
			eval("oDoorL"+index+".openDoorLeft("+index+")");
			eval("oDoorR"+index+".openDoorRight()");
			playDoorSound();
			whichOpen = index;
		}
	}
}
function closeDoors(index,page) {
	doorUI_off(index);
	whichPage = (!page) ? "" : ','+page;
	eval("oDoorL"+index+".closeDoorLeft("+index+whichPage+")");
	eval("oDoorR"+index+".closeDoorRight()");
	playDoorSound();
	whichOpen = null;
}

function obj_openDoorLeft(index) {
	if (this.xLeft > xLeftOpen) {
		this.xLeft -= doorStep;
		this.css.left = this.xLeft + px;
		openLeftTimer = setTimeout(this.obj+".openDoorLeft("+index+")",doorSpeed);
	}
	else {
		clearTimeout(openLeftTimer);
		openLeftTimer = null;
		this.xLeft = xLeftOpen;
		doorUI_on(index);
	}
}
function obj_openDoorRight() {
	if (this.xRight < xRightOpen) {
		this.xRight += doorStep;
		this.css.left = this.xRight + px;
		openRightTimer = setTimeout(this.obj+".openDoorRight()",doorSpeed);
	}
	else {
		clearTimeout(openRightTimer);
		openRightTimer = null;
		this.xRight = xRightOpen;
	}
}
var reopenTimer = null;

var currentPage = new Array(); //tracks which content layer is visible for each doorway
	currentPage[0] = 1;
	currentPage[1] = 1;
	currentPage[2] = 1;
	currentPage[3] = 1;
	currentPage[4] = 1;

function changePage(index,page) {
	hidePage = currentPage[index];
	if (document.getElementById) { //DOM browsers
		document.getElementById("content"+index+hidePage).style.visibility = "hidden";
		document.getElementById("content"+index+page).style.visibility = "visible";
	}
	if (document.layers && !document.getElementById) {// NS4
		document["doorway"+index].document["content"+index+hidePage].visibility = "hidden";
		document["doorway"+index].document["content"+index+page].visibility = "visible";
	}
	currentPage[index] = page;
	eval("oContent"+index+page+".moveIt(0,0)");//scrolls the visible content layer back to the top; oContent is defined in scroll_content.js
}
	
function obj_closeDoorLeft(index,page) {
	if (this.xLeft < xLeftClosed) {
		this.xLeft += doorStep;
		this.css.left = this.xLeft + px;
		whichPage = (!page) ? "" : ','+page;
		closeLeftTimer = setTimeout(this.obj+".closeDoorLeft("+index+whichPage+")",doorSpeed);
	}
	else {
		clearTimeout(closeLeftTimer);
		closeLeftTimer = null;
		this.xLeft = xLeftClosed;
		if(page) { //reopens the doors with different content layer visible
			changePage(index,page);
			reopenTimer = setTimeout("openDoors("+index+")",50);
		}
	}
}
function obj_closeDoorRight() {
	if (this.xRight > xRightClosed) {
		this.xRight -= doorStep;
		this.css.left = this.xRight + px;
		closeRightTimer = setTimeout(this.obj+".closeDoorRight()",doorSpeed);
	}
	else {
		clearTimeout(closeRightTimer);
		closeRightTimer = null;
		this.xRight = xRightClosed;
	}
}

//plays sound from flash movie when doors open and close
var sound = "off"
function playDoorSound() {
	var movie = window.document.doorSound;
	if (sound != "off") {movie.Play();}
}
function soundToggle() {
	sound = (sound == "off")? "on" : "off";
}

var numDoors = 4; //sets the total number of doorways
function initDoors() {
	if (bw.bw) {
		oDoorL1 = new makeDoor('door1L','doorway1');
		oDoorR1 = new makeDoor('door1R','doorway1');
		oDoorL2 = new makeDoor('door2L','doorway2');
		oDoorR2 = new makeDoor('door2R','doorway2');
		oDoorL3 = new makeDoor('door3L','doorway3');
		oDoorR3 = new makeDoor('door3R','doorway3');
		oDoorL3 = new makeDoor('door3L','doorway3');
		oDoorR3 = new makeDoor('door3R','doorway3');
		oDoorL4 = new makeDoor('door4L','doorway4');
		oDoorR4 = new makeDoor('door4R','doorway4');
		for (i=1; i<=numDoors; i++) {
			oDoorL = eval("oDoorL"+i);
			oDoorR = eval("oDoorR"+i);
			oDoorL.css.visibility = "visible";
			oDoorR.css.visibility = "visible";
		}
		preloadImgs();
		//setTimeout ('openDoors(1)',1000);		
	}
}

//toggles the door UI on and off
function doorUI_on(index) {
	if (document.getElementById) {
		document.getElementById("ui"+index).style.visibility = "visible";
	}
	if (bw.ns4) {
		document["ui"+index].visibility = "visible";
	}
}
function doorUI_off(index) {
	if (document.getElementById) {
		document.getElementById("ui"+index).style.visibility = "hidden";
	}
	if (bw.ns4) {
		document["ui"+index].visibility = "hidden";
	}
}

