//globais

var posAtual = 0;
var galeria = Array();

function nextImage(){
	var pos = posAtual+1;
	if(pos >= galeria.length){
		return false;
	}else{
		showImage(pos);		
	}
}

function prevImage(){
  	var pos = posAtual-1;
	if(pos < 0){
		return false;
	}else{
		showImage(pos);		
	}
}

function loadGaleria(registro){
	ajax = new ObjAjax();
	var pos = posAtual-1;
 	ajax.onreadystatechange = function(){
    	if(ajax.readyState == 4){
			montaModal(ajax.responseXML);
		}
  	}
 	ajax.open("GET","galeria_modelo.php?idRegistro="+registro,true);
  	ajax.send(null);
}

function montaModal(xml){
	var i;
	galeria = xml.getElementsByTagName("imagem");
	if(galeria.length > 0){	
		showImage(0);
	}else{
		var div = document.getElementById("galeria");
		div.parentNode.removeChild(div);
	}
}

function showImage(pos){
	var image = document.getElementById("imgGaleria");
	image.src = galeria[pos].firstChild.nodeValue;
	document.getElementById("contGaleria").innerHTML = pos+1+"/"+galeria.length;
	if(galeria[pos].getAttribute("amp") !== null){
		$("#imgGaleria").bind('click',abreModal);
		$("#galeriaZoom").bind('click',abreModal);
		image.style.cursor = 'pointer';
		document.getElementById("galeriaZoom").style.display = '';
		document.getElementById("galeriaZoom").style.cursor = 'pointer';
	}else{
		$("#imgGaleria").unbind('click',abreModal);
		$("#galeriaZoom").unbind('click',abreModal);
		image.style.cursor = '';
		document.getElementById("galeriaZoom").style.display = 'none';
		document.getElementById("galeriaZoom").style.cursor = '';
	}
	posAtual = pos;
}

function abreModal(){
	var pos = posAtual;
	var i = galeria[pos].getAttribute("amp");
	tb_show(null,i,null);
}

function fechaModal(){
	var parent = document.getElementById("modalBox").parentNode;
	
	parent.removeChild(document.getElementById("modalBox"));
}
