function neteja_nodo(element) {
	while(element.hasChildNodes()) {
		element.removeChild(element.lastChild);
	}
}

function onclick_comparses() {
	var menu = document.getElementById('menu_comparses');
	var links = menu.getElementsByTagName('a');
	for(var i=0; i<links.length; i++) {
		var a = links[i];
		a.onclick = pilla_dades_comparsa;
	}
}

function pilla_dades_comparsa() {
	var id_comparsa = this.getAttribute('id').substring(9);
	var xmlHttp = createXmlHttpRequestObject();
	try {
		displayLoading(document.getElementById('info'));			
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4) {
				if (xmlHttp.status==200) {
					canvia_comparsa(xmlHttp.responseXML);
				}
			}
		}
		query_str = 'id_comparsa='+id_comparsa+'&lang='+idioma;
		xmlHttp.open('POST', '../scripts/php/comparses_xml.php');
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send(query_str);
	} catch (e) {
		alert('No es pot connectar amb el servidor '+e.toString());
	}
	return false;	
}

function canvia_comparsa(xml) {
	var comparsa = xml.getElementsByTagName('comparsa')[0];
	var id_comparsa = comparsa.getAttribute('id_comparsa');
	var nom = comparsa.getAttribute('nom');
	var escut = comparsa.getAttribute('escut');
	var vestit = comparsa.getAttribute('vestit');
	var web = comparsa.getAttribute('web');
	var email = comparsa.getAttribute('email');
	//var fundacio = comparsa.getElementsByTagName('fundacio')[0].firstChild.nodeValue;
	var historia = comparsa.getElementsByTagName('historia')[0].firstChild.nodeValue;
	comparsa_actual(id_comparsa);
	canvia_dades_comparsa(nom, escut, vestit, historia, web, email);
}

function canvia_dades_comparsa(nom_c, escut_c, vestit_c, historia_c, web_c, email_c) {
	var img_escut = document.getElementById('cab_col3').getElementsByTagName('img')[0];
	img_escut.setAttribute('src', '../imatges/comparses/escuts/'+escut_c+'.jpg');
	var img_vestit = document.getElementById('foto_secundaria').getElementsByTagName('img')[0];
	img_vestit.setAttribute('src', '../imatges/comparses/vestits/'+vestit_c+'.jpg');
	var info = document.getElementById('info');
	neteja_nodo(info);
	var h2 = document.createElement('h2');
	var h2_txt = document.createTextNode(nom_c);
	h2.appendChild(h2_txt);
	info.appendChild(h2);
	var img_foto = document.createElement('img');
	img_foto.setAttribute('src', '../imatges/comparses/fotos/f_'+escut_c+'.jpg');
	img_foto.setAttribute('alt', 'Comparsa '+nom_c);
	info.appendChild(img_foto);
	var paragrafs = historia_c.split(/\n\r|\r\n|\n/);
	for(var i=0; i<paragrafs.length; i++) {
		var p = document.createElement('p');
		if (i==0) { p.setAttribute('id', 'paragraf1'); }
		var text = document.createTextNode(paragrafs[i]);
		p.appendChild(text);
		info.appendChild(p);
	}
	var div_contacte = document.createElement('div');
	div_contacte.setAttribute('id', 'contacte_comparsa');
	var a_web = document.createElement('a');
	if(web_c=='') {
		a_web.setAttribute('href', '');
		a_web.appendChild(document.createTextNode('-'));
	} else {
		a_web.setAttribute('href', 'http://'+web_c);
		a_web.setAttribute('title', 'Visitar la web de la comparsa');
		a_web.setAttribute('id', 'web_comparsa');
		a_web.appendChild(document.createTextNode(web_c));
	}
	var a_email = document.createElement('a');
	if(email_c=='') {
		a_email.setAttribute('href', '');
		a_email.appendChild(document.createTextNode('-'));
	} else {
		a_email.setAttribute('href', 'mailto:'+email_c);
		a_email.setAttribute('title', 'Enviar email');
		a_email.setAttribute('id', 'email_comparsa');
		a_email.appendChild(document.createTextNode(email_c));
	}
	div_contacte.appendChild(a_web);
	div_contacte.appendChild(a_email);
	info.appendChild(div_contacte);
}

function comparsa_actual(id) {
	var menu_comparses = document.getElementById('menu_comparses');
	var links = menu_comparses.getElementsByTagName('a');
	for(var i=0; i<links.length; i++) {
		var id_link = links[i].getAttribute('id');
		if(links[i].getAttribute('class')=='comparsa_actual' || links[i].className=='comparsa_actual') {
			links[i].setAttribute('class', '');
			links[i].className = '';
		}
		if(id_link=='comparsa_'+id) {
			links[i].className = 'comparsa_actual';
			links[i].setAttribute('class', 'comparsa_actual');
		}
	}
}
