// Methods for load scenes and positions.
var gCurrentScene = "default";
var gCurrentTnt = "Youngvic_groundfloor";
var gWaitingCommand = null;
var gMenuItems = new Array("boxOffice","grndFloorBar","theMaria","theClare","frstFloorBar","frstFloorTrrce","mainHouseStageView","mainHouseLevel1","mainHouseGround");
var gCurrentArea = "boxOffice";
var gMode = "walkAround";

function walkAround() {
	gMode = "walkAround";
	
	// Hide / Show Objects	
	styleShow("menuItems");
	styleShow("floorPlan");
	styleShow("instructions");		
	styleHide("guidedTourOptions");	
	
	// Start Turntool
	initTurnTool();
}
function guidedTour() {
	gMode = "guidedTour";	
	
	// Hide / Show Objects
	styleHide("menuItems");
	styleHide("floorPlan");
	styleHide("instructions");	
	styleShow("guidedTourOptions");
	
	// Write flash object to turntool div
	var so = new SWFObject("swf/largeMovie.swf", "largeMovie", "540", "360", "7", "#ffffff");
	so.write("TurnTool");

	// If flash has been detected and the embed object is in the container; give instructions
	var mc = element("TurnTool").innerHTML;
	var mm = element("guidedTourOptions");
	
	// Search for flash in turntool div
	var displayed = mc.search(/x-shockwave-flash/i);
	var displayed_ie = mc.search(/flashvars/i);
	
	// Provide info
	if (displayed > 0 || displayed_ie > 0) {
		mm.innerHTML = "<p style='clear: both'>Press the play button to begin the animation.</p>";
	}
	else {
		mm.innerHTML = '<p style="color: black">To view this movie you must have Flash Player 8 installed.</p>';
		mm.innerHTML += '<a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank"><img style="display:inline;" src="images/getAdobeFlash.gif" alt="Get Adobe Flash" /></a>';
	}	
	window.status = "You are currently viewing the Animated Tour Movie.";	
}
function loadScene(scene) {
	gCurrentScene = scene;
	window.status = "Loading " + scene + "...";	
	
	switch(scene) {
		case "boxOffice":
			if(gCurrentTnt != "Youngvic_groundfloor") {
				gCurrentTnt = "Youngvic_groundfloor";
				changeScene("tnt/Youngvic_groundfloor.tnt");
				commandWaiting("loadArea('boxOffice')");
			}
			else {
				loadArea('boxOffice');				
			}
			highlightMenuItem("boxOffice");
		break;
		case "grndFloorBar":
			if(gCurrentTnt != "Youngvic_groundfloor") {
				gCurrentTnt = "Youngvic_groundfloor";
				changeScene("tnt/Youngvic_groundfloor.tnt");
				commandWaiting("loadArea('grndFloorBar')");
			}
			else {
				loadArea('grndFloorBar');				
			}
			highlightMenuItem("grndFloorBar");			
		break;
		case "theMaria":
			if(gCurrentTnt != "Youngvic_groundfloor") {
				gCurrentTnt = "Youngvic_groundfloor";
				changeScene("tnt/Youngvic_groundfloor.tnt");
				commandWaiting("loadArea('theMaria')");
			}
			else {
				loadArea('theMaria');				
			}
			highlightMenuItem("theMaria");				
		break;	
		case "theClare":
			if(gCurrentTnt != "Youngvic_groundfloor") {
				gCurrentTnt = "Youngvic_groundfloor";
				changeScene("tnt/Youngvic_groundfloor.tnt");
				commandWaiting("loadArea('theClare')");
			}
			else {
				loadArea('theClare');				
			}
			highlightMenuItem("theClare");				
		break;				
		case "frstFloorBar":
			if(gCurrentTnt != "Youngvic_firstfloor") {
				gCurrentTnt = "Youngvic_firstfloor";
				changeScene("tnt/Youngvic_firstfloor.tnt");
				commandWaiting("loadArea('frstFloorBar')");
			}
			else {
				loadArea('frstFloorBar');				
			}
			highlightMenuItem("frstFloorBar");			
		break;
		case "frstFloorTrrce":
			if(gCurrentTnt != "Youngvic_firstfloor") {
				gCurrentTnt = "Youngvic_firstfloor";
				changeScene("tnt/Youngvic_firstfloor.tnt");
				commandWaiting("loadArea('frstFloorTrrce')");
			}
			else {
				loadArea('frstFloorTrrce');				
			}
			highlightMenuItem("frstFloorTrrce");				
		break;
		case "mainHouseStageView":
			if(gCurrentTnt != "Youngvic_mainHouse") {
				gCurrentTnt = "Youngvic_mainHouse";
				changeScene("tnt/Youngvic_mainHouse.tnt");
				commandWaiting("loadArea('mainHouseStageView')");
			}
			else {
				loadArea('mainHouseStageView');				
			}
			highlightMenuItem("mainHouseStageView");			
		break;
		case "mainHouseLevel1":
			if(gCurrentTnt != "Youngvic_mainHouse") {
				gCurrentTnt = "Youngvic_mainHouse";
				changeScene("tnt/Youngvic_mainHouse.tnt");
				commandWaiting("loadArea('mainHouseLevel1')");
			}
			else {
				loadArea('mainHouseLevel1');				
			}
			highlightMenuItem("mainHouseLevel1");				
		break;
		case "mainHouseGround":
			if(gCurrentTnt != "Youngvic_mainHouse") {
				gCurrentTnt = "Youngvic_mainHouse";
				changeScene("tnt/Youngvic_mainHouse.tnt");
				commandWaiting("loadArea('mainHouseGround')");
			}
			else {
				loadArea('mainHouseGround');				
			}
			highlightMenuItem("mainHouseGround");				
		break;		
	}
}
function changeScene(scene) {
	element("TNTCtrl").src = scene;	
}
function loadArea(area) {
	physicsSphere = "Sphere01";
	gCurrentArea = area;
	
	window.status = "Loading " + area + "...";
	
	switch(area) {
		case "boxOffice":
			Move(physicsSphere,26126.82,-4158.8,115.53066,0.,0.,25.494974);
			window.status = "You are currently viewing the Box Office.";			
		break;
		case "grndFloorBar":
			Move(physicsSphere,19291.812,4211.9487,115.53065,0.,0.,107.71441);
			window.status = "You are currently viewing the Ground Floor bar.";			
		break;
		case "theMaria":
			Move(physicsSphere,7195.476,-3676.812,115.53065,0.,0.,50.762317);
			window.status = "You are currently viewing the Maria Studio.";				
		break;
		case "theClare":
			Move(physicsSphere,-3647.754,16242.152,115.61967,0.,0.,262.76736);	
			window.status = "You are currently viewing the Clare Studio.";				
		break;
		case "frstFloorBar":
			Move(physicsSphere,12982.314,6092.562,3745.9414,0.,0.,147.43251);	
			window.status = "You are currently viewing the First Floor Bar.";				
		break;
		case "frstFloorTrrce":
			Move(physicsSphere,8242.125,-3125.606,3754.1699,0.,0.,250.90873);
			window.status = "You are currently viewing the First Floor Terrace.";			
		break;
		case "mainHouseStageView":
			Move(physicsSphere,727.55609,-1462.61,-6325.077,0.,0.,39.144672);	
			window.status = "You are currently viewing the Main House from the stage.";			
		break;
		case "mainHouseLevel1":
			Move(physicsSphere,-11745.47,2734.0104,-735.6743,0.,0.,267.59362);
			window.status = "You are currently viewing the Main House from the first floor.";			
		break;	
		case "mainHouseGround":
			Move(physicsSphere,-10542.,2746.187,-4318.695,0.,0.,267.50405);	
			window.status = "You are currently viewing the Main House from the ground floor.";			
		break;	
	}
}
function highlightMenuItem(menuItem) {
	
	if(element(menuItem).style.borderBottomColor == "none" || element(menuItem).style.borderBottomColor == "")
	{
		element(menuItem).style.borderBottomColor = "#D9082B";
	}
	for(i=0;i<gMenuItems.length;i++) {
		if(gMenuItems[i] != menuItem) {
			var m = element(gMenuItems[i]);
			m.style.borderBottomColor = "";
		}
	}
	
}
function Move(SourceObject, X, Y, Z,rotX, rotY, rotZ) {
	updateX = X;
	updateY = Y;
	updateZ = Z;
	updateRotX = rotX;
	updateRotY = rotY;
	updateRotZ = rotZ;
	
	TNTDoCommand('Objects("'+SourceObject+'").SetPositionLocal('+updateX+','+updateY+','+updateZ+')');
	TNTDoCommand('Objects("'+SourceObject+'").SetRotationLocal('+updateRotX+','+updateRotY+','+updateRotZ+')');
	
}
function getPosition() {
	posX = TNTDoCommand('Objects("Camera01").GetPositionLocalX()');
	posY = TNTDoCommand('Objects("Camera01").GetPositionLocalY()');
	posZ = TNTDoCommand('Objects("Camera01").GetPositionLocalZ()');
	rotX = TNTDoCommand('Objects("Camera01").GetRotationLocalX()');
	rotY = TNTDoCommand('Objects("Camera01").GetRotationLocalY()');
	rotZ = TNTDoCommand('Objects("Camera01").GetRotationLocalZ()');
	element("debug").innerHTML = (posX + "," + posY + "," + posZ + "," + rotX + "," + rotY + "," + rotZ);
}

function commandWaiting(command) {
	// Sets the command waiting to the command supplied, this will be executed when the onReady() state of
	// of the 3D scene is true. Commands can not be executed before this is the case, therefore a wait is necessary.
	gWaitingCommand = command;
}
function styleHide(obj) {
	element(obj).style.display = "none";
}
function styleShow(obj) {
	element(obj).style.display = "block";
}
// Turntool Events
function OnReady() {
	// If there is a command in the queue then execute when 3D scene is ready	
	if(gWaitingCommand != null) {
		eval(gWaitingCommand);
		gWaitingCommand = null;
	}
}
function OnZoneEnter( zone, physics )
{
	focus();
	// window.status = "Zone Entered: " + zone + ", by physics object: " + physics;
	switch(zone) {
		case "Sphere_reception_groundfloor":
			loadScene('boxOffice');
			commandWaiting("Move('Sphere01',20785.238,6481.9218,115.53066,0.,0.,117.57988)");		
		break;
		case "Sphere_reception_firstfloor":
			loadScene('frstFloorBar');
			commandWaiting("Move('Sphere01',22162.974,6465.5561,3745.9438,0.,0.,88.313781)");
		break;
		case "Sphere_auditorium_groundfloor":
			loadScene('mainHouseGround');
		break;
		case "Sphere_firstfloor":
			loadScene('frstFloorBar');
			commandWaiting("Move('Sphere01',19682.136,6165.4072,3745.9628,0.,0.,78.849495)");
		break;		
		case "Sphere_groundfloor":
			loadScene('boxOffice');
			commandWaiting("Move('Sphere01',20063.531,-344.1734,115.59453,0.,0.,179.1051)");		
		break;		
		case "Sphere_auditorium_firstfloor":
			loadScene('mainHouseLevel1');		
			commandWaiting("Move('Sphere01',-16023.99,3540.9055,-734.3867,0.,0.,253.55595)");
		break;
	}
}

function OnZoneExit( zone, physics )
{
	focus();
	// window.status = "Zone Exited: " + zone + ", by physics object: " + physics;	
}