function getFlash(movieName)
{
	if(navigator.appName.indexOf('Microsoft') != -1)
	{
		return window[movieName];
	}
	else
	{
		return document[movieName];
	}
}

function _automaticReload()
{
	navigator.plugins.refresh();
	if(gameUnity.DetectUnityWebPlayer()) 
	{
		window.location.reload();
	}
	else
	{
		setTimeout('_automaticReload();', 2000);
	}
}

Object.extend(Prototype, {
	OS: {
		Win: !navigator.platform.indexOf('Win'),
		Mac: !navigator.platform.indexOf('Mac'),
		MacIntel: navigator.platform == 'MacIntel',
		MacPPC: navigator.platform == 'MacPPC'
	}
});

// Create unity class
var Unity = Class.create();

Object.extend(Unity.prototype, {
	initialize: function()
	{
		this.installed = this._detectUnity();
		this.divUnity = this._getDivUnity();
		this.iFrameUnity = this._getiFrameUnity();
		this.unity = null;
		this.downloadUrl = this._GetInstallerPath();
		this.count = 0;
		this.lastProj = -1;
	},
	
	_getUnity: function()
	{
		if(!this.iFrameUnity)
		{
			this.iFrameUnity = this._getiFrameUnity();
		}
		var reference = this.iFrameUnity.contentWindow.document;
		var objUnity;
		try
		{
			if(navigator.appVersion.indexOf('MSIE') != -1 && navigator.appVersion.toLowerCase().indexOf('win') != -1)
			{
				objUnity = reference.getElementById('UnityObject');
			}
			else if(navigator.appVersion.toLowerCase().indexOf('safari') != -1)
			{
				objUnity = reference.getElementById('UnityObject');
			}
			else
			{
				objUnity = reference.getElementById('UnityEmbed');
			}
			return objUnity;
		}
		catch (e)
		{
			return false;
		}
	},
	
	_getiFrameUnity: function()
	{
		return document.getElementById('iframeUnity');
	},
	
	_GetInstallerPath: function()
	{
		var address = '';
		if(Prototype.OS.MacIntel)
		{
			address = 'http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg';
		}
		else if(Prototype.OS.MacPPC)
		{
			address = 'http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg';
		}
		else if(Prototype.OS.Win)
		{
			address = 'http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe';
		}
		else
		{
			address = 'http://www.unity3d.com';
		}
		return address;
	},
	
	GetInstallerPath: function()
	{
		return this.downloadUrl;
	},

	_detectUnity: function() 
	{
		var tInstalled = false;
		if(Prototype.Browser.IE && Prototype.OS.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;
	},
	
	DetectUnityWebPlayer: function()
	{
		navigator.plugins.refresh();
		this.installed = this._detectUnity();
		return this.installed;
	},

	_getDivUnity: function()
	{
		return document.getElementById('divUnity');
	},

	preloadGame: function()
	{
		if(this.installed)
		{
			var iFrameUnity = document.getElementById('iframeUnity');
			iFrameUnity.src = 'unity/unity.html';
		}
	},

	AutomaticReload: function() 
	{
		_automaticReload();	
	},
	
	gameProgress: function(value)
	{
		var progresso = parseFloat(value);
		
		var flash = getFlash('flashMovie');
		if(progresso < 1.0)
		{
			if(progresso != this.lastProj)
			{
				this.count = 0;
				this.lastProj = progresso;
				
				flash.setLoadingGamePercent(progresso);
			}
		}
		else if(this.count < 2)
		{
			if(flash)
			{
				this.count++;
				flash.setLoadingGamePercent(1);
			}
		}
		return false;
	},
	
	hideUnity: function()
	{
		this.divUnity = this._getDivUnity();
		this.iFrameUnity = this._getiFrameUnity();
		
		var ua = navigator.userAgent.toLowerCase();
		
		if(ua.indexOf('firefox') != -1)//Prototype.Browser.Gecko)
		{
			if(navigator.platform.toLowerCase().indexOf('mac') != -1)
			{
				this.divUnity.style.visibility = 'visible';
				this.divUnity.style.width = '1px';
				this.divUnity.style.height = '1px';
				this.iFrameUnity.visibility = 'visible';
				this.iFrameUnity.width = '1px';
				this.iFrameUnity.height = '1px';
			}
			else
			{
				this.divUnity.style.visibility = 'hidden';
			}
		}
		else
		{
			this.divUnity.style.visibility = 'visible';
			this.divUnity.style.width = '1px';
			this.divUnity.style.height = '1px';
			this.iFrameUnity.visibility = 'visible';
			this.iFrameUnity.width = '1px';
			this.iFrameUnity.height = '1px';
		}
	},
	
	centralizeUnity: function()
	{	
		if(this.divUnity == null)
		{
			this.divUnity = this._getDivUnity();
		}
		var windowHeight;
		var windowWidth;
		if(typeof(window.innerWidth) == 'number' )
		{
			windowHeight = window.innerHeight;
			windowWidth = window.innerWidth;
		}
		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			windowHeight = document.documentElement.clientHeight;
			windowWidth = document.documentElement.clientWidth;
		}
		else if(document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			windowHeight = document.body.clientHeight;
			windowWidth = document.body.clientWidth;
		}
		var elementTop = Math.ceil((windowHeight / 2) - 250) + 'px';
		if(this.divUnity.style.top != elementTop)
		{
			this.divUnity.style.top = elementTop;
		}
		var elementLeft = Math.ceil((windowWidth / 2) - 450) + 'px';
		if(this.divUnity.style.left != elementLeft)
		{
			this.divUnity.style.left = elementLeft;
		}
	},
	
	showUnity: function()
	{
		this.centralizeUnity();
		if(this.divUnity == null)
		{
			this.divUnity = this._getDivUnity();
		}
		if(this.iFrameUnity == null)
		{
			this.iFrameUnity = this._getiFrameUnity();
		}
		
		var ua = navigator.userAgent.toLowerCase();
		
		if(ua.indexOf('firefox') != -1)//Prototype.Browser.Gecko)
		{
			if(navigator.platform.toLowerCase().indexOf('mac') != -1)
			{
				this.divUnity.style.visibility = 'visible';
				this.divUnity.style.width = '900px';
				this.divUnity.style.height = '500px';
				this.iFrameUnity.visibility = 'visible';
				this.iFrameUnity.width = '900px';
				this.iFrameUnity.height = '500px';
			}
			else
			{
				this.divUnity.style.visibility = 'visible';
			}
		}
		else
		{
			this.divUnity.style.visibility = 'visible';
			this.divUnity.style.width = '900px';
			this.divUnity.style.height = '500px';
			this.iFrameUnity.visibility = 'visible';
			this.iFrameUnity.width = '900px';
			this.iFrameUnity.height = '500px';
		}
	},
	
	exitGame: function(value)
	{
		var localSaida = parseInt(value);
		var flash = getFlash('flashMovie');
		flash.onUserLeftGame(localSaida);
	},
	
	playGame: function(qualityLevel)
	{
		if(!this.unity)
		{
			this.unity = this._getUnity();
		}
		var stringToPass = qualityLevel + ':';

		this.unity.SendMessage('gameManager', 'StartGame', stringToPass);
		if(Prototype.Browser.Gecko)
		{
			this.showUnity();
		}
	},

	communicate: function(deaths, level)
	{
		var flash = getFlash('flashMovie');
		flash.setUserScore(deaths, level);
	}
});

var gameUnity = new Unity();