var dir='js/';

var tip; // I - Imagem, F - Frame, X - Ajax, A - fullscreen, M - mensagem simples
var tit; // Se não setado, título será o URL (abaixo)
var url; // Obrigatório
var tam; // Se não setado o tamanho será adequado ao conteúdo
var lar; // A string até a posição do x
var alt; // A string depois a posição do x
var CallBack;

function modal(dados){
	CallBack = lar = alt = url = tit = tam = false;
	tip = dados['tipo'];
	tit = (dados['titulo'])?dados['titulo']:dados['url'];
	url = dados['url'];
	tam = dados['tam'];
	msg = dados['msg'];
	CallBack = dados['call'];
	if(tam){ lar = tam.substr(0,tam.indexOf('x')); alt = tam.substr(tam.indexOf('x')+1); }
	if(tip=='I'){
		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="'+dir+'loading.gif" /></div><img style="display:none" id="foto_id" src="'+dir+cont+'" />');
		t=setInterval('carrega()',200);
	} else if(tip=='F'){
		Flar = '100%'; Falt = '100%'; Func_centra = 'centra2()';
		if(lar && alt){ Flar = lar;	Falt = alt;	Func_centra = 'centra()'; }
		show_modal(tit,'<iframe src="'+url+'" frameborder="0" height="'+Falt+'" width="'+Flar+'"></iframe>');
		t2=setInterval(Func_centra,1);
	} else if(tip=='X'){
		show_modal(tit,'<div id="charging"><img src="'+dir+'loading.gif" /></div>');
		t2=setInterval('centra()',1);
		AjaxGet(url);
	} else if(tip=='A'){
		fullscreen('<iframe src="'+url+'" frameborder="0" style="width:100%; height:100%"></iframe>');
	} else if(tip=='M'){
		show_modal(tit,'<div style="padding:10px">'+msg+'</div>');
		t2=setInterval('centra()',1);
	}
}
function fullscreen(html){
	$('html').css('overflow','hidden');
	$("#popup").remove();
	$("#modal").remove();
	$("#full").remove();
	var popdiv = document.createElement("div");
	popdiv.id = "popup";
	popdiv.style.height='100%';
	popdiv.style.width='100%';
	popdiv.style.overflow = 'auto';
	popdiv.style.top = 0;
	popdiv.style.left = 0;
	var fulldiv = document.createElement("div");
	fulldiv.id = "full";
	
	var condiv = popdiv.appendChild(document.createElement("div"));
	condiv.id = "popupcontents";
	condiv.innerHTML=html;
	condiv.style.height = '100%';
	popdiv.style.zIndex=1000;
	
	document.body.appendChild(popdiv);
	document.body.appendChild(fulldiv);
	//$("#popup iframe").css('height',popdiv.offsetHeight-2);
	fulldiv.style.display="none";
	popdiv.style.display="none";
	$("#full").fadeIn("normal");
	$("#popup").fadeIn("normal");
}
function carrega(){
	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;
	if(lar && alt){
		condiv.style.width = lar+'px';
		condiv.style.height = alt+'px';
	}
	
	moddiv.style.zIndex=1000;
	popdiv.style.zIndex=1001;
	popdiv.style.display="none";
	moddiv.style.display="none";
	
	document.body.appendChild(popdiv);
	document.body.appendChild(moddiv);
	
	$("#modal").slideDown("fast", 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("fast", function(){ if(CallBack) no_fim(); }); });
}

function no_fim(){ eval(CallBack+'()'); } // executa uma função ao fim de tudo

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(dir+'loading.gif');