/* ************** LOAD ************** */
/* ************** LOAD ************** */

function SameWidth(){
  
  var d = $("#ContentPhoto"); 
  var w = d.width();
  var i = 0, els = $("div.photos").not('moving');
  var nbtot = els.length;
  if(nbtot==0) return;
  var w2 = els.width();
  var nb = Math.floor(w/w2);
  var rest = w-(nb*w2);
  var margin = Math.floor( Math.floor( rest/(nb) ) /2 );
  els.height('');
  var maxHeight = 0; els.each(function(){ var h=$(this).height(); if(maxHeight<h) maxHeight=h; });
  els.css({'margin-right':margin+"px",'margin-left':margin+"px",height:maxHeight+"px"});
  
  if(ADMIN){
		$("div.adminCheck").each(function(){
			var elem = $(this), p = elem.parent(), Width = p.width(), Height = p.find('img').height();
			elem.css({width: Width, height: Height, 'z-index':'600', 'cursor':'pointer'}).click(function(){
				var el = $(this).find('input'); var atr = el.attr('checked'); 
				if(atr==true) atr=false; else atr=true; el.attr('checked',atr);
			});
		});
  }
  
};

// variables
var animateTextTime=null;
var nbLoadAjax=NbResult;
var CurrentPourcent=0;
var startLoad=false;
var loadBug=false;
var currentLoad=0;
var isShow=false;
var waitList=0;
var timerLoad=null;

function animateText(el,started,end,time,before,after){
	try{
		var elem = document.getElementById(el);
		if(elem){ $("#LoadBar2").css({"width": Number(started)*4}); var txt = ''; if(before) txt+=before; txt+=started; if(after) txt+=after; elem.innerHTML = txt; }
    if( started==end && started=='100' ){ showContent(); return; } started++;
	  if( started<=end ){ var nb = time/end; animateTextTime=setTimeout(function(){ animateText(el,started,end,time,before,after); },nb); }
	}catch(e){ showContent(); }
};
function tryUpdateLoad(){ try{ updateLoad(); }catch(e){ setTimeout(tryUpdateLoad,200); } };
function updateLoad(el){
  waitList++;
  try{ $(el).addClass('isload'); }catch(e){ }
  if(!startLoad){
    startLoading(); 
    startLoad=true;
  } 
};
timerLoad=setTimeout(function(){ 
	var els = $(".photos img").not('.isload');
	els.attr('src','images/blanc.gif').width('150px').css('background','#666');
	if(!ADMIN) els.parent().parent().remove(); 
},10000);
function startLoading(){

	if(currentLoad<waitList){

		clearTimeout(timerLoad);
		timerLoad=setTimeout(function(){ 
			var els = $(".photos img").not('.isload');
			els.attr('src','images/blanc.gif').width('150px').css('background','#666');
			if(!ADMIN) els.parent().parent().remove(); 
		},5000);
	
		currentLoad++;
		var nb = Math.round(100/nbLoadAjax); 

		var pourcent = CurrentPourcent + nb ;
		if(pourcent>100) pourcent=100;
		
		if(NbResult>0){
			var curr=(currentLoad>NbResult)?NbResult:currentLoad;
			$("#LoadBarElem").html("Photo "+(curr)+" sur "+(NbResult));
		}
		animateText('LoadBarPourcent',CurrentPourcent,pourcent,100,'','%');

	  CurrentPourcent=pourcent;

		setTimeout(startLoading,110);
		
	}else if( currentLoad < nbLoadAjax ){
	
    setTimeout(startLoading,110);
    
  }else{
   
		if(CurrentPourcent<100){ waitList++; startLoading(); }
		
	}
	
};
function loadPages(){
  
  if(isLoadPages){ return false; }else{ isLoadPages=true; }
	if(CurrentPage!=1){ 
    window.location.href = 'index.php';
	}else{
	
	  var url = window.location.href;
    var pos = url.indexOf('#');
    if( pos>0 ){ 
      CatID=url.substring(pos+1);
	  if( $("#Cat"+CatID).length==0 ){ 
		  CatID=$("#MenuLeft a:first").attr('rel');
		  var url = window.location.href;
		  var pos = url.indexOf('#');
		  if( pos>0 ) url=url.substring(0,pos);
		  window.location.href = url+"#"+CatID;
	  }
      $("#MenuLeft a").each(function(){
          var el = $(this); 
          if( el.attr('rel') == CatID ){ el.parent().addClass('select'); } 
          else{ el.parent().removeClass('select'); }
      });
    }
	getContentPage();
		
	}
	
};
function loadingPage(){

  var height = $(document).height(); var height2 = $(window).height(); var width = $(window).width();
	$("#loadPage2").css({ 'margin-top': ((height2/2)-30) }); isShow=false;
	$("#loadPage").show().css({ width: width, height:height, 'z-index':1000 });
	window.scrollTo(0,0);
	
};
function showContent(){
 
	if(isShow) return; isShow=true;
	clearTimeout(timerLoad);
	if(ADMIN){
	  	$(".adminCheck").remove();
	  	$("div.photos").each(function(){
	  		var el = $(this), ID = el.attr('rev'), style="block"; if(!MODEADMIN) style="none";
	  		el.append('<div class="adminCheck AdminZone" style="display:'+style+'"><label for="check'+ID+'">&nbsp;</label><span><input type="checkbox" class="delete" id="check'+ID+'" name="Photo[]" value="'+ID+'" /></span></div>');
	  	});
	  	if(MODEADMIN){
	      SwitchMode2(); 
	      SwitchMode2();  	
	  	}
	}
	try{ startBox(); SameWidth(); }catch(e){ };
  $("div.navig").hide();
	$("#loadPage").fadeOut(250);
	
};

/* ************** LOAD ************** */
/* ************** LOAD ************** */