var loge='';

var sd='k'+Math.floor(Math.random()*10);
//obj.ajaxdone() si null => eval

esca=window.encodeURIComponent?encodeURIComponent:escape;
escat=function(t){
	t=t.replace(/ /,'-');
	return esca(t);
}
if (location.search.search(/code=ubb/)!=-1) {	code='ubb';}
if (location.search.search(/code=html/)!=-1){	code='html';}
function $(t){return document.getElementById(t);}
function ajax(page,method,data,sync,obj){
req = false;if(sync){sync=true;}else{sync=false;}if(obj){}else{obj=null;}if(window.XMLHttpRequest){try{req=new XMLHttpRequest();}catch(e){loge+='///'+e;req=false;}}else if(window.ActiveXObject){
try{req=new ActiveXObject("Msxml2.XMLHTTP");} catch(e){try{req=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){loge+='///'+e;
req=false;}}}
if(req){
  req.onreadystatechange = function(){
   if (req.readyState == 4) {
    if (req.status == 200){
   var res=req.responseText;
   if (res.length>9 && res.substring(0,9) == "while(1);"){
		res = res.substring(10);
   }
   try{
		if (obj!=null){
			obj.ajaxdone(res);
		}
		else{eval(res);}
	}catch(e){alert('Erreur:'+res+e);loge+='///'+e;}
	}
	else{
   try{
		if (obj!=null){
			obj.ajaxko(req.status);
		}
	}catch(e){}
   }
   }
  }
  if(method == "GET"){
   if(data==null){req.open("GET", page,!sync);}
   else{req.open("GET", page+"?"+data,!sync);}
  }
  else if(method == "POST"){req.open("POST", page,!sync);}
  if(method == "GET"){req.send(null);}
  else if(method == "POST"){req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');req.send(data);}
  if (sync) {
  try{
     var res=req.responseText;
	 if (res.length>9 && res.substring(0,9) == "while(1);"){
		res = res.substring(10);
     }
	 if (obj!=null){
			obj.ajaxdone(res);
	 }
	 else{eval(res);}
  }catch(e){alert('Erreur'+e+' /// '+req.responseText);loge+='///'+e;}
  }
  return true;
}
else{//vieux nav
return false;
}
}// fin ajax

var encours=false;
function quittepage(){
	if (encours){
		return "Attention : le transfert en cours échouera si vous quittez la page.";
	}
	return void(0);
}
function npass(n){
	var a='0123456789abcdefghijklmnopqrstuvwxyz';
	var ret='';var i=0;
	for (i=0;i<n;i++){ret+=a.charAt(Math.floor(Math.random()*36));}
	return ret;
}

var tstart=0;//date départ
var tlast=0;//date last check
var tnow=0;//date now
var olast=0;//octet last
var onow=0;//octet now
var ofile=0;//size file
var filename='';//nom file
var fileext='';//extension

var idfile=npass(8);
var pw=npass(8);

function submit_upload(){
	var a=document.formupload.fichier.value;
	if (a.length<2){
		alert('Veuillez d\'abord choisir un fichier.');
		return false;
	}
	a=a.split(/\\/);
	a=a[a.length-1];
	a=a.split(/\//);
	a=a[a.length-1];
	filename=a;
	a=a.split(/\./);
	a=a[a.length-1];
	fileext=a.toLowerCase();
	encours=true;
	document.title="Démarrage...";
	$('progressbar').innerHTML="Démarrage...";
	$('progressbar').style.display='block';
	tprog.start();
	
	tnow=(new Date()).getTime();
	tstart=tnow;
	return true;
}

var to=null;
var tom=null;
function Progress() {
 this.nbko=0;
 this.vitos=1;
 this.sret=1000000;
 this.ajaxdone=function(t){
	this.majbar(t);
 }
 this.ajaxko=function(status){
	this.nbko++;
	if (this.nbko<10){
		to=setTimeout(function(){tprog.goajax();},3000);
		document.title="KO "+t+" "+this.nbko;
		$('progressbar').innerHTML="KO "+t+" "+this.nbko;
	}
 }
 this.start=function(){
 	$('formupload').style.display="none";
	to=setTimeout(function(){tprog.goajax();tprog.majest();},2000);
	var txtinfo="<fieldset><legend>Partage de votre fichier en streaming</legend>"+
	'<label for="uploadlink"><span class="lab"><b>Votre fichier est déjà récupérable en streaming à l\'adresse suivante :</b><br /><small>Copier/coller dans une page web, un email, msn, etc.. pour créer un lien vers le fichier</small></span></label><br />'+
	'<input id="uploadlink" class="txtcopy" onclick="this.select();" title="Clic droit puis copier" value="http://ti1ca.com/'+idfile+'-'+escat(filename)+'.html" readonly="readonly" type="text" style="width:560px;"><a href="http://ti1ca.com/'+idfile+'-'+escat(filename)+'.html" target="_blank" title="Lien vers le fichier"><img src="http://ti1ca.com/ti1ca-go.gif" wisth="18" height="8" border="0" ></a><br /><br />';
	if (code!='ubb'){
			txtinfo+='<label for="uploadlinkhtmlmini"><span  class="lab"><b style="color:#039;">Code html</b> : <small>Copiez/collez sur votre site web ou votre blog : MySpace, Blogger, Friendster, eBay, Autotitre</small></span></label><br />'+
			'<input id="uploadlinkhtmlmini" class="txtcopy" onclick="this.select();" title="Clic droit puis copier" value="&lt;a href=&quot;http://ti1ca.com/'+idfile+'-'+escat(filename)+'.html&quot;&gt;&lt;img src=&quot;http://m'+sd+'.ti1ca.com/'+idfile+'.jpg&quot; /&gt;&lt;a&gt;" readonly="readonly" type="text" style="width:600px;color:#039;"><br /><br />';
	}
	if (code!='html'){
		txtinfo+='<label for="uploadlinkubbmini"><span class="lab"><b style="color:#C33;">Code forum</b> : <small>Copier/coller dans un forum pour créer un lien vers le fichier</small></span></label><br />'+
			'<input id="uploadlinkubbmini" class="txtcopy" onclick="this.select();" title="Clic droit puis copier" value="[URL=http://ti1ca.com/'+idfile+'-'+escat(filename)+'.html][IMG]http://m'+sd+'.ti1ca.com/'+idfile+'.jpg[/IMG][/URL]" readonly="readonly" type="text" style="width:600px;color:#C33;"><br />';
	}
	txtinfo+='</fieldset>'
	$('infolink').style.display='block';
	$('infolink').innerHTML=txtinfo;
 }
 
 this.goajax=function(){
	ajax('/info/'+sd+'/'+idfile.substring(0,1)+'/'+idfile+'.info','POST',null,false,tprog);
 }
 
 this.majbar=function(t){
   var e;var v;var lu;
   if (t.length>5){
     var e=t.substring(0,2);
	 do{
	 if (e=="OK"){
		this.majdone();
		break;
	 }
	 if (e=="TR"){
		v=t.split(/\|/);
		lu=parseInt(v[3]);
		ofile=parseInt(v[4]);
		olast=onow;
		tlast=tnow;
		onow=lu;
		tnow=(new Date()).getTime();
		this.vitos=Math.floor((onow-olast)*1000/(tnow-tlast))+1;
		this.sret=Math.floor((ofile-onow)/this.vitos);
		if (this.sret<8){
			if (this.sret<1) this.sret=1;
			to=setTimeout(function(){tprog.goajax();},this.sret*1000);
		}
		else{
			to=setTimeout(function(){tprog.goajax();},8000);
		}
		break;
	 }
	 if (e=="ER"){
		this.nbko=10;
		document.destiframe.src="about:blank";
		encours=false;
	 }
	 if (this.nbok<10){
		to=setTimeout(function(){tprog.goajax();},2000);	 
	 }
	 }while(false);



   }
   else{
	 this.nbko++;
   }
 } 
 
 this.majest=function(){
	if (this.vitos>1){
		var _tnow=(new Date()).getTime();
		var _onow=onow+this.vitos/1000*(_tnow-tnow);
		if (_onow>ofile) _onow=ofile-1000;
		var dprc=_onow*100/ofile;
		var prc=Math.floor(dprc);
		var sret=Math.floor((ofile-_onow)/this.vitos);
		var restetxt=sret;
		tret='';
		var mret=0;var msret=0;
		
		if (sret>60) mret=Math.floor(sret/60);
		msret=sret%60;
		if (msret<10) msret="0"+msret;
		txt=(prc)+'% '+mret+'\''+msret+'" '+(Math.floor(this.vitos/100)/10)+'ko/s, '+filename ;
		
		txti='<b>'+(prc)+'%</b>, Reste : '+mret+'\''+msret+'", '+(Math.floor(this.vitos/100)/10)+'ko/s<br /><b>'+filename+'</b> ('+Math.round(ofile/1024)+'ko)';
		document.title=txt;
		
		var prc=Math.round(dprc*5)-500;
		$('progressbar').style.backgroundPosition=prc+'px 0px';
		$('progressbar').innerHTML=txti;
		
	}
	tom=setTimeout(function(){tprog.majest();},200);
 }
 this.majdone=function(){
	document.title='100%';
	$('progressbar').innerHTML='100%';
	clearTimeout(tom);
	clearTimeout(to);
	encours=false;
	if ($('ckalerte').checked==true){
		//alert('Fichier '+filename+' transmis');
	}
	$('upload_next').submit();
 }
}


function ZipAlbum() {
 this.nbko=0;
 this.vitos=1;
 this.sret=1000000;
 this.ajaxdone=function(t){
	this.majbar(t);
 }
}

za=new ZipAlbum();

var optionopen=false;
function clickoption(){
	if (optionopen){
		$('imgoption').src='http://www.autotitre.com/forum/img/plus.gif';
		$('infooption').style.display='none';
	}
	else{
		$('imgoption').src='http://www.autotitre.com/forum/img/moins.gif';
		$('infooption').style.display='';
	}
	optionopen=!optionopen;
}
var tprog=new Progress();
