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; 
}

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";
				}
			}
		}
	}
}

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";
				}
			}
		}
	}
}

// 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;
	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 />';
	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 abreModalArquivo(caminho, w, h) {
	var imgAmpSrc = caminho;
	modal_aberto = true;
	
	var lObjSpan = document.getElementById("span_foto");
	lObjSpan.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><iframe id="modal_img" src="'+ caminho +'" width="'+ w +'" height="'+ h +'" scrolling="auto" frameborder="0" /></div>';
	lObjSpan.style.width = w + 'px';
	lObjSpan.style.height = h + 'px';

	// Elimina rolagem da página (as 2 primeiras linhas fazem com que tudo se recarregue no mozilla)
	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");
	modal.style.display = '';
	modal_img_fnd.style.display = '';
	
	posicionaFoto();
	hideSelectBoxes();
	
	if(document.all){
		var iframe = document.getElementById('modal_img');
		iframe.src = iframe.src
	}
}

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);
