// Template specefic script

/*
----------------------------------------------------------------------------------
						Template for TurnToolBox v. 3.0
----------------------------------------------------------------------------------
	Template Name: 		default
	Template Arthour: 	TurnTool ApS
	Template Date: 		04-07-2006
	Last Update:		04-07-2006
	Template Version:	1.0
	Template Contact:	info@turntool.com
	
	Template features:
		- TurnToolBox version 3 default HTML!
	
	Template maunal: Look in manual.txt
	
	Template files:
		-index.html		-	start page
		-turntool.tnt	-	your TNT file
		-skin.css		-	CSS file for page
		-files			-	folder with files used in template
		-

	
----------------------------------------------------------------------------------
*/
function OnReady()
{
	loadExternalTextures();
	update_menu("01");
	setTimeout(get_frame, updateDelay);
	focus();
	TNTDoCommand('Objects(loop_*).PlayAnimation(0,15,true)');
}
var updateDelay = 500
var language = "DK";
var menu_type = 1;
var menu_item = "01";
var playing = false;
var animations = new Array();
var hide_show = new Array();
var current_anim = 1;
var current_frame = 0;

animations[1] = {start:0,     end:570,   lockCamera:true};
animations[2] = {start:2000,  end:3550,  lockCamera:false};
animations[3] = {start:4000,  end:4500,  lockCamera:false};
animations[4] = {start:6000,  end:6700,  lockCamera:false};
animations[5] = {start:8000,  end:9250,  lockCamera:false};
animations[6] = {start:10000, end:10400, lockCamera:false};
animations[7] = {start:12000, end:13700, lockCamera:false};
animations[8] = {start:14000, end:15900, lockCamera:true};
animations[9] = {start:16100, end:16250, lockCamera:true};

//anim 2
hide_show[hide_show.length] = {object:'msv_screwdriver',     start:2100,  end:2350,  action:'unhide', hidden:true};
hide_show[hide_show.length] = {object:'pil_anim2_01',        start:2500,  end:2650,  action:'unhide', hidden:true};
hide_show[hide_show.length] = {object:'msv_screwdriver',     start:3220,  end:3450,  action:'unhide', hidden:true};

//anim 3
hide_show[hide_show.length] = {object:'msv_s_32_int_*',      start:4000,  end:4280,  action:'unhide', hidden:true};
hide_show[hide_show.length] = {object:'anim3_arrow_*',       start:4050,  end:4200,  action:'unhide', hidden:true};

//anim 5
hide_show[hide_show.length] = {object:'msv_s_25_int_39',     start:8300,  end:9050,  action:'hide',   hidden:false};
hide_show[hide_show.length] = {object:'msv_anim05_*',        start:8300,  end:9100,  action:'unhide', hidden:true};
hide_show[hide_show.length] = {object:'msv_HexagonKey',      start:8400,  end:9000,  action:'unhide', hidden:true};

//anim 7
hide_show[hide_show.length] = {object:'msv_anim7_*',         start:12000, end:13500, action:'unhide', hidden:true};
hide_show[hide_show.length] = {object:'loop_anim7_*',        start:12500, end:13200, action:'unhide', hidden:true};
hide_show[hide_show.length] = {object:'msv_s_25_int_opaque', start:12300, end:13300, action:'unhide', hidden:true};
hide_show[hide_show.length] = {object:'msv_s_25_int_00',     start:12300, end:13300, action:'hide',   hidden:false};
hide_show[hide_show.length] = {object:'msv_HexagonKey',      start:12300, end:13500, action:'unhide', hidden:true};





function get_frame(){
	
	current_frame = TNTDoCommand('Objects("msv_Dummy01").GetFrame()')*1;
	
	for (i=animations.length-1 ; i>=1;i--)
	{
		if(current_frame >= animations[i].start){
			current_anim = i;
			if(current_frame >= animations[i].end){
				i++;
				if(i>animations.length-1){
					i=1;
				}
				jumpto(i);
			}
			break;
		}
	}
	
	for (i=0;i<hide_show.length;i++){
		if(current_frame > hide_show[i].start && current_frame < hide_show[i].end){
			if(hide_show[i].action == 'unhide' && hide_show[i].hidden){
				TNTDoCommand('Objects('+hide_show[i].object+').SetVisible(1)');
				hide_show[i].hidden = false;
			}else if(hide_show[i].action == 'hide' && !hide_show[i].hidden){
				TNTDoCommand('Objects('+hide_show[i].object+').SetVisible(0)');
				hide_show[i].hidden = true;
			}
		}else{
			if(hide_show[i].action == 'unhide' && !hide_show[i].hidden){
				TNTDoCommand('Objects('+hide_show[i].object+').SetVisible(0)');
				hide_show[i].hidden = true;
			}else if(hide_show[i].action == 'hide' && hide_show[i].hidden){
				TNTDoCommand('Objects('+hide_show[i].object+').SetVisible(1)');
				hide_show[i].hidden = false;
			}	
		}
	}

	setTimeout(get_frame, updateDelay);
}

function jumpto(anim){
	TNTDoCommand('Objects(msv_*).SetFrame('+animations[anim].start+')');
	
	TNTDoCommand('CameraCtrl.SetControllable(false)');

	if(!animations[anim].lockCamera){
		TNTDoCommand('CameraCtrl.SetControllable(true)');
	}
	
	
	if(current_anim == 8){
		TNTDoCommand('ObjectTree(intro).SetVisible(false)');
		TNTDoCommand('Objects(hider).SetVisible(false)');
	}
	switch(anim){
		
		case 8:
			TNTDoCommand('Objects(hider).SetVisible(true)');
			TNTDoCommand('ObjectTree(intro).SetVisible(true)');
			break;

	}
	current_anim = anim;
	
	if(anim<10){
		_menu_item = '0'+anim;
	}else{
		_menu_item = anim;
	}
	update_menu(_menu_item);
}

function update_menu(new_menu_item){
	TNTDoCommand('Objects(menu*_'+menu_item+'*).PlayAnimation(,0)');
	menu_item = new_menu_item;
	TNTDoCommand('Objects(menu*_'+menu_item+'*).PlayAnimation(,5)');
	
	menu_item_int = menu_item*1
	TNTDoCommand('Objects(slide_menu).PlayAnimation(,'+(menu_item_int*4)+')');
	
	TNTDoCommand('SceneGraph.Bitmap("MSV_background_01_DK").Load("files/MSV_background_'+menu_item+'_'+language+'.jpg")');
}

function OnClick(obj)
{	
	if(obj.substr(0,4) == "menu"){
		_menu_item = obj.substr(6,7);
		jumpto(_menu_item*1);
	}else{	
		switch(obj){
			case "button_menu":
				switch(menu_type){
					case 1:
						TNTDoCommand('Objects(hide_menu*).SetFrame(2)'); 
						menu_type = 2;
						break;
					case 2:
						TNTDoCommand('Objects(hide_menu*).SetFrame(3)');
						menu_type = 3;
						break;
					case 3:
						TNTDoCommand('Objects(hide_menu*).SetFrame(1)'); 
						menu_type = 1;
						break;
				}
				break;
				
			case "button_prev":
				if(current_frame > animations[current_anim].start+30){
					jumpto(current_anim);
				}else{
					jumpto(current_anim-1);
				}
				break;
				
			case "button_play":
				if(playing){
					TNTDoCommand('CameraCtrl.SetControllable(true)');
					TNTDoCommand('Objects(msv_*).StopAnimation()');
					TNTDoCommand('Objects(controls_*).SetFrame(0)');
					playing = false;
				}else{
					if(animations[current_anim].lockCamera){
						TNTDoCommand('CameraCtrl.SetControllable(false)');
					}
					TNTDoCommand('Objects(msv_*).StartAnimation()');
					TNTDoCommand('Objects(controls_*).SetFrame(1)');
					playing = true;
				}
				break;
				
			case "button_next":
				jumpto(current_anim+1);
				break;
				
			case 'button_DataSheet':
				window.open(datasheet_link[language]);
				break;
		}
	}
}

function OnMouseEnter( obj )
{
	if(obj == "hover_top"){
		TNTDoCommand('Objects(slide_menu).PlayAnimation(,0,false,15)');
	}else{
		TNTDoCommand('Objects(slide_menu).PlayAnimation(,40,false,15)');
	}
}

function OnMouseExit( obj )
{
	TNTDoCommand('Objects(slide_menu).StopAnimation()');
}

function reset_cam(){
	TNTDoCommand('CameraCtrl.Match("Camera01",500)');
}

function loadExternalTextures()
{
	var texcnt = 0;
	texcnt = TNTDoCommand('SceneGraph.GetBitmapCount()');
	var bExternal = 64;
	var bHasAlpha = 32;
	for( var i=0; i<texcnt; i++ )
	{
		var props = 0;
		props = TNTDoCommand('SceneGraph.Bitmap(' + i + ').GetProperties()');
		if(props & bExternal)
		{
			var name = TNTDoCommand('SceneGraph.Bitmap(' + i + ').GetName()');
			if(props & bHasAlpha)
			{
				name += '.png';
			}
			else
			{
				name += '.jpg';
			}
			TNTDoCommand('SceneGraph.Bitmap('+i+').Load("files/'+name+'")');
		}
	}
}

function saveImage()
{
	var thisDate = new Date();
	var year = thisDate.getFullYear();
	var month = thisDate.getMonth();
	var day = thisDate.getDate();
	var hours = thisDate.getHours();
	var minutes = thisDate.getMinutes();
	var seconds = thisDate.getSeconds();
	var milliseconds = thisDate.getMilliseconds();
	var sessionID = year+""+month+""+day+""+hours+""+minutes+""+seconds+""+milliseconds;	
	var index = element("ImageSize").selectedIndex;
	TNTDoCommand('Renderer.SaveImage("ScreenShot'+sessionID+'.bmp",'+index+')')
}