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 = "cod";
tipo = "DESC";
pg = 0;
var conteudo='';

var ajax = {
	album : function(codigo, pagina, foto, fotoID){
		pg = pagina;
		diretorio = codigo;
		alvo = document.getElementById('miniaturas');
		anime.fade(alvo, 100, 20);
		xmlhttp.open('GET', '../galeria/carregaminiaturas.asp?cod='+codigo+'&pagina='+pagina, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200) {
					alvo.innerHTML = xmlhttp.responseText;
					imagens = alvo.getElementsByTagName('img');
					if (foto == '' && fotoID == ''){
						ajax.exibe(imagens[0].getAttribute("imagemGrande"), imagens[0]);
					} else {
						ajax.exibe(foto, document.getElementById(fotoID));
					}
					document.getElementById("fotoPagina").value=pagina
					ultimo = imagens.length;
					imagens[ultimo-1].setAttribute("onload", anime.appear(alvo, 20, 100, 100));
				}	
			}
		}
		xmlhttp.send(null);
	},
	
	mostraFoto : function(lado){
		total_de_fotos = document.getElementById('TotalDeFotos').value;
		var total_atual = eval(document.getElementById('TotalAtual').value);
		if(lado == 'ant') {
			if(foto > 0) {
				foto -= 1;
				ajax.exibe(imagens[foto].getAttribute("imagemGrande"), imagens[foto]);
			}else{				
				pg -= 1;		
				if(pg >= 1) {
					ajax.album(diretorio, pg);					
				}
			}
		}else{
			if(foto < total_atual-1) {
				foto = parseInt(foto)+1;
				if(imagens[foto]){
					ajax.exibe(imagens[foto].getAttribute("imagemGrande"), imagens[foto]);
				}else{
					foto -= 1;
				}
			}else{
				if(total_atual==10) {
					pg += 1;
					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); 
		}
		document.getElementById("codFoto").value=document.getElementById(foto).getAttribute("cod")
		document.getElementById("fotoID").value=foto
		
		if(document.getElementById(foto).getAttribute("title"))
			document.getElementById("legenda").innerHTML=document.getElementById(foto).getAttribute("title");
		else
			document.getElementById("legenda").innerHTML='&nbsp;';
		
		if(document.getElementById(foto).getAttribute("comentarios"))
			document.getElementById("comentarios").innerHTML=document.getElementById(foto).getAttribute("comentarios");
		else
			document.getElementById("comentarios").innerHTML='&nbsp;';
		
		if(document.getElementById(foto).getAttribute("estrelinha"))
			document.getElementById("estrelinha").innerHTML=flash(139, 26, '../images/_carinhas.swf?nivel='+document.getElementById(foto).getAttribute("estrelinha")); 
		//alert(document.getElementById(foto).position);
		var x, y
		if(foto%2==0)
			x = 10
		else
			x = 99
			
		switch(eval(foto)){
			case 0:
				y=42
			  	break;    
			case 1:
			  	y=42
			 	 break;
			case 2:
			 	y=111
			  	break;
			case 3:
			 	y=111
			  	break;
			case 4:
			 	y=180
			  	break;
			case 5:
			 	y=180
			  	break;
			case 6:
			 	y=249
			  	break;
			case 7:
			 	y=249
			  	break;
			case 8:
			 	y=318
			  	break;
			case 9:
			 	y=318
			  	break;
			default:
			  y=41
		}

		$("#quadro").animate({top:y+'px', left:x+'px'}, 600);
	},
	
	abreEmail : function(destino, largura, altura) {
		
		div = document.getElementById('tudo');
		if(!document.getElementById('mail')){
			
			mail = document.createElement('div');
			mail['id'] = 'mail';
			mail.className='caixa';
			if(!largura)
				largura = 350
			else
				largura = eval(largura);
			if(!altura)
				altura=180
			else
				altura = eval(altura);
			
			div.appendChild(mail);
			
			//abrindo página
			xmlhttp.open("POST", destino, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						conteudo=xmlhttp.responseText;
						anime.appear(mail, 0, 100, 500);
						
						anime.resizeAbre(mail, 35, 35, largura, 18, 18, altura, conteudo);
					}
				}
			}
			xmlhttp.send('');
			//
		}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);
		
	},
	
	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';
		
	}
}
