/**
 * @author Dark
 */
/*JavaScript source file, Dark */

modal = function() {
	var MSIE;
	var modal_div;		// Fundo transparente que cobre a pagina;
	var iframe; 			// Ultilizado para bug do IE
	var height; 			// Altura
	var width; 			// Largura
	var janela; 			// Janela que recebera conteudo
	var color;
	var top;
	var left;
	var transparency;
	var cssModal;
	var cssJanela;
	var modalColor;
	var borderColor;
	var borderStyle;
	var borderSize;
	var gradiente1;
	var gradiente2;
	var conteudo;
	var round;
	var rounded;
	var divround;
	
	this.height = 100;		// Altura Padrao
	this.width = 100;		// Largura Padrao
	this.MSIE = false;		// Valor Padrao
	this.top = '50%'
	this.left = '50%'
	this.transparency = 60;
	this.modalColor = '#000000';
	this.color = '#FFFFFF';
	this.borderColor = '#000000';
	this.borderStyle = 'solid';
	this.borderSize = 2;
	this.rounded = true;
	this.animado = true;
	this.duracao = 0.5;
	this.loading = false;
	this.fechar = false;
	
	this.gradient = 'progid:DXImageTransform.Microsoft.gradient(enabled=\'true\',startColorstr=red, endColorstr=yellow)';

	if(navigator.userAgent.indexOf('MSIE')>=0) this.MSIE = true;
}

modal.prototype = {
	setSize : function(width,height)
	{
		if(width)this.width = width;
		if(height)this.height = height;	
	}

	,

	setFechar: function(x) 
	{
		this.fechar = x;	
	}
	
	,

	setColor : function(color) {
		if(color)this.color = color;
	}
	
	,
	
	setAnimado : function(x) {
		this.animado = x;
	}
	
	,
	
	setDuracao : function(duracao) {
		this.duracao = duracao;
	}
	
	,
	
	setModalColor : function(color) {
		if(color)this.modalColor = color;
	}
	
	,
	
	setTransparency : function(trans) {
		if(trans)this.transparency = trans;
	}
	
	,
	
	setCssModal : function(css) {
		if(css)this.cssModal = css;
	}
	
	,
	
	setBoderColor : function(color) {
		if(color)this.borderColor = color;
	}
	
	,
	
	setColor : function(color) {
		if(color)this.color = color;
	}
	
	,
	
	setBorder : function(color, style, size) {
		if(color)this.borderColor = color;
		if(style)this.borderStyle = style;
		if(size)this.borderSize = size;
	}
	
	,
	
	setLoading : function(x) {
		this.loading = x;
	}
		
	,
	
	setGradiente : function(color1, color2) {
		if(color1)this.gradiente1 = color1;
		if(color2)this.gradiente2 = color2;
	}
	
	,
	
	setConteudo : function(html) {
		if(html)this.conteudo = html;
	}
	
	,

	setJanela: function(divName) {
		this.janela = document.getElementById(divName);
	}
	,
	display : function()
	{
		if(!this.modal_div){
			this.__createDivs();
		}
		 //Mostra as divs*/
		if(this.animado) {
			new Effect.Appear(this.modal_div, { duration: this.duracao, from: 0.0, to: ((100-this.transparency)/100) });
			new Effect.BlindDown(this.janela, { delay: this.duracao});
		}
		else {
			this.modal_div.style.display='block';
			this.janela.style.display='block';
		}
		if(this.fechar) {
			this.fechar_div.style.display='block';
		}
		if(this.MSIE)this.iframe.style.display='block';	
	}

	,
	
	close : function()
	{
		if(this.animado) {
			new Effect.BlindUp(this.janela, { duration: this.duracao});
			new Effect.Fade(this.modal_div, { delay: this.duracao ,duration: this.duracao, from: ((100-this.transparency)/100), to: 0.0, afterFinish: this.closeiframe() });
		}
		else {
			this.modal_div.style.display='none';
			this.janela.style.display='none';
			this.closeiframe();
		}
		
		if(this.fechar) {
			this.fechar_div.style.display='none';
		}
		
	}	
	
	,
	
	closeiframe : function()
	{
		if(this.MSIE)this.iframe.style.display='none';
	}
	
	,
	
	__rounded : function () {
		this.round = document.createElement('b');
		this.round.style.display = 'block';
		r1 = document.createElement('b');
		r1.id = 'r1';
		r1.style.margin = '0 5px';
		r1.style.backgroundColor = this.color;
		r1.style.display = 'block';
		r1.style.height = '1px';
		r1.style.overflow = 'hidden';
		this.round.appendChild(r1);
		r2 = document.createElement('b');
		r2.id = 'r2';
		r2.style.margin = '0 3px';
		r2.style.backgroundColor = this.color;
		r2.style.display = 'block';
		r2.style.height = '1px';
		r2.style.overflow = 'hidden';
		this.round.appendChild(r2);
		r3 = document.createElement('b');
		r3.id = 'r1';
		r3.style.margin = '0 2px';
		r3.style.backgroundColor = this.color;
		r3.style.display = 'block';
		r3.style.height = '1px';
		r3.style.overflow = 'hidden';
		this.round.appendChild(r3);
		r4 = document.createElement('b');
		r4.id = 'r4';
		r4.style.margin = '0 1px';
		r4.style.backgroundColor = this.color;
		r4.style.display = 'block';
		r4.style.height = '2px';
		r4.style.overflow = 'hidden';
		this.round.appendChild(r4);
	}
	
	,
	__createDivs : function()
	{
		// Cria div Transparent
		this.modal_div = document.createElement('DIV');
		if(this.cssModal) { 
			this.modal_div.className=this.cssModal; 
		}
		else {
			this.modal_div.style.backgroundColor = this.modalColor;
			this.modal_div.style.position='absolute';
			this.modal_div.style.top=0+'px';
			this.modal_div.style.left=0+'px';
			var theBody = document.getElementsByTagName("BODY")[0];
			
			this.modal_div.style.width = '100%';
			this.modal_div.style.height = '100%';
			if(this.animado) {
				if(this.MSIE) {
					this.modal_div.style.filter='alpha(opacity='+(0)+')';
				}
				else {
					this.modal_div.style.opacity = 0;
				}
			}
			else {
				if(this.gradiente1 && this.MSIE) this.modal_div.style.filter='progid:DXImageTransform.Microsoft.gradient(enabled=\'true\',startColorstr='+this.gradiente1+', endColorstr='+this.gradiente2+'), '+'alpha(opacity='+(100-this.transparency)+')';
				else {
					if(this.MSIE)
						this.modal_div.style.filter='alpha(opacity='+(100-this.transparency)+')';
					else
						this.modal_div.style.opacity = (100-this.transparency)/100;
				}
			}
			this.modal_div.style.zIndex = 95000;
		}
		document.body.appendChild(this.modal_div);
		

		// Cria Div Conteudo
		if(!this.janela) {
			this.janela = document.createElement('DIV');
			if(this.cssJanela) {
				this.janela.className=this.cssJanela; 
			}
			this.janela.style.backgroundColor = this.color;
			this.janela.style.zIndex = 100000;
			this.janela.style.position='absolute';
			this.janela.style.top = this.top;
			this.janela.style.left = this.left;
			this.janela.style.height = this.height+'px';
			this.janela.style.width = this.width+'px';
			this.janela.style.marginLeft= this.width/2*-1+'px';
			this.janela.style.marginTop = this.height/2*-1+'px';
			this.janela.style.display = 'none';
			this.janela.style.border = this.borderSize+'px '+this.borderStyle+' '+this.borderColor;
			this.janela.id = 'janela';
			if(this.loading) {
				this.setConteudo('<table width="100%" height="100%" border="0"><tr><td align="center" valign="middle"><img src="loading.gif" width="128" height="128"></td></tr></table>');
			}
			if(this.conteudo) this.janela.innerHTML = this.conteudo;
			document.body.appendChild(this.janela);	
			if(this.fechar) {
				this.fechar_div = document.createElement('DIV');
				this.fechar_div.style.display = 'none';
				this.fechar_div.style.position='absolute';
				this.fechar_div.style.zIndex = 100000;
				this.fechar_div.style.width = this.width+'px';
				this.fechar_div.style.left = this.left;
				this.fechar_div.style.top = this.top;
				this.fechar_div.style.textAlign = 'right';
				this.fechar_div.style.marginLeft= this.width/2*-1+'px';
				this.fechar_div.style.height = '15px';
				this.fechar_div.style.marginTop = ((this.height/2)+19)*-1+'px';
				this.fechar_div.innerHTML = '<a href="#" onclick="modal.close();"> X Fechar </a>';
				this.fechar_div.style.backgroundColor = this.borderColor;
				document.body.appendChild(this.fechar_div);	
			}
		}
		else {
			this.janela.style.zIndex = 100000;
			this.janela.style.position='absolute';
			this.janela.style.top = this.top;
			this.janela.style.left = this.left;
			this.height = this.height;
			this.width = this.width;
			this.janela.style.marginLeft= this.width/2*-1+'px';
			this.janela.style.marginTop = this.height/2*-1+'px';
			this.janela.style.display = 'none';
		}
		if(this.MSIE){
			this.iframe = document.createElement('<IFRAME src="about:blank" frameborder=0>');
			this.iframe.style.position = 'absolute';
			this.iframe.style.width = '100%';
			this.iframe.style.height = '100%';
			this.iframe.id = 'iframe';
			this.iframe.style.border = '0px none #000000'; 
			this.iframe.style.height = '100%';
			this.iframe.style.width = '100%';
			this.iframe.style.position = 'absolute';
			this.iframe.style.top = 0;
			this.iframe.style.left = 0;
			this.iframe.style.zIndex = 1;
			this.iframe.style.filter = 'Alpha(Opacity=0)';
			this.iframe.style.display = 'none';
			document.body.appendChild(this.iframe);	
		}
	}
}
