/**
 * @version 20100217
 */


var idFotoUsuario = 2;


function _cambiarFoto() {
	if(imagenes[fotoActual]) {
		indiceActual = variables[fotoActual];
		if(!correcciones[fotoActual]) {
			document.getElementById('contenedorFotoPrincipal').innerHTML = '<a target="_blank" href="' + imagenes[fotoActual] + '.jpg"><img id="fotoPrincipal" src="' + imagenes[fotoActual] + '_big.jpg" title="' + etiquetas[fotoActual] + ': ' + notas[fotoActual] + '" alt="" /></a> <a target="_blank" href="' + imagenes[fotoActual] + '.jpg" style="position:absolute;bottom:5px;right:5px;"><img src="/img/zoom_in.png" title="Clic para aumentar" alt="Aumentar" style="width:16px;height:16px;" /> Aumentar</a>';
			if(notas[fotoActual].length == 0)
				document.getElementById('descripcionFoto').innerHTML = etiquetas[fotoActual];
			else
				document.getElementById('descripcionFoto').innerHTML = etiquetas[fotoActual] + ': ' + notas[fotoActual];
		}
		else {
			document.getElementById('contenedorFotoPrincipal').innerHTML = '<a target="_blank" href="' + imagenes[fotoActual] + '.jpg"><img id="fotoPrincipal" src="' + imagenes[fotoActual] + '_big.jpg" title="' + etiquetas[fotoActual] + ': ' + notas[fotoActual] + '" alt="" style="' + correcciones[fotoActual] + '" /></a> <a target="_blank" href="' + imagenes[fotoActual] + '.jpg" style="position:absolute;bottom:5px;right:5px;"><img src="/img/zoom_in.png" title="Clic para aumentar" alt="Aumentar" style="width:16px;height:16px;" /> Aumentar</a>';
			if(notas[fotoActual].length == 0)
				document.getElementById('descripcionFoto').innerHTML = etiquetas[fotoActual];
			else
				document.getElementById('descripcionFoto').innerHTML = etiquetas[fotoActual] + ': ' + notas[fotoActual];
		}
	}
}


function _cambiarFotoUsuarios() {
	if(fotosUsuarios[fotoActualUsuarios]) {
		document.getElementById('contenedorFotoUsuariosPrincipal').innerHTML = '<a target="_blank" href="' + fotosUsuarios[fotoActualUsuarios] + extensiones[fotoActualUsuarios] + '"><img id="fotoPrincipalUsuarios" src="' + fotosUsuarios[fotoActualUsuarios] + '_big.jpg" title="' + textosFotosUsuarios[fotoActualUsuarios] + '" alt="" /></a> <a target="_blank" href="' + fotosUsuarios[fotoActualUsuarios] + extensiones[fotoActualUsuarios] + '" style="position:absolute;bottom:5px;right:5px;"><img src="/img/zoom_in.png" title="Clic para aumentar" alt="Aumentar" style="width:16px;height:16px;" /> Aumentar</a>';
		document.getElementById('descripcionFotoUsuarios').innerHTML = textosFotosUsuarios[fotoActualUsuarios];
	}
}


function cambiarFotoPrincipal(nuevaFoto) {
	fotoActual = nuevaFoto;
	indiceActual = variables[fotoActual];
	_cambiarFoto();
}


function cambiarFotoPrincipalUsuarios(nuevaFoto) {
	fotoActualUsuarios = nuevaFoto;
	_cambiarFotoUsuarios();
}


function anterior() {
	if(indiceActual > 0) {
		indiceActual--;
		fotoActual = indices[indiceActual];
		_cambiarFoto();
	}
}


function anteriorUsuarios() {
	if(fotoActualUsuarios > 0) {
		fotoActualUsuarios--;
		_cambiarFotoUsuarios();
	}
}


function siguiente() {
	if(indiceActual < indices.length-1) {
		indiceActual++;
		fotoActual = indices[indiceActual];
		_cambiarFoto();
	}
}


function siguienteUsuarios() {
	if(fotoActualUsuarios < fotosUsuarios.length-1) {
		fotoActualUsuarios++;
		_cambiarFotoUsuarios();
	}
}


function agregarFoto() {
	$('#contenedorInputsFile').append('<br /><p>Fotograf&iacute;a: <input type="file" id="foto'+idFotoUsuario+'" name="foto'+idFotoUsuario+'" /><br />Comentario: <input type="text" id="texto'+idFotoUsuario+'" name="texto'+idFotoUsuario+'" size="45" /></p>');
	idFotoUsuario++;
	$('#subirFotosBoton').attr('value', 'Subir fotografías');
}


function validarFormularioFotos() {
	var email = $('#emailFotos').get(0);
	if(!validaEmail(email.value)) {
		alert('Por favor, introduce una dirección de correo válida.');
		email.focus();
		return false;
	}
	if(obj('numeroFotos').value == '' || obj('numeroFotos').value == 0) {
		alert('Aún no ha cargado ninguna fotografía.');
		return false;
	}
	
	return true;
}


var xmlhttp;
function loadDivHTML(accion, url, divId) {
	if(window.XMLHttpRequest){		// code for Mozilla, etc.
		if(xmlhttp){
			xmlhttp.abort;
		}
		xmlhttp = new XMLHttpRequest();
	}
	else{
		if(window.ActiveXObject){	// code for IE
            try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
			catch(e){
                try{
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
				catch(e){
					alert("Name:\t" + e.name + "\nMessage:\t" + e.message);
				}
            }
		}
	}
    if(xmlhttp){
    	xmlhttp.open("GET", url, false);
        xmlhttp.send(null);
        
		if(accion == "listaArchivos"){
            checkLista(divId);
		}
		if(accion == "deleteMedia"){
            deleteMedia(divId);
		}
    }
	else{
    	alert('Giving up :( Cannot create an XMLHTTP instance');
    	return false;
	}
	return 0;
}


function checkLista(divId) {
    if(xmlhttp.responseText) {
        obj(divId).innerHTML = xmlhttp.responseText;
    }
	return 0;
}


function deleteMedia(divId) {
	if(xmlhttp.responseText) {
        var jsonObj = eval("(" + xmlhttp.responseText + ")");
        if(jsonObj.msgError == "YES") {
			obj('numeroFotos').value = parseInt(obj('numeroFotos').value) - 1;
			if(obj('numeroFotos').value == '0') {
				obj('subirFotosBoton').disabled = 'disabled';
			}
			loadDivHTML("listaArchivos", "/admin/services/listaArchivosSitioParques.php?pseudo=" + obj('pseudo').value, divId);
        }
        else{
            alert(jsonObj.msgError);
        }
    }
}


function init() {
	$('#formularioFotos').get(0).onsubmit = validarFormularioFotos;
	// Recargando las imagenes para asegurarse que carguen
	$('div#logo img').attr('src', '').attr('src', 'img/logoParques.jpg');
	$('div#encabezado').css('background', 'none').css('background', "url('img/encabezado.jpg') no-repeat");
}


// Inicializando
jQuery(document).ready(init);


