var dir='fotos/';

function topo() {
	window.location.href="#div_head";
	window.scroll(0,0);
	window.scrollbars=false;
}

function modal(tipo,cont){
	if(tipo=='img'){
		var doc=document;
		doc.imgs=new Array();
		if(!doc.imgs[cont]){
			doc.imgs[cont]=new Image;
			doc.imgs[cont].src=dir+cont;
		}
		show_modal(cont,'<div id="charging"><img src="loading.gif" /></div><img style="display:none" id="foto_id" src="'+dir+cont+'" />');
		t=setInterval('carregaM()',200);
	} else if(tipo=='frame'){
		show_modal("Depoimento",'<iframe src="'+cont+'" frameborder="0" height="254" width="700"></iframe>');
		t2=setInterval('centra()',1);
	} else if(tipo=='ajax'){
		show_modal('Depoimento','<div id="charging"><img src="loading.gif" /></div>');
		t2=setInterval('centra()',1);
		AjaxGet(cont);
	} else if(tipo=='ajax2'){
		show_modal('Inscrição','<div id="charging"><img src="loading.gif" /></div>');
		t2=setInterval('centra()',1);
		AjaxGet(cont);
	} else if(tipo=='ajax3'){
		show_modal('Informações','<div id="charging"><img src="loading.gif" /></div>');
		t2=setInterval('centra()',1);
		AjaxGet(cont);
	}
	topo();
}
function carregaM(){
	ok=document.getElementById('foto_id').complete;
	if(ok){
		clearInterval(t);
		$('#charging').remove();
		t2=setInterval('centra()',1);
		$('#foto_id').fadeIn("slow");
	}
}
function centra(){
	var popdiv=document.getElementById('popup');
	alt=popdiv.offsetHeight;alt=(alt/2)*(-1);
	lar=popdiv.offsetWidth;lar=(lar/2)*(-1);
	popdiv.style.left="50%";
	popdiv.style.top="50%";
	popdiv.style.marginLeft=lar+"px";
	popdiv.style.marginTop=alt+"px";
	
	if(self.innerWidth!=undefined){
		max_width=self.innerWidth-34;
		max_height=self.innerHeight-56;
	} else {
		var D= document.documentElement;
		max_width=D.clientWidth-34;
		max_height=D.clientHeight-56;
	}
	
	$("#popupcontents").css('max-width',max_width+'px');
	$("#popupcontents").css('max-height',max_height+'px');
}

function centra2(){
	var popdiv=document.getElementById('popup');
	alt=popdiv.offsetHeight;alt=(alt/2)*(-1);
	lar=popdiv.offsetWidth;lar=(lar/2)*(-1);
	popdiv.style.left="50%";
	popdiv.style.top="50%";
	popdiv.style.marginLeft=lar+"px";
	popdiv.style.marginTop=alt+"px";
	
	if(self.innerWidth!=undefined){
		max_width=self.innerWidth-34;
		max_height=self.innerHeight-56;
	} else {
		var D= document.documentElement;
		max_width=D.clientWidth-34;
		max_height=D.clientHeight-56;
	}
	
	$("#popupcontents").css('width',max_width+'px');
	$("#popupcontents").css('height',max_height+'px');
}

function show_modal(title,html){
	$("#popup").remove();
	$("#modal").remove();
	var popdiv = document.createElement("div");
	popdiv.id = "popup";
	
	var moddiv = document.createElement("div");
	moddiv.id = "modal";
	
	var bardiv = popdiv.appendChild(document.createElement("div"));
	bardiv.id = "popupbar";
	bardiv.innerHTML='<div id="ptitle">'+title+'</div><a class="modclose" href="javascript:close_modal();">X</a>';
	
	var condiv = popdiv.appendChild(document.createElement("div"));
	condiv.id = "popupcontents";
	condiv.innerHTML=html;
	
	popdiv.style.zIndex=1000;
	
	document.body.appendChild(popdiv);
	document.body.appendChild(moddiv);
	popdiv.style.display="none";
	var ttt = $(document).height();
	moddiv.style.height=ttt+'px';
	moddiv.style.display="none";
	$("#modal").slideDown("normal", function(){ $("#popup").fadeIn("normal"); });
	alt=popdiv.offsetHeight;alt=(alt/2)*(-1);
	lar=popdiv.offsetWidth;lar=(lar/2)*(-1);
	popdiv.style.left="50%";
	popdiv.style.top="50%";
	popdiv.style.marginLeft=lar+"px";
	popdiv.style.marginTop=alt+"px";
}
function close_modal(){
	clearInterval(t2);
	$("#popup").fadeOut("normal", function(){ $("#modal").slideToggle("normal", function(){ closed_modal(); } ); });
}

function ajaxInit() {
	var req;
	try {
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
	  try {
	    req = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch(ex) {
	    try {
	      req = new XMLHttpRequest();
	    } catch(exc) {
	      alert("Esse browser não suporta a tecnologia Ajax");
	      req = null;
	    }
	  }
	}
	return req;
}

function AjaxGet(url) {
	ajaxG = ajaxInit();
	ajaxG.onreadystatechange = procesGet;
	ajaxG.open("GET", url, true);
	ajaxG.send(null);
}
function procesGet() {
  if (ajaxG.readyState == 4) {
    if (ajaxG.status == 200) {
		$('#charging').remove();
		document.getElementById('popupcontents').innerHTML=ajaxG.responseText;
    } else {
        alert("Desculpe, erro:\n"+ajaxG.responseText);
		close_modal();
	}
  }
}
function preload() { //v3.0
	var d=document;
	if(d.images){
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=preload.arguments;
		for(i=0; i<a.length; i++){
			if (a[i].indexOf("#")!=0){
				d.MM_p[j]=new Image;
				d.MM_p[j++].src=a[i];
			}
		}
	}
}
preload('loading.gif');
