function unityw()
{
	this.stats = new stats(this);
	
	//var xfb = new fbw(this);
	this.maze = 0;
	this.hidden = 0;
	this.forcedHide = 0;
	
	this.get = function()
	{
		if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1)
			return gE("UnityObject");
		else if (navigator.appVersion.toLowerCase().indexOf("safari") != -1)
			return gE("UnityObject");
		else
			return gE("UnityEmbed");
	}

	this.detect = function()
	{
		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"] && navigator.mimeTypes["application/vnd.unity"].enabledPlugin && navigator.plugins && navigator.plugins["Unity Player"])
			tInstalled = true;
			
		return tInstalled;
	}
	
	this.getInstallerPath = function()
	{
		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;
		}    			
	}
	
	this.autoReload = function()
	{
		navigator.plugins.refresh();
	
		if(xunity.detect())
			window.location.reload();
		
		setTimeout(xunity.autoReload, 500);
	}

	this.init = function(build, server, maze, auth)
	{
		xfb.init(auth);
		
		this.maze = maze;
		
		var content = "";
		
		var detected = this.detect();
		if(detected)
		{
			content += "<object id=\"UnityObject\" classid=\"clsid:444785F1-DE89-4295-863A-D46C3A781394\" width=\"800\" height=\"600\">";
			content += "<param name=\"src\" value=\""+server+"/unity/"+build+".unity3d\" />";
			content += "<param name=\"backgroundcolor\" value=\"000000\" />";
			content += "<param name=\"bordercolor\" value=\"000000\" />";
			content += "<param name=\"textcolor\" value=\"999999\" />";
			content += "<param name=\"disableFullscreen\" value=\"true\" />";
			content += "<param name=\"logoimage\" value=\"images/logo.png\" />";
			content += "<param name=\"progressbarimage\" value=\"images/progressbar.png\" />";
			content += "<param name=\"progressframeimage\" value=\"images/progressframe.png\" />";
			content += "<embed id=\"UnityEmbed\" src=\""+server+"/unity/"+build+".unity3d\" width=\"800\" height=\"600\" disableFullscreen=\"true\" logoimage=\"images/logo.png\" progressbarimage=\"images/progressbar.png\" progressframeimage=\"images/progressframe.png\" backgroundcolor=\"000000\" bordercolor=\"000000\" textcolor=\"999999\" type=\"application/vnd.unity\" pluginspage=\"http://www.unity3d.com/unity-web-player-2.x\" />";
			content += "</object>";
			
			gE("info-box").style.display = "block";
		}
		else
		{
      content += "<div id=\"unity-install1\">You are about to play Mind Crime!<br /><br /><br /><br />";
      content += "Mind Crime requires the Unity Web Player plug-in for your Internet browser. Unity enables you to view 3D content directly in your browser.<br /><br />";
      content += "Click here to install the Unity:<br /><br /></div>";
        
			var installerPath = this.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
				
				content += "<div align=\"center\" id=\"UnityPrompt\">";
				content += "<a href=\"" + installerPath + "\"><img src=\"http://webplayer.unity3d.com/installation/getunity.png\" border=\"0\"/></a>";
				content += "</div>";
				content += "<iframe name=\"InstallerFrame\" height=\"0\" width=\"0\" frameborder=\"0\"></iframe>";
			}
			else
			{
				content += "<div align=\"center\" id=\"UnityPrompt\">";
				content += "<a target=\"_new\ \"href=\"http://www.unity3d.com/unity-web-player-2.x\"><img src=\"http://webplayer.unity3d.com/installation/getunity.png\" border=\"0\"/></a>";
				content += "</div>";
			}
			
			content += "<div id=\"unity-install2\">Need help installing the Unity? Please select your browser for detailed instructions:<br /><br />";
      content += "<a target=\"_new\" href=\"/instructions/ie.html\">Internet Explorer</a><br />";
      content += "<a target=\"_new\" href=\"/instructions/firefox.html\">Mozilla Firefox</a><br />";
      content += "<a target=\"_new\" href=\"/instructions/safari.html\">Safari (Mac)</a>";
			content += "</div>";
			
			this.autoReload();
		}
		
		var e = gE('game');
		
		if(e)
			e.innerHTML = content;
			
		e.style.display = "block";
	}
	/*
	this.show = function()
	{
		var e = gE('game');
		
		if(!e)
			return;
		
		if(e.style.display != "block")
			e.style.display = "block";
	}
	
	this.hide = function()
	{
		var e = gE('game');
		
		if(!e)
			return;
			
		if(e.style.display != "none")
			e.style.display = "none";
	}
	*/
	
	this.show = function()
	{
		var u = this.get();
		
		if(u && xunity.hidden && !xunity.forcedHide)
		{
			xunity.hidden = 0;
			
			u.style.width = "800px";
			u.style.height = "600px";
			
			window.scroll(0,0);
		}

		/*
		var e = gE('game');
		
		if(!e || !e.style.marginTop || e.style.marginTop == "0px")
			return;
		
		//e.style.marginTop = (getDocHeight() - (e.innerHeight / 2)) + "px";
		e.style.marginTop = "0px";
		
		//window.scroll(0,0);
		//window.onscroll = null;
		//document.body.style.overflow = "auto";
		*/
	}
	
	this.hide = function()
	{
		var u = this.get();
		
		if(u && !xunity.hidden)
		{
			xunity.hidden = 1;
			
			u.style.width = "1px";
			u.style.height = "1px";
			
			window.scroll(0,0);
		}

    /*
		var e = gE('game');
		
		if(!e)
			return;
			
		//e.style.display = "none";		
		e.style.marginTop = "5000px";
		
		//scroll to the top
		window.scroll(0,0);
		window.onscroll = function(){ window.scroll(0,0); };
		//document.body.style.overflow = "hidden";
		*/
	}
	
	this.checkFBMessage = function()
	{
		var e = gE('RES_ID_fb_pop_dialog_table');
		if(e)
		{
			e.style.top = "100px";
			this.hide();
			
			try
      {
			 this.unityCall("WebClient", "FacebookNotificationDidDisplay", 1);
      }
      catch(e){}
		}
		else if(!this.forcedHide)
		{
			this.show();
    }
	}
		
	var fbStatusChangeObj = null;
	var fbStatusChangeFunc = null;
	this.fbStatusChange = function()
	{
		if(fbStatusChangeObj != null && fbStatusChangeFunc != null)
			xunity.get().SendMessage(fbStatusChangeObj, fbStatusChangeFunc, xfb.getConnected());
	}
	
	this.jsCall = function(callbackObj, callbackFunc, callType)
	{
		var value = null;
		switch(callType)
		{
			case "FB_CONNECT":
				xfb.connect(xunity.fbStatusChange);
			break;
			
			case "FB_ISONLINE":
				fbStatusChangeObj = callbackObj;
        fbStatusChangeFunc = callbackFunc;
        value = xfb.getConnected() || 0;
			break;
			
			case "MAZE_ID":
				value = this.maze;
			break;
			
			/*
			case "REVEAL_ASSET":
				xasset.show();
			break;
			
			case "REVEAL_ASSET2":
				xasset2.show();
			break;
			*/
		}
		
		this.unityCall(callbackObj, callbackFunc, parseInt(value));
	}
	
	var statsCallbackObj = null;
	var statsCallbackFunc = null;
	this.jsStatsCall = function(callbackObj, callbackFunc, type, maze, value)
	{
		var hash = null;
		xunity.stats.store(type, maze, value, hash);
		
		statsCallbackObj = callbackObj;
		statsCallbackFunc = callbackFunc;
	}
	
	this.jsStatsCallback = function()
	{
		this.unityCall(statsCallbackObj, statsCallbackFunc, 0);
	}
		
	this.unityCall = function(unityObject, unityFunc, value)
	{
		if(unityObject != null && unityFunc != null)
			this.get().SendMessage(unityObject, unityFunc, value);
	}
}

var xunity = new unityw();

var fbMessageCheck = setInterval('xunity.checkFBMessage()', 80);
