/*  Unity JavaScript
 *  (c) 2008
 *
 *  For details, try your knowledge.
 *
 *-------------------------------------------------------------------------- */

/*
 * Browser/OS details
 *-------------------------------------------------------------------------- */
Object.extend(Prototype, {
	OS: {
		Win: !navigator.platform.indexOf('Win'),
		Mac: !navigator.platform.indexOf('Mac'),
		MacIntel: navigator.platform == 'MacIntel',
		MacPPC: navigator.platform == 'MacPPC'
	}
});


/*
 * Unity Funcions
 *-------------------------------------------------------------------------- */
var unityPosition = function(intLeft, intTop) {
	$('unity-3d').style.left = intLeft + 'px';
	$('unity-3d').style.top  = intTop + 'px';
}


// Create unity class
var Unity = Class.create();

Object.extend(Unity.prototype, {

	initialize: function() {
		this.detectionTimeout = null;
		this.loaded = false;
		this.installed = false;
		this.obj_unity = null;
		this.iframeUnity = null;
		this.divUnity = null;
		this.downloadUrl = this._getDownloadUrl();
	},
	
	_getDownloadUrl: function() {
		if (Prototype.OS.MacIntel)
			return 'http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg';
		else if (Prototype.OS.MacPPC)
			return 'http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg';
		else if (Prototype.OS.Win)
			return 'http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe';
		else
			return 'http://www.unity3d.com';
	},

	_detect: function() {
		if (Prototype.Browser.IE && Prototype.OS.Win)			
			this.installed = DetectUnityWebPlayerActiveX();
		else {
			if (navigator.mimeTypes && navigator.mimeTypes['application/vnd.unity'])
				if (navigator.mimeTypes['application/vnd.unity'].enabledPlugin && navigator.plugins && navigator.plugins['Unity Player'])
					this.installed = true; 
		}
	},

	_getDivUnity: function() {
		this.divUnity = $('unity-3d');
		return this.divUnity;
	},

	_getIframeUnity: function() {
		this.iframeUnity = $('iframeUnity');
	},

	_setIframeSource: function(idioma) {
		this._getIframeUnity();
		this.iframeUnity.src = 'unity_' + idioma + '.html';

		this.loaded = true;

		this._getDivUnity();
		this._originalWidth = this.divUnity.getStyle('width');
		this._originalHeight = this.divUnity.getStyle('height');
		this._originalIframeWidth = this.iframeUnity.getStyle('width');
		this._originalIframeHeight = this.iframeUnity.getStyle('height');
	},

	detect: function() {
		var _this = this;
		this._detect();

		if (this.installed) {
			if (this.detectionTimeout)
				clearTimeout(this.detectionTimeout);

			$('flashSite').gameUnityDetect(1);
		}
		else {
			$('flashSite').gameUnityDetect(0, this.downloadUrl);

			this.detectionTimeout = setTimeout( function() {
				_this.detect();
				navigator.plugins.refresh();
			}, 2000);
		}
	},

	clearDetectPlugin: function() {
		clearTimeout(this.detectionTimeout);
	},

	show: function() {
		var element = this.divUnity || this._getDivUnity();

		if (Prototype.Browser.Gecko) // firefox
			element.setStyle({visibility:'visible'});
		else { // other browsers
			element.setStyle({visibility:'visible', width: this._originalWidth, height: this._originalHeight});
			this.iframeUnity.setStyle({visibility:'visible', width: this._originalIframeWidth, height: this._originalIframeHeight});
		}

		//GA.track('/dark/site/game/acesso');
	},

	hide: function() {
		var element = this.divUnity || this._getDivUnity();

		if (Prototype.Browser.Gecko) // firefox
			element.setStyle({visibility:'hidden'});
		else { // other browsers
			element.setStyle({visibility:'visible', width:'1px', height:'1px'});
			this.iframeUnity.setStyle({visibility:'visible', width:'1px', height:'1px'});
		}
	},

	play: function(idioma) {
		if (!this.loaded)
			this._setIframeSource(idioma);
		else
			this.show();
	},

	/*
	Chamadas Unity
	*/
	loading: function() {
		if (!this.obj_unity)
			this.obj_unity = this.iframeUnity.contentWindow.document.getElementById('game_unity');

		this.obj_unity.SendMessage('Camera1', 'playGame', '');
		this.show();
	},

	setFullScreen: function() {
		if (!this.obj_unity)
			this.obj_unity = this.iframeUnity.contentWindow.document.getElementById('game_unity');

		this.obj_unity.SendMessage('BotaoFullScreen', 'FullScreen', screen.width + ':' + screen.height);
	},

	exit: function() {
		$('flashSite').gameUnityExit();
		this.hide();
	}	
});


/*
 * Start
 *--------------------------------------------------------------------------*/
var gameUnity = new Unity();