function veure_acte_inicial() {
	veure_actes(5);
}

function veure_actes(id_dia) {
	neteja(document.getElementById('info'));
	pilla_dades_dia(id_dia);
}

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

function pilla_dades_dia(id) {
	var xmlHttp = createXmlHttpRequestObject();
	try {
		displayLoading(document.getElementById('info'));
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4) {
				if (xmlHttp.status==200) {
					inserta_actes(xmlHttp.responseXML);
				}
			}
		}
		xmlHttp.open('GET', '../scripts/php/actes_xml.php?id_dia='+id+'&lang='+idioma);
		xmlHttp.send(null);
	} catch (e) {
		alert('No es pot connectar amb el servidor '+e.toString());
	}	
}

function inserta_actes(res) {
	var info = document.getElementById('info');
	neteja(info);
	var nodo_dia = res.getElementsByTagName('dia')[0];
	inserta_titol_dia(nodo_dia.getAttribute('nom'), info);
	var actes = res.getElementsByTagName('acte');
	for(var i=0; i<actes.length; i++) {
		inserta_acte_individual(actes[i], info);
	}
}

function inserta_titol_dia(nom, nodo) {
	var h2 = document.createElement('h2');
	var txt = document.createTextNode(nom);
	h2.appendChild(txt);
	nodo.appendChild(h2);
}

function inserta_acte_individual(acte, nodo) {
	var div = crea_element('div', 'acte');
	var h3 = crea_element('h3', '-');
	var span_hora_acte = crea_element('span', 'hora_acte');
	var span_hora_numero = crea_element('span', 'hora_numero');
	var txt_hora_numero = document.createTextNode(acte.getAttribute('hora'));
	span_hora_numero.appendChild(txt_hora_numero);
	var txt_hora = document.createTextNode('hora: ');
	span_hora_acte.appendChild(txt_hora);
	span_hora_acte.appendChild(span_hora_numero);
	var span_nom_acte = crea_element('span', 'nom_acte');
	var txt_nom_acte = document.createTextNode(acte.getAttribute('nom'));
	span_nom_acte.appendChild(txt_nom_acte);
	h3.appendChild(span_hora_acte);
	h3.appendChild(span_nom_acte);
	div.appendChild(h3);
	if(acte.getAttribute('foto')!='-') {
		var img = crea_element('img', '-');
		img.setAttribute('src', '../imatges/actes/'+acte.getAttribute('foto'));
		img.setAttribute('alt', acte.getAttribute('nom'));
		div.appendChild(img);
	}
	var paragrafs = acte.getElementsByTagName('p');
	for (var i=0; i<paragrafs.length; i++) {
		var p = crea_element('p', '-');
		var txt_p = document.createTextNode(paragrafs[i].firstChild.nodeValue);
		p.appendChild(txt_p);
		div.appendChild(p);
	}
	switch(idioma) {
		case 'val':
			txt_itinerari = 'itinerari';
			break;
		case 'es':
			txt_itinerari = 'itinerario';
			break;
	}
	if (acte.getAttribute('mapa')!='-') {
		var a = crea_element('a', 'link_mapa');
		var span_itinerari = crea_element('span', 'itinerari');
		var txt_itinerari = document.createTextNode(txt_itinerari);
		span_itinerari.appendChild(txt_itinerari);
		a.appendChild(span_itinerari);
		div.appendChild(a);
		var ruta = 'http://localhost/festers.net/mapes/mostra_mapa.php?mapa='+acte.getAttribute('mapa');
		a.setAttribute('onClick', "obri_finestra('"+ruta+"', "+acte.getAttribute('mapa_w')+", "+acte.getAttribute('mapa_h')+")");
	}
	nodo.appendChild(div);
}

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

function obri_finestra(ruta, width, height) {
	if (width==0 || height==0) {
		alert('No es pot mostrar l\'itinerari');
	} else {
		var w = window.open(ruta, 'mapa', 'toolbar=0, location=0, directories=0, status=0, menubar=1, scrollbars=0, resizable=0, width='+width+', height='+height);
	}
	return false;
}
