try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}

foto = 0;

inicioSlide = 0;

campo = "idevento";

tipo = "DESC";

pg = 0;

var ajax = {
	
	album : function(pasta, pagina){
		pg = pagina;
		diretorio = pasta;
		alvo = document.getElementById('miniaturas');
		anime.fade(alvo, 100, 20);
		xmlhttp.open('GET', 'include/ajax.php?pasta='+pasta+'&pg='+pagina, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {				
					alvo.innerHTML = xmlhttp.responseText;
					imagens = alvo.getElementsByTagName('img');
					ajax.exibe(imagens[0].title, imagens[0]);
					ultimo = imagens.length;
					imagens[ultimo-1].onload = anime.appear(alvo, 20, 100, 100);
				}	
			}
		}
		xmlhttp.send(null);
	},
	
	mostraFoto : function(lado){
		
		total_de_fotos = document.getElementById('TotalDeFotos').value;
		
		if(lado == 'ant') {
			if(foto > 0) {
				foto -= 1;
				ajax.exibe(imagens[foto].title, imagens[foto]);
			}else{				
				pg -= 1;		
				if(pg >= 0) {
					ajax.album(diretorio, pg);					
				}
			}
		}else{
			if(foto < 14) {
				foto = parseInt(foto)+1;
				if(imagens[foto]){
					ajax.exibe(imagens[foto].title, imagens[foto]);
				}else{
					foto -= 1;
				}
			}else{
				pg += 1;
				if(pg < (total_de_fotos/15)) {
					ajax.album(diretorio, pg);					
				}
			}
		}
	},
	
	exibe : function(caminho, proxima){
		foto = proxima.id;
		//fotoAtual = imagens[foto].title;
		exibe = document.getElementById('foto');
		loaded = document.getElementById('load');
		anime.fade(loaded, 100, 20, 100);
		loaded.style.display = 'block';
		exibe.style.display = 'none';
		exibe.src = caminho;
		exibe.onload = function(){
			loaded.style.display = 'none';
			exibe.style.display = 'block';
			if(exibe.width > exibe.height){				
				exibe.style.width = 373;
				exibe.style.height = '';
			} else {				
				exibe.style.height = 280;
				exibe.style.width = '';
			}
			anime.appear(exibe, 0, 100, 100); 
			//alert (exibe.width+"-"+exibe.height);
		}
		
		
		//botao add
		xmlhttp.open('GET', 'include/verif.php?img='+document.getElementById(foto).title, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {
					document.getElementById('addFoto').innerHTML =  xmlhttp.responseText;
				} else {
				}
			}
		}
		xmlhttp.send(null);
		
		
	},
	
	abreEmail : function() {
		
		div = document.getElementById('tudo');
		if(!document.getElementById('mail')){
			
			mail = document.createElement('div');
			mail['id'] = 'mail';
			div.appendChild(mail);
		
			anime.appear(mail, 0, 100, 500);
			anime.resizeAbre(mail, 35, 35, 350, 18, 18, 180);
			
		}else{
			alert('A tela de e-mail já se encontra aberta!');
		}
		
	},
	
	fechaEmail : function() {
		
		mail.innerHTML = '';	
		anime.fade(mail, 100, 0, 500);		
		anime.resizeFecha(mail, 350, 35, 35, 180, 18, 18);
		
	},
	
	enviaEmail : function(){
		
		img = document.getElementById('foto').src;
		nome = document.getElementById('nome').value
		destino = document.getElementById('destino').value
		email = document.getElementById('email').value
		
		if(nome == "" || nome == null){
			alert("Por favor preencha seu nome!");
			return false;
		} else if(destino == "" || destino == null){
			alert("Por favor preencha seu nome do destinatario!");
			return false;
		}else if(email == "" || email == null){
			alert("Por favor preencha o e-mail pra quem a foto sera enviada!");
			return false;
		}else{
			
			param = "nome="+nome+"&destino="+destino+"&email="+email+"&img="+img;
			xmlhttp.open("POST", "include/email.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					document.getElementById('formulario').innerHTML = carregandoHTML;
				}
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						document.getElementById('enviandomail').innerHTML = '<span>'+xmlhttp.responseText+'</span>';
					}
				}
			}
			xmlhttp.send(param);
			
		}
		
	},
	
	camposForm : function(oForm){
		var aParams = new Array();
		for (var i=0 ; i < oForm.length; i++) {
			var sParam = oForm[i].id;
			sParam += "=";
			sParam += oForm[i].value;
			aParams.push(sParam);
		}
		return aParams.join("&");
	},
	
	cadastraGaleria : function(){
		
		formulario = document.cadastra.elements;
		param = ajax.camposForm(formulario);
		
		xmlhttp.open('POST', '../include/ajax.php?cadastro=1', true);
		xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xmlhttp.onreadystatechange = ajax.repostasCadastro;
		xmlhttp.send(param);
		
	},
	
	repostasCadastro : function(){
		
		mensagens = document.getElementById('msg');
		
		if(xmlhttp.readyState == 1)				
			mensagens.innerHTML = 'Aguarde...';
		
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				mensagens.innerHTML = xmlhttp.responseText;
				ajax.listaGaleria(campo, tipo);
				document.cadastra.reset();
			}
		}
		
	},
	
	listaGaleria : function(campo, tipo){
		
		ordena = campo;
		ordenar = tipo;
		
		local = document.getElementById('lista');
		
		xmlhttp.open('GET', '../include/ajax.php?lista=1&campo='+ordena+'&tipo='+ordenar, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 1) {
				if(document.getElementById('tabelaLista'))
					anime.fade(document.getElementById('tabelaLista'), 100, 20, 500);
			}
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200){
					local.innerHTML = xmlhttp.responseText;
					if(document.getElementById('tabelaLista'))
						anime.fade(document.getElementById('tabelaLista'), 20, 100, 500);
				}
			}
		}
		xmlhttp.send(null);
		
	},
	
	deletaGaleria : function(id){
		
		if(confirm("Tem certeza que deseja excluir a galeria!")) {
			xmlhttp.open('GET', '../include/ajax.php?deleta=1&id='+id, true);
			xmlhttp.onreadystatechange = ajax.repostasCadastro;
			xmlhttp.send(null);
		}
		
	},
	
	edicaoTratamento : function(){
		
		formulario = document.cadastra.elements;
		if(xmlhttp.readyState == 1)
			formulario[1].value = 'Aguarde...';
			
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				codigo = eval('('+xmlhttp.responseText+')');
				for(i=0;i<formulario.length-1;i++){
					formulario[i].value = codigo[i];
				}
				abrePasta = codigo['pasta'];
			}
		}
		
	},
	
	editaGaleria : function(id){
		
		xmlhttp.open('GET', '../include/ajax.php?edita=1&id='+id, true);
		xmlhttp.onreadystatechange = ajax.edicaoTratamento;
		xmlhttp.send(null);
		
	},
	
	verificaDiretorio : function(pasta){
		
		resp = document.getElementById('respotadir');
		
		xmlhttp.open('GET', '../include/dir.php?pasta='+pasta, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				if(xmlhttp.status == 200) {
					if(xmlhttp.responseText == '1'){
						resp.innerHTML = '* pasta ok!';
						resp.style.color = '#99CC00';
						abrePasta = pasta;
					}else{
						resp.innerHTML = '* pasta não encontrada!';
						resp.style.color = '#FF0000';
						abrePasta = false;
					}
				}
			}
		}
		xmlhttp.send(null);
		
	},
	
	listaImagens : function(){
		
		listaFotos = document.getElementById('listaFotos');
		listaFotos.style.display = 'block';
		
		if(abrePasta !== null) {
			xmlhttp.open('GET', '../include/dir.php?imagens=1&dir='+abrePasta, true);
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					listaFotos.innerHTML = "carregando...";
				}
				if(xmlhttp.readyState == 4){
					if(xmlhttp.status == 200) {
						listaFotos.innerHTML = xmlhttp.responseText;				
					}
				}
			}
			xmlhttp.send(null);
		}else{
			
			alert("Pasta Invalida!");
			
		}
		
	},
	
	completaCampo : function(valor){
		
		document.getElementById('foto').value = valor;
		
	},
	
	fechaLista : function(){
		
		document.getElementById('listaFotos').style.display = 'none';
		
	},

	addItem : function(){
		document.getElementById('addFoto').innerHTML = '<img id="iconRemove" src="img/adding.gif" width="16" height="16" />';		
		xmlhttp.open('GET', './include/ajax.php?item='+document.getElementById(foto).title, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {
					document.getElementById('addFoto').innerHTML = '<a href="javascript:void(0);" onclick="ajax.removeItem();"><img id="iconRemove" title="Remove foto do pacote." src="img/trash.gif" width="16" height="16" /></a>';
					document.getElementById(foto).src = document.getElementById(foto).src+'#';
				} else {
				}
			}
		}
		xmlhttp.send(null);
	},
	
	removeItem : function(){
		document.getElementById('addFoto').innerHTML = '<img id="iconRemove" src="img/adding.gif" width="16" height="16" />';	
		xmlhttp.open('GET', './include/ajax.php?removeitem='+document.getElementById(foto).title, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {
					document.getElementById('addFoto').innerHTML = '<a href="javascript:void(0);" onclick="ajax.addItem();"><img id="iconAdd" title="Adiciona foto no pacote." src="img/add.gif" width="16" height="16" /></a>';
					document.getElementById(foto).src = document.getElementById(foto).src+'#';
				} else {
				}
			}
		}
		xmlhttp.send(null);
	},

	callPage : function(target_page){
		document.getElementById('content').innerHTML = '<div id="loading_page"><img src="img/loadedmail.gif"></div>';
		xmlhttp.open('GET', './'+target_page+'.php', true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {
					document.getElementById("topo_img").src = 'img/topo_'+target_page+'.png';	
					document.getElementById('content').innerHTML = xmlhttp.responseText;
				} else {
				}
			}
		}
		xmlhttp.send(null);
	},

	nextPage : function(target_page){
		document.getElementById('content').innerHTML = '<div id="loading_page"><img src="img/loadedmail.gif"></div>';
		xmlhttp.open('GET', './coberturas.php?pg='+target_page, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {	
					document.getElementById('content').innerHTML = xmlhttp.responseText;
				} else {
				}
			}
		}
		xmlhttp.send(null);
	},

	callPageEvento : function(target_page){
		document.getElementById('content').innerHTML = '<div id="loading_page"><img src="img/loadedmail.gif"></div>';
		xmlhttp.open('GET', './evento.php?id='+target_page, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {	
					document.getElementById('content').innerHTML = xmlhttp.responseText;
				} else {
				}
			}
		}
		xmlhttp.send(null);
	},
		
		enviaEmailFale : function(){
		
		nome = document.getElementById('nome').value;
		email = document.getElementById('email').value;
		msg = document.getElementById('msg').value;
		error = false;
		document.getElementById('error_nome').innerHTML = '<img src="img/loaded.gif" width="16" height="16" />';
		document.getElementById('error_email').innerHTML = '<img src="img/loaded.gif" width="16" height="16" />';
		document.getElementById('error_msg').innerHTML = '<img src="img/loaded.gif" width="16" height="16" />';
		
		
		if(nome == "" || nome == null){
			document.getElementById('error_nome').innerHTML = '<img src="img/action_stop.gif" width="16" height="16" />';
			error = true;
		} else {
			document.getElementById('error_nome').innerHTML = '<img src="img/accept.png" width="16" height="16" />';	
		}
		
		if(email == "" || email == null || !checkMail(email)){
			document.getElementById('error_email').innerHTML = '<img src="img/action_stop.gif" width="16" height="16" />';
			error = true;
		}  else {
			document.getElementById('error_email').innerHTML = '<img src="img/accept.png" width="16" height="16" />';	
		}
		
		if(msg == "" || msg == null){
			document.getElementById('error_msg').innerHTML = '<img src="img/action_stop.gif" width="16" height="16" />';
			error = true;
		} else {
			document.getElementById('error_msg').innerHTML = '<img src="img/accept.png" width="16" height="16" />';	
		}
		
		if(error == false) {
			document.getElementById('submitB').value = "Enviando...";
			param = "nome="+nome+"&email="+email+"&msg="+msg;
			xmlhttp.open("POST", "include/emailFale.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					//document.getElementById('formulario').innerHTML = carregandoHTML;
				}
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						document.getElementById('nome').value = "";
						document.getElementById('email').value = "";
						document.getElementById('msg').value = "";
						document.getElementById('submitB').value = "Enviar";
						alert('Email enviado!');
					}
				}
			}
			xmlhttp.send(param);	
		}
		
	}
}