JSMsgr = new Class({

	root: null,
	defaultWidth: '400px',
	tipos: ['alert', 'info', 'error', 'confirm', 'img', 'custom'],

	// ----------------------------------------------------------------------------------------------------
	initialize: function() {

		$('msg').makeDraggable({handle: $('msg_top')});

	},

	// ----------------------------------------------------------------------------------------------------
	show: function(options) {

		if(!$chk(options.tipo) || !this.tipos.contains(options.tipo)) return;

//		for(var n in options) console.log(n + ': ' + options[n]);

		// Tipo
		var icone = null;
		switch(options.tipo) {
			case 'alert':
				options.btns = ['ok'];
				icone = '<img src="modulos/msgr/alert.png" width="48" height="48" />';
				break;
			case 'info':
				options.btns = ['ok'];
				icone = '<img src="modulos/msgr/info.png" width="48" height="48" />';
				break;
			case 'error':
				options.btns = ['ok'];
				icone = '<img src="modulos/msgr/error.png" width="48" height="48" />';
				break;
			case 'confirm':
				options.btns = ['sim', 'nao'];
				icone = '<img src="modulos/msgr/confirm.png" width="48" height="48" />';
				break;
			case 'img':
				options.btns = ['fechar']
				break;
			case 'custom':
				break;
			default:
				return;
		}

		// Overlay
		$('overlay').setStyles({
			width: window.getScrollWidth(),
			height: window.getScrollHeight()
		});

		// Icone
		if(icone != null) {
			$('msg_icone').setStyle('width', '60px');
			$('msg_icone').innerHTML = icone;
		} else {
			$('msg_icone').setStyle('width', '0px');
			$('msg_icone').empty();
		}

		// Conteudo
		if(options.tipo != 'img') $('msg_conteudo').innerHTML = options.html;
		else {
			var a = new Element(
				'a',
				{
					id: 'a_novo_autor_' + this.autorCount,
					href: 'javascript: msgr.hide();',
					title: 'Carregue para fechar'
				}
			);
			var img =  new Element(
				'img',
				{
					src: options.img.src,
					width: options.img.width,
					height: options.img.height
				}
			);
			$('msg_conteudo').empty();
			a.grab(img);
			$('msg_conteudo').grab(a);
		}

		// Botoes
		if($chk(options.btns)) {
			$('msg_btns').empty();
			options.btns.each(function(item, index) {
				var btn;
				switch(item) {
					case 'ok':
						btn = new Element(
							'input',
							{
								name: 'msg_ok_' + index,
								type: 'button',
								value: 'Ok',
								events: {
									click: function() {
										msgr.hide();
									}
								}
							}
						);
						break;
					case 'cancelar':
						btn = new Element(
							'input',
							{
								type: 'button',
								value: 'Cancelar'
							}
						);
						break;
					case 'sim':
						btn = new Element(
							'input',
							{
								id: 'msg_sim_' + index,
								name: 'msg_sim_' + index,
								type: 'button',
								value: 'Sim',
								events: {
									click: function() {
										msgr.hide();
										options.onComplete(true);
									}
								}
							}
						);
						break;
					case 'nao':
						btn = new Element(
							'input',
							{
								id: 'msg_nao_' + index,
								name: 'msg_nao_' + index,
								type: 'button',
								value: 'Não',
								events: {
									click: function() {
										msgr.hide();
										options.onComplete(false);
									}
								}
							}
						);
						break;
					case 'fechar':
						btn = new Element(
							'input',
							{
								type: 'button',
								value: 'Fechar',
								events: {
									click: function() {
										msgr.hide();
									}
								}
							}
						);
						break;
					default:
						return;
				}
				btn.setStyle('width', '70px');
				$('msg_btns').appendText(' ');
				$('msg_btns').grab(btn);
			});
		}

		// Opacidades a 0
		$('overlay').setStyles({
			display: 'block',
			opacity: 0
		});
		$('msg').setStyles({
			display: 'block',
			opacity: 0
		});

		// Dimensoes e margens
		if($chk(options.largura)) {
			$('msg').setStyles({
				width: options.largura,
				marginLeft: -options.largura / 2
			});
		} else {
			$('msg').setStyles({
				width: this.defaultWidth,
				marginLeft: -parseInt(this.defaultWidth) / 2
			});
		}
		$('msg').setStyle('marginTop', -$('msg').getSize().y / 2);

		// Fx
		new Fx.Tween(
			'overlay',
			{
				duration: 'short',
				onComplete: function() {
					$('msg').setStyle('opacity', 1);
				}
			}
		).start('opacity', 0, 0.7);

	},

	// ----------------------------------------------------------------------------------------------------
	hide: function() {

		// Fx
		$('msg').setStyles({
			display: 'none',
			opacity: 0
		});
		new Fx.Tween('overlay', {duration: 'short'}).start('opacity', 0.7, 0);

	}

});
