var comptecount=1;
function createXHR() 
{
    var request = false;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
		try {
			request = new XMLHttpRequest();
		}
		catch (err1) 
		{
			request = false;
		}
            }
        }
    return request;
}




function gojson( torsh ) 
  {
	 document.getElementById("info").innerHTML  ='Loading ... ';
	document.getElementById("spinner").style.visibility="visible";
  // handle the json object here
  //alert(torsh.engine);  //<---------- works
  //$.each(torsh.torrents, function(i,torrent){
    //alert(torrent.title); //<---------- works
	tbody = document.getElementById("tbodytorrent");
	if (torsh.error=='no error')
	{

		for( var f in torsh.torrents ) 
		{
				
			trtemp = document.createElement("tr");
		
			td = document.createElement("td");
			td.appendChild(document.createTextNode(torsh.torrents[f].age));
			trtemp.appendChild(td);
		
		
			td = document.createElement("td");
			//<a href="/get/648266" class="dl" onclick="pageTracker._trackPageview('/get/id')"><img src="/images/down.gif" alt="[D]"></a>
			
			
			/*// torrent direct download 
			a=document.createElement("a");
			a.setAttribute('href','r?url='+ torsh.torrents[f].torrent);
			a.setAttribute('class','dl');
			img=document.createElement("img");
			img.setAttribute('src','/images/down_mininova.gif');
			img.setAttribute('width','9px');
			img.setAttribute('height','9px');
			a.appendChild(img);
			td.appendChild(a);
			*/


			a=document.createElement("a");
			a.setAttribute("title", torsh.torrents[f].title);
			a.setAttribute('href','r?url='+ torsh.torrents[f].link);
			a.setAttribute('target','_blank');
			if ( torsh.torrents[f].title.length>50)
			{
				a.appendChild(document.createTextNode(torsh.torrents[f].title.substring(0,50) + '...'));
			}
			else
			{
				a.appendChild(document.createTextNode(torsh.torrents[f].title));
			}
			td.appendChild(a);
			trtemp.appendChild(td);
		
			td = document.createElement("td");
			td.appendChild(document.createTextNode(torsh.torrents[f].category));
			trtemp.appendChild(td);
		
			/*
			td = document.createElement("td");
			td.appendChild(document.createTextNode(torsh.torrents[f].nbrfiles));
			trtemp.appendChild(td);
			*/
		
			td = document.createElement("td");
			td.appendChild(document.createTextNode(torsh.torrents[f].size));
			trtemp.appendChild(td);
		
			td = document.createElement("td");
			td.setAttribute('class','g');
			td.appendChild(document.createTextNode(torsh.torrents[f].seed));
			trtemp.appendChild(td);
		
			td = document.createElement("td");
			td.setAttribute('class','b');
			td.appendChild(document.createTextNode(torsh.torrents[f].leech));
			trtemp.appendChild(td);
		
			td = document.createElement("td");
			img = document.createElement("img");
			img.setAttribute('src','images/engines/' + torsh.engine + '.png');
			img.setAttribute('alt',torsh.engine);
			td.appendChild(img)
			//td.appendChild(document.createTextNode(torsh.engine));
			trtemp.appendChild(td);
		
			var id=torsh.engine+'_'+torsh.page+'_'+f;
			tmptr = document.getElementById(id);
			trtemp.setAttribute("id", id);
			tbody.appendChild(trtemp);
		}
		//---succes
		//document.getElementById("spinner_" + torsh.engine).setAttribute('class',"state_2");
	}
	else
	{
	//----------- error
	//document.getElementById("spinner_" + torsh.engine).setAttribute('class',"state_3");
	}

				//-------------------debut modification de la progression ----------------------------
				comptecount=comptecount-1;
				if (comptecount===0)
				{
					document.getElementById("info").innerHTML  = "done";
					document.getElementById("spinner").style.visibility="hidden";
				}
				else
				{
					document.getElementById("info").innerHTML = comptecount + ' Restants';
				}
				//-------------------fin modification de la progression ----------------------------

}





function loadJSON(host,query) 
{	
	document.getElementById("info").innerHTML  ='Loading ... ';
	document.getElementById("spinner").style.visibility="visible";
	var xhr=createXHR();
	fname="http://torrentvortex" + host + '.appspot.com/f?query='+query+'&jsoncallback=?';
	//alert(fname);
	xhr.open("GET", fname,true);
	xhr.onreadystatechange=function() 
	{
		if (xhr.readyState == 4) 
		{
			if (xhr.status != 404) 
			{
				try
				{
				//Run some code here
				var myObject = eval( xhr.responseText  );
				}
				catch(err)
				{
				//Handle errors here
				}
				

				if (typeof torsh != "undefined") {
					//object exists ... execute code here.
					//------ si page = 1 -> loader les 5 prochaines
					maxpages=1; //torsh.pages //nbr max de page a ramener
					if (torsh.pages < maxpages)
					{
						maxpages=torsh.pages;
					}
					if (torsh.page==1)
					{
						/*if (torsh.pages>=2)
						{
							loadJSON('index_' + torsh.engine + '.php?query='+torsh.query+'&page=2');
							loadJSON('index_' + torsh.engine + '.php?query='+torsh.query+'&page=3');
							comptecount=comptecount+2
						}
						else
						{
						}*/

						pages=maxpages+1;
							for (i = 2; i < pages; i++)
							{
								loadJSON('index_' + torsh.engine + '.php?query='+torsh.query+'&page='+i);
								comptecount=comptecount+1
							}

					
					}
					else
					{	
						nextpage=torsh.page+5
						if (nextpage <= maxpages)
						{
							loadJSON('index_' + torsh.engine + '.php?query='+torsh.query+'&page='+nextpage)
							comptecount=comptecount+1
						}
						// pas 1 
					}
					//------ si pages >1 -> loader page + 5 si possible sinon rien
				
					// ---------------- ajout Comptes -------------------------------
					/*
					tbodycomptes = document.getElementById("tbodycomptes");
					trcomptestmp = document.createElement("tr");
				
					tdcomptes = document.createElement("td");
					tdcomptes.appendChild(document.createTextNode(torsh.engine));
					trcomptestmp.appendChild(tdcomptes);
				
				
					var bandwith=torsh.infos.bandwith_used+' '+torsh.infos.bandwith_usedunit+'/'+torsh.infos.bandwith_total+' '+torsh.infos.bandwith_totalunit;
					tdcomptes = document.createElement("td");
					tdcomptes.appendChild(document.createTextNode(bandwith));
					trcomptestmp.appendChild(tdcomptes);
				
					var disk=torsh.infos.disk_used+' '+torsh.infos.disk_usedunit+'/'+torsh.infos.disk_total+' '+torsh.infos.disk_totalunit;
					tdcomptes = document.createElement("td");
					tdcomptes.appendChild(document.createTextNode(disk));
					trcomptestmp.appendChild(tdcomptes);
				
					trcomptestmp2 = document.getElementById(torsh.user+'_compte');
				
				
					if (trcomptestmp2!=null)
					{
						trcomptestmp2.innerHTML=trcomptestmp.innerHTML;
					}
					else
					{
						trcomptestmp.setAttribute("id", torsh.user+'_compte');
						tbodycomptes.appendChild(trcomptestmp);
					}
					*/				
					// ---------------- fin ajout Comptes -------------------------------
				
				
				
				
					//-------------------debut ajout de tout les torrents ----------------------------
					tbody = document.getElementById("tbodytorrent");
					for( var f in torsh.torrents ) {
				
						trtemp = document.createElement("tr");
					
						td = document.createElement("td");
						td.appendChild(document.createTextNode(torsh.torrents[f].age));
						trtemp.appendChild(td);
					
					
						td = document.createElement("td");
						//<a href="/get/648266" class="dl" onclick="pageTracker._trackPageview('/get/id')"><img src="/images/down.gif" alt="[D]"></a>
						a=document.createElement("a");
						a.setAttribute('href','r?url='+ torsh.torrents[f].torrent);
						a.setAttribute('class','dl');
						img=document.createElement("img");
						img.setAttribute('src','/images/down_mininova.gif');
						a.appendChild(img);
					
						td.appendChild(a);
						a=document.createElement("a");
						a.setAttribute("title", torsh.torrents[f].title);
						a.setAttribute('href','r?url='+ torsh.torrents[f].link);
						if ( torsh.torrents[f].title.length>50)
						{
							a.appendChild(document.createTextNode(torsh.torrents[f].title.substring(0,50) + '...'));
						}
						else
						{
							a.appendChild(document.createTextNode(torsh.torrents[f].title));
						}
						td.appendChild(a);
						trtemp.appendChild(td);
					
						td = document.createElement("td");
						td.appendChild(document.createTextNode(torsh.torrents[f].category));
						trtemp.appendChild(td);
					
						/*
						td = document.createElement("td");
						td.appendChild(document.createTextNode(torsh.torrents[f].nbrfiles));
						trtemp.appendChild(td);
						*/
					
						td = document.createElement("td");
						td.appendChild(document.createTextNode(torsh.torrents[f].size));
						trtemp.appendChild(td);
					
						td = document.createElement("td");
						td.setAttribute('class','g');
						td.appendChild(document.createTextNode(torsh.torrents[f].seed));
						trtemp.appendChild(td);
					
						td = document.createElement("td");
						td.setAttribute('class','b');
						td.appendChild(document.createTextNode(torsh.torrents[f].leech));
						trtemp.appendChild(td);
					
						td = document.createElement("td");
						img = document.createElement("img");
						img.setAttribute('src','images/engines/' + torsh.engine + '.png');
						img.setAttribute('alt',torsh.engine);
						td.appendChild(img)
						//td.appendChild(document.createTextNode(torsh.engine));
						trtemp.appendChild(td);
					
						var id=torsh.engine+'_'+torsh.page+'_'+f;
						tmptr = document.getElementById(id);
						if (tmptr!=null)
						{
							tmptr.innerHTML=trtemp.innerHTML;
						}
						else
						{
							trtemp.setAttribute("id", id);
							tbody.appendChild(trtemp);
						}

				  	}
					document.getElementById("spinner_" + torsh.engine).setAttribute('class',"state_2");
					//-------------------debut ajout de tout les torrents ----------------------------
				
					
				}
				else
				{
					//torsh né po un object error !!!!
					document.getElementById("spinner_" + host).setAttribute('class',"state_3");
					
				}
				//-------------------debut modification de la progression ----------------------------
				comptecount=comptecount-1;
				if (comptecount===0)
				{
					document.getElementById("info").innerHTML  = "done";
					document.getElementById("spinner").style.visibility="hidden";
				}
				else
				{
					document.getElementById("info").innerHTML = comptecount + ' Restants';
				}
				//-------------------fin modification de la progression ----------------------------
				

			} 
			else 
			{
				document.getElementById("spinner").style.visibility="hidden";
				document.getElementById("spinner_" + host).setAttribute('class',"state_3");
				//document.getElementById("elArticles").innerHTML = fname + " not found";
				//document.getElementById("r").innerHTML='not found';
				//---------------------------- 404 : NOT FOUND!!! -------------------------------------------
			}
		} // end of ------------------------ > if (xhr.readyState == 4) 
		else
		{
		//document.getElementById("spinner_" + host).setAttribute('class',"state_3");
		}
		
	} // end of ------------------------------> xhr.onreadystatechange=function() 

	xhr.send(null);
	
}
