var currentClick=null;

if(ADMIN){

  function loadConfig(){ $("#configBlock").empty().load('config.php'); };
  function toggleConfig(){
    if( $("#configBlock").length==0 ){
      $("#Contenu").append('<div id="configBlock" class="padding" style="display:none"></div>');
      loadConfig();
    }
    $("#Contenu > .padding").toggle();
  };


  function SwitchMode(){ 
	SwitchMode2(); var mode='invite';
	if( MODEADMIN ) mode='admin';
	$.get('ax.session.php',{mode:mode}); 
  };
  function SwitchMode2(){
  	if(MODEADMIN){ // Passage à invité
      MODEADMIN=false; $(".AdminZone").hide();
      $("#SwitchMode").html("MODE ADMIN");
      $("#MenuSort").sortable('disable');
      $("#MenuSort > .separe").remove();
      $("#MenuSort li.categ, #MenuSort li.souscat").css('cursor','auto');
      if( isVignette ) switchVignette();
    }else{ // Passage à admin
      MODEADMIN=true; $(".AdminZone").show();
      $("#SwitchMode").html("MODE INVITE");
      $("#MenuSort").sortable('enable');
      $("#MenuSort li.categ, #MenuSort li.souscat").css('cursor','move');
      CreateSepare();
    }
  };


  function DeletePhotos(){
    var ArrayEl=new Array(), ArrayID=new Array();
    $("input.delete").each(function(){
      var el = $(this);
      if( el.is(":checked") ){ 
        ArrayEl[ArrayEl.length] = el.parent().parent().parent();
        ArrayID[ArrayID.length] = el.val();
      }
    });
	if( ArrayEl.length==0 ) return;
	else if( ArrayEl.length==1 ) var msg = "la photo ?";
	else var msg = "les "+ArrayEl.length+" photos ?";
	if( confirm('Souhaitez-vous rééllement supprimer '+msg) ){
	    var ListID = ArrayID.join("|");
  		$.post('ax.delete.php',{ListID:ListID},function(e){
  		    for(i=0;i<ArrayEl.length;i++){ el=ArrayEl[i]; el.remove(); }
  		});
	 }
  };
  function EditPhotos(){
    var ArrayEl=new Array(), ArrayID=new Array();
    $("input.delete").each(function(){
      var el = $(this);
      if( el.is(":checked") ){ 
        ArrayEl[ArrayEl.length] = el;
        ArrayID[ArrayID.length] = el.val();
      }
    });
    var ListID = ArrayID.join("|");
	  if(ListID!=''){
		  var Width = $(window).width()-50;
	    var Height = $(window).height()-80;
		  openWin( Width, Height, 'edit_photos.php?List='+ListID );
	  }
  };
  function AddPhoto(){
    var Width = $(window).width()-50;
	  var Height = $(window).height()-80;
    openWin( Width, Height, 'upload/index.php?CatID='+CatID );
  };
  function AddGalerie(){
    openWin( 350, 200, 'edit_galerie.php?mode=add&CatID='+CatID );
  };
  function AddGalerie2(){
    openWin( 350, 200, 'edit_galerie.php?mode=add&CatID=0' );
  };
  function EditGalerie(){
    openWin( 350, 200, 'edit_galerie.php?mode=edit&CatID='+CatID );
  };
  function switchVignette(){
      if(isVignette){
          $("#ContentPhoto").removeClass('vignette');
          $(".photos > a > strong, .photos > a > br").show();
          $(".photos, .photos > a > img").width('');
          var text = 'Passer en mode vignette';
          isVignette=false;
      }else{
          $("#ContentPhoto").addClass('vignette');
          $(".photos > a > strong, .photos > a > br").hide();
          $(".photos, .photos > a > img").width(50);
          var text = 'Passer en mode normal';
          isVignette=true;
      }
      SameWidth();
      $(".SwitchVignette").val(text); 
  };
  function DeleteCat(){
    var cat = $(this).prev().text(); var msg="\nAttention, toutes les photos de celle-ci seront supprimées !";
    if( $(this).prev().is('.categ') ) msg="\nAttention, toutes les photos et sous-catégories de celles-ci seront supprimées !"; 
    if( confirm("Souhaitez vous supprimer la catégorie '"+cat+"' ?"+msg) ){
      var ID = $(this).attr('rel'); 
      if( currentClick==null ){ currentClick = $(this).parent();
        $.post('ax.delete_cat.php',{CatID:ID},function(e){
          
          if(e=="true"){
            var el = currentClick;
            if( el.is('.souscat') ) el.remove();
            else{
              var el2=el.next();
              while( el2.is('.souscat') ){
                var el3=el2; el2=el2.next(); el3.remove();
              }
              el.remove();
            }
            if( $("#MenuLeft li.select").length==0 ){
              var el = $( $("#MenuLeft a")[0] );
              CatID = el.attr('rel'); el.parent().addClass('select');
              reloadCat();
            }
            currentClick=null;
          }else alert("Erreur de suppression ..");
          
        });
      }
    }
  };


}

function menuClick(){
  $("#MenuLeft a").not('.DeleteCat').click(Menu);
  if(ADMIN) $("#MenuLeft a.DeleteCat").unbind('click',DeleteCat).click(DeleteCat);
}





$(document).ready(function(){

	loadingPage();
	menuClick();
	if(ADMIN){
    $("#ConfigAdmin").click(toggleConfig);
    $("#SwitchMode").click(SwitchMode);
    $("input.DeletePhotos").click(DeletePhotos);
    $("input.EditPhotos").click(EditPhotos);
    $("input.SwitchVignette").click(switchVignette);
    $("input.AddPhoto").click(AddPhoto);
    $("input.AddGalerie").click(AddGalerie);
    $("input.AddGalerie2").click(AddGalerie2);
    $("input.EditGalerie").click(EditGalerie);
    CreateSepare();
  }

  
});

addOnLoad('SameWidth');
if(ADMIN && !MODEADMIN){ 
  MODEADMIN=true;
  addOnLoad('SwitchMode2'); 
} 
window.onerror=function(){ return; };
