/*
	<aquiris-u3dobject> aka Unity Integration API
    Copyright (C) 2008 Raphael Lopes Baldi, Eduardo Pons Dias da Costa, Aquiris Realidade Virtual

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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 = new Array();
var div_height = new Array();
var unity_objects = new Array();
var iframe_objects = new Array();
var div_objects = new Array();

var counter = 0;

var templateHTML = "";

//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;

var u_file = "";
var u_div_name = "";
var u_width = 0;
var u_height = 0;

var detectionTimeout = null;

function embbed(p_file, p_div_name, p_width, p_height)
{	
	// BGW DEBUG
	//alert("p_file = " + p_file + ", p_div_name = " + p_div_name + ", p_width = " + p_width + ", p_height = " + p_height);
	
	try
	{
		//console.log(p_div_name);
	
	//if(isUnityInstalled() && div_objects[p_div_name] == null)
	u_div_name = p_div_name;
	
	if(isUnityInstalled())
	{
		// BGW DEBUG
		//alert("isUnityInstalled = yes");
		
		var div_unity = document.getElementById(p_div_name);
		
		div_objects[p_div_name] = div_unity;
		
		var write_iframe = '<iframe id="iframe_unity' + counter + '" frameborder="0" width="100%" height="' + p_height + '" scrolling="no" src="about:blank"></iframe>';
		div_unity.innerHTML = write_iframe;
		
		var iframe = document.getElementById("iframe_unity" + counter);
		iframe_objects[p_div_name] = iframe;
		
		div_unity.appendChild(iframe);
		doc = null;
		
		if(iframe.contentDocument)
			doc = iframe.contentDocument;
		else if(iframe.contentWindow)
			doc = iframe.contentWindow.document;
		else if(iframe.document)
			doc = iframe.document;
		
		if(doc == null)
		{
			throw "An error ocurred while trying to access the created element. The content div was not processed!";
		}
		else
		{
			templateHTML = templateHTML.replace( /\${UNITY_FILE_SRC}/g, p_file );
			templateHTML = templateHTML.replace( /\${UNITY_OBJECT_ID}/g, 'UnityObject' + counter );
			templateHTML = templateHTML.replace( /\${UNITY_EMBED_ID}/g, 'UnityEmbed' + counter );
			templateHTML = templateHTML.replace( /\${UNITY_WIDTH}/g, p_width );
			templateHTML = templateHTML.replace( /\${UNITY_HEIGHT}/g, p_height );

			doc.open();
			doc.write( templateHTML );
			doc.close();
			div_width[p_div_name] = p_width;
			div_height[p_div_name] = p_height;
			
			var reference = iframe.contentWindow.document;
		
			try
			{
				switch(browser)
				{
					case "MSIE_win":
					case "safari":
						id = "UnityObject" + counter;
					break;
					
					default:
						id = "UnityEmbed" + counter;
					break;
				}
				unity_objects[p_div_name] = reference.getElementById(id);
				counter += 1;
			}
			catch (e) 
			{
				unity_objects[p_div_name] = null;
				//alert("here");
			}
		}
	}
	else
	{
		// BGW DEBUG
		//alert("isUnityInstalled = no");

	}
	
	}
	catch(er)
	{
		// BGW DEBUG
		alert("An error has occurred loading unity.");
	}
}





function beginInterstitialInstallation ( p_file, p_div_name, p_width, p_height )
{
	u_file = p_file;
	u_div_name = p_div_name;
	u_width = p_width;
	u_height = p_height;
	clearInterval( detectionTimeout );
	detectionTimeout = setInterval( "waitForUnityInstallation()", 2000 );
}



function waitForUnityInstallation ()
{
	navigator.plugins.refresh();
	if (isUnityInstalled())
	{
		clearInterval( detectionTimeout );
		embbed(u_file, u_div_name, u_width, u_height)
	}
}



function beginUpfrontInstallation ()
{
	clearInterval( detectionTimeout );
	detectionTimeout = setInterval( "startMissionAfterInstallation()", 2000 );
}


function startMissionAfterInstallation ()
{
	if (isUnityInstalled())
	{
		clearInterval( detectionTimeout );
		getObject(shellID).startMissionAfterInstallation();
	}
}



function clearAllIntervals ()
{
	clearInterval( detectionTimeout );
}



function onFile ( xmlhttp )
{
	if (xmlhttp.readyState==4)
	{
		if (xmlhttp.status==200)
		{
			templateHTML = xmlhttp.responseText;
		}
		else
		{
			alert("Problem retrieving XML data");
		}
	}
}
		
		

function readFileHttp(fname ) 
{
   xmlhttp = getXmlHttp();
   callback = onFile;
   xmlhttp.onreadystatechange = function() 
   {
      callback(xmlhttp);
   }
   xmlhttp.open("GET", fname, true);
   xmlhttp.send(null);
}


function getXmlHttp() 
{
   if (window.XMLHttpRequest) 
   {
      xmlhttp=new XMLHttpRequest();
   } 
   else if (window.ActiveXObject) 
   {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   if (xmlhttp == null) 
   {
      alert("Your browser does not support XMLHTTP.");
   }
   return xmlhttp;
}




//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(p_div_name)
{
	var div_unity = div_objects[p_div_name];
	var iframe_unity = iframe_objects[p_div_name]
	var div_style = div_unity.style;
	
	var ua = navigator.userAgent.toLowerCase();
		
	if(ua.indexOf('firefox') != -1)
	{
		if(navigator.platform.toLowerCase().indexOf('mac') != -1)
		{
			div_unity.style.visibility = 'hidden';
			div_unity.style.width = '1px';
			div_unity.style.height = '1px';
			iframe_unity.visibility = 'hidden';
			iframe_unity.width = '1px';
			iframe_unity.height = '1px';

		}
		else
		{
			div_unity.style.visibility = 'hidden';
		}
	}
	else
	{
		div_unity.style.visibility = 'hidden';
		div_unity.style.width = '1px';
		div_unity.style.height = '1px';
		iframe_unity.visibility = 'hidden';
		iframe_unity.width = '1px';
		iframe_unity.height = '1px';
	}
}

function show(p_div_name)
{
	var divUnity = div_objects[p_div_name];
	var iFrameUnity = iframe_objects[p_div_name];
	var div_style = divUnity.style;
	
	var ua = navigator.userAgent.toLowerCase();
		
		if(ua.indexOf('firefox') != -1)
		{
			if(navigator.platform.toLowerCase().indexOf('mac') != -1)
			{
				divUnity.style.visibility = 'visible';
				divUnity.style.width = div_width[p_div_name] + 'px';
				divUnity.style.height = div_height[p_div_name] + 'px';
				iFrameUnity.visibility = 'visible';
				iFrameUnity.width = div_width[p_div_name] + 'px';
				iFrameUnity.height = div_height[p_div_name] + 'px';
			}
			else
			{
				divUnity.style.visibility = 'visible';
			}
		}
		else
		{
			divUnity.style.visibility = 'visible';
			divUnity.style.width = div_width[p_div_name] + 'px';
			divUnity.style.height = div_height[p_div_name] + 'px';
			iFrameUnity.visibility = 'visible';
			iFrameUnity.width = div_width[p_div_name] + 'px';
			iFrameUnity.height = div_height[p_div_name] + 'px';
		}
}

function destroy()
{
	var div_unity = div_objects[u_div_name];
	div_unity.parentNode.removeChild(div_unity);
}

function getPluginPath()
{
	var platform = (navigator.platform == "MacIntel")		? "mac_intel" :
				    (navigator.platform == "MacPPC")       	? "mac_ppc"   :
					(navigator.platform.indexOf("Mac")>=0) 	? "mac"       :
					(navigator.platform.indexOf("Win")>=0) 	? "win"       : "other";
				
	switch(platform)
	{
		//case "mac_intel": { return "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg"; break; }
		//case "mac"  	: { return "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg"; break; }
		//case "mac_ppc"  : { return "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg"; break; }
		//case "win"      : { return "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe"; break; }

		case "mac_intel": { return "/Style%20Library/MOD/unity/downloads/webplayer-i386.dmg"; break; }
		case "mac"  	: { return "/Style%20Library/MOD/unity/downloads/webplayer-ppc.dmg"; break; }
		case "mac_ppc"  : { return "/Style%20Library/MOD/unity/downloads/webplayer-ppc.dmg"; break; }
		case "win"      : { return "/Style%20Library/MOD/unity/downloads/UnityWebPlayer.exe"; break; }
	}	
	return "https://unity3d.com/unity-web-player-2.x";
}

function autoReload()
{
	navigator.plugins.refresh();
	if(isUnityInstalled()) 
	{
		window.location.reload();
	}
	else
	{
		setTimeout('autoReload();', 1000);
	}
}

function isUnityInstalled()
{
	navigator.plugins.refresh();
	
	var tInstalled = false;
	if(this.browser == "MSIE_win")	
	{
		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; 
			}
		}
	}
	
	if(tInstalled == 1)
	{
		tInstalled = true;
	}
	else if(tInstalled == 0)
	{
		tInstalled = false;
	}
	
	return tInstalled;
}

function dispatchUnityEvent(p_div_name, p_evt_name, p_data)
{
	unity_objects[p_div_name].SendMessage("ExternalInterface", "OnExternalEvent", p_evt_name + '(' + p_data + ')');
}

function callUnityFunction(p_div_name, p_target, p_function)
{
	var function_name = p_function.split("(")[0];
	var params = p_function.substring(p_function.indexOf("(") + 1, p_function.indexOf(")"));
	unity_objects[p_div_name].SendMessage(p_target, function_name, params);
}

function callFlashFunction(p_swf, p_function)
{
	var swf_obj = (navigator.appName.indexOf('Microsoft') >=0) ? window[p_swf] : document[p_swf];
	
	swf_obj.callFlashInterface(p_function);
}

function callLoadProgress(p_swf, p_progress)
{
	var swf_obj = (navigator.appName.indexOf('Microsoft') >=0) ? window[p_swf] : document[p_swf];
	
	swf_obj.callProgressEvent(p_progress);
}

function callCompleted(p_swf)
{
	var swf_obj = (navigator.appName.indexOf('Microsoft') >=0) ? window[p_swf] : document[p_swf];
	
	swf_obj.callCompleteEvent();
}

if(!isUnityInstalled)
{
	autoReload();
}