var albumactive;
var conserve; 
var albumsema ;


var starttime=0 ;
var regif=0;



function waitforupload() {
 if ( document.getElementById('doc_c_fichier').value == '' &&  document.getElementById('doc_c_conserve1') == null) { 
// alert(  document.getElementById('doc_c_conserve') ) ;
    alert('Merci de sélectionner le fichier (document texte ou photo) à déposer sur notre serveur ...');
     document.getElementById('doc_c_fichier').focus();
  } else {
      var tstamp=new Date();

    setTimeout( 'putmsg(\'divuploadgo\',\'<big>Etape 1/2 : transfert en cours, merci de patienter ... <img src="pics/wait.gif" /></big>\')', 600); 
    
  //    zma_reload( 'upload_state', 'divuploadgo', 500 );
  p = document.getElementById('reagir_valider'); 
  if ( p!=undefined ) { p.value = ''; }
  document.getElementById('doc_c_fichier_box').style.display='none';
  document.getElementById('doc_mode').value = 'add';

      document.getElementById("progressbox").style.display='';
      starttime=parseInt( tstamp.getTime()/1000) ;
      setTimeout("getProgress('"+ document.getElementById('progress_key').value +"')", 100);

      document.getElementById('ff').submit();
      return true;
  }
}



function getProgress(id) {
    var xhr;
    var tstamp = new Date();
    var elapsedtime,totaltime,restetime;
    xhr=getxhr();

    if ( !regif) {
	regif=1;
	document.getElementById('waitgif').src='wait.gif';
    }

    xhr.onreadystatechange = function() {
	if (xhr.readyState == 4 && xhr.status== 200 ) {
//     trace(xhr.responseText);
	    var update = new Array();
	    update = xhr.responseText.split(',');

	    if ( update[0] > 0 ||  update[2] > 0 ) {
		var current  = update[0];
		var total = update[1];
		
		var convertOK =  update[2];
		var convertTOTAL =  update[3];
		
		// entre 2 : le convert a commencé : on reinitialize l'horloge
		if ( convertTOTAL > 0 && convertOK == 0 ) starttime=parseInt( tstamp.getTime()/1000) ;
		
		
		if ( convertTOTAL > 0 ) { // ---------------------------- convert progress
		  var pourcent = Math.ceil(( convertOK / convertTOTAL) *100);
		  var elapsedtime=parseInt(tstamp.getTime()/1000) - starttime;		   
		  var totaltime = parseInt(elapsedtime / (  convertOK / convertTOTAL)) ;
		  var restetime = totaltime - elapsedtime ;
				     
		  document.getElementById("progressinner").style.width = (pourcent*4) + 'px';
                  document.getElementById("progressbrasse").innerHTML = pourcent + '%';				   
		  document.getElementById("progressbrasse").innerHTML += ' ( ' + convertOK + ' / ' + convertTOTAL + ' - estimation temps restant : ' + format_time(restetime) + ' )';
		  
		  putmsg('divuploadgo','<big>Etape 2/2 : Création automatique des vignettes, merci de patienter ... <img src="pics/wait.gif" /></big>');
		  
		}
		else { // ----------------------------------------------- upload progress
		 var pourcent = Math.ceil((current/total) *100);
		 var elapsedtime=parseInt(tstamp.getTime()/1000) - starttime;
		 var totaltime = parseInt(elapsedtime / ( current/total)) ;
		 var restetime = totaltime - elapsedtime ;
		 var debit = Math.floor(current*8/elapsedtime);
		 document.getElementById("progressinner").style.width = (pourcent*4) + 'px';
		 document.getElementById("progressbrasse").innerHTML = pourcent + '%';

                 if ( pourcent<100 ) 
		  document.getElementById("progressbrasse").innerHTML += ' (Reste : ' + format_time(restetime) + ' - Débit : '+ format_taille(debit)   + ' )';
		 else  document.getElementById("progressbrasse").innerHTML += ' - Transfert terminé'; 

                 if ( pourcent >= 99 ) { 
                     putmsg('divuploadgo','<big>Etape 2/2 : Création automatique des vignettes, merci de patienter ... <img src="pics/wait.gif" /></big>');
		 }
                }
//     trace('ici');
	    }
	    
	    
//	      document.getElementById("progressbrasse").innerHTML += ' ( ' + convertOK + ' / ' + convertTOTAL; 
	    
	    setTimeout("getProgress('"+id+"')", 300);
	}
	
    };


    xhr.open( "POST", "/ajax_progress.php?t=" + tstamp.getTime(), true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("progress_key="+id+"&t="+tstamp.getTime());


}

function format_time( n) {
    if ( n > 3600 )  return ( Math.ceil(n/3600)+' h');
    if ( n > 60)  return ( Math.ceil(n/60)+' min');
    return (n + ' sec');
}

function format_taille ( n ) {
    if ( n == 0 || n == Infinity) return ('?');
    if ( n > 999999 ) return ( Math.ceil(n/1000000)+' Mb/sec');
    if ( n > 999 ) return ( Math.ceil(n/1000)+' Kb/sec');
    return ( n +' bits/sec');
}






function album_open_add_diagbox() {
document.getElementById('doc_c_titre').value='';
document.getElementById('div_album_add_box').style.display='';
}

function  close_album_dialog_box() {
document.getElementById('div_album_modif_box').style.display='none';
document.getElementById('div_album_modif').style.height =  '300px';
}

function doc_maj_filename(s ) {
   if (s!= '' && s!= undefined && document.getElementById('doc_c_titre').value =='' ) {
   var t = s.replace(/.*[\/\\]/, '');
    document.getElementById('doc_c_titre').value = t.replace( /\..*/, '' ) ; 
   }
}

function grossir( pa,pimg ) {
 pimg.style.border  = '1px solid #eaa';
 pimg.style.height='2em';
}

function reduire( pa,pimg ) {
 pimg.style.border  = 'none';
 pimg.style.height='';
}



function album_active2( id , mode ) {
  if ( mode == 'overvoir' && document.getElementById( id+'_tools' ) ) {
     document.getElementById( id+'_tools' ).style.display = '';
  }
  if ( mode == 'outvoir' && document.getElementById( id+'_tools' ) ) {
     document.getElementById( id+'_tools' ).style.display = 'none';
  }

}


function album_active( id , mode ) {

 var coloractif= '#fee';
 var colorinactif='#fff';
 var bordactif = '1px solid #888'; 
 var bordinactif = '1px solid #fff'; 
 
  
 if ( mode == 'overvoir' ) {
 

 if ( albumactive !=undefined ) {album_active( albumactive , 'outvoir' ); albumactive= undefined; }
 
  document.getElementById( id ).style.background  = coloractif;
  document.getElementById( id ).style.border  = bordactif;
  if ( document.getElementById( id+'_tools' ) ) {
     document.getElementById( id+'_tools' ).style.display = '';
     document.getElementById( id+'_tools' ).style.background  = coloractif;
     grossir(  document.getElementById( id+'_tools_voir' ),  document.getElementById( id+'_tools_voir_img' )); 
  }
  document.getElementById( id+'_brasse' ).style.display = '';
  albumactive = id ; 
/*  document.getElementById( id+'_brasse' ).innerHTML = 'EXPLORER cet album';*/
  
  
  
  } else  if ( mode == 'outvoir' ) {
  document.getElementById( id ).style.background = '#fff';
  document.getElementById( id ).style.border  = bordinactif;
  if ( document.getElementById( id+'_tools' )) {document.getElementById( id+'_tools' ).style.display = 'none';}
  document.getElementById( id+'_brasse' ).style.display = 'none';

  
  
  
  } else  if ( mode == 'outmod' ) {
//  document.getElementById( id ).style.background = '#fff';  
//  document.getElementById( id+'_tools' ).style.background  = '#faa';
    document.getElementById( id+'_brasse' ).innerHTML = 'EXPLORER cet album';
    
   grossir(  document.getElementById( id+'_tools_voir' ),  document.getElementById( id+'_tools_voir_img' ));   
   reduire(  document.getElementById( id+'_tools_mod' ),  document.getElementById( id+'_tools_mod_img' ));
    
 //   document.getElementById( id+'_tools' ).innerHTML='X';
  } else  if ( mode == 'overmod' ) {
//  document.getElementById( id ).style.background = coloractif;  
//    document.getElementById( id ).style.border  = bordactif;
    
//  document.getElementById( id+'_tools' ).style.background  = '#ccc';
  document.getElementById( id+'_brasse' ).innerHTML = 'MODIFIER cet album'; 
  
   reduire(  document.getElementById( id+'_tools_voir' ),  document.getElementById( id+'_tools_voir_img' ));   
   grossir(  document.getElementById( id+'_tools_mod' ),  document.getElementById( id+'_tools_mod_img' ));

  }      

}
 


function zma_album_refresh() {
    var xhr;
    xhr = getxhr();
 
    xhr.onreadystatechange = function() {        
         if (xhr.readyState == 4 && xhr.status == 200 ) {	  
   	    document.getElementById('d_doc').innerHTML = xhr.responseText;
               document.getElementById('div_album_modif_box').style.display='none';
	 }
    }; 
    xhr.open( "POST", "/ajaxd.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("doc_intoalbum="+ document.getElementById('doc_intoalbum').value +"&mod=album_refresh");
}


function zma_close(id) {
    document.getElementById(id).style.display='none';
}



function zma_album_desc_refresh() {
    var xhr;
    xhr = getxhr();
 
    xhr.onreadystatechange = function() {        
         if (xhr.readyState == 4 && xhr.status == 200 ) {	  
   	    document.getElementById('album_into_desc').innerHTML = xhr.responseText;
	    setTimeout( 'zma_close(\'div_album_modif_box\')', 1200);
	 }
    };
    xhr.open( "POST", "/ajaxd.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("mod=album_desc_refresh&doc_intoalbum="+document.getElementById('doc_desc_album_id').value);
}




function zma_album_alerter(album_id,mel,valider) {
    var xhr,msg;
    xhr = getxhr();
 
    xhr.onreadystatechange = function() {        
         if (xhr.readyState == 4 && xhr.status == 200 ) {
   	    document.getElementById('div_album_modif').innerHTML = xhr.responseText;
	    if ( xhr.responseText.match(/<zmok>/) ) {zma_album_desc_refresh(); }
	    else { document.getElementById('doc_partager_msg').focus();}
	 }
    }; 
    
    if (valider) { msg = escape(document.getElementById('doc_partager_msg').value); }     
        
    document.getElementById('div_album_modif_box').style.display='';	
    document.getElementById('div_album_modif').innerHTML = 'Chargement en cours ...  <img src="pics/wait.gif" />';
 
    xhr.open( "POST", "/ajaxd.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("mod=album_alerter&album_id="+album_id+"&album_alerter_mel="+mel+"&valider="+valider+"&msg="+msg  );
       
}



function zma_album_del(id) {
    var xhr;
    xhr = getxhr();
 
    xhr.onreadystatechange = function() {        
         if (xhr.readyState == 4 && xhr.status == 200 ) {	  
   	    document.getElementById('div_album_modif').innerHTML = xhr.responseText;
	    if ( xhr.responseText.match(/<zmok>/) ) {
	       document.getElementById('div_album_modif').innerHTML += '<br /><br />Mise à jour en cours, merci de patienter ...  <img src="pics/wait.gif" />';
	       setTimeout('zma_album_refresh()',2000);
	    }	    
	 }
    }; 
        
    document.getElementById('div_album_modif_box').style.display='';	
    document.getElementById('div_album_modif').innerHTML = 'Suppression en cours ...  <img src="pics/wait.gif" />';

    xhr.open( "POST", "/ajaxd.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("mod=album_del&album_id="+id);
       
}


function zma_doc_del(id) {
    var xhr;
    xhr = getxhr();
 
    xhr.onreadystatechange = function() {        
         if (xhr.readyState == 4 && xhr.status == 200 ) {	  
   	    document.getElementById('div_album_modif').innerHTML = xhr.responseText;
	    if ( xhr.responseText.match(/<zmok>/) ) {
	       document.getElementById('div_album_modif').innerHTML += '<br /><br />Mise à jour en cours, merci de patienter ...  <img src="pics/wait.gif" />';
	       setTimeout('zma_album_refresh()',200);
	    }
	 }
    }; 
        
    if (confirm('Certain de vouloir supprimer définitivement ce document ?')) {
     document.getElementById('div_album_modif_box').style.display='';	
     document.getElementById('div_album_modif').innerHTML = 'Suppression en cours ...  <img src="pics/wait.gif" />';

     xhr.open( "POST", "/ajaxd.php", true);
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     xhr.send("mod=doc_del&doc_id="+id);
    } 
}




function zma_album_modif(id) { 
    var xhr;
    xhr = getxhr();
 
    xhr.onreadystatechange = function() {        
         if (xhr.readyState == 4 && xhr.status == 200 ) {
	  
   	    document.getElementById('div_album_modif').innerHTML = xhr.responseText;
	 }
    }; 
        
    document.getElementById('div_album_modif_box').style.display='';	
 document.getElementById('div_album_modif').innerHTML = 'Chargement en cours ...  <img src="pics/wait.gif" />';
 
    xhr.open( "POST", "/ajaxd.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("mod=album_modif&album_id="+id);
       
}



function zma_album_modif_valider(id) { 
    var xhr, c_public;
    xhr = getxhr();
    
    
    if ( document.getElementById('doc_album_theme').value == '' ) {
      alert("Merci de préciser le nom de l'album svp ...");
       document.getElementById('doc_album_theme').focus(); 
       return false ;
    }						 
    xhr.onreadystatechange = function() {        
         if (xhr.readyState == 4 && xhr.status == 200 ) {
	 
	    document.getElementById('div_album_modif').innerHTML = xhr.responseText;
	    
	    if ( xhr.responseText.match(/<zmok>/) ) {
	       document.getElementById('div_album_modif').innerHTML += '<br /><br />Mise à jour en cours, merci de patienter ...  <img src="pics/wait.gif" />';
	       zma_album_refresh();
	    }
	    	 

	 }
    }; 
        
 
 if ( document.getElementById('doc_c_public1').checked ) c_public=1; 
 if ( document.getElementById('doc_c_public2').checked ) c_public=2; 
 if ( document.getElementById('doc_c_public3').checked ) c_public=3;
 
    xhr.open( "POST", "/ajaxd.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("mod=album_modif_valider&doc_album_id="+ document.getElementById('doc_album_id').value + 
               "&doc_album_theme=" + zmarecode(document.getElementById('doc_album_theme').value) + 
               "&doc_c_prive_amis=" + zmarecode(document.getElementById('doc_c_prive_amis').value) +
               "&doc_c_public=" + c_public );

    
 document.getElementById('div_album_modif_valider').innerHTML = 'Validation en cours ...  <img src="pics/wait.gif" />' ;

       
}


