/////////////////////////////////////////////////////
// funções que pegam largura e altura do navegador //
/////////////////////////////////////////////////////
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}
//////////////////////////////////////////////////////////////////////////
// funções que escondem os selects se for Internet Explorer abaixo de 6 //
//////////////////////////////////////////////////////////////////////////
/* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT") {
					document.forms[i].elements[e].style.visibility="hidden";
				}
			}
		}
	}
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility="visible";
				}
			}
		}
	}
}
///////////////////////////
//         MODAL         //
/////////////////////////// 
var modal_aberto;
//////////////////////////////////////////////////////////////////////////////////////
// função que faz o posicionamento dinâmico da foto em relação ao tamanho da janela //
//////////////////////////////////////////////////////////////////////////////////////
function posicionaFoto() {
	if(modal_aberto == true) {
		// Definição de onde está a janela em relação ao topo da página
		var scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrOfY = window.pageYOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			scrOfY = document.documentElement.scrollTop;
		}
		var modal = document.getElementById("modal_fundo");
		var modal_img_fnd = document.getElementById("modal_img_fnd");
		var modal_img = document.getElementById("modal_img");
		// fundo do modal
		modal.style.width = getViewportWidth()+"px";
		modal.style.height = getViewportHeight()+"px";
		modal.style.top = scrOfY+"px";
		// Posicionamento da imagem
		var largura_tela = getViewportWidth()/2;
		var altura_tela = getViewportHeight()/2;
		var largura_imagem = modal_img.clientWidth/2;
		var altura_imagem = modal_img.clientHeight/2;
		var posicao_vertical = (altura_tela - altura_imagem);
		var posiciona_h = (largura_tela - largura_imagem)+"px";
		modal_img_fnd.style.left = posiciona_h;
		// Validação de tamanho da imagem em relação ao tamanho da tela
		if (posicao_vertical>0){
			modal_img_fnd.style.top = (posicao_vertical + scrOfY) + "px";
		} else {
			modal_img_fnd.style.top = scrOfY;
		}
	}
}
function abreModal(caminho) {
	var imgAmpSrc = caminho;
	var modal_img = new Image(); 
	modal_img.src = imgAmpSrc;
	modal_aberto = true;
	document.getElementById("span_foto").innerHTML = '<div id="modal_fundo" class="modal_fundo"></div><div class="modal_img_fnd" id="modal_img_fnd"><div class="fechar_btn"><a href="javascript:;" onclick="fechaModal()"><img src="img/fechar_btn.gif" alt="Fechar" /></a></div><a href="javascript:;" onclick="fechaModal()"><img src="'+caminho+'" id="modal_img" onload="posicionaFoto()" onload="posicionaFoto()" /></a></div><br />';
	// Elimina rolagem da página	
	document.documentElement.style.overflowY = "hidden"; 
	document.body.style.overflowY = "hidden";
	document.documentElement.style.width = getViewportWidth() - 16 + "px"
	document.body.style.width = getViewportWidth() - 16 + "px"
		
	var modal = document.getElementById("modal_fundo");
	var modal_img_fnd = document.getElementById("modal_img_fnd");
	var modal_img = document.getElementById("modal_img");
	modal.style.display = '';
	modal_img_fnd.style.display = '';
	
	posicionaFoto();
	hideSelectBoxes();
}
function fechaModal() {
	modal_aberto = false;
	document.documentElement.style.overflowY = ''; 
	document.body.style.overflowY = '';
	document.body.style.width = '';
	document.documentElement.style.width = '';
	
	var modal = document.getElementById("modal_fundo");
	var modal_img_fnd = document.getElementById("modal_img_fnd");
	
	modal.style.display = 'none';
	modal_img_fnd.style.display = 'none';
	document.getElementById("span_foto").innerHTML = '';
	displaySelectBoxes();
}
/////////////////////////////////////////////
// adiciona listener para Resize da página //
/////////////////////////////////////////////
function adEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}
adEvent(window, "resize", posicionaFoto);