var Avisos = new Class({
	Implements : [Options],

	options : {
		'modal' : false,
		'clickToCancel' : true,
		// Set seconds to autoclose, 0 => disabled
		'autoclose' : 0
	},

	cortina : null,

	initialize : function(options) {
		this.setOptions(options);

		this.create();
	},

	create : function() {
		this.cortina = new Element('div').setStyles({
			'position' : 'absolute',
			'background-color' : '#000',
			'opacity' : '0.3',
			'display' : 'none'
		}).injectInside(document.body);

		this.avisos = new Element('div').setStyles({
			'position' : 'absolute',
			//'background-color' : '#f9f590',
			//'color' : '#000',
			//'padding' : '5px 15px',
			//'text-align' : 'center',
			//'font-size' : '1.2em',
			'display' : 'none'
		}).injectInside(document.body);
	},

	show : function() {
		if(this.options.modal) {
			this.cortina.setStyles({
				'display' : 'block',
				'visibility' : 'hidden'
			});
		}

		this.avisos.setStyles({
			'display' : 'block',
			'visibility' : 'hidden'
		});
		
		this.resize();

		if(this.options.modal) {
			this.cortina.setStyles({
				'visibility' : 'visible'
			});
		}

		this.avisos.setStyles({
			'visibility' : 'visible'
		});

		if(this.options.clickToCancel) {
			this.cortina.addEvent('click', this.hide.bindWithEvent(this));
		}

		if(this.options.autoclose > 0) {
			this.hide.delay(this.options.autoclose * 1000, this);
		}

		window.addEvent('resize', this.resize.bindWithEvent(this));
		window.addEvent('scroll', this.resize.bindWithEvent(this));
	},

	getHandle : function() {
		return this.avisos;
	},

	hide : function(e) {
		if(e) {
			new Event(e).stop();
		}
		this.cortina.setStyles({
			'display' : 'none'
		});

		this.avisos.setStyles({
			'display' : 'none'
		});
		window.removeEvents('resize');
		window.removeEvents('scroll');
	},

	resize : function() {
		var w = window.getSize();
		w.x = w.x.toInt();
		w.y = w.y.toInt();
		var ws = window.getScroll();

		if(this.options.modal) {
			this.cortina.setStyles({
				'top' : ws.y + 'px',
				'left' : '0px',
				'width' : w.x + 'px',
				'height' : w.y + 'px'
			});
		}

		var a = this.avisos.getSize();
		a.x = a.x.toInt();
		a.y = a.y.toInt();

		var top = 0;
		var left = 0;
		if(w.y < a.y) {
			top = w.y * 0.1;
		}
		else {
			//top = (w.y / 3) - (a.y / 2);
			top = ws.y + (w.y - a.y) / 3;
		}

		if(w.x < a.x) {
			left = w.x * 0.05;
		}
		else {
			left = (w.x - a.x) / 2;
		}

		this.avisos.setStyles({
			'top' : top + 'px',
			'left' : left + 'px'
		});
	}
});

