try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = 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
		
		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';
			anime.appear(exibe, 0, 100, 100); 
		}
		
		
	},
	
	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';
		
	}
}
