/******************************************************************

  CRB Galery V2.3 - 2012-01-25 &copy; christophe Guillaume
  
*******************************************************************/  

var semaModeGalery =0 ; // activée et visible
var semaInitGalery =0 ; // initalisée
var semaPlayGalery =0 ;  // playing
var lockTimerGalery=0 ;  // le timer is running
var lockTimerRedraw=0 ;

var galerymarge=64;
var galerydelay=5000;

var semaCarouselLeft=0;
var semaCarouselRight=0;
var delta=50; 

var semaratata=0;
var cpt=0;
var pcaroussel=0, pcarousselprev='', pcarousselnext='', pcarousselfirst='', pcaroussellast='';
//var memoWinL= $(window).width(), memoWinH=$(window).height();



/* code a utiliser sur les articles de scribe pour lancer automatiquement : <script type="text/javascript">crb_galery_letsgo();</script> */ 

function crb_galery_letsgo() {
 $(document).ready( function() { 
  crb_winopen3( $('#img_mediatheque_1').attr('src').replace(/_2.jpg.*/,'_3.jpg'));
  setTimeout('if(pcarousselprev==pcaroussellast)crb_galery_playpause()',3000);
 });
}



 function crb_needtoscrollcarousel(p) {
     if ( p.offset().left + p.outerWidth(true) > $(window).width() ) $('#zgalcar').stop(true,true).animate({ left:$(window).width()-p.position().left - p.outerWidth(true) -5 }, 500);
   // sil est en dehors du cadre alors on va en x = - sa position + marge 
   if ( p.offset().left < p.width() ) {var x=-1*p.position().left +5 ;  $('#zgalcar').stop(true,true).animate({ left:x}, 500); }
}


function crb_galery_redraw() {


  // --- bascule displays 
      $('#zgaleryimg').fadeIn(150); $('#zgalerywait').hide();
      
      // reinit 
      $('#zgaleryimg').css('width','').css('height','');

      // --- resize image
      /*      if ( $('#zgaleryimg').width()  > $(window).width() -galerymarge ) { if ($(window).width()<300) $('#zgaleryimg').width(300); else $('#zgaleryimg').width( $(window).width() -galerymarge );}
      if ( $('#zgaleryimg').height() > $(window).height() -galerymarge) { if ($(window).height()<200) $('#zgaleryimg').height(200); else $('#zgaleryimg').height(  $(window).height() -galerymarge );}
      */
      if ( $('#zgaleryimg').width()  > $(window).width() ) { $('#zgaleryimg').css('width','100%'); }
      //      if ( $('#zgaleryimg').height() > $(window).height() -galerymarge) { if ($(window).height()<200) $('#zgaleryimg').height(200); else $('#zgaleryimg').height(  $(window).height() -galerymarge );}
      



      // --- center
      $('#zgaleryimg').offset({'left': ($(window).width()/2-$('#zgaleryimg').width()/2), 'top': ($(window).height()/2-$('#zgaleryimg').height()/2 + $(window).scrollTop())});
      recentre_wait_et_bigs(); 

     // recenter asap
     if ( !lockTimerRedraw) { lockTimerRedraw=1;setTimeout('crb_galery_redraw();lockTimerRedraw=0;',500); }

      
}


function crbCarouselLeft() {
    if ( semaCarouselLeft==1 && $('#zgalcar').position().left<  - delta/2 ) { 
    $('#zgalcar').animate({left:'+='+delta}, 100, 'linear', crbCarouselLeft ); }
}

function crbCarouselRight() {
    if ( semaCarouselRight==1 && $('#zgalcar').position().left + $('#zgalcar').width() > $(window).width() + delta/2 ) { 
    $('#zgalcar').animate({left:'-='+delta}, 100, 'linear', crbCarouselRight ); }
}
    

function crb_galery_playpause()  { 
  if ( semaPlayGalery==0) {  // --------------> autoplay : bascule bouton play pause, fadeout ctrlpanel et caroussel  
     semaPlayGalery=1; $('#zgalplay').hide(); $('#zctrlplay').hide();$('#zgalpause').fadeIn(0); 
     if (!lockTimerGalery) crb_galery_playing(); else crb_galery_next(); 
     $('#zctrlpanel').add($('#zgalcarbox')).fadeTo(1000,0.1);
     $('#zbigpause').stop(true,true); $('#zbigplay').stop(true,true).fadeTo(0,0.8).fadeOut(1500);
  } else { semaPlayGalery=0; $('#zctrlpanel').fadeTo(200,0.7);  $('#zgalplay').fadeIn(200);$('#zgalpause').hide(); $('#zbigplay').stop(true,true);  $('#zbigpause').stop(true,true).fadeTo(0,0.8).fadeOut(1500);  }
}
function crb_galery_playing() { lockTimerGalery=0; if (semaPlayGalery) { crb_galery_next(); lockTimerGalery=1; /*setTimeout('crb_galery_playing()',galerydelay);*/ }}
function crb_galery_next() { $('#zgalerywait').show();$('#zgaleryimg').hide().attr('src',pcarousselnext); crb_galery_carrousselit(); }
function crb_galery_prev() { $('#zgalerywait').show();$('#zgaleryimg').hide().attr('src',pcarousselprev); crb_galery_carrousselit(); }
function crb_galery_close() {if (semaPlayGalery)crb_galery_playpause(); $('#zdivgalery').hide(); semaModeGalery=0;}
function basename(s) { return (s.replace( /.*\// , '' ));}


function zgal_resize_zgalcar() {
    cpt=10;$('#zgalcar img').each( function() { cpt+=$(this).outerWidth(true); });
    if ( semaratata<cpt) { semaratata=cpt;$('#zgalcar').width(cpt).offset({'left':($(window).width()/2 - $('#zgalcar').width()/2)}); crb_galery_carrousselit();}
    
}

function crb_galery_carrousselit() { 
      // --------------------------------------------------------------------- carrousel it !
    
    pcarousselprev=pcarousselnext=pcarousselfirst=pcaroussellast='';hit=0;


      $('#zgalcar img').each( function() {

          // pointer next et last
	  if (hit && pcarousselnext=='') pcarousselnext= $(this).attr('alt');
	  if (hit) pcaroussellast = $(this).attr('alt');
	  
	  // eteindre l'ancien
	  if ( $(this).attr('actif') == 1 ) { $(this).css('border','1px solid #000').fadeTo(0,0.5).attr('actif',0); }
	  
	  // hit ?
          if ( basename($(this).attr('alt')) == basename($('#zgaleryimg').attr('src')) ) { 
	    $(this).css('border','1px solid #fff').fadeTo(200,1).attr('actif',1); hit=1;
	    
	    // scroll ?
	    crb_needtoscrollcarousel( $(this) );

	  }
	  
	  // pointer first et prev
	  if ( pcarousselfirst=='')pcarousselfirst=$(this).attr('alt');
	  if (!hit) pcarousselprev=$(this).attr('alt');
      });
      if ( pcarousselprev=='') pcarousselprev=pcaroussellast;
      if ( pcarousselnext=='') pcarousselnext=pcarousselfirst;

}


function recentre_wait_et_bigs() { 
    $('#zbigplay').add($('#zbigpause')).add($('#zgalerywait')).show().each(function() {
	    $(this).offset({'left':$(window).width()/2 - $(this).width()/2  ,'top': ($(window).height()/2 - $(this).height()/2 + $(window).scrollTop() ) }); }).hide();
}


function crb_winopen3(f) {

 // --- init : construction des elemts gfx et des hooks 
 if ( !semaInitGalery ) {

     ///images/communes/fond85.png
   $('body').append('<div style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background-color:#000;" id="zdivgalery"></div>');
   $('#zdivgalery').append('<div id="zgalerybloc" style="position:absolute;"><img id="zgalerywait" style="position:absolute;z-index:999" src="/images/galery/wait.gif"><img id="zgaleryimg" style="display:none;border:1px solid #000"></div>');
    
   $('#zdivgalery').append('<img id="zbigplay"  style="position:absolute;z-index:1000;display:none;height:128px" src="/images/galery/Play1Disabled.png">');
   $('#zdivgalery').append('<img id="zbigpause" style="position:absolute;z-index:1001;display:none;height:128px" src="/images/galery/PauseDisabled.png">');
    
    
   // --- bando carroussel en bas
    $('#zdivgalery').append('<div id="zgalcarbox" style="position:absolute;height:52px;width:100%;overflow:hidden;left:0;bottom:0;text-align:center;z-index:4096"></div>');
    $('#zgalcarbox').append('<div id="zgalcar" style="position:relative;padding:0;height:48px;top:0;left:0;"></div>');
			     
  // genere a la volee
    $('div.crbgaleryminis a img').each( function() {
      $('#zgalcar').append('<img alt="'+$(this).attr('src').replace(/_1.jpg.*/,'_3.jpg')+'" style="cursor:pointer;border:1px solid #000; margin:0 0.3em;height:48px" src="'+$(this).attr('src') + '">');
    });
    
    // clic et hover sur img du carousel : 
    $('#zgalcar img').fadeTo(0,0.5)
      .hover(function(){ if ($(this).attr('actif')!=1) { $(this).fadeTo(100,1); }}, 
         function(){ if ($(this).attr('actif')!=1) $(this).fadeTo(0,0.5);} )
      .click(function(){  // -------------------------------- clic sur caroussel : 
         if (semaPlayGalery) crb_galery_playpause(); 
         $('#zgalcarbox').fadeTo(200,1);
         if ($(this).attr('actif')!=1) { // --------------  loading : 
           $('#zgalerywait').show();$('#zgaleryimg').hide().attr('src',$(this).attr('alt')); crb_galery_carrousselit(); 
         }  
      });    
    $('#zgalcarbox').hover( function(){ $(this).fadeTo(200,1);} , function(){ $(this).fadeTo(300,0.2);} );
   
   
    // div sensible pour defiler le caroussel à la souris 
    $('#zgalcarbox').append('<div id="zgalcarboxs1" style="position:absolute;top:0;left:0;height:50px;width:20px;background-color:#f90;"></div>');
    $('#zgalcarboxs1').fadeTo(0,0.1).hover(function(){ $(this).fadeTo(100,0.5);semaCarouselLeft=1;  crbCarouselLeft(); }, function(){semaCarouselLeft=0; $(this).fadeTo(0,0); } );

    $('#zgalcarbox').append('<div id="zgalcarboxs2" style="position:absolute;top:0;right:0;height:50px;width:20px;background-color:#f90;"></div>');
    $('#zgalcarboxs2').fadeTo(0,0.1).hover(function(){ $(this).fadeTo(100,0.5); semaCarouselRight=1;  crbCarouselRight(); }, function(){semaCarouselRight=0;$(this).fadeTo(0,0); } );
    
   
    // recentre wait et zbigs
    $('#zgalerywait').add($('#zbigpause')).add($('#zgalerywait')).load(recentre_wait_et_bigs);



    // --- ctrl panel 
    $('#zdivgalery').append('<div style="position:absolute;top:0;width:100%;text-align:center,z-index:4096" id=zctrlpanel></div>');
    $('#zctrlpanel').hover(function(){ $(this).fadeTo(100,1);}, function(){if (1==1||semaPlayGalery) $(this).fadeTo(300,0.1);} );
    
    // --- bouton close : append + hover + clic 
    $('#zctrlpanel').append('<img id=zgalclose src="/images/galery/button_grey_close.png" alt="Fermer" title="Fermer (clavier : echap)" >');    
    $('img#zgalclose')
     .hover(function(){$(this).attr('src','/images/galery/button_delete_blue.png');}, function(){$(this).attr('src','/images/galery/button_grey_close.png');} )
     .click( crb_galery_close );



    // --- bouton play :
    $('#zctrlpanel').append('<img id=zgalplay src="/images/galery/Play1Disabled.png" alt="Lecture" title="Lecture (clavier : espace)">');
    $('#zgalplay')
     .hover(function(){$(this).attr('src','/images/galery/Play1Normal.png');}, function(){$(this).attr('src','/images/galery/Play1Disabled.png');} )
     .click( crb_galery_playpause);
     
    // --- bouton pause :
    $('#zctrlpanel').append('<img id=zgalpause src="/images/galery/PauseDisabled.png" style="display:none" alt="Pause" title="Pause (clavier : espace)">');
    $('#zgalpause')
     .hover(function(){$(this).attr('src','/images/galery/PauseHot.png');}, function(){$(this).attr('src','/images/galery/PauseDisabled.png');} )
     .click( crb_galery_playpause);



    // --- bouton next : append + hover + clic 
    $('#zctrlpanel').append('<img id=zgalnext src="/images/galery/NextNB.png" alt="Suivant" title="Suivant (clavier : droite)" >');
    $('img#zgalnext')
     .hover(function(){$(this).attr('src','/images/galery/Next.png');}, function(){$(this).attr('src','/images/galery/NextNB.png');} )
     .click(function(){if (semaPlayGalery)crb_galery_playpause();  crb_galery_next(); $('#zgalcarbox').fadeTo(200,1); } );
     
    // --- bouton previous : append + hover + clic 
    $('#zctrlpanel').append('<img id=zgalprev src="/images/galery/PreviousNB.png" alt="Précédent" title="Précedent (clavier : gauche)">');
    $('img#zgalprev')
     .hover(function(){$(this).attr('src','/images/galery/Previous.png');}, function(){$(this).attr('src','/images/galery/PreviousNB.png');} )
     .click( function(){if (semaPlayGalery)crb_galery_playpause();  crb_galery_prev(); $('#zgalcarbox').fadeTo(200,1); } );




    // skin it 
    $('#zctrlpanel img').css({'cursor':'pointer','padding':'1.5em', 'float':'right','height':'48px'});
    //    $('img#zgalclose').add( $('img#zgalplay')).add( $('img#zgalpause')).css({'margin-left':'2em'});
    //    $('img#zgalclose').css({'margin-right':'1em'});
    
    
    // clavier 
    $(document).keydown(function (e) {      
       var code=e.which||e.KeyCode;       
       if (code==37) { e.preventDefault();if (semaPlayGalery)crb_galery_playpause(); crb_galery_prev();  $('#zgalcarbox').fadeTo(200,1); }  // left
       else if (code==39) { e.preventDefault();if (semaPlayGalery)crb_galery_playpause(); crb_galery_next();  $('#zgalcarbox').fadeTo(200,1); }  // rigth
       else if (code==27) { e.preventDefault();crb_galery_close(); }  // echap
       else if (code==32) { e.preventDefault(); crb_galery_playpause(); }  // barre espace       
    });



   // hook sur resize window 
    $(window).resize( function(){ crb_galery_redraw();  });
    $(window).scroll( function(){ crb_galery_redraw();  });

  
   
   // clic sur la photo
    $('#zgaleryimg').click(function(e){$('#zctrlpanel').fadeTo(800,0.7); if (semaPlayGalery)crb_galery_playpause(); if (e.pageX < $(window).width()/2) crb_galery_prev(); else crb_galery_next();  $('#zgalcarbox').fadeTo(200,1); });
    $('#zgaleryimg').scroll(function(e){$('#zctrlpanel').fadeTo(800,0.7); if (semaPlayGalery)crb_galery_playpause(); if (e.pageX < $(window).width()/2) crb_galery_prev(); else crb_galery_next();  $('#zgalcarbox').fadeTo(200,1); });
   

    // --- hook : sur chargement img : il faut reorganiser l'affichage
    // ------------------------------------------------------------
    $('#zgaleryimg').load(function() {    

    // resize it 
    zgal_resize_zgalcar();

      crb_galery_redraw();
      if (semaPlayGalery) setTimeout('crb_galery_playing()',galerydelay);
    // crb_galery_carrousselit();
            
    });
    
    semaInitGalery=1;
 }
 
 
 // ---- bascule affichage on/off 
 if (!semaModeGalery) {
    $('#zdivgalery').fadeIn(200);
    semaModeGalery=1; 
 }
 
 // --- loading la photo demandee :
 $('#zgalerywait').show(); 
 $('#zgaleryimg').hide().attr('src','').attr('src',f );  crb_galery_carrousselit(); 

}

