/*  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.hide_unity = 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() {
		this._getIframeUnity();
		this.iframeUnity.src = 'unity.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);

			$('swfSite').gameUnityDetect(1, '');
		}
		else {
			$('swfSite').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();
		this.hide_unity = false;

		// habilita botão jogar agora
		this.obj_unity.SendMessage('LoadingManager', 'HabilitaBotao', '');

		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});
		}
		$('swfSite').gameUnityShow();
	},

	hide: function() {
		var element = this.divUnity || this._getDivUnity();
		this.hide_unity = true;

		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() {
		// se não estiver previamente carregada
		if (!this.loaded)
			this._setIframeSource();
		else
			this.show();
	},

	hideUnity: function() {
		// se estiver visível, oculta Unity
		if (this.loaded && !this.hide_unity){
			this.obj_unity.SendMessage('ExternalNavigation', 'GoToOptions', '');
			this.hide();
		}
	},

	/*
	Chamadas Unity
	*/
	loading: function() {
		if (!this.obj_unity)
			this.obj_unity = this.iframeUnity.contentWindow.document.getElementById('game_unity');

		var sUrl = 'http://www.axe.com.br/billions/site/';
		// se for local, url interno para testes
		if (location.hostname == '10.1.1.253' || location.hostname == 'web.cubolocal.cc')
			sUrl = 'http://10.1.1.253/axe/billions/site/';

		this.obj_unity.SendMessage('LoadingManager', 'PlayGame', sUrl);
		this.show();
	},

	registerGame: function(t, m, s) {
		this.hide();
		setTimeout(function() { $('swfSite').gameUnityRegister(t, m, s); }, 500);
	},

	exit: function() {
		this.hide();
		setTimeout(function() { $('swfSite').gameUnityExit(); }, 500);
	}
});


/*
 * Start
 *--------------------------------------------------------------------------*/
var gameUnity = new Unity();