var thumWidth = 85;
var thumWidthLine = 300;
var thumView = 3;

var currentAnimation;
var currentTransition;
var currentImgUrl;
var currentImgB;
function alerieNavLeft(n){
  var bR = dojo.byId(n);
  var newPos = (galerieGetPos(bR.style.left) + thumWidth * thumView);
  if(!galMaxPos('left',newPos)) {
    galDoAnimation( n, newPos);
    //bR.style.left = newPos + "px";
  }
}
function galerieNavRight(n){
  var bR = dojo.byId(n);
  var newPos = (galerieGetPos(bR.style.left) - thumWidth * thumView);
  if(!galMaxPos('right',newPos)){
    galDoAnimation(n, newPos)
  }
}  
//, onEnd: galerieDisplay2(src, n)
function galerieDisplay(src, n){
  currentImgUrl = src;
  currentImgB = n;
  currentTransition = dojo.fadeOut({node: n,duration: 250, onEnd:function(){ galerieDisplay2()}});
  currentTransition.play();  
}

function galerieDisplay2(){
  dojo.byId(currentImgB).src=currentImgUrl;
  currentTransition = dojo.fadeIn({node: currentImgB,duration: 250});
  currentTransition.play(); 
}

function galerieGetPos(t){
  return new Number(t.replace("px",""));
} 

function galCenterThumb(n){
   var bR = dojo.byId(n);
   bR.style.left = Math.round(thumWidthLine/2 - (thumWidth * nbThumbTotal)/2)+"px";
}

function galMaxPos(dir, newPos){
  if(dir == "left"){
    if(newPos > 0) return true;
    else return false;
  }else if(dir == "right"){
    var minX = -1 * thumWidth * (nbThumbTotal - 1);
    if(newPos < minX) return true;
    else return false;
  }
  
}

function galInitDisplay(){
  if(thumView >= nbThumbTotal ){
    galCenterThumb("thumbP");
    galCenterThumb("thumbPZ");
    
    dojo.query(".navL").forEach(function(node, index, arr){
      node.style.visibility = "hidden";
    }); 
    dojo.query(".navR").forEach(function(node, index, arr){
      node.style.visibility = "hidden";
    });         
  }
}

function afficherZoom(src){
  openPopIn(560,540,'cPopInGal');
  //var src = dojo.byId('imgSlideShow').src.replace('.44.','.45.');
  var srcZ = dojo.byId('imgSlideShowZ').src;
  if(src != srcZ)
  galerieDisplay(src, 'imgSlideShowZ');
}

function galDoAnimation(n, newPos) {

  if(currentAnimation && currentAnimation.status() != "stopped"){
    return;//do not interrupt a running animation
  }

  currentAnimation = dojo.fx.slideTo({node: n,duration: 300, left: newPos, top:0});
  if (currentAnimation) {
    dojo.connect(currentAnimation, "onEnd", function(){
      currentAnimation = null;
    });
    currentAnimation.play();
  }
}
function openPopIn(width, height, name){
  var popIn = dojo.byId(name);
  var alpha = dojo.byId('fondAlphaNoir');
  if(document.compatMode && document.compatMode != "BackCompat"){
    var theHeight = document.documentElement.scrollHeight;
    var theHeight2  = document.documentElement.clientHeight
    if(theHeight2>theHeight) theHeight = theHeight2;
  }else{
    var theHeight = document.body.scrollHeight;
  } 
  alpha.style.cssText = 'display:block;height: '+theHeight + 'px';
  var offsetY = window.pageYOffset;
  if(isNaN(offsetY)) offsetY = document.documentElement.scrollTop; //IE6
  if(isNaN(offsetY)) offsetY = 0; //cas inconnu

  
  popIn.style.width = width+"px";
  popIn.style.height = height+"px";
  
  var top = (Math.round(height/2) - offsetY) * -1;
  popIn.style.marginLeft = "-"+Math.round(width/2)+"px";
  popIn.style.marginTop = top+"px";  

  
  alpha.style.display = "block";
  popIn.style.display = "block";   
}

function closePopIn(name){
  var popIn = dojo.byId(name);
  var alpha = dojo.byId('fondAlphaNoir');
  alpha.style.display = "none";
  popIn.style.display = "none"; 
}