// 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 gSceneOptionListId = new Array();
var gSceneListId       = new Array();
var gSceneListPath     = new Array();
var gSceneListURL      = new Array();
var gSceneOptionLabel  = new Array();
var gSceneOptionValue  = new Array();
var gSceneOptionLevel  = new Array();
var gSceneOptionAlias  = new Array();
var gSceneOptionStyle  = new Array();
var gSceneOptionShort  = 0;
var gSceneOptionTagObj = "";
var gSceneMessageTagObj  = "";
var gSceneLoadingMessage = "<div style='text-decoration:blink'>Chargement en cours / Download in process</div>";
var gSceneLoadedMessage  = "Chargement terminé / Download ended";
var gSceneDebug = 0;
var gStatLocalProjet = "";
var gStatMainProjet = "";
var gMuteSound = 0;


var gRotate_Scene        = new Array();
var gRotate_Scene_size   = 0;
var gRotate_Scene_stand  = -1;
var gRotate_Scene_object = "";

var gSceneTime = new Date();

//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 TimeStamp() {
	var d=new Date();
	var an = d.getFullYear();
	var mois = d.getMonth() + 1;
	var jour = d.getDate();
	var heure = d.getHours();
	var minute = d.getMinutes();
    var seconde = d.getSeconds();
    var milliseconde = d.getMilliseconds();
    var offset = d.getTimezoneOffset();
    var timbre = an+"/";
    if (mois < 10) { timbre += "0"; }
    timbre += mois+"/";
    if (jour < 10) { timbre += "0"; }
    timbre += jour+"-";
    if (heure < 10) { timbre += "0"; }
    timbre += heure+":";
    if (minute < 10) { timbre += "0"; }
    timbre += minute+":";
    if (seconde < 10) { timbre += "0"; }
    timbre += seconde+".";
    if (milliseconde < 100) { timbre += "0"; }
    if (milliseconde <  10) { timbre += "0"; }
    timbre += milliseconde+"(GMT";
    if (offset < 0) { timbre += "-"; } else { timbre += "+"; }
    var oHr  = Math.abs(offset/60);
    var oMin = Math.abs(offset) - Math.abs(oHr*60);
    timbre += oHr+":";
    if (oMin < 10) { timbre += "0"; }
    timbre += oMin+")";
    return timbre;
}

function Duration() {
	var d=new Date();
	var delta = d - gSceneTime;
	var heure = Math.abs(d.getHours() + 24 * d.getDate()); ;
	var minute = d.getMinutes();
    var seconde = d.getSeconds();
    var milliseconde = d.getMilliseconds();
    var timbre = "";
    if (heure < 10) { timbre += "0"; }
    timbre += heure+":";
    if (minute < 10) { timbre += "0"; }
    timbre += minute+":";
    if (seconde < 10) { timbre += "0"; }
    timbre += seconde+".";
    if (milliseconde < 100) { timbre += "0"; }
    if (milliseconde <  10) { timbre += "0"; }
    timbre += milliseconde;
    return timbre;
}

function ResetTimer() {
	gSceneTime = new Date();
}

function AddScene(id, repository, tntFile) {
	if (id != "") {
		var nbItem = gSceneOptionListId.length;
		gSceneOptionListId  [nbItem]=id;
		nbItem = gSceneListId.length;
		gSceneListId  [nbItem]=id;
		gSceneListPath[nbItem]=repository;
		gSceneListURL [nbItem]=tntFile;
	}
}

function AddSubScene(id, repository, tntFile) {
	if (id != "") {
		var nbItem = gSceneListId.length;
		gSceneListId  [nbItem]=id;
		gSceneListPath[nbItem]=repository;
		gSceneListURL [nbItem]=tntFile;
	}
}

function AddScenesOptions() {
	var nbItem = gSceneOptionListId.length;
	for (var i=0; (i<nbItem); i++) {
		var string = "Add_" + gSceneOptionListId[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 IndexOfSceneId (label) {
	var nbItem = gSceneListId.length;
	for (var i=0; (i<nbItem); i++) {
		if (gSceneListId[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 JumpToSceneId(index) {
	window.status = "JumpToSceneId("+index+") = '"+gSceneListId[index]+"'";
	CLEexecScript('LoadScene("'+gSceneListId[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="">Plan du site - Site Map ...</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><input type="hidden" NAME="mute" value="'+gMuteSound+'">';
	if (!(ie4||ie5)) { tags += '<input type="submit" value="Go" href="#">'; }
	tags += '</form>';
	element(obj).innerHTML = tags;
	gSceneOptionTagObj = obj;
}

function UpdateScenesOption() {
    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="">Plan du site - Site Map ...</option>';
	var nbItem = gSceneOptionLabel.length;
	// find current scene
	var curItem  = IndexOfSceneAlias (gCurrentScene);
	var curLevel = -1;
	if (curItem > -1) { curLevel = gSceneOptionLevel[curItem]; }
	var subLevel = 0;
	for (var i=0; (i<nbItem); i++) {
		var level = gSceneOptionLevel[i];
		if (subLevel > 0 && level < curLevel) { subLevel = 0; }
		if (gSceneOptionAlias[i] == gCurrentScene) { subLevel = 1; }
		if (level <= (curLevel+subLevel)) {
			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><input type="hidden" NAME="mute" value="'+gMuteSound+'">';
	if (!(ie4||ie5)) { tags += '<input type="submit" value="Go" href="#">'; }
	tags += '</form>';
	element(gSceneOptionTagObj).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);
	if (gStatMainProjet != "") {
		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('Objects(physics_camera*).SetEnable(false)');	
		TNTDoCommand('CameraCtrl.SetCurrent("'+newCamera+'")');
		TNTDoCommand('Objects(physics_'+newCamera+').SetEnable(true)');	
	}
}

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 = "";
	}

	string = 'loadExternalTextures_' + gCurrentScene + '()';
	CLEexecScript (string);	

	if (gSceneMessageTagObj != "") {
		element(gSceneMessageTagObj).innerHTML = gSceneLoadedMessage;
	}

	//Statistics
	var info = "/"+gCurrentScene;
	if (gSetCamera != "") { info += "/"+gSetCamera; }
	info += " at " + TimeStamp();
	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; }
		info += " at " + TimeStamp();
		SceneTrackerMessage(info);
		Restart(camera_name);
	}
}

function ChangeCurrentScene(scene, repository, URL) {
	gLastScene    = gCurrentScene;
	gCurrentScene = scene;
	tntRepository = repository;
	tntSource     = URL;
	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);
	}
	if (gSceneOptionShort == 1) {
		UpdateScenesOption();
	}
}

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] + '", "' + gSceneListURL[i] + '")';
			CLEexecScript (string);
			if (gSceneMessageTagObj != "") {
				element(gSceneMessageTagObj).innerHTML = gSceneLoadingMessage;
			}
			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;
}

function ResetRing_Scene(nbRing, step) {
	if (0 < nbRing) { gRotate_Scene_size = nbRing; gRotate_Scene_step = step;}
	var nbItem = gRotate_Scene.length;
	if (nbItem < nbRing) { nbItem = nbRing; }
	for (var i=0; (i<nbItem); i++) { gRotate_Scene[i] = -1; }
	gRotate_Scene_stand = -1;
	gRotate_Scene_object = "";
}

function OnClickStand_Scene(obj, ind) {
	if (ind > -1 && ind < gRotate_Scene_size) {
		if (gRotate_Scene [ind] == 0) {
			// Stop rotation
			TNTDoCommand('Objects("'+obj+'").StopAnimation()');
			gRotate_Scene [ind] = 1;
		}
	}
}

function OnClickRing_Scene(obj, ind) {
	// Manage rotation
	if (ind > -1 && ind < gRotate_Scene_size) {
		if (gRotate_Scene [ind] == -1) {
			// Start rotation 
			if (ind != gRotate_Scene_stand) {
				if (gRotate_Scene_object != "") {
					if (gRotate_Scene [gRotate_Scene_stand] == 0) {
						TNTDoCommand('Objects("'+gRotate_Scene_object+'").SetLoop(0)');
						gRotate_Scene [gRotate_Scene_stand] = 2;
					}
				}
				gRotate_Scene_object = obj;
				gRotate_Scene_stand  = ind;
			}
			var from = gRotate_Scene_step * ind;
			var to   = from + gRotate_Scene_step;
			TNTDoCommand('Objects("'+obj+'").PlayAnimation('+from+','+to+',true)');
			gRotate_Scene [ind] = 0;
		}
		else if (gRotate_Scene [ind] == 0) {
			// Stop rotation
			TNTDoCommand('Objects("'+obj+'").StopAnimation()');
			gRotate_Scene [ind] = 1;
		}
		else {
			// Restart rotation
			if (ind != gRotate_Scene_stand) {
				if (gRotate_Scene_object != "") {
					if (gRotate_Scene [gRotate_Scene_stand] == 0) {
						TNTDoCommand('Objects("'+gRotate_Scene_object+'").SetLoop(0)');
						gRotate_Scene [gRotate_Scene_stand] = 2;
					}
				}
				gRotate_Scene_object = obj;
				gRotate_Scene_stand  = ind;
			}

			if (gRotate_Scene [ind] == 2) {
				TNTDoCommand('Objects("'+obj+'").SetLoop(1)');
			}
			TNTDoCommand('Objects("'+obj+'").StartAnimation()');
			gRotate_Scene [ind] = 0;
		}
	}
}

// Copyright 2007-2008 NEW3S s.a. - NEW3S.com
// Christophe LEMOINE + Michael BRICOUT
