var installTextFail = 'Die Installation ist leider fehlgeschlagen,\nbitte schließen Sie alle Browserfenster und\nstarten sie die Anwendung neu.\nFalls das Problem weiter besteht, wenden\nSie sich bitte an den Computer-Administrator.';
var installTextAbort = 'Sie müssen das Plug-In installieren, um\ndie Visualisierung betrachten zu können.';
var installTextSuccess = 'Um die Installation abzuschließen,\nstarten Sie bitte Ihren Browser neu\nund kehren Sie zu dieser Seite zurück';

downloadText = "Bitte warten ...";

var rightMouseButton = 	'off';		// switch on/off

var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var xpcomversion = "1.4.0.0";

function element(id)
{
	return document.getElementById(id);
}

function createTurnTool(width, height, file, color, transparent)
{
	if (ie4||ie5)
	{
		turntoolObject = ' <object id="TNTCtrl" width="'+width+'" height="'+height+'" classid="CLSID:402ee96e-2ce8-482d-ada5-ceceea07e16d"';
		turntoolObject += ' codebase="http://www.turntool.com/ViewerInstall.exe"><param name="transparent"'; 
		turntoolObject += ' value="'+transparent+'"><param name="ctrl_color" value="'+color+'"><param name="tnt_back_color" value="'+color+'">';
		turntoolObject += '<param name="script" value="ProgressBar.SetDownloadText(\''+downloadText+'\');ProgressBar.SetCoreDownloadText(\''+downloadText+'\');TNTCtrl.SetLogo(\'None\');">';
		turntoolObject += '<param name="src" value="'+file+'"></object>';
		turntoolObject += '<sc'+'r'+'ipt LANGUAGE=JavaScript FOR=TNTCtrl EVENT=TNTEvent(string)> execScript(string); </sc'+'r'+'ipt>';
	}
	else
	{
		turntoolObject = '<embed id="TNTCtrl" width="'+width+'" height="'+height+'" script="ProgressBar.SetDownloadText(\''+downloadText+'\');ProgressBar.SetCoreDownloadText(\''+downloadText+'\');TNTCtrl.SetLogo(\'None\');" src="'+file+'" transparent="'+transparent+'" ctrl_color="'+color+'"';
		turntoolObject += ' tnt_back_color="'+color+'" pluginspage="http://www.turntool.com/ViewerInstall.xpi" pluginsurl="http://www.turntool.com/ViewerInstall.xpi" type="application/tntfile"></embed>';
	}
	
	return turntoolObject;
		
}

function manualInstallTurnTool()
{
	if (ie4||ie5)
	{
		window.location.href = 'http://www.turntool.com/ViewerInstall.exe';
	}
	else
	{
		var xpi = {'TurnTool Viewer Installation':'http://www.turntool.com/ViewerInstall.xpi'};
		InstallTrigger.install(xpi,installFinish);
	}
}

function initTurnTool()
{
	
	//centerWindow();
	
	if(TNTsupportedPlatform()==true)
	{
		var installStatus = isTurnToolInstalled();
		
		if(installStatus==false && (ie4||ie5)) 
		{
			tntSizeX = 195;tntSizeY=55; //2 zeilig
			//width = 145;height=65;	//4 zeilig
			//tntSizeX = 145;tntSizeY=78; //für IE
		}
		
		var turntoolObject = createTurnTool(tntSizeX,tntSizeY,tntSource, tntCtrlColor, tntTransparentOption);	// create embed and object tag
		
		if (installStatus==false && ns6)	// vormals die Funktion isTurnToolInstalledNewestVersion
		{
			// this is mozilla only, if missing plugin or plugin is outdated
			element("plugin_FF").style.visibility = "visible";
			manualInstallTurnTool();
		}
		else if(installStatus == false && (ie4||ie5))
		{
			element("plugin_IE").style.visibility = "visible";
			element('TT_prev').innerHTML = turntoolObject;
			checkRefresh();
		}
		else if(installStatus == true)
		{
			element('TurnTool').innerHTML = turntoolObject;;
		}
	}	
}

function isTurnToolInstalled()
{
	var tntInstalled = false;
	if (ie4||ie5)
	{
		try
		{
			var xObj = new ActiveXObject("TNT.TNTCtrl");
			if (xObj)
			{
				tntInstalled = true;
			}
		}
		catch (e)
		{
		}
	}
	else
	{
		var v = navigator.plugins.namedItem("TurnTool XPCOM Plugin");
		if(v && v.description) {
			var version = v.description.replace(/([a-zA-Z]|\s)+/, "").split(".");
			var minversion = xpcomversion.split(".");
			for (var i=0; i<minversion.length; i++)
			{
				if (i >= version.length)
					break;
				if (version[i] > minversion[i])
				{
					tntInstalled = true;
					break;
				}
				if (version[i] < minversion[i])
					break;
				if (i == (minversion.length-1))
					tntInstalled = true;
			}
		}
	}
	return tntInstalled;
}

function installTurnTool() 
{
	if (ns6)
	{
		return false;
	}
	checkRefresh();
}

function installFinish(url, result)
{
	if (result>=0)
	{
		element('TurnTool').innerHTML = installTextSuccess;
		alert(installTextSuccess);
	}
	else if(result == -210)
	{
		alert(installTextAbort);
	}
	else
	{
		alert(installTextFail);
	}
}

var checkCounter = 0;
function checkRefresh()
{
	var tntInstalled = isTurnToolInstalled();
	if(tntInstalled==false)
	{
		checkCounter++;
		if (checkCounter>120)
		{
			checkCounter=0;
			//installTurnTool();
		}
		if(ie4||ie5)
		setTimeout('checkRefresh()',500);
	}
	else
	{
		window.location.href = window.location;
	}
}

function TNTDoCommand(string)
{
	var control = element("TNTCtrl");
	if(control && control.ready)
		return control.TNTDoCommand(string);
	return "";
}

function focus()
{
	element("TNTCtrl").focus();
}

function OnClick( obj )
{
	if(TNTDoCommand('Objects(' + obj + ').GetStopFrame()')!=100)
		TNTDoCommand('Objects(' + obj + ').PlayAnimation(,100)');
	else
		TNTDoCommand('Objects(' + obj + ').PlayAnimation(,0)');
	window.status = "Object clicked: " + obj;	
}

function OnMouseEnter( obj )
{
	TNTDoCommand('Objects(' + obj + ').SetDiffuseColor(#FF0000)');
	window.status = "Mouse Entered: " + obj;	
}

function OnMouseExit( obj )
{
	TNTDoCommand('Objects(' + obj + ').ResetMaterial()');
	window.status = "Mouse Exited: " + obj;	
}

function OnZoneEnter( zone, physics )
{
	focus();
	window.status = "Zone Entered: " + zone + ", by physics object: " + physics;	
}

function OnZoneExit( zone, physics )
{
	focus();
	window.status = "Zone Exited: " + zone + ", by physics object: " + physics;	
}

function OnKeyPress( keyCode, ascii )
{
	window.status = "KeyPress: KeyCode=" + keyCode + " Ascii=" + ascii;
}

function OnKeyRelease( keyCode, ascii )
{
	window.status = "KeyRelease: KeyCode=" + keyCode + " Ascii=" + ascii;
}

function Restart()
{
	TNTDoCommand('SceneGraph.Physics.Reset()');
	TNTDoCommand('Objects(*).StopAnimation()');
	TNTDoCommand('Objects(*).SetFrame(0)');
	cameraName = TNTDoCommand('SceneGraph.Camera(0).GetName()');
	TNTDoCommand('CameraCtrl.SetCurrent("'+ cameraName+'")');
}

function TNTsupportedPlatform()
{
	if (navigator.platform.toString().indexOf("Win") != -1)
		return true;
	if (navigator.platform.toString().toLowerCase().indexOf("mac")!=-1 || navigator.platform.toString().toLowerCase().indexOf("linux")!=-1)
		return false;
	return true;
}

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		 obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj.attachEvent( "on"+type, function() { obj["e"+type+fn](); } );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj["e"+type+fn] );
		obj["e"+type+fn] = null;
	}
}

addEvent(window,"load",initTurnTool);


// ###################################################################


function centerWindow()
{

  if (window.screen)
  {
  	
	var screenWidth = screen.availWidth;
	var screenHeight = screen.availHeight;
	var windowWidth = screenWidth;
	var windowHeight = screenHeight;
	var topSpacing = false;
	
	if (screenWidth<810)						//800x600
	{
		tntWidth = 400;
		tntHeight = 300;
		topSpacing = false;
	}
	if ((screenWidth>810)&&(screenWidth<1030))	//1024x768
	{	windowWidth = 720;
		windowHeight = 730;
		tntWidth = 464;
		tntHeight = 384;
	}
	if (screenWidth>1030)						//1280x1024
	{
		if ((screenWidth>1270)&&(screenHeight>900))
		{
			windowWidth = 800;
			windowHeight = 870;
			tntWidth = 640;
			tntHeight = 480;
		}
		else
		{
			windowWidth = 760;
			windowHeight = screenHeight;
			tntWidth = 576;
			tntHeight = 434;
		}	
	}
	var offsetX = screenWidth/2 - windowWidth/2;
	var offsetY = screenHeight/2 - windowHeight/2;
	if (offsetX<0)
		offsetX=0;
	if (offsetY<0)
		offsetY=0;
	if (topSpacing)
	{
		element('TopSpace').innerHTML = '<br>';
	}
	//element('TNTTable').style.width = tntWidth + 25;
	element('TurnTool').style.width = tntWidth;
	element('TurnTool').style.height = tntHeight;
	top.moveTo(offsetX, offsetY);
	top.resizeTo(windowWidth,windowHeight);
  }
}




// ----------------- Right Mouse Button Lock ------------------- //

var message = "";
function clickIE() {
    if(document.all) {
     (message);
     return false;
 }
}

function clickNS(e) {
    if(document.layers || (document.getElementById && !document.all)) {
        if(e.which == 2 || e.which == 3) {
      (message); 
      return false;
  }
 }
}

function noRightMouseClick() {
if(document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS;
} else {
    document.onmouseup = clickNS;
    document.oncontextmenu = clickIE;
}

document.oncontextmenu = new Function("return false")
}
if(rightMouseButton=='off') {
	noRightMouseClick();
}

