document.write('<scr' + 'ipt language=vbscript type=text/vbscript\> \n');
document.write('function DetectUnityWebPlayerActiveX \n');
document.write('  on error resume next \n');
document.write('  dim tControl, res, ua, re, matches, major \n');
document.write('  res = 0 \n');
document.write('  set tControl = CreateObject("UnityWebPlayer.UnityWebPlayer.1") \n');
document.write('  if IsObject(tControl) then \n');  
document.write('    if tControl.GetPluginVersion() = "2.5.0f5" then \n');  
document.write('      ua = Navigator.UserAgent \n');
document.write('      set re = new RegExp \n');
document.write('      re.Pattern = "Windows NT (\d+)\." \n');
document.write('      set matches = re.Execute(ua) \n');
document.write('      if matches.Count = 1 then \n');
document.write('        major = CInt(matches(0).SubMatches(0)) \n');
document.write('        if major < 6 then \n');
document.write('          res = 1 \n');
document.write('        end if \n');
document.write('      end if \n');
document.write('    else \n');
document.write('      res = 1 \n');
document.write('    end if \n');
document.write('  end if \n');
document.write('  DetectUnityWebPlayerActiveX = res \n');
document.write('end function \n');
document.write('<\/scr' + 'ipt\> \n');

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");
}
			
function DetectUnityWebPlayer () {

	var tInstalled = false;
	if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1) {
		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 GetInstallerPath () {
	var tDownloadURL = "";
	var hasXpi = navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1;
	   			
	// Use standalone installer
	if (1)
	{
		if (navigator.platform == "MacIntel")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg";
		else if (navigator.platform == "MacPPC")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg";
		else if (navigator.platform.toLowerCase().indexOf("win") != -1)
			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 AutomaticReload () {

	navigator.plugins.refresh();
	if (DetectUnityWebPlayer())
		window.location.reload();

	setTimeout('AutomaticReload()', 500)
}

function playUnityFile(objectId, unityFileSrc, height, width, getPluginImage, restartPluginImage) {

	document.write(getUnityPlayerHtml(objectId, unityFileSrc, height, width, getPluginImage, restartPluginImage));
}

function setUnityPlayerAsElementInnerHtml(targetElementId, objectId, unityFileSrc, height, width) {

	document.getElementById(targetElementId).innerHTML = getUnityPlayerHtml(objectId, unityFileSrc, height, width, getPluginImage, restartPluginImage);
}

function getUnityPlayerHtml(unityFileSrc, height, width, getPluginImage, restartPluginImage) {

	var unityHtml;
	var hasUnity = DetectUnityWebPlayer();
	var brokenUnity = false;
	
	var selectedGetPluginImage = 'http://webplayer.unity3d.com/installation/getunity.png';
	var selectedRestartPluginImage = 'http://webplayer.unity3d.com/installation/getunityrestart.png';
	
	if (getPluginImage != null && getPluginImage.length > 0) {
		selectedGetPluginImage = getPluginImage;
	}
	if (restartPluginImage != null && restartPluginImage.length > 0) {
		selectedRestartPluginImage = restartPluginImage;
	}
	
	unityHtml = '<scr' + 'ipt language="javascript1.1" type="text/javascript">var hasUnity = ' + hasUnity + '; </scr' + 'ipt> \n'
	
	if (hasUnity) {
					
		// if Unity does not define to GetPluginVersion on Safari on 10.6, we presume the plugin
		// failed to load because it is not compatible with 64-bit Safari.
		if (navigator.appVersion.indexOf("Safari") != -1
			&& navigator.appVersion.indexOf("Mac OS X 10_6") != -1
			&& document.getElementById("UnityEmbed").GetPluginVersion == undefined)
			brokenUnity = true;
			
		unityHtml = unityHtml
			+ '<scr' + 'ipt language="javascript1.1" type="text/javascript">var brokenUnity = ' + brokenUnity + '; </scr' + 'ipt> \n'
		
		unityHtml = unityHtml
			+ '<object id="UnityObject"' + 'classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" '
			+ 'width="' + width + '" height="' + height + '"> \n'
			+ '<param name="src" value="' + unityFileSrc + '" /> \n'
			+ '<embed id="UnityEmbed" src="' + unityFileSrc + '" width="' + width + '" height="' + height + '" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" /> \n'
			+ '</object> \n';
					
	}
				
	if (!hasUnity || brokenUnity) {
				
		var installerPath = GetInstallerPath();
		if (installerPath != "") {
			// Place a link to the right installer depending on the platform we are on. The iframe is very important! Our goals are:
			// 1. Don't have to popup new page
			// 2. This page still remains active, so our automatic reload script will refresh the page when the plugin is installed
			unityHtml = unityHtml + '<div align="center" id="UnityPrompt"> \n';
			if (brokenUnity)
				unityHtml = unityHtml + '  <a href="' + installerPath + '"><img src="' + selectedRestartPluginImage + '" border="0"/></a> \n';
			else
				unityHtml = unityHtml + '  <a href="' + installerPath + '"><img src="' + selectedGetPluginImage + '" border="0"/></a> \n';
			unityHtml = unityHtml + '</div> \n';
						
			// By default disable ActiveX cab installation, because we can't make a nice Install Now button
//						if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1)
			if (0)
			{	
				unityHtml = unityHtml 
					+ '<div id="InnerUnityPrompt">'
					+ '<object id="UnityInstallerObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="320" height="50"  codebase="http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0">\n'
				    + '</object>'
					+ '</div>';
			}
						
			unityHtml = unityHtml
				+ '<iframe name="InstallerFrame" height="0" width="0" frameborder="0"></iframe> \n';
		}
		else {
			unityHtml = unityHtml
				+ '<div align="center" id="UnityPrompt"> \n';
			if (brokenUnity)
				unityHtml = unityHtml
					+ '  <a href="javascript: window.open("http://www.unity3d.com/unity-web-player-2.x"); "><img src="' + selectedRestartPluginImage + '" border="0"/></a> \n';
			else
				unityHtml = unityHtml
					+ '  <a href="javascript: window.open("http://www.unity3d.com/unity-web-player-2.x"); "><img src="' + selectedGetPluginImage + '" border="0"/></a> \n';
			unityHtml = unityHtml
				+ '</div> \n';
		}
					
		unityHtml = unityHtml
			+ '<scr' + 'ipt language="javascript1.1" type="text/javascript"> \n'
			+ '//<!-- \n';
			
		if (brokenUnity)
			unityHtml = unityHtml
				+ 'document.getElementById("UnityEmbed").height = 0; \n'
		else 
			unityHtml = unityHtml
				+ 'AutomaticReload(); \n'
				
		unityHtml = unityHtml
			+ '//--> \n'
			+ '</scr' + 'ipt>'
	}
	
	return unityHtml;
}