// Classe Caixa de Luz - Estática
// Danilo Trindade

CaixaDeLuz = {
	version : '1.0',
	chave : 'CaixaDeLuz',
	dd : document.documentElement,
	classe : null,
	distanciaTop : 50,
	botaoFechar : null
}

CaixaDeLuz.definirClasses = function(classe) {
	CaixaDeLuz.classe = classe;
}			

CaixaDeLuz.definirDistanciaTop = function(distanciaTop) {
	CaixaDeLuz.distanciaTop = distanciaTop;
}

CaixaDeLuz.definirBotaoFechar = function(botaoFechar) {
	CaixaDeLuz.botaoFechar = botaoFechar;
}

CaixaDeLuz.carrega = function() {
	rels = Rel.obterElementos(CaixaDeLuz.chave);
	
	for(indice in rels) {
		rels[indice].onclick = function() {
			var body   = document.body;
			var janela = document.createElement('div');
			var fade   = document.createElement('div');
			var box    = document.createElement('div');
			var img    = new Image();
			
			onresize = function () {
				fade.style.width  = CaixaDeLuz.dd.scrollWidth + 'px';
				
				if(CaixaDeLuz.dd.scrollHeight > CaixaDeLuz.dd.clientHeight) {
					fade.style.height = CaixaDeLuz.dd.scrollHeight + 'px';
				} else {
					fade.style.height = CaixaDeLuz.dd.clientHeight + 'px';
				}
			}
			
			with(janela.style) {
				zIndex   = 100;
				position = 'absolute';
				left     = '0px';
				top      = '0px';
				width    = '100%';
				height   = '100%';
			}
			
			with(fade.style) {
				zIndex     = 101;
				position   = 'absolute';
				left       = '0px';
				top        = '0px';
				width      = CaixaDeLuz.dd.scrollWidth + 'px';
				background = '#000000';
				filter     = 'alpha(opacity=50)';
				opacity    = '0.5';
				
				if(CaixaDeLuz.dd.scrollHeight > CaixaDeLuz.dd.clientHeight) {
					height = CaixaDeLuz.dd.scrollHeight + 'px';
				} else {
					height = CaixaDeLuz.dd.clientHeight + 'px';
				}
			}
			
			with(box.style) {
				zIndex   = 102;
				position = 'relative';
				display  = 'block';
				margin   = '0px auto';
			}
			
			if(CaixaDeLuz.classe != null) {
				box.className = CaixaDeLuz.classe;
				img.className = CaixaDeLuz.classe + 'IMG';
			}
			
			if(CaixaDeLuz.botaoFechar != null) {
				fechar = document.createElement('img');
				
				fechar.src = CaixaDeLuz.botaoFechar;
				
				with(fechar.style) {
					position = 'absolute';
					right    = '0px';
					top      = '0px';
				}
				
				if(CaixaDeLuz.classe != null) {
					fechar.className = CaixaDeLuz.classe + 'FECHAR';
				}
				
				fechar.onclick = function() { body.removeChild(janela) }
				
				box.appendChild(fechar);
			} else {
				img.onclick = function() { body.removeChild(janela) }
			}
			
			box.appendChild(img);
			
			if(this.title != '') {
				objP = document.createElement('p');
				
				objP.innerHTML = this.title;
				
				if(CaixaDeLuz.classe != null) {
					objP.className = CaixaDeLuz.classe + 'P';
				}
				
				box.appendChild(objP);
			}
			
			img.onload = function() {
				box.style.width  = img.width + 'px';
				
				box.style.top    = (CaixaDeLuz.dd.scrollTop + Math.ceil((CaixaDeLuz.dd.clientHeight - img.height) / 2)) + 'px';
				
				janela.appendChild(fade);
				janela.appendChild(box);
				body.appendChild(janela);
			}
			
			img.src = this.rev;
		}
	}
}

$(document).ready( function(){
	CaixaDeLuz.definirClasses('fotoLightBox');
	CaixaDeLuz.definirBotaoFechar('http://www.estadao.com.br/estadao/img/close.gif');
	CaixaDeLuz.carrega();
	
} );
