var rate_locked;
var thx_locked;
var prev_letter = '';
var letter2;
var seeds, leechs;
var updateTorrentStats_ok;

function Popup( title, target, imagesrc, look )
{
	popwin = window.open(imagesrc);
//	popwin.document.open();
	// popwin.document.write('<html><head><title>'+title+'</title></head><body style="margin:0;padding:0;"><a href="" onclick="javascript:window.close()"><img src="'+imagesrc+'" border="0"><a/></body></html>');
//	popwin.document.close();
	popwin.focus();
}

function rate_mouseover(id_pic)
{
	if(rate_locked==null)
	{
		setrate(id_pic);
	}
}

function rate_mouseout(id_pic)
{
	if(rate_locked==null)
	{
		for(i=1; i<=id_pic; i++)
		{
			document.getElementById('rating_'+i).src= 'images/contenu/rate_big_white.png';
		}
	} else {
	setrate(rate_locked);
	}
}

function lockrate(id_pic)
{
	rate_locked=id_pic;
}

function setrate(rate)
{
		for(i=1; i<=rate; i++)
		{
			document.getElementById('rating_'+i).src= 'images/contenu/rate_big_black.png';
		}
}

function makethx(id)
{
	if(thxed == 1)
	{
	alert('Only one thank per torrent :)');
	// window.location.replace(window.self.location);
	} else {
	// $.ping('thx.php', 'id='+id);
	window.location.replace('thx.php?id='+id);
	thxed = 1;
	}
}

function LetterSelect(letter)
{
	if(prev_letter != '')
	{
			letter2 = letter;
      $("#letter_list_"+prev_letter).fadeOut(200);
			setTimeout('$("#letter_list_"+letter2).fadeIn("slow")', 200);
			prev_letter = letter;
	} else {
      $("#letter_list_"+letter).fadeIn("slow");
			prev_letter = letter;
	}
}

function deletetorrent(lql)
{
if (!confirm("Do you really want to delete this torrent?"))
		return false;
	location = lql;
}

function deletecomm(lql)
{
if (!confirm("Do you really want to delete this review ?"))
		return false;
	location = lql;
}

function report(lql, reason)
{
if (!confirm("Do you confirm this torrent as "+reason+" ?"))
		return false;
	location = lql;
}

function nothing(){
	return true;
}

function updateTorrentStats(id)
{
	updateTorrentStats_ok=false;
	$("#updatenow").fadeOut(200);
	setTimeout('document.getElementById("updatenow").innerHTML = "Processing...";', 200);
	setTimeout('$("#updatenow").fadeIn("slow")', 200);
	$.ping('ask_refresh_torrent.php', 'id_torrent='+id);
	var date = new Date();
	
	for(i=date.valueOf()+90*1000;(i>date.valueOf() && !updateTorrentStats_ok);nothing()) { // on essaie pendant 90s de refresh
	 $.ajax({
   type: "GET",
   url: "js/getStats.php",
   data: "id_torrent="+id,
	 async: false, 
   success: function(msg){
		if(msg.search(seeds) == -1 || msg.search(leechs) == -1){ // si les valeurs ont changé
     // document.getElementById('seeds').innerHTML = msg.split(':')[0];
     // document.getElementById('leechs').innerHTML = msg.split(':')[1];
		 seeds = msg.split(':')[0];
		 leechs = msg.split(':')[1];
		$("#updatenow").fadeOut(200);
		setTimeout('document.getElementById("updatenow").innerHTML = "Updated !";', 200);
		setTimeout('$("#updatenow").fadeIn("slow")', 200);
		$("#seeds").fadeOut(200);
		setTimeout('document.getElementById("seeds").innerHTML = seeds;', 200);
		setTimeout('$("#seeds").fadeIn("slow")', 200);
		$("#leechs").fadeOut(200);
		setTimeout('document.getElementById("leechs").innerHTML = leechs;', 200);
		setTimeout('$("#leechs").fadeIn("slow")', 200);
		
		 updateTorrentStats_ok=true;
		 } else {
			// alert('wait...');
		 }
   }
  });
	date = new Date();
 }
}
