var fotos_per_pag = 10;
var pagina_actual = 0;
var total_pagines = 0;

function fotos_inicials() {
	veure_fotos(0);
}

function veure_fotos(primera) {
	//neteja(document.getElementById('cont_fotos'));
	displayLoading(document.getElementById('cont_fotos'));
	pilla_fotos(primera);
}

function neteja(nodo) {
	/*if (nodo.hasChildNodes) {
		for (var i=0; i<nodo.childNodes.length; i++) {
			alert(nodo.childNodes.length);
			nodo.removeChild(nodo.childNodes[0]);
		}
	}*/
	if(nodo.hasChildNodes) {
	while(nodo.childNodes.length>0) {
		nodo.removeChild(nodo.lastChild);
	}
	}
}

function pilla_fotos(foto_inicial) {
	var xmlHttp = createXmlHttpRequestObject();
	try {
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4) {
				if (xmlHttp.status==200) {
					pagina_actual = Math.floor(foto_inicial/fotos_per_pag)+1;
					inserta_fotos(xmlHttp.responseXML);
					//initLightbox();
					myLightbox.updateImageList();
				}
			}
		}
		xmlHttp.open('GET', '../scripts/php/galeria_xml.php?desde='+foto_inicial+'&fotos_pag='+fotos_per_pag);
		xmlHttp.send(null);
	} catch (e) {
		alert('No es pot connectar amb el servidor '+e.toString());
	}	
}

function inserta_fotos(xml) {
	total_pagines = Math.floor(parseInt(xml.getElementsByTagName('resultats')[0].getAttribute('total'))/fotos_per_pag)+1;
	var contenidor = document.getElementById('cont_fotos');
	var fotos = xml.getElementsByTagName('foto');
	neteja(contenidor);
	ruta = '../imatges/galeria/';
	var llista_fotos = crea_element('ul', 'llista_fotos', '');
	for(var i=0;i<fotos.length;i++) {
		var li = crea_element('li', '', '');
		var a = crea_element('a', '', '');
		a.setAttribute('href', ruta+fotos[i].getAttribute('imatge'));
		a.href = ruta+fotos[i].getAttribute('imatge');
		a.setAttribute('rel', 'lightbox[galeria]');
		a.rel = 'lightbox[galeria]';
		a.setAttribute('title', 'Autor: '+fotos[i].getAttribute('autor'));
		a.title = 'Autor: '+fotos[i].getAttribute('autor');
		var img = crea_element('img', '', 'thumb');
		img.setAttribute('src', ruta+'t_'+fotos[i].getAttribute('imatge'));
		img.src = ruta+'t_'+fotos[i].getAttribute('imatge');
		img.setAttribute('alt', 'Autor: '+fotos[i].getAttribute('autor'));
		img.alt = 'Autor: '+fotos[i].getAttribute('autor');
		a.appendChild(img);
		li.appendChild(a);
		llista_fotos.appendChild(li);
	}
	contenidor.appendChild(llista_fotos);
	gestiona_pagines();
}

function crea_element(tag, id, classe) {
	var el = document.createElement(tag);
	if (id!='') {
		el.setAttribute('id', id);
		el.id = id;
	}
	if (classe!='') {
		el.setAttribute('class', classe);
		el.className = classe;
	}
	return el;
}

function paginacio_sig() {
	var p_sig = document.getElementById('pag_siguiente');
		var foto_inicial = (pagina_actual)*fotos_per_pag;
		pilla_fotos(foto_inicial);
		return false;
}
function paginacio_ant() {
	p_ant = document.getElementById('pag_anterior');
		var foto_inicial = (pagina_actual-2)*fotos_per_pag;
		pilla_fotos(foto_inicial);
		return false;
}

function gestiona_pagines() {
	var p_ant = document.getElementById('pag_anterior');
	var p_sig = document.getElementById('pag_siguiente');
	if(pagina_actual==1) {
		desactiva(p_ant);
		activa(p_sig);
	} else if(pagina_actual==total_pagines) {
		desactiva(p_sig);
		activa(p_ant);
	} else {
		activa(p_ant);
		activa(p_sig);
	}
}

function desactiva(nodo) {
	nodo.style.display = 'none';
}

function activa(nodo) {
	nodo.style.display = 'block';
}