JSMsgr = new Class({

	defaultWidth: '460px',
	tipos: ['alert', 'info', 'error', 'confirm', 'wait', 'iframe', 'div', 'img', 'custom'],
	countInicial: 10,

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

		$('overlay').set('tween', {duration: 'short'});
		$('msg').set('tween', {
			duration: 400,
			link: 'cancel',
			onComplete: function() {
				if($chk($('msg_default'))) $('msg_default').focus();
			}
		});
		$('msg').setStyle('opacity', 0);

		$('fundo_aviso').set('tween', {
			duration: 400,
			link: 'cancel'
		});

		window.addEvent('resize', function() {
			$('overlay').setStyles({
				width: window.getScrollWidth(),
				height: window.getScrollHeight(),
			});
		});

	},

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

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

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

		// Titulo
		if(options.tipo == 'wait' && !$chk(options.titulo)) $('msg_titulo').innerHTML = 'Aguarde';
		else $('msg_titulo').innerHTML = $chk(options.titulo) ? options.titulo : '&nbsp;';

		// Icone
		var icone;
		switch(options.tipo) {
			case 'wait':
				icone = 'modulos/msgr/wait.gif';
				break;
		}
		if(icone != null) {
			$('msg_icone').set('src', icone);
			$('td_msg_icone').setStyle('display', 'table-cell');
		} else $('td_msg_icone').setStyle('display', 'none');

		// Conteudo
		options.html = options.html ? options.html : '';
		$('msg_conteudo').empty();
		if(options.tipo == 'img') {
			var a = new Element('a', {
				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').grab(a.grab(img));
		} else if(options.tipo == 'iframe') {
			var i = new IFrame({
				id: options.id,
				src: options.src,
				styles: {
			        width: options.largura + 20,
			        height: options.altura + 20,
					border: '1px solid gray'
				}
			});
			$('msg_conteudo').grab(i);
		} else if(options.tipo == 'div') {
			var div = new Element('div', {
				id: options.id,
				styles: {
					height: options.altura ? options.altura : 'auto',
					overflow: 'hidden'
				}
			});
			$('msg_conteudo').grab(div);
			new Request.HTML({
				update: div,
				onComplete: function() {
					msgr.loadScripts();
				}
			}).get(options.src);
		} else {
			$('msg_conteudo').innerHTML = options.html;
		}

		// Botoes
		$('form_msg_btns').empty();
		var btn;
		if(!$chk(options.btns)) options.btns = new Array();
		switch(options.tipo) {
			case 'alert':
				$('form_msg_btns').appendText(' ');
				$('form_msg_btns').grab(new Element('input', {
					id: 'msg_default',
					type: 'button',
					'class': 'default',
					value: 'Ok',
					styles: {
						width: '70px'
					},
					events: {
						click: function() {
							msgr.hide();
							if($chk(options.onComplete)) options.onComplete();
						}
					}
				}));
				break;
			case 'info':
				$('form_msg_btns').appendText(' ');
				$('form_msg_btns').grab(new Element('input', {
					id: 'msg_default',
					type: 'button',
					'class': 'default',
					value: 'Ok',
					styles: {
						width: '70px'
					},
					events: {
						click: function() {
							msgr.hide();
							if($chk(options.onComplete)) options.onComplete();
						}
					}
				}));
				break;
			case 'error':
				$('form_msg_btns').grab(new Element('input', {
					id: 'msg_default',
					type: 'button',
					'class': 'default',
					value: 'Ok',
					styles: {
						width: '70px'
					},
					events: {
						click: function() {
							msgr.hide();
							if($chk(options.onComplete)) options.onComplete();
						}
					}
				}));
				break;
			case 'confirm':
				$('form_msg_btns').grab(new Element('input', {
					id: 'msg_default',
					type: 'button',
					'class': 'default',
					value: 'Sim',
					styles: {
						width: '70px'
					},
					events: {
						click: function() {
							msgr.hide();
							if($chk(options.onComplete)) options.onComplete(true);
						}
					}
				}));
				$('form_msg_btns').appendText(' ');
				$('form_msg_btns').grab(new Element('input', {
					id: 'msg_nao',
					type: 'button',
					value: 'Não',
					styles: {
						width: '70px'
					},
					events: {
						click: function() {
							msgr.hide();
							if($chk(options.onComplete)) options.onComplete(false);
						}
					}
				}));
				break;
			case 'img':
				$('form_msg_btns').grab(new Element('input', {
					id: 'msg_default',
					type: 'button',
					'class': 'default',
					value: 'Fechar',
					styles: {
						width: '70px'
					},
					events: {
						click: function() {
							msgr.hide();
							if($chk(options.onComplete)) options.onComplete();
						}
					}
				}));
				break;
			default:
				if($chk(options.btns)) for(var n = 0; n < options.btns.length; n++) {
					var btnClass = $chk(options.btns[n]['class']) ? options.btns[n]['class'] : '';
					$('form_msg_btns').appendText(' ');
					$('form_msg_btns').grab(new Element('input', {
						name: options.btns[n]['name'],
						id: options.btns[n]['id'],
						type: 'button',
						'class': btnClass,
						value: options.btns[n]['value'],
						styles: {
							width: '70px'
						},
						events: {
							click: options.btns[n]['onClick']
						}
					}));
				}
		}
		$('tr_msg_botoes').setStyle('display', $('form_msg_btns').getChildren().length ? 'table-row' : 'none');

		// Tweens
		$('msg').set('tween', {onComplete: function() {}});
		$('overlay').set('tween', {onComplete: function() {}});

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

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

		// Fade in
		$('overlay').fade(0.4);
		(function() {$('msg').fade('in')}).delay(200);

	},

	// ----------------------------------------------------------------------------------------------------
	loadScripts: function() {

		if(!$chk(this.options.scripts)) return;

		var headScripts = $(document.head).getElements('script');

		if($type(this.options.scripts) == 'array') {
			for(var s = 0; s < this.options.scripts.length; s++) {
				for(var hs = 0; hs < headScripts.length; hs++) if(headScripts[hs].get('src') == this.options.scripts[s]) headScripts[hs].dispose();
				Asset.javascript(this.options.scripts[s], {id: 'script_' + this.options.id + '_' + s});
			}
		} else {
			for(var hs = 0; hs < headScripts.length; hs++) if(headScripts[hs].get('src') == this.options.scripts) headScripts[hs].dispose();
			Asset.javascript(this.options.scripts, {id: 'script_' + this.options.id + '_0'});
		}

	},

	// ----------------------------------------------------------------------------------------------------
	loadCSS: function() {

		if(!$chk(this.options.css)) return;

		if($type(this.options.css) == 'array') for(var s = 0; s < this.options.css.length; s++) {
			if(!this.loadedCSS.contains(this.options.css[s])) {
				this.loadedCSS.push(this.options.css[s]);
				Asset.css(this.options.css[s], {id: 'css_' + this.options.id + '_' + s});
			}
		} else if(!this.loadedCSS.contains(this.options.css)) {
			this.loadedCSS.push(this.options.css);
			Asset.css(this.options.css, {id: 'css_' + this.options.id + '_0'});
		}

	},

	// ----------------------------------------------------------------------------------------------------
	resizeTo: function(w, h) {

		if(this.options.tipo != 'div') return;

		if(w == null && !isNaN(h)) {					// So altura
			var marginTop = Math.round(-($('msg').getSize().y - $(this.options.id).getStyle('height').toInt() + h) / 2 - 20);
			new Fx.Elements([$(this.options.id), $('msg')]).start({
				0: {
					height: h
				},
				1: {
					marginTop: marginTop
				}
			});
		} else if(!isNaN(w) && h == null) {				// So largura
			new Fx.Elements([$(this.options.id), $('msg')]).start({
				0: {
					width: w
				},
				1: {
					marginLeft: Math.round(-(w + 30) / 2)
				}
			});
		} else if(!isNaN(w) && !isNaN(h)) {				// Ambos
			var marginTop = Math.round(-($('msg').getSize().y - $(this.options.id).getStyle('height').toInt() + h) / 2 - 20);
			var marginLeft = Math.round(-(w + 30) / 2);
			new Fx.Elements([$(this.options.id), $('msg')]).start({
				0: {
					width: w,
					height: h
				},
				1: {
					marginLeft: marginLeft,
					marginTop: marginTop
				}
			});
		}

	},

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

		// Fade out
		$('msg').set('tween', {
			onComplete: function() {
				$('msg').setStyle('display', 'none');
			}
		});
		$('overlay').set('tween', {
			onComplete: function() {
				$('overlay').setStyle('display', 'none');
			}
		});
		$('msg').fade('out');
		(function() {$('overlay').fade('out')}).delay(200);

	},

	// ----------------------------------------------------------------------------------------------------
	mostraAviso: function(aviso, countInicial) {

		this.escondeAviso();

		$('fundo_aviso').setStyles({
			display: 'block',
			opacity: 0.8
		});

		var a = new Element('a', {
			href: 'javascript:msgr.escondeAviso();',
			html: 'Ok '
		});
		var span = new Element('span', {
			id: 'span_contador',
			styles: {
				fontSize: '9px'
			},
			html: '(' + (countInicial != null ? countInicial : this.countInicial) + ')'
		});
		$('corpo_aviso').innerHTML = aviso;
		$('corpo_aviso').appendText('    ');
		a.grab(span);
		$('corpo_aviso').grab(a);
		$('corpo_aviso').setStyle('display', 'inline');

		var countObj = {count: countInicial != null ? countInicial : this.countInicial};
		var countFn = function() {
			if(--this.count <= 0) {
				msgr.escondeAviso();
			}
			$('span_contador').innerHTML = '(' + this.count + ')';
		}
		this.countPeriodical = countObj.countPeriodical = countFn.periodical(1000, countObj);

		$('fundo_aviso').tween('top', 0);

	},

	// ----------------------------------------------------------------------------------------------------
	escondeAviso: function() {

		if($chk(this.countPeriodical)) $clear(this.countPeriodical);
		$('fundo_aviso').tween('top', -30);

	},

	// ----------------------------------------------------------------------------------------------------
	setTitulo: function(titulo) {

		$('msg_titulo').innerHTML = $chk(titulo) ? titulo : '&nbsp;';

	},

	// ----------------------------------------------------------------------------------------------------
	setHTML: function(html) {

		if($chk(html) && html != ''){
			$('msg_conteudo').innerHTML = html;
			$('tr_msg_conteudo').setStyle('display', 'table-row');
		} else {
			$('tr_msg_conteudo').setStyle('display', 'none');
		}

	},

	// ----------------------------------------------------------------------------------------------------
	limpaBotoes: function() {

		$('tr_msg_botoes').setStyle('display', 'none');
		$('form_msg_btns').empty();

	}

});

