/**
 * @version 20100319
 */


function obj(id)
{
    return document.getElementById(id);
}


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


function cambiarFotoPrincipal(nuevaFoto) {
	fotoActual = nuevaFoto;
	_cambiarFoto();
}


function anterior() {
	if(fotoActual > 0) {
		fotoActual--;
		_cambiarFoto();
	}
}


function siguiente() {
	if(fotoActual < imagenes.length-1) {
		fotoActual++;
		_cambiarFoto();
	}
}


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() {
	// Asignando los eventos para los botones para cambiar fotografias
	$('#anterior').hover(function() { this.src = 'img/anteriorVertical2.gif'; }, function() { this.src = 'img/anteriorVertical1.gif'; });
	$('#siguiente').hover(function() { this.src = 'img/siguienteVertical2.gif'; }, function() { this.src = 'img/siguienteVertical1.gif'; });
	
	// Asignando los eventos para los botones para cambiar fotografias
	$('#anterior').click(anterior);
	$('#siguiente').click(siguiente);
	
	// Asignando los eventos para los botones para cambiar las paginas de resultados
	$('#anteriorPagina').hover(function() { this.src = 'img/anteriorPagina2.gif'; }, function() { this.src = 'img/anteriorPagina1.gif'; });
	$('#siguientePagina').hover(function() { this.src = 'img/siguientePagina2.gif'; }, function() { this.src = 'img/siguientePagina1.gif'; });
	
	// Asignando la funcion que valida los datos del formulario
	$('#formularioFotos').get(0).onsubmit = validarFormularioFotos;
	
	// Asignando las funciones para habilitar o deshabilitar el campo del nombre
	$('#anonimo').click(function() {
			if($('#nombre').attr('disabled')) {
				$('#nombre').removeAttr('disabled');
				this.checked = false;
			}
			else {
				$('#nombre').attr('disabled',true).attr('value', '');
				this.checked = true;
			}
	});
	
	// 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);

