
//Scripts spécifiques au chargement des fichiers unity
//UNITY3D
function GetUnity () 
{
	if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1)
		return document.getElementById("UnityObject");
	else if (navigator.appVersion.toLowerCase().indexOf("safari") != -1)
		return document.getElementById("UnityObject");
	else
		return document.getElementById("UnityEmbed");
}

//bug snow leopard
function IsBrokenUnity()
{
	var lBrokenUnity = false;
	/* //C'est réparé
	if (		navigator.appVersion.indexOf("Safari") != -1
			&& navigator.appVersion.indexOf("Mac OS X 10_6") != -1
			&& document.getElementById("UnityEmbed").GetPluginVersion == undefined)
		lBrokenUnity = true;*/
	return lBrokenUnity;
}

function DetectUnityWebPlayer ()
{
// alert("DetectUnityWebPlayer");
	var tInstalled = false;
	if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1) {
		if(typeof(window['DetectUnityWebPlayerActiveX']) != "undefined") {
			tInstalled = DetectUnityWebPlayerActiveX();
		}
	}
	else {
		if (navigator.mimeTypes && navigator.mimeTypes["application/vnd.unity"]) {
			if (navigator.mimeTypes["application/vnd.unity"].enabledPlugin && navigator.plugins && navigator.plugins["Unity Player"]) {
				tInstalled = true;	
			}
		}	
	}
	return tInstalled;	
}

function GetUnityInstallerPath ()
{
	var tDownloadURL = "";
	var hasXpi = navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1;
	
	// Use standalone installer
	if (1)
	{
		if (navigator.platform == "MacIntel")
		{
			//tDownloadURL = "CcDownload.php?file=unity/download_webplayer-2.x/webplayer-i386.dmg"; //téléchargement local
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg";			           			         
		}
		else if (navigator.platform == "MacPPC")
		{
			//tDownloadURL = "CcDownload.php?file=unity/download_webplayer-2.x/webplayer-ppc.dmg";
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg";
		}
		else if (navigator.platform.toLowerCase().indexOf("win") != -1)
		{
			//tDownloadURL = "CcDownload.php?file=unity/download_webplayer-2.x/UnityWebPlayer.exe";
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe";
		}
		return tDownloadURL;
	}
	// Use XPI installer
	else
	{
		if (navigator.platform == "MacIntel")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerOSX.xpi";
		else if (navigator.platform == "MacPPC")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerOSX.xpi";
		else if (navigator.platform.toLowerCase().indexOf("win") != -1)
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerWin32.xpi";
		return tDownloadURL;
	}    			
}

function UnityAutomaticReload ()
{
	
	navigator.plugins.refresh();
	if (DetectUnityWebPlayer())
	{
		
		//alert("DetectUnityWebPlayer = true");
		sendBenchRecordDirect("JavaScript function UnityAutomaticReload () --> Web player detected.","WP DETECT");
		window.location.href = "CcMain.php?application=true";
		//window.location.reload();
		sendBenchRecordDirect("Javascript function UnityAutomaticReload","DOWNLOAD END");
	}	
	else
	{
		setTimeout('UnityAutomaticReload()', 500);
	}
}

function EmbedUnity(pWidth, pHeight, pUnityFile, pLogoFile)
{
	if (DetectUnityWebPlayer()) 
	{
		document.write('<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="'+pWidth+'" height="'+pHeight+'">');
		document.write('  <param name="src" value="'+pUnityFile+'" >');
		document.write('  <param name="logoimage" value="'+pLogoFile+'" >');
		document.write('  <param name="disableContextMenu" value="true" >'	);
		document.write('  <embed id="UnityEmbed" src="'+pUnityFile+'" width="'+pWidth+'" height="'+pHeight+'" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" logoimage="'+pLogoFile+'" disableContextMenu="true" >');
		document.write('</object>');
		//Sinon au Standard XHTML:
		//<object type="application/vnd.unity" data="'+pUnityFile+'" width="'+pWidth+'" height="'+pHeight+'">
		//<param name="src" value="'+pUnityFile+'" />
		//<param name="logoimage" value="'+pLogoFile+'" />
		//<param name="disableContextMenu" value="true" />
		//</object>
	}
	else 
	{
		var installerPath = GetUnityInstallerPath();
		if (installerPath != "") 
		{
			document.write('<div align="center" id="UnityPrompt"> \n');
			document.write(' <a href= ' + installerPath + ' onclick="sendBenchRecordDirect(\'JavaScript, EmbedUnity: Start downloading: '+installerPath+'\',\'DOWNLOAD START\')">' );
			document.write(' <b><br><br><br>3D plugin required</B><BR><br><img src="http://webplayer.unity3d.com/installation/getunity.png" border="0"/></a> \n');
			document.write(' <small><BR><BR><BR>Click <a href="CcMain.php?UnityAutomaticReload=true" onclick="sendBenchRecordDirect(\'Warning: JavaScript, EmbedUnity: force reload\',\'WP DETECT\')"> here </a> ');
			document.write(' if the application does not start automatically after the plug-in installation.');
			document.write('</div> \n');
			//document.write('<iframe name="InstallerFrame" height="0" width="0" frameborder="0">\n');
			sendBenchRecordDirect("Javascript function EmbedUnity: Installer path:"+installerPath,"INSTALLER");
		}
		else 
		{
			document.write('<div align="center" id="UnityPrompt"> \n');
			document.write('  <a href="javascript: window.open("http://www.unity3d.com/unity-web-player-2.x"); "><B><BR><BR><BR>3D plugin required</B><BR><br><img src="http://webplayer.unity3d.com/installation/getunity.png" border="0"/></a> \n');
			document.write('</div> \n');
			sendBenchRecordDirect("Warning: Javascript function EmbedUnity: Unable to detect installer path -> pop up to unity web site","INSTALLER");
		}
		
		//UnityAutomaticReload();
	}
}