var xmlDoc
rutaServer = ''
function cargarXML_(ruta, doc){
	if (window.ActiveXObject)  {
	  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async=false;
	  xmlDoc.load(ruta + doc);
	  
	  //getmessage()
	  }
// code for Mozilla, etc.
	else if (document.implementation && document.implementation.createDocument){
	  xmlDoc= document.implementation.createDocument("","",null);
	  xmlDoc.load(ruta + doc);
	 // xmlDoc.onload=getmessage
	  }
	else{
	  alert('Your browser cannot handle this script');
	  }
}


function cargarXML(ruta, doc){
	var error = "";
	var file = ruta + doc;
	try{ //Internet Explorer
	 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	 xmlDoc.async=false;
	 xmlDoc.load(file);
	}
	catch(e){
		try{ //Firefox, Mozilla, Opera, etc.
			xmlDoc=document.implementation.createDocument("","",null);
			xmlDoc.async=false;
			xmlDoc.load(file);
		}
		catch(e){
			try{ //Google Chrome
				var xmlhttp = new window.XMLHttpRequest();
				xmlhttp.open("GET",file,false);
				xmlhttp.send(null);
				xmlDoc = xmlhttp.responseXML.documentElement;
			}
			catch(e){
				error=e.message;
			}
		}
	}
}

numImagen = 0;
function cambiaImagen(c){
	//alert('entro')
	c == '+' ? numImagen++ : numImagen--;
	indis = xmlDoc.getElementsByTagName('imagen').length;
	numImagen >= indis ? numImagen = 0: '';
	numImagen < 0 ? numImagen = indis-1: '';
	document.getElementById("ima").src=rutaServer + xmlDoc.getElementsByTagName("imagen")[numImagen].getAttribute('src').substr(2)
	document.getElementById("nFoto").firstChild.nodeValue = numImagen + 1
 //alert(xmlDoc.getElementsByTagName('imagen').length)
/*alert(document.getElementById("ima").src)
document.getElementById("tipo").innerHTML=xmlDoc.getElementsByTagName("tipo")[cont].firstChild.nodeValue
document.getElementById("descripcion").innerHTML=xmlDoc.getElementsByTagName("descripcion")[cont].firstChild.nodeValue*/
}
function cambiaImagenNota(c){
	c == '+' ? numImagen++ : numImagen--;
	indis = xmlDoc.getElementsByTagName('imagen').length;
	numImagen >= indis ? numImagen = 0: '';
	numImagen < 0 ? numImagen = indis-1: '';
	document.getElementById("imagen").src=xmlDoc.getElementsByTagName("imagen")[numImagen].getAttribute('src')
	document.getElementById("nFoto").firstChild.nodeValue = numImagen + 1
	if(xmlDoc.getElementsByTagName("comentario")[numImagen].hasChildNodes)
	document.getElementById("nota_foto").firstChild.nodeValue = xmlDoc.getElementsByTagName("comentario")[numImagen].firstChild.nodeValue;
	else document.getElementById("nota_foto").firstChild.nodeValue = ""
}
function enviaForm(f,campos){

	campos = campos.split(',')

	for(m=0; m<campos.length; m++){

		campo = eval('f.' + campos[m])

		if(campo.value == ''){

			alert('Por favor, rellene el campo: ' + campo.name)
			campo.focus()

			return false

		}

	}

}
var scroll_top = function(){

	if(window.pageYOffset){
		return window.pageYOffset;
	 }
	 else {
		return  Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	 }

}
id_contenedor_foto = 'ficha_animal';
function ampliarFotos(){
	if(!document.getElementById('cerrar')){
		_top = scroll_top()
		con = document.getElementById('contenedor_foto')
		con.className = 'ampliarImagen'
		if(document.getElementById('form_adoptame')) document.getElementById('form_adoptame').style.display = 'none'
		//con.parentNode.style.overflow = 'visible'
		document.getElementById(id_contenedor_foto).style.overflow = 'visible'
		with(con.style){
			//position = 'absolute';
			//top = _top + "px"
		}
		con_cerrar = document.createElement('span')
		con_cerrar.id = 'cerrar'
		con_cerrar.title = 'cerrar ventana'
		con_cerrar.onclick = restaurarFotos
		con_cerrar.appendChild(document.createTextNode('Cerrar'))
		con.insertBefore(con_cerrar,document.getElementById(idImagen));
		document.getElementById(idImagen).title = ''
		document.getElementById(idImagen).style.cursor = 'default'
		if(document.getElementById('video_nota')) document.getElementById('video_nota').style.display = 'none';
		if(document.getElementById('video')) document.getElementById('video').style.display = 'none';
	}
}
function restaurarFotos(){
	con = document.getElementById('contenedor_foto')
	con.removeChild(document.getElementById('cerrar'))
	con.className = ''
	if(document.getElementById('form_adoptame')) document.getElementById('form_adoptame').style.display = 'block'
	if(document.getElementById('video')) document.getElementById('video').style.display = 'block';
	if(document.getElementById('video_nota')) document.getElementById('video_nota').style.display = 'block';
	//con.parentNode.style.overflow = 'hidden'
	document.getElementById(id_contenedor_foto).style.overflow = 'hidden'
	document.getElementById(idImagen).title = 'Click para ver a tamaņo real'
	document.getElementById(idImagen).style.cursor = 'pointer'
}
function linkVolver(){
	lks = document.getElementsByTagName('a')
	l = lks.length;
	for(m in lks){
		if(lks[m].className == 'volver'){
			lks[m].onclick= function() {
				history.back();
				return false;
			}
		}
	}
	
}
/************ NUEVO ENERO 2012 *****************/
var indis;
function fotoFicha(){
	if(document.getElementById('contenedor_foto')){
		idImagen = document.getElementById('imagen') ? 'imagen' : 'ima'
		document.getElementById(idImagen).style.cursor = 'pointer'
		document.getElementById(idImagen).title = 'Click para ver a tamaņo real'
	/*	document.getElementById(idImagen).onclick = ampliarFotos*/
		document.getElementById(idImagen).onclick = lanzarVisor
	
	}
}
function fotoSeguimiento(){
	if(document.getElementById('contenedor_foto')){
		idImagen = document.getElementById('imagen') ? 'imagen' : 'ima'
		document.getElementById(idImagen).style.cursor = 'pointer'
		document.getElementById(idImagen).title = 'Click para ver a tamaņo real'
	/*	document.getElementById(idImagen).onclick = ampliarFotos*/
		document.getElementById(idImagen).onclick = lanzarVisorSeguimiento
	
	}
}

function lanzarVisor(){
	//alert('yuju');
	indis = xmlDoc.getElementsByTagName('imagen').length;
	lanzaPantalla('ima');
}
function lanzarVisorSeguimiento(){
	//alert('yuju');
	indis = xmlDoc.getElementsByTagName('imagen').length;
	lanzaPantalla('imagen');
}


function lanzaPantalla(idf){
	pantalla = document.createElement('div');
	pantalla.id = 'pantalla';
	altura = window.innerHeight == 'undefined' ? document.body.clientHeight : window.innerHeight;
	anchura = window.innerWidth == 'undefined' ? document.body.clientWidth : window.innerWidth;
	pantalla.style.height = (altura - 2) + 'px';
	//pantalla.style.width = (anchura - 2) + 'px';
	pos_top = window.pageYOffset; pos_left = window.pageXOffset;
	
	
	
	
	with(pantalla.style){
		top = pos_top + "px";
		left = '0px';
	}
	
	pantalla.onclick =removerPantalla;
	
	
	barra = document.createElement('div');
	barra.id = 'bmb_barra';
	
	caja_cont = document.createElement('div');
	cont = document.createElement('span');
	cont.id = 'bmb_contador';
	
	cont_cerrar = document.createElement('span');
	cont_cerrar.id = 'bmb_cerrar';
	cont_cerrar.appendChild(document.createTextNode(' X '));
	
	caja_cont.appendChild(document.createTextNode('Foto '));
	cont.appendChild(document.createTextNode( numImagen +1));
	caja_cont.appendChild(cont);
	caja_cont.appendChild(document.createTextNode(' de ' + indis));
	
	caja_cont.appendChild(cont_cerrar);
	
	
	barra.appendChild(caja_cont);
	pantalla.appendChild(barra);
	
	if(idf == 'galeria') foto = lanzaFotoGal();
	else foto = lanzaFoto(idf);
	
	if(idf == 'galeria'){
		titulo = lanzaTituloFoto();
		foto.appendChild(titulo);
		
		texto = lanzaTextoFoto();
		foto.appendChild(texto);
		
		link_navegacion = document.createElement('div');
		link_navegacion.id = 'bmb_link_galeria';
		
		link_galeria = document.createElement('a');
		link_galeria.href= permalink;
		link_galeria.appendChild(document.createTextNode(titulo_galeria));
		
		link_navegacion.appendChild(link_galeria);
		
				
		link_navegacion.appendChild(document.createTextNode(' >> '));
		
		link_foto = document.createElement('a');
		link_foto.id = "bmb_link_foto";
		link_foto.href= foto_gal_actual.permalink;
		titulo_foto = foto_gal_actual.titulo ? unescape(foto_gal_actual.titulo) : foto_gal_actual.nombre;
		link_foto.appendChild(document.createTextNode(titulo_foto));
		
		link_navegacion.appendChild(link_foto);
		
		caja_cont.appendChild(link_navegacion);
		
		cont_like_fb = lanzaLikeFb();
		foto.appendChild(cont_like_fb);
	}
	
	if(idf == 'imagen'){
		texto = lanzaTextoFotoSeguimiento();
		foto.appendChild(texto);
	}
	
	pantalla.appendChild(foto);
	
	
	if(indis > 1){
		boton_atras = lanzaBotonNavFoto('btn_atras', retrocedeImagen);
		boton_adelante = lanzaBotonNavFoto('btn_adelante', avanzaImagen);
		pantalla.appendChild(boton_atras);
		pantalla.appendChild(boton_adelante);
	}
	
	
	//document.getElementById('general').appendChild(pantalla);
	document.body.appendChild(pantalla);
	document.body.style.overflow ='hidden';
	ev(document,'keydown',keyEscape)
	
	

	//alert("pageXOffset: " + window.pageXOffset + ", pageYOffset: " + window.pageYOffset)
}
function keyEscape(evt){
		evt = evt || window.event;
		if (evt.keyCode == 27) {
		   removerPantalla();
		}
}
function removerPantalla(){
	//document.getElementById('general').removeChild(document.getElementById('pantalla'))
	document.body.removeChild(document.getElementById('pantalla'))
	document.body.style.overflow ='auto';
	rev(document,'keydown',keyEscape)
}

function lanzaFoto(idf){
	con_foto = document.createElement('div');
	con_foto.id = 'bmb_contenedor_foto';
	//con_foto.addEventListener('click', clic);
	//con_foto.addEventListener('click', avanzaImagen);
	ev(con_foto,'click',clic)
	if(indis > 1){
		ev(con_foto,'click',avanzaImagen)
	}
	
	foto = document.createElement('img');
	foto.id = 'bmb_ima';
	foto.src = document.getElementById(idf).src
	foto.lowsrc = 'imas/cargando.gif';
	con_foto.appendChild(foto);
	return con_foto;
}
function lanzaTextoFoto(){
	texto = document.createElement('div');
	texto.id = 'bmb_texto';
	texto.appendChild(document.createTextNode(unescape(foto_gal_actual.texto))); 
	texto.innerHTML = unescape(foto_gal_actual.texto); 
	
	return texto;
}
function lanzaTextoFotoSeguimiento(){
	texto = document.createElement('div');
	texto.id = 'bmb_texto';
	
	if(xmlDoc.getElementsByTagName("comentario")[numImagen].hasChildNodes)
			texto_nota = xmlDoc.getElementsByTagName("comentario")[numImagen].firstChild.nodeValue;
	else texto_nota = ""
	
	texto.appendChild(document.createTextNode(texto_nota)); 

	return texto;
}

function lanzaBotonNavFoto(id, fnc){
	boton = document.createElement('div');
	boton.id = id;
	//boton.addEventListener('click', clic);
	//boton.addEventListener('click', fnc);
	if(indis > 1){
		ev(boton,'click',clic)
		ev(boton,'click',fnc)
	}
	return boton;
}
function cambiarImagen(c){
	//alert('entro')
	c == '+' ? numImagen++ : numImagen--;
	//indis = xmlDoc.getElementsByTagName('imagen').length;
	numImagen >= indis ? numImagen = 0: '';
	numImagen < 0 ? numImagen = indis-1: '';
	//fuente_foto = rutaServer + xmlDoc.getElementsByTagName("imagen")[numImagen].getAttribute('src').substr(2)
	//idImagen = document.getElementById('imagen') ? 'imagen' : 'ima'
	if(document.getElementById('imagen')){
		idImagen = 'imagen';
		fuente_foto = xmlDoc.getElementsByTagName("imagen")[numImagen].getAttribute('src');
		
		if(xmlDoc.getElementsByTagName("comentario")[numImagen].hasChildNodes)
			texto_nota = xmlDoc.getElementsByTagName("comentario")[numImagen].firstChild.nodeValue;
		else texto_nota = ""
		
		document.getElementById("bmb_texto").firstChild.nodeValue = texto_nota;
	}
	else{
		idImagen = 'ima';
		fuente_foto = rutaServer + xmlDoc.getElementsByTagName("imagen")[numImagen].getAttribute('src').substr(2);
	}
	document.getElementById(idImagen).src= fuente_foto;
	document.getElementById("bmb_ima").src= fuente_foto
	document.getElementById("nFoto").firstChild.nodeValue = numImagen + 1
	document.getElementById("bmb_contador").firstChild.nodeValue = numImagen + 1

//alert(xmlDoc.getElementsByTagName('imagen').length)
/*alert(document.getElementById("ima").src)
document.getElementById("tipo").innerHTML=xmlDoc.getElementsByTagName("tipo")[cont].firstChild.nodeValue
document.getElementById("descripcion").innerHTML=xmlDoc.getElementsByTagName("descripcion")[cont].firstChild.nodeValue*/
}
function avanzaImagen(){
	 cambiarImagen('+')
}
function retrocedeImagen(){
	cambiarImagen('-')
}
function clic(e){ 
    if (e === undefined)
        e = window.event;
    if (e !== undefined){
        if (e.preventDefault !== undefined) { e.preventDefault();  ;}
        else if (e.returnValue !== undefined) { e.returnValue = false;}
        if (e.cancelBubble !== undefined) { e.cancelBubble = true; }
    }
}
function posY(){
	pos_top = window.pageYOffset;
	document.getElementById('pantalla').style.top = pos_top + 'px';
}
function ev(x,y,z) { 
	if (document.addEventListener){ 
		x.addEventListener(y,z,false);
	} else { 
		x.attachEvent('on'+y,z); 
	}
}
function rev(x,y,z) { 
	if (document.addEventListener){ 
		x.removeEventListener(y,z,false);
	} else { 
		x.detachEvent('on'+y,z); 
	}
}

/************ FIN NUEVO ENERO 2012 *****************/

function formBuscar(f){
	for(m = 1; m < f.length; m++){
		if(!f[m].value || f[m].value == 0){
			f[m].disabled = true;
			
		} 
	}
		return true;
}

onload = linkVolver;
