var lstfic		=	new Array();
var lstficinfo	=	new Array();
var selall=false;

var suruntr=false;
function seltous(){
	selall=!selall;
	for (i=0;i<lstfic.length;i++){
		omclicktronoff(i,selall);
	}
}

function omovertr(i){
	suruntr=true;
	dessusi=i;
	getId("ck"+i).src="/img/ck"+((lstfic[i])?"on":"off")+"over.gif";
	getId("tr"+i).style.backgroundColor=((lstfic[i])?"#CFD9EC":"#E0E6F2");
}
function omouttr(i){
	suruntr=false;
	getId("ck"+i).src="/img/ck"+((lstfic[i])?"on":"off")+".gif";
	getId("tr"+i).style.backgroundColor=((lstfic[i])?"#BFCCE5":"#FFFFFF");
}
function omdowntr(i){
	getId("ck"+i).src="/img/ck"+((lstfic[i])?"on":"off")+"down.gif";

}
function omclicktronoff(i,onoff){
	lstfic[i]=onoff;
	getId("tr"+i).style.backgroundColor=((lstfic[i])?"#BFCCE5":"#FFFFFF");
	getId("ckh"+i).value=((lstfic[i])?lstficinfo[i].code:"");
	omovertr(i);
	omouttr(i);
}
function omclicktr(i){
	lstfic[i]=!lstfic[i];
	getId("tr"+i).style.backgroundColor=((lstfic[i])?"#BFCCE5":"#FFFFFF");
	getId("ckh"+i).value=((lstfic[i])?lstficinfo[i].code:"");
	omovertr(i);
}

var editencours=false;
var tdAvantEdit='';
var nomapres='';
var editid=0;

function getext(t){
	ext=t.split('.');
	if (ext.length==1 || ext[ext.length-1].length>8) return '';
	return ext[ext.length-1];
}
var keyupactif=true;

function keyup(e){
	if (keyupactif && suruntr){
		if (key(e)==113){
			ren(dessusi);
		}
		else if (key(e)==13){
			//location.href="/"+lstficinfo[dessusi].code+"-"+lstficinfo[dessusi].nom+".html";
		}
	}
}
function ren(i){
	nom=getId("noma"+i).innerHTML;
	nomtd=getId('nom'+i);
	tdAvantEdit=nomtd.innerHTML;
	editencours=true;
	keyupactif=false;
	nomtd.innerHTML='<input type="text" size="50" maxlength="200" value="'+nom+'" id="nomi'+i+'" onkeyup="if (key(event)==27) rencancel('+i+');if (key(event)==13) this.onblur()" onblur="renfin('+i+');" style="border:1px solid #C33"/>';
	getId('nomi'+i).focus();
}

function rencancel(i){
	keyupactif=true;
	nomtd=getId('nom'+i);
	nomtd.innerHTML=tdAvantEdit;
	editencours=false;
}

function renfin(i){
	nomapres=getId("nomi"+i).value;
	editid=i;
	var arenommer=(lstficinfo[i].nom!=nomapres);
	if (getext(lstficinfo[i].nom)!=getext(nomapres)){
		arenommer=confirm("Attention, vous allez changer l'extension du fichier, cela risque de rendre le fichier inutilisable.");
	}
	if (arenommer){
		ajaxon("/moncompte/ren.php","POST","ti1caid="+lirecookie('ti1caid')+"&c="+lstficinfo[i].code+"&nom="+esca(nomapres),false,renjson);
	}
	else{
		renjson('{etat:"ko"}');
	}
	keyupactif=true;
}

function renjson(json){
	var o=eval('('+json+')');
	
	if (o.etat=="ok"){
		lstficinfo[editid].nom=nomapres;
	}
	else{
		if (o.cause) alert(o.cause);
	}
	
	var nomtd=getId('nom'+editid);
	nomtd.innerHTML='<a href="/'+lstficinfo[editid].code+'-'+lstficinfo[editid].nom+'.html" id="noma'+editid+'">'+lstficinfo[editid].nom+'</a>';
	editencours=false;
}

function deljson(json){
	var o=eval('('+json+')');

	if (o.etat=="ok"){
		selall=true;
		seltous();
		document.location.reload();
	}
	else{
		if (o.cause) alert(o.cause);
	}
}
function jsonalbum(json){
	var o=eval('('+json+')');

	if (o.etat=="ok"){
		location.href=o.url;
	}
	else{
		if (o.cause) alert(o.cause);
	}

}
function key(e){
	var var_key;
	if (window.event && window.event.keyCode) return window.event.keyCode;
	if (e.which) return e.which
	return -1;
}

function getListeSel(sep){
	if (!sep) sep=',';
	var res='';
	var trouve=false;
	for (i=lstfic.length-1;i>=0;i--){
		if (lstfic[i]){
		 res+=(trouve?sep:'')+lstficinfo[i].code;
		 trouve=true;
		}
	}
	if (trouve) return res;
	return false;
}


function popin(titre,contenu,w,h){
	getId('popin').innerHTML='<div style="text-align:center;font-weight:bold;background-color:#039;color:#FFF"><a href="#" onclick="getId(\'popin\').style.display=\'none\';"><img src="/img/close.gif" align="right" border="0" /></a>'+titre+'</div>'+contenu+'<div style="text-align:center;><a href="#" onclick="getId(\'popin\').style.display=\'none\';">fermer</a></div>';
	getId('popin').style.display="block";
	window.scrollTo(0,0);
}
function getImgMini(i){
	return "http://m"+lstficinfo[i].serveur+"/"+lstficinfo[i].code+".jpg";
}
function getImgMiniHTML(i){
	return '<img src="'+getImgMini(i)+'" />';
}
function doAction(type){
	var sel=getListeSel();
	if (!sel){
		alert('Veuillez sélectionner des fichiers');
	}
	else{
		if (type=="del"){
			if (confirm("Attention, les fichiers sélectionnés ne seront plus accessibles par les liens fournis à vos amis !")){
				ajaxon("/moncompte/del.php","POST","ti1caid="+lirecookie('ti1caid')+"&codes="+sel,false,deljson);
			}
			else{
				deljson('{etat:"ko"}');
			}
		}
		if (type=="eff"){
			if (confirm("Les fichiers ne seront plus associés à votre compte")){
				ajaxon("/moncompte/del.php?action=eff","POST","ti1caid="+lirecookie('ti1caid')+"&codes="+sel,false,deljson);
			}
			else{
				deljson('{etat:"ko"}');
			}

		}
		
		if (type=="ALBUM"){
			if (res=prompt("Quel nom souhaitez vous donner à votre album ?","")){
					ajaxon("/moncompte/getalbum.php","POST","ti1caid="+lirecookie('ti1caid')+"&codes="+sel+"&nom="+esca(res),false,jsonalbum);
			}
		}

		if (type=="link" || type=="forum" || type=="html"){
			var title="";
			var lien;
			var mini;
			var txt;
			if (type=="link") title="Lien vers les fichiers sélectionnés";
			if (type=="forum") title="Code forum pour les fichiers sélectionnés";
			if (type=="html") title="Code html pour les fichiers sélectionnés";
			
			popin(title,'<textarea id="tacode" style="width:590px;height:230px;margin:5px;"></textarea>');
			for (i=lstfic.length-1;i>=0;i--){
				if (lstfic[i]){
				  lien="http://ti1ca.com/"+lstficinfo[i].code+"-"+esca(lstficinfo[i].nom)+".html";
				  mini=getImgMini(i);
				  if (type=="link") txt=lien+"\n";
				  if (type=="forum") txt="[URL="+lien+"][IMG]"+mini+"[/IMG][/URL] ";
				  if (type=="html") txt='<a href="'+lien+'" target="_blank"><img src="'+mini+'" /></a> ';

				  getId('tacode').value+=txt;
				 
				 }
			}
			getId('tacode').select();
		}
	}
}



