// JavaScript Document
var time;
function get(parametri_get) {
	
	var ajax=null;
	
	if (window.XMLHttpRequest) {
		ajax=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		ajax=new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	//ricavo id_cartella
		var parte1 = parametri_get.split('&');
		var parte2 = parte1[0].split('=');
		id_cartella = parte2[1];
	
	ajax.onreadystatechange=function () {
		switch (ajax.readyState) {
			case 0:
				alert('errore');
			break;
			case 1:
				var tabella = document.getElementById('tabelladocumenti');
				
				//recupero tutti i <tr> della tabella per eliminarli tranne il primo
				var n_tr = tabella.getElementsByTagName("tr").length;
				//cancello i dati	
				for (var i=n_tr; i > 1; i--) {
					tabella.deleteRow(i-1); //li cancello
				}		
				
				// Insert a row in the table at row index 0
				var newRow  = tabella.insertRow(-1);
			
				// Insert a cell in the row at index 0
				var newCell = newRow.insertCell(-1);				
				newCell.setAttribute('colspan',3);
				newCell.innerHTML = '<p><img src="../../img/loading.gif" /> Caricamento in corso...</p>';
				
				
			break;
			case 4:
				try { 
					if (time) {
						clearTimeOut(time);
					}
				} catch (e) {}
				//carico e stampo i dati
				var xml = ajax.responseXML;
				var cartelle = xml.getElementsByTagName('cartella');
				var file = xml.getElementsByTagName('file');
				var tabella = document.getElementById('tabelladocumenti');
				
				//cancello il <tr> del caricamento in corso
				tabella.deleteRow(1);
				//visualizzo le cartelle
				for (var i=0; i < cartelle.length; i++) {
					//link
					var link = '<a class="link" onclick="get(\'cartella='+cartelle.item(i).getElementsByTagName('id').item(0).firstChild.nodeValue+'\')">';
					var stop_link = '</a>';
					
					//Inserisco un <tr>
					var newRow  = tabella.insertRow(-1);
				
					//Inserisco celle
					var cella_img = newRow.insertCell(-1);				
					cella_img.innerHTML = link+'<img src="img/cartella.png" width="80"/>'+stop_link;
					
					var cella_nome = newRow.insertCell(-1);
						var nome = '<p class="nome">'+link+cartelle.item(i).getElementsByTagName('nome').item(0).firstChild.nodeValue+stop_link+'</p>';
						try {
							var descrizione = '<p>'+link+cartelle.item(i).getElementsByTagName('descrizione').item(0).firstChild.nodeValue+stop_link+'</p>';	
						} catch (e) {
							var descrizione = '';
						}
					cella_nome.innerHTML = nome+descrizione;
				}
				
				//visualizzo i file
				for (var i=0; i < file.length; i++) {
					//link
					var link = '<a class="link" onclick="vedi('+file.item(i).getElementsByTagName('id').item(0).firstChild.nodeValue+')">';
					var stop_link = '</a>';
					
					//Inserisco un <tr>
					var newRow  = tabella.insertRow(-1);
				
					//Inserisco celle
					var cella_img = newRow.insertCell(-1);				
					cella_img.innerHTML = link+'<img src="'+file.item(i).getElementsByTagName('urlthumb').item(0).firstChild.nodeValue+'" width="120" />'+stop_link;
					
					var cella_nome = newRow.insertCell(-1);
						try {
							var nome = '<p class="nome">'+link+file.item(i).getElementsByTagName('nome').item(0).firstChild.nodeValue+stop_link+'</p>';
						} catch (e) {
							var nome = '';
						}
						
						try {
							var descrizione = '<p>'+link+file.item(i).getElementsByTagName('descrizione').item(0).firstChild.nodeValue+stop_link+'</p>';	
						} catch (e) {
							var descrizione = '';
						}
					cella_nome.innerHTML = nome+descrizione;
					
					var cella_media = newRow.insertCell(-1);				
						var media = file.item(i).getElementsByTagName('media').item(0).firstChild.nodeValue;
						//preparo stelline
						var txt_media = '';
						for (var n=0; n < 5; n++) {
							if (n < media) {
								txt_media = txt_media+'<img src="img/star.gif" width="20" title="Voto" />';
							} else {
								txt_media = txt_media+'<img src="img/star_white.gif" width="20" title="Voto" />';
							}
						}
						var voti = file.item(i).getElementsByTagName('voti').item(0).firstChild.nodeValue;
					cella_media.innerHTML = '<p>'+txt_media+'<br />N. Voti: '+voti+'</p>';
				}
				//aggiorno i link della pagina
				document.getElementById('ordina_nome').onclick= function() {get('cartella='+id_cartella+'&ordine=nome');}
				document.getElementById('ordina_download').onclick= function() {get('cartella='+id_cartella+'&ordine=download');}
				document.getElementById('ordina_voti').onclick= function() {get('cartella='+id_cartella+'&ordine=voti');}
				document.getElementById('ordina_top').onclick= function() {get('cartella='+id_cartella+'&ordine=top');}
				document.getElementById('linkrss').href= 'myrss.php?sezione=cartella&id='+id_cartella;
				document.getElementById('rss_google').href= 'http://fusion.google.com/add?feedurl=http%3A//zannas.netsons.org/myrss.php%3Fsezione%3Dcartella%26id%3D'+id_cartella;
				document.getElementById('rss_yahoo').href= 'http://us.rd.yahoo.com/my/atm/Zannas/Ultimi%20file%20inseriti%20in%20questa%20cartella/*http://add.my.yahoo.com/rss?url=http%3A//zannas.netsons.org/myrss.php%3Fsezione%3Dcartella%26id%3D'+id_cartella;
				document.getElementById('rss_to_pdf').href= 'http://rss2pdf.com?url=http%3A//zannas.netsons.org/myrss.php%3Fsezione%3Dcartella%26id%3D'+id_cartella;
				
				//aggiorno info cartella
				if (parametri_get.indexOf('cerca=') == 0) {} else {
				document.getElementById('condividicartella').value='http://zannas.netsons.org/Applicazioni/ApplicazionieDocumenti/'+xml.getElementsByTagName('cartella_corrente').item(0).getElementsByTagName('nome').item(0).firstChild.nodeValue+'-'+id_cartella;
				document.getElementById('id_cartellafiledains').value='http://zannas.netsons.org/Applicazioni/ApplicazionieDocumenti/'+xml.getElementsByTagName('cartella_corrente').item(0).getElementsByTagName('nome').item(0).firstChild.nodeValue+'-'+id_cartella;
				}
				document.getElementById('id_cartellafiledains').value = id_cartella;
				document.getElementById('nome_cartella').innerHTML = xml.getElementsByTagName('cartella_corrente').item(0).getElementsByTagName('nome').item(0).firstChild.nodeValue;
				try {
					document.getElementById('descrizione_cartella').innerHTML = xml.getElementsByTagName('cartella_corrente').item(0).getElementsByTagName('descrizione').item(0).firstChild.nodeValue;
				} catch (e) {document.getElementById('descrizione_cartella').innerHTML = '';}
				document.getElementById('sottocartelle').innerHTML = xml.getElementsByTagName('cartella_corrente').item(0).getElementsByTagName('sottocartelle').item(0).firstChild.nodeValue;
				document.getElementById('file_presenti').innerHTML = xml.getElementsByTagName('cartella_corrente').item(0).getElementsByTagName('file_presenti').item(0).firstChild.nodeValue;
				document.getElementById('cartella_indietro').onclick= function() {get('cartella='+xml.getElementsByTagName('cartella_corrente').item(0).getElementsByTagName('cartella_superiore').item(0).firstChild.nodeValue);}
				document.getElementById('pulsante_aggiorna').onclick= function() {get(parametri_get);}
			break;
		}
	}
	
	ajax.open("GET",'dati_xml.php?'+parametri_get+'&n='+Math.random(),true);
	ajax.send(null);
}

function cerca(par) {
	if (time == undefined) {
		time = setTimeout(function () {get(par);},1000);
	} else {
		clearTimeout(time);
		time = setTimeout(function () {get(par);},1000);
	}
}