browser = (navigator.appVersion.indexOf("MSIE") >=0) && 
		  (navigator.appVersion.toLowerCase().indexOf('win')>=0)     ?  "MSIE_win"  : 
		  (navigator.appVersion.toLowerCase().indexOf('safari') >=0) ?  "safari"    : 
		  (navigator.appVersion.toLowerCase().indexOf('firefox') >=0) ? "firefox"   : "other";

var div_width;
var div_height;
var unity_object;
var iframe_object;
var div_object;

//Embbed function, responsible for embeding the unity content into the website.
//p_file -> The unity's filename (can be a relative path, like unity/myFile.unity3d);
//p_div_name -> The div where you want the content to be placed. You must pass in the div name of a div that have not been used before to hold any unity file (the function will not embed if the div is holding another unity file);
//p_width -> The width of the unity;
//p_height -> The height of the unity;
function embbed(p_file, p_div_name, p_width, p_height){	
	div_width = p_width;
	div_height = p_height;

	div_object = jQuery("#"+p_div_name);
	div_object.append('<iframe id="iframe_unity" frameborder="0" width="100%" height="' + p_height + '" scrolling="no" src="unity.html"></iframe>');
	iframe_object = jQuery("#iframe_unity");
	iframe_object.load(function(){
		var body = jQuery(this).contents().find("body");

		var hasUnity = isUnityInstalled();
		var brokenUnity = false;
		if (hasUnity) {
			body.append('<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="'+div_width+'" height="'+div_height+'"> \n'
					+'  <param name="src" value="'+p_file+'" /> \n'
					+'  <param name="allowScriptAccess" value="always" /> \n'
					+'  <embed id="UnityEmbed" allowScriptAccess="always" src="'+p_file+'" width="'+div_width+'" height="'+div_height+'" type="application/vnd.unity" pluginspage="http://unity3d.com/webplayer" /> \n'
					+'</object>');
			
			if(jQuery.browser.safari || (jQuery.browser.msie && navigator.appVersion.toLowerCase().indexOf("win") != -1))
				unity_object	= jQuery(this).contents().find("#UnityObject")[0];
			else
				unity_object	= jQuery(this).contents().find("#UnityEmbed")[0];

			// 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(jQuery.browser.safari && navigator.appVersion.indexOf("Mac OS X 10_6") != -1 && unity_object.GetPluginVersion == undefined)
				brokenUnity = true;
			// 2.5.0 cannot auto update on ppc. Treat as broken.
			else if(unity_object.GetPluginVersion() == "2.5.0f5" 
				&& navigator.platform == "MacPPC")
				brokenUnity = true;

			if (jQuery.browser.safari || (jQuery.browser.msie && navigator.appVersion.toLowerCase().indexOf("win") != -1))
				unity_object	= jQuery(this).contents().find("#UnityObject")[0];
			else
				unity_object	= jQuery(this).contents().find("#UnityEmbed")[0];

			hide();
		}
		if (!hasUnity || brokenUnity) {
			var installerPath = getPluginPath();
			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
				body.append('<div align="center" id="UnityPrompt"> \n');
				if (brokenUnity)
					body.append('  <a href= ' + installerPath + '><img src="http://webplayer.unity3d.com/installation/getunityrestart.png" border="0"/></a> \n');
				else
					body.append('  <a href= ' + installerPath + '><img src="http://webplayer.unity3d.com/installation/getunity.png" border="0"/></a> \n');
				body.append('</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){	
					body.append('<div id="InnerUnityPrompt"> <p>Title</p>'
							+'<p> Contents</p>'
							+"</div>");
					var innerUnityPrompt = jQuery(this).contents().find("#InnerUnityPrompt");
					var innerHtmlDoc =
						'<object id="UnityInstallerObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="320" height="50" codebase="http://unity3d.com/webplayer">\n' + 
						'</object>';
						
					innerUnityPrompt.innerHTML = innerHtmlDoc;
				}
				body.append('<iframe name="InstallerFrame" height="0" width="0" frameborder="0"></iframe>\n');
			}else {
				body.append('<div align="center" id="UnityPrompt"> \n');
				if (brokenUnity)
					body.append('  <a href="javascript: window.open("http://unity3d.com/webplayer"); "><img src="http://webplayer.unity3d.com/installation/getunityrestart.png" border="0"/></a> \n');
				else
					body.append('  <a href="javascript: window.open("http://unity3d.com/webplayer"); "><img src="http://webplayer.unity3d.com/installation/getunity.png" border="0"/></a> \n');
				body.append('</div> \n');
			}
			
			// hide broken player
			if (brokenUnity)
				jQuery(this).contents().find("#UnityEmbed").height = 0;
				
			// Reload when detected unity plugin - but only if no previous plugin is installed 
			// - in that case a browser restart is needed.
			if (!brokenUnity)
				autoReload();
		}
	});
}

//Hide function, responsible for hide the unity content.
//If you want to place the unity div on top of some flash content, you must place the unity div in a higher z-index trough CSS
//p_div_name -> The name of the div that contains the content you want to hide
function hide(){
	div_object.css({
		'width'			: '1px',
		'height'		: '1px'
	});
	//jQuery(unity_object).attr('width', 1).attr('height', 1);
}

function show(){
	div_object.css({
		'width'			: div_width + 'px',
		'height'		: div_height + 'px'
	});
	//jQuery(unity_object).attr('width', div_width).attr('height', div_height);
}

function getPluginPath(){
	var tDownloadURL = "";
	var hasXpi = navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1;
	
	// Use standalone installer
	if (1){
		if (navigator.platform == "MacIntel")
			tDownloadURL = "http://unity3d.com/webplayer";
		else if (navigator.platform == "MacPPC")
			tDownloadURL = "http://unity3d.com/webplayer";
		else if (navigator.platform.toLowerCase().indexOf("win") != -1)
			tDownloadURL = "http://unity3d.com/webplayer";
		return tDownloadURL;
	// Use XPI installer
	}else{
		if (navigator.platform == "MacIntel")
			tDownloadURL = "http://unity3d.com/webplayer";
		else if (navigator.platform == "MacPPC")
			tDownloadURL = "http://unity3d.com/webplayer";
		else if (navigator.platform.toLowerCase().indexOf("win") != -1)
			tDownloadURL = "http://unity3d.com/webplayer";
		return tDownloadURL;
	}
}

function autoReload(){
	navigator.plugins.refresh();
	if(isUnityInstalled()) {
		window.location.reload();
	}else{
		setTimeout('autoReload();', 1000);
	}
}

function isUnityInstalled(){
	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 u3d_call(p_function){
	//console.log("u3d_call "+p_function);
	try{eval(p_function);}catch(e){erreur(''+e);}
}


function callUnityFunction(p_div_name, p_target, p_function, p_params){
	//console.log(p_div_name+", "+p_target+", "+p_function+", "+p_params+" -- "+typeof u3d);
	unity_object.SendMessage(p_target, p_function, p_params);
}

if(!isUnityInstalled){
	autoReload();
}
