// Copyright 2007-2008 NEW3S s.a. - NEW3S.com
// Christophe LEMOINE + Michael BRICOUT

var gSalonURL = window.location.hostname;
var gCurrentProjet = "";
var gLocalProjet   = "";
var gCurrentScene  = "";
var gLastScene     = "";
var gSetCamera     = "";
var gJumpToSceneOption = 0;
var gSceneListId       = new Array();
var gSceneListPath     = new Array();
var gSceneListName     = new Array();
var gSceneOptionLabel  = new Array();
var gSceneOptionValue  = new Array();
var gSceneOptionLevel  = new Array();
var gSceneOptionAlias  = new Array();
var gSceneOptionStyle  = new Array();
var gSceneDebug = 0;

//var mysound = new FlashSound();

// Resizing the window
function Resize(width, height) {
	if (ie4||ie5) {
		self.resizeTo(width, height);
	} else {
		window.outerHeight = width; 
		window.outerWidth  = height; 
	}
}

function SleepFor(time) {
	d=new Date();
	while(1){
		n=new Date();
		diff=n-d;
		if (diff > time)
			break;
	}
}

function AddScene(id, repository, tntFile) {
	if (id != "") {
		var nbItem = gSceneListId.length;
		gSceneListId  [nbItem]=id;
		gSceneListPath[nbItem]=repository;
		gSceneListName[nbItem]=tntFile;
	}
}

function AddScenesOptions() {
	var nbItem = gSceneListId.length;
	for (var i=0; (i<nbItem); i++) {
		var string = "Add_" + gSceneListId[i] + "_option()";
		CLEexecScript (string);
	}
}

function AddSceneOption (level, label, value, alias) {
	if (label != "") {
		var style = ""; // Default style
		if (level == "0") { style += "font-weight:bold;"; }
		AddSceneOptionWithStyle (level, label, value, alias, style);
	}
}

function AddSceneOptionWithStyle (level, label, value, alias, style) {
	if (label != "") {
		var nbItem = gSceneOptionLabel.length;
		gSceneOptionLevel[nbItem]=level;
		gSceneOptionLabel[nbItem]=label;
		gSceneOptionValue[nbItem]=value;
		gSceneOptionAlias[nbItem]=alias;
		gSceneOptionStyle[nbItem]=style;
	}
}

function IndexOfSceneOption (label) {
	var nbItem = gSceneOptionLabel.length;
	for (var i=0; (i<nbItem); i++) {
		if (gSceneOptionLabel[i] == label) {
			return i;
		}
	}
	return '-1';
}

function IndexOfSceneAlias (label) {
	var nbItem = gSceneOptionAlias.length;
	for (var i=0; (i<nbItem); i++) {
		if (gSceneOptionAlias[i] == label) {
			return i;
		}
	}
	return '-1';
}

function GoToAdresse() {
    var adresse=document.getElementById('adresse');
	var ind=adresse.selectedIndex;
	var pos=adresse.options[ind].value;
	window.status = "JumpToSceneOption("+pos+")'";
	JumpToSceneOption (pos);
	adresse.selectedIndex = 0;
}

function JumpToSceneOption(index) {
	window.status = "JumpToSceneOption("+index+") = '"+gSceneOptionValue[index]+"'";
	CLEexecScript(gSceneOptionValue[index]);
}

function BuildScenesOption(obj) {
    var isColourSet = 0;
	var tags = '<form NAME="formulaire">';
	if (ie4||ie5) {	tags += '<select NAME="adresse" onchange="JavaScript:void GoToAdresse()">'; }
	else { tags += '<select NAME="index" size="1" style="font-family: Tahoma;" >'; }
	tags += '<option value="">Allez Directement ...</option>';
	var nbItem = gSceneOptionLabel.length;
	for (var i=0; (i<nbItem); i++) {
		var level = gSceneOptionLevel[i];
		var label = '';
		for (var l=0; l < level ; l++) { label += '&nbsp; &nbsp; '; }
		label += gSceneOptionLabel[i];
		tags += '<option VALUE="'+i+'"';
		if (gSceneOptionStyle[i] != "") { tags += ' style="' + gSceneOptionStyle[i] + '"'; }
		//if (level == "0") { tags += '><b>'+label+'</b></option>'; }
		//else              { tags += '>'+label+'</option>'; }
		tags += '>'+label+'</option>';
	}
	tags += '</select>';
	if (!(ie4||ie5)) { tags += '</select><input type="submit" value="Go" href="#">'; }
	tags += '</form>';
	element(obj).innerHTML = tags;
}

function SceneTracker(who, obj, data) {
	//Statistics
	var info = "/"+gCurrentScene;
	if (who  != "") { info += "/"+who; }
	if (obj  != "") { info += "/"+obj; }
	if (data != "") { info += "/"+data; }
	SceneTrackerMessage(info);
}

function SceneTrackerMessage(info) {
	//Statistics
	pageTracker._trackPageview(info);
	info = gSalonURL + info;
	allPageTracker._trackPageview(info);
}

function ChangeSceneCamera(newCamera) {
	var currentCamera = TNTDoCommand('CameraCtrl.GetCurrent()');
	var srcParentNodeIdx = TNTDoCommand('Objects('+currentCamera+').GetParentNodeIndex()');
	var desParentNodeIdx = TNTDoCommand('Objects('+newCamera+').GetParentNodeIndex()');
	if (srcParentNodeIdx == desParentNodeIdx) {
		var frameCount = TNTDoCommand('Objects('+newCamera+').GetFrameCount()');
		if (frameCount>1) {
			TNTDoCommand('Objects(*).PlayAnimation(0,'+frameCount+')');	
			TNTDoCommand('CameraCtrl.SetCurrent("'+newCamera+'")');	
			element('PlayButton').value = "Play Backward";			
		} else {
			TNTDoCommand('CameraCtrl.Match("'+newCamera+'",2000)');
		}
	} else {
		TNTDoCommand('SceneGraph.Physics.Reset()');	
		TNTDoCommand('CameraCtrl.SetCurrent("'+newCamera+'")');
	}
}

function OnReady()
{
	focus();
	if (gJumpToSceneOption != 0) {
	    JumpToSceneOption (gJumpToSceneOption);
	    gJumpToSceneOption = 0;
		return;
	}
	//Gestion des OnReady
	var string = 'OnReady_';
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '()';
	CLEexecScript (string);

	if (gSetCamera != "") {
		TNTDoCommand('CameraCtrl.SetCurrent("'+ gSetCamera +'")');
		gSetCamera = "";
	}
	loadExternalTextures();	

	//Statistics
	var info = "/"+gCurrentScene;
	if (gSetCamera != "") { info += "/"+gSetCamera; }
	SceneTrackerMessage(info);
}

function LoadSceneAndGo(scene,camera_name) {
	if (gCurrentScene != scene) {
		gSetCamera = camera_name;
		LoadScene(scene);
	} else {
		//Statistics
		var info = "/"+gCurrentScene;
		if (gSetCamera != "") { info += "/"+camera_name; }
		SceneTrackerMessage(info);
		Restart(camera_name);
	}
}

function ChangeCurrentScene(scene, repository, name) {
	gLastScene    = gCurrentScene;
	gCurrentScene = scene;
	tntRepository = repository;
	tntSource     = tntRepository + "/" + name;
	var string = '';
	if (gLastScene != "") {
		string += 'OnUnload_';
		if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
		string += gLastScene + '(); ';
	}
	string += 'OnLoad_';
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene+ '()';
	CLEexecScript (string);
	if (0 != tntInit) {
		createTurnTool(tntSizeX, tntSizeY, tntSource, "#FFFFFF", 0);
	}
}

function LoadScene(scene) {
	var nbItem = gSceneListId.length;
	for (var i=0; (i<nbItem); i++) {
		if (gSceneListId[i] == scene) {
			var string = 'ChangeCurrentScene("' + gSceneListId[i] + '", "' + gSceneListPath[i] + '", "' + gSceneListName[i] + '")';
			CLEexecScript (string);
			break;
		}
	}
}

function Restart(camera_name) {
	TNTDoCommand('SceneGraph.Physics.Reset()');
	TNTDoCommand('Objects(*).StopAnimation()');
	TNTDoCommand('Objects(*).SetFrame(0)');
	if (camera_name == "") {
		cameraName = TNTDoCommand('SceneGraph.Camera(0).GetName()');
	} else {
		cameraName = camera_name;
	}
	TNTDoCommand('CameraCtrl.SetCurrent("'+ cameraName+'")');
}

function OnMouseEnter(obj) {
	TNTDoCommand('Objects(' + obj + ').SetDiffuseColor(#FF0000)');
	//Gestion des OnMouseEnter
	var string = 'OnMouseEnter_';
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '("' + obj + '")';
	CLEexecScript (string);
	window.status = "Mouse Entered: " + obj + " gCurrentScene: " + gCurrentScene;	
}

function OnMouseExit(obj) {
	TNTDoCommand('Objects(' + obj + ').ResetMaterial()');
	//Gestion des OnMouseExit
	var string = 'OnMouseExit_';
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '("' + obj + '")';
	CLEexecScript (string);
	window.status = "Mouse Exited: " + obj + " gCurrentScene: " + gCurrentScene;	
}

function OnZoneEnter(zone, physics) {
	focus();
	//Gestion des OnZoneEnter
	var string = 'OnZoneEnter_';
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '("' + zone + '", "' + physics + '")';
	CLEexecScript (string);
	window.status = "Zone Entered: " + zone + ", by physics object: " + physics + " gCurrentScene: " + gCurrentScene;	
}

function OnZoneExit(zone, physics) {
	focus();
	//Gestion des OnZoneExit
	var string = 'OnZoneExit_'; 
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '("' + zone + '", "' + physics + '")';
	CLEexecScript (string);
	window.status = "Zone Exited: " + zone + ", by physics object: " + physics + " gCurrentScene: " + gCurrentScene;	
}

function OnKeyPress(keyCode, ascii) {
	//Gestion des OnKeyPress
	var string = 'OnKeyPress_';
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '("' + keyCode + '", "' + ascii + '")';
	CLEexecScript (string);
	window.status = "KeyPress: KeyCode=" + keyCode + " Ascii=" + ascii + " gCurrentScene: " + gCurrentScene;
}

function OnKeyRelease(keyCode, ascii) {
	//Gestion des OnKeyRelease
	var string = 'OnKeyRelease_'; 
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '("' + keyCode + '", "' + ascii + '")';
	CLEexecScript (string);
	window.status = "KeyRelease: KeyCode=" + keyCode + " Ascii=" + ascii + " gCurrentScene: " + gCurrentScene;
}

function OnClick(obj) {
	//Gestion des OnClick
	var string = 'OnClick_';
	if (gCurrentProjet != "") { string += gCurrentProjet + '_'; }
	string += gCurrentScene + '("' + obj + '")';
	CLEexecScript (string);
	window.status = "OnClick: '" + obj + "' gCurrentScene: " + gCurrentScene;
}

// Copyright 2007-2008 NEW3S s.a. - NEW3S.com
// Christophe LEMOINE + Michael BRICOUT

