// JavaScript Document
var txt_to_view_file_small=new Array();
function estensione (url) {
	var array = url.split('.');
	var e = array[array.length-1];
	return e;
}

function up_download(id_file) {
	var n = new Number(document.getElementById('download_file_'+id_file).innerHTML);
	document.getElementById('download_file_'+id_file).innerHTML = (n+1);
}

function up_download2(id_file) { //funzione utilizzata per il download di file Google e Youtube, deve slo aggiornare il valore
	var n = new Number(document.getElementById('download_file_'+id_file).innerHTML);
	document.getElementById('download_file_'+id_file).innerHTML = (n+1);
	
	var ajax=null;
	var layer = document.getElementById('schermo_'+id_file);
	
	if (window.XMLHttpRequest) {
		ajax=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		ajax=new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	ajax.onreadystatechange=function () {
		switch (ajax.readyState) {
			case 0:
				alert('errore');
			break;
		}
	}
	
	ajax.open("GET",'download.php?mode=2&id='+id_file+'&n='+Math.random(),true);
	ajax.send(null);
}

function get_info(id_file) {
	var ajax=null;
	var layer = document.getElementById('schermo_'+id_file);
	
	if (window.XMLHttpRequest) {
		ajax=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		ajax=new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	ajax.onreadystatechange=function () {
		switch (ajax.readyState) {
			case 0:
				alert('errore');
			break;
			case 1:
				//layer.style.left = (((document.documentElement.clientWidth)/2)-300)+'px';
				layer.innerHTML = '<p align="center"><img src="../../img/loading.gif" /> Caricamento in corso...</p>';
				try {
					document.getElementById('divloadfile'+id_file).style.display= 'block';
				} catch (e) {}
				
			break;
			case 4:
				var xml = ajax.responseXML;
				var id = xml.getElementsByTagName('id').item(0).firstChild.nodeValue;
				var nome = xml.getElementsByTagName('nome').item(0).firstChild.nodeValue;
				var url = xml.getElementsByTagName('url').item(0).firstChild.nodeValue;
				try {
					var descrizione = xml.getElementsByTagName('descrizione').item(0).firstChild.nodeValue;
				} catch (e) {var descrizione = '';}
				var download = xml.getElementsByTagName('ndownload').item(0).firstChild.nodeValue;
				var media = xml.getElementsByTagName('media').item(0).firstChild.nodeValue;
				var voti = xml.getElementsByTagName('numero_voti').item(0).firstChild.nodeValue;
				
				
				//fadeIn(layer.id,20);
				
				var re_videogoogle = /^http:\/\/video.google.\w+/;
				var re_youtube = /^http:\/\/www.youtube.com\/\w+/;
				var re_youtube2 = /^http:\/\/youtube.com\/\w+/;
				
				if (re_videogoogle.test(url) == false && re_youtube.test(url) == false && re_youtube2.test(url) == false) {
					var url_download = 'download.php?id='+id_file;
					var funzione_download = 'up_download('+id_file+')';
				} else {
					var url_download = 'http://www.videodownloader.net/get/?url='+url;
					var funzione_download = 'up_download2('+id_file+')';
				}
				
				if (re_videogoogle.test(url) == false && re_youtube.test(url) == false && re_youtube2.test(url) == false) {
					var est = estensione(url);
					switch (est.toLowerCase()) {
						case 'gif':
						case 'jpg':
						case 'jpeg':
							txt_to_view_file = '<img id="file_'+id_file+'" src="../../resize_img.php?src='+base64encode(url)+'&h=400" alt="Immagine" height="400" align="center" onload="document.getElementById(\'divloadfile'+id_file+'\').style.display=\'none\'" />';
							txt_to_view_file_small[id_file] = '<img id="file_small_'+id_file+'" src="../../resize_img.php?src='+base64encode(url)+'&w=190" alt="Immagine" width="190" align="center" />';
						break;
						case 'mp3':
						case 'mpeg':
						case 'mpg':
						case 'swf':
							txt_to_view_file = '<embed id="file_'+id_file+'" src="'+url+'" width="600" height="450" align="center" autostart="true"></embed>';
							txt_to_view_file_small[id_file] = '<embed id="file_small_'+id_file+'" src="'+url+'" width="190" height="150" align="center" autostart="true"></embed>';
						break;
						case 'asx':
						case 'mov':
							txt_to_view_file = '<embed id="file_'+id_file+'" src="'+url+'" width="480" height="360" align="center" autostart="true"></embed>';
							txt_to_view_file_small[id_file] = '<embed id="file_small_'+id_file+'" src="'+url+'" width="190" height="180" align="center" autostart="true"></embed>';
						break;
						default:
							txt_to_view_file = 'Abilita il tuo browser all\'apertura delle popup da parte di questo sito.';
							txt_to_view_file_small[id_file] = '';
							window.open('download.php?id='+id_file,'Download','width=0,height=0');
						break;
					}
				} else {
					if (re_videogoogle.test(url)) {
						
						url = url.replace('videoplay','googleplayer.swf');
						url = url.replace('.it','.com');
						
						txt_to_view_file = '<embed id="VideoPlayback" width="480" height="360" type="application/x-shockwave-flash" src="'+url+'" flashvars="" > </embed>';
						txt_to_view_file_small[id_file] = '<embed id="VideoPlayback" width="190" height="180" type="application/x-shockwave-flash" src="'+url+'" flashvars=""> </embed>';
					}
					if (re_youtube.test(url) || re_youtube2.test(url)) {
						url = url.replace('watch?v=','v/');
						/*url = url.replace('.it','.com');*/
						
						txt_to_view_file = '<object width="480" height="360"><param name="movie" value="'+url+'"></param><param name="wmode" value="transparent"></param><embed src="'+url+'" type="application/x-shockwave-flash" wmode="transparent" width="480" height="360"></embed></object>';
						txt_to_view_file_small[id_file] = '<object width="190" height="180"><param name="movie" value="'+url+'"></param><param name="wmode" value="transparent"></param><embed src="'+url+'" type="application/x-shockwave-flash" wmode="transparent" width="190" height="180"></embed></object>';
					}
				}
				
				layer.innerHTML='<div id="minimenufile_'+id_file+'" align="right" style="cursor:move;">'+
					'<a class="link" id="mostra_nascondi_tabella_file_'+id+'" onclick="mostra_nascondi_tabella('+id+')">Nascondi</a> '+
					'<a class="link" onclick="document.getElementById(\'schermo_'+id_file+'\').style.display = \'none\'">Chiudi</a>'+
				'</div>'+
				'<div id="pcondividifile'+id_file+'" align="left">Condividi File: <input name="condividifile" type="text" id="condividifile" onclick="this.select()" value="http://'+window.location.host+window.location.pathname+'File-'+nome+'-'+id_file+'" size="60"/></div>'+
				'<table id="tabella_file_'+id+'" style="width:100%;">'+
				  '<caption>'+
				    'Visualizzazione File'+
				  '</caption>'+
				  '<tr>'+
				    '<td rowspan="3" align="center">'+txt_to_view_file+'<div class="divloadfile" id="divloadfile'+id_file+'" style="display=\'block\';"><img src="../../img/loading.gif" /> Caricamento in corso, attendere prego...</div></td>'+
				    '<td colspan="-2">Nome File: <b id="nomefile_'+id_file+'">'+nome+'</b></td>'+
				  '</tr>'+
				  '<tr>'+
				    '<td colspan="-2">Numero Download: <b><span id="download_file_'+id_file+'">'+download+'</span></b>'+
					'<br/><br/><div class="down">Scarica questo File:<br /><a href="'+url_download+'" onclick="'+funzione_download+'" target="_blank"><img src="../../img/download.gif" alt="Download"/></a><!--<img src="img/downloadZIP.gif" alt="Download"/>--></div></td>'+
				  '</tr>'+
				  '<tr>'+
				    '<td colspan="-2" id="votazionestellinefile" valign="top"><h2>Vota questo File</h2> Media:<br /><span id="file-'+id_file+'" onmouseout="ripristina_stelline('+id_file+','+media+');">'+
												'<img src="img/star.gif" alt="Voto: 1" id="file-'+id_file+'-voto-1" onmouseover="sopra_stelline(this,1)" onmouseout="fuori_stelline(this,1);" onclick="vota('+id_file+',1)" />'+
												'<img src="img/star.gif" alt="Voto: 2" id="file-'+id_file+'-voto-2" onmouseover="sopra_stelline(this,2)" onmouseout="fuori_stelline(this,2);" onclick="vota('+id_file+',2)" />'+
												'<img src="img/star.gif" alt="Voto: 3" id="file-'+id_file+'-voto-3" onmouseover="sopra_stelline(this,3)" onmouseout="fuori_stelline(this,3);" onclick="vota('+id_file+',3)" />'+
												'<img src="img/star.gif" alt="Voto: 4" id="file-'+id_file+'-voto-4" onmouseover="sopra_stelline(this,4)" onmouseout="fuori_stelline(this,4);" onclick="vota('+id_file+',4)" />'+
												'<img src="img/star.gif" alt="Voto: 5" id="file-'+id_file+'-voto-5" onmouseover="sopra_stelline(this,5)" onmouseout="fuori_stelline(this,5);" onclick="vota('+id_file+',5)" />'+
											'</span><br /> Numero Voti: <b><span id="voti_file_'+id_file+'">'+voti+'</b></span></td>'+
				  '</tr>'+
				  '<tr>'+
				    '<td colspan="2">Descrizione: '+
					'<p id="descrizione_file">'+descrizione+'</p>'+
					'<!--<p align="center"><iframe src="google_468x60.php" height="65" width="465" scrolling="no" style="border:none"></iframe></p>-->'+
					'</td>'+
				  '</tr>'+
				'</table>';
				var est = estensione(url);
				switch (est.toLowerCase()) {
					case 'gif':
					case 'jpg':
					case 'jpeg':
					break;
					case 'mp3':
					case 'mpeg':
					case 'mpg':
					case 'swf':
					case 'asx':
					case 'mov':
						document.getElementById('divloadfile'+id_file).style.display = 'none';
					break;
					default:
						document.getElementById('divloadfile'+id_file).style.display = 'none';
					break;
				}
				if (re_videogoogle.test(url) || re_youtube.test(url) || re_youtube2.test(url)) {
					document.getElementById('divloadfile'+id_file).style.display = 'none';
				}
				ripristina_stelline(id_file,media);
				mostra_nascondi_tabella(id_file);
			break;
		}
	}
	
	ajax.open("GET",'dati_xml.php?mode=file&id='+id_file+'&n='+Math.random(),true);
	ajax.send(null);
	
	
}

function vedi(id_file) {
	var layer = document.getElementById('schermo_'+id_file);
	
	try {
		layer.innerHTML;
	} catch (e) {
		layer = document.createElement('div');
		layer.setAttribute('id','schermo_'+id_file);
		if (window.ie) {
			layer.setAttribute('className','schermo');
		} else {
			layer.setAttribute('class','schermo');
		}
		document.body.appendChild(layer);
		
		layer.style.zIndex="0";					
	}
	//drag schermo
	new Drag.Move('schermo_'+id_file); //mootools
	
		/*var scroll_y = 0;

		if (window.ie) {
			scroll_y = document.body.scrollTop;
		} else if (window.gecko) {
			scroll_y = window.scrollY;
		}*/
		
	//layer.style.top = (scroll_y+10)+'px';
	
	/*window.onscroll = function () {
			if (window.ie) {
			scroll_y = document.body.scrollTop;
			} else if (window.gecko) {
				scroll_y = window.scrollY;
			}

			layer.style.top = (scroll_y+10)+'px';
	}*/
	
	get_info(id_file);
}

function mostra_nascondi_tabella(id_file) {
	var layer = document.getElementById('schermo_'+id_file);
	var tab = document.getElementById('tabella_file_'+id_file);
	var pulsante = document.getElementById('mostra_nascondi_tabella_file_'+id_file);
	var minimenu = document.getElementById('minimenufile_'+id_file);
	
	layer.style.display = 'block';
	
	if (tab.style.display == 'none' || tab.style.display == '') { //appena aperto o aperto tutto
		layer.style.width = '';
		tab.style.display = "block";
		document.getElementById('pcondividifile'+id_file).style.display= "block";
		//layer.style.left = (document.documentElement.clientWidth/2-layer.clientWidth/2)+'px';//(((document.documentElement.clientWidth)/2)-300)+'px';
		
		try {
			document.getElementById('video_piccolo'+id_file).style.display = 'none';
		} catch (e) {}
		
		minimenu.innerHTML = '<a class="link" id="mostra_nascondi_tabella_file_'+id_file+'" onclick="mostra_nascondi_tabella('+id_file+')">Nascondi</a> <a class="link" onclick="document.getElementById(\'schermo_'+id_file+'\').style.display = \'none\'">Chiudi</a>';
		
	} else { //chiuso imposto miniatura
		tab.style.display = 'none';
		document.getElementById('pcondividifile'+id_file).style.display= "none";
		layer.style.width = '190px';
		//layer.style.left = (document.documentElement.clientWidth-layer.clientWidth-20)+'px';
		
		minimenu.innerHTML = '<a class="link" id="mostra_nascondi_tabella_file_'+id_file+'" onclick="mostra_nascondi_tabella('+id_file+')">Mostra</a> <a class="link" onclick="document.getElementById(\'schermo_'+id_file+'\').style.display = \'none\'">Chiudi</a>';
		
		var video_piccolo = document.getElementById('video_piccolo'+id_file);
		try {
			video_piccolo.innerHTML;
		} catch (e) {
			var video_piccolo = document.createElement('div');		
			video_piccolo.setAttribute('id','video_piccolo'+id_file);
			document.getElementById(minimenu.id).appendChild(video_piccolo);
		}
		video_piccolo.innerHTML = '<p align="center">'+txt_to_view_file_small[id_file]+'</p>'+'<!--<p align="center"><iframe src="google_125x125.php" height="135" width="125" scrolling="no" style="border:none"></iframe></p>-->';
		video_piccolo.style.display = 'block';
	}
}


/*
esempio di ultilizzo:

<div id="file-0" onmouseout="ripristina_stelline(0,2);">
				<img src="star.gif" alt="Voto: 1" id="file-0-voto-1" onmouseover="sopra_stelline(this,1)" onmouseout="fuori_stelline(1);" onclick="vota(0,1)" ->
				<img src="star.gif" alt="Voto: 2" id="file-0-voto-2" onmouseover="sopra_stelline(this,2)" onmouseout="fuori_stelline(2);" onclick="vota(0,2)" ->
				<img src="star_white.gif" alt="Voto: 3" id="file-0-voto-3" onmouseover="sopra_stelline(this,3)" onmouseout="fuori_stelline(3);" onclick="vota(0,3)" ->
				<img src="star_white.gif" alt="Voto: 4" id="file-0-voto-4" onmouseover="sopra_stelline(this,4)" onmouseout="fuori_stelline(4);" onclick="vota(0,4)" ->
				<img src="star_white.gif" alt="Voto: 5" id="file-0-voto-5" onmouseover="sopra_stelline(this,5)" onmouseout="fuori_stelline(5);" onclick="vota(0,5)" ->
	<p id="attendifile-0"><-p>
<p id="mediafile-0"><-p>
<-div>
<div id="file-1" onmouseout="ripristina_stelline(1,2);">
				<img src="star.gif" alt="Voto: 1" id="file-1-voto-1" onmouseover="sopra_stelline(this,1)" onmouseout="fuori_stelline(1);" onclick="vota(1,1)" ->
				<img src="star.gif" alt="Voto: 2" id="file-1-voto-2" onmouseover="sopra_stelline(this,2)" onmouseout="fuori_stelline(2);" onclick="vota(1,2)" ->
				<img src="star_white.gif" alt="Voto: 3" id="file-1-voto-3" onmouseover="sopra_stelline(this,3)" onmouseout="fuori_stelline(3);" onclick="vota(1,3)" ->
				<img src="star_white.gif" alt="Voto: 4" id="file-1-voto-4" onmouseover="sopra_stelline(this,4)" onmouseout="fuori_stelline(4);" onclick="vota(1,4)" ->
				<img src="star_white.gif" alt="Voto: 5" id="file-1-voto-5" onmouseover="sopra_stelline(this,5)" onmouseout="fuori_stelline(5);" onclick="vota(1,5)" ->
	<p id="attendifile-1"><-p>
<p id="mediafile-1"><-p>
<-div>

il file php deve ritornare solo un numero intero rappresentante la media


Copyright by Zannas

Vietato l'utilizzo in qualunque sito previo accordo con l'autore.
Contatto: zannas@email.it
*/

function sopra_stelline(ancora,n) {
	var array = ancora.id.split('-');
	var id_file = array[1];
	for (i = 1; i <= 5; i++) {
		var img = document.getElementById('file-'+id_file+'-voto-'+i);
		
		if (i <= n) {
			img.src="img/star.gif";
		} else {
			//coloro di bianco le altre
			img.src="img/star_white.gif";
		}
	}
}

function ripristina_stelline(id_file,voto_medio) {
	for (var i = 1; i <= voto_medio; i++) {
		var img = document.getElementById('file-'+id_file+'-voto-'+i);
		img.src="img/star.gif";
	}
	var voto_medio = new Number(voto_medio);
	var n = new Number(voto_medio+1);
	for (n; n <= 5; n++) {
		var img = document.getElementById('file-'+id_file+'-voto-'+n);
		img.src="img/star_white.gif";
	}
}

function fuori_stelline(ancora,n) {
	var array = ancora.id.split('-');
	var id_file = array[1];
	for (i = 1; i <= n; i++) {
		var img = document.getElementById('file-'+id_file+'-voto-'+i);
		img.src="img/star_white.gif";
	}
}

function vota(file_id, voto) { 
	id_file = file_id;
	
	//inizializzo ajax
	var XMLHttp_voto=null;

	try { (XMLHttp_voto=new ActiveXObject("Msxml2.XMLHTTP"))
	} catch(e) {
		try { (XMLHttp_voto=new ActiveXObject("Microsoft.XMLHTTP"))}
		catch(e) {
			XMLHttp_voto=new XMLHttpRequest();
		}
	} 
	
	var url = 'vota.php';
	url = url+'?id='+id_file+'&voto='+voto+'&number='+Math.random();
	
	XMLHttp_voto.onreadystatechange = function () {
		if (XMLHttp_voto.readyState == 4) {	
			var risposta = XMLHttp_voto.responseXML;
			var media = risposta.getElementsByTagName('media').item(0).firstChild.nodeValue;
			var voti = risposta.getElementsByTagName('voti').item(0).firstChild.nodeValue;
			
			//coloro le stelline in base alla media
			sopra_stelline(document.getElementById('file-'+id_file),media);
			
			var html = "";
			var i;
			for(i = 1; i <= 5; i++) {
				if (i <= media) {
					html = html+'<img src="img/star.gif" alt="Voto: '+i+'" id="file-'+id_file+'-voto-'+i+'"/>';
				} else {
					html = html+'<img src="img/star_white.gif" alt="Voto: '+i+'" id="file-'+id_file+'-voto-'+i+'"/>';
				}
			}
			html = html+'<br/>Grazie per aver votato';
			
			document.getElementById('voti_file_'+id_file).innerHTML = voti;
			document.getElementById("file-"+id_file).innerHTML = html;
			document.getElementById("file-"+id_file).onmouseout = "ripristina_stelline("+media+")";
		}
	};
	XMLHttp_voto.open("GET",url,true);
	XMLHttp_voto.send(null);		
}