function ajaxGallery(xml){
  if($('gallery')){
    slideDiapo();
    var ajax = new ajaxRequest(xml);
    ajax.callBack(function getGallery(data){
      var pictureGallery = new gallery(data.responseXML)
      pictureGallery.loader();
    });
    ajax.get();
  }
};
function gallery(data){

  $('gallery_menu').style.overflow = 'hidden';
  
  var photo = $('gallery_image');
  var photoBox = $('gallery_box');
  var photoDescription = $('gallery_description');
  var loadingImg = $('loading');
  var preLoader = [];
  var galleryMenu = $el($('gallery_menu'),'a');

  addEvents(galleryMenu,'click',function(event,fnc){
    galleryMenu.forEach(function(img,i){
      preLoader[i].src = '';
    });
    fnc.displayImage(this.id.replace('diapo_',''));    
    stopEvent(event);
  },this);
    
  this.loader = function(){
    galleryMenu.forEach(function(item,index){ 
      preLoader[index] = new Image();
      preLoader[index].src = data.getElementsByTagName('photo_'+item.id.replace('diapo_',''))[0].getElementsByTagName('src')[0].firstChild.nodeValue;
    });
  },
      
  this.displayImage = function(numIm){

    // display loading
    photo.style.display = 'none';
    loadingImg.style.display = 'inline';
  
    // recuper la bonne photo
    var xmlPhoto = Xml(data).tag('photo_'+numIm).first;
    var srcPhoto = Xml(xmlPhoto).tag('src').value;
    var descPhoto = Xml(xmlPhoto).tag('description').value;
    var widthPhoto = Xml(xmlPhoto).tag('width').value;
    var heightPhoto = Xml(xmlPhoto).tag('height').value;

    // charge l'image
    var imagesLoader=new Image();
    addEvent(imagesLoader,'load',function(event,fnc){         
      setTimeout(function(){
        var fx = new Fx;
        fx.initialize(photo,{duration:800,curve:['cos']});
        loadingImg.style.display = 'none';      
        photoDescription.innerHTML = (descPhoto)? descPhoto :'';// description
        setStyles(photo,{'width':widthPhoto+'px','height':heightPhoto+'px','display':'inline','opacity':0})
        photo.id = 'gallery_image';
        photo.src = srcPhoto;
        fx.start({'opacity':[0,1]});
        fnc.loader();
      }, 100);                
    },this);
    imagesLoader.src=srcPhoto;
    
  }
}

function slideDiapo(){

  var galleryMenu = $('gallery_menu');
  var gallerySlide = $('gallery_slide');
  var leftArrow = $('left_arrow');
  var rightArrow = $('right_arrow');

  var leftPosition = findPos(gallerySlide).left;

  setStyles(galleryMenu,{'position':'relative'});  
  setStyles(gallerySlide,{'overflow':'hidden'});
   
  var scrollFx = new scrollBox();
  
  this.set = function(){
    var galleryWidth = gallerySlide.offsetWidth;
    var galleryScroll = gallerySlide.scrollWidth;
    leftArrow.style.display = (galleryScroll > (galleryWidth+10))? 'block' : 'none';
    rightArrow.style.display = (galleryScroll > (galleryWidth+10))? 'block' : 'none';  
    scrollFx.set(gallerySlide);
  }
  
  //addEvent(window,'resize',function(event,fnc){fnc.set()},this);
  
  this.set();
  
  addEvent(gallerySlide,'mousemove',function(event){
    if(!hasClass(gallerySlide,'sliding')){
      cursorRatio = (Cursor(event).page.x - (leftPosition+(gallerySlide.offsetWidth/2)))/80;       
      scrollFx.init({'Left':cursorRatio});   
    }
  });
  addEvent(gallerySlide,'mouseleave',function(event){
  //alert('leave');
  scrollFx.stop()});
  
  addEvent(leftArrow,'mouseover',function(event){scrollFx.init({'Left':-3})});  
  addEvent(leftArrow,'mouseout',function(event){scrollFx.stop()});
  addEvent(rightArrow,'mouseover',function(event){
    addClass(gallerySlide,'sliding');
    scrollFx.init({'Left':3});
  });  
  addEvent(rightArrow,'mouseout',function(event){
    removeClass(gallerySlide,'sliding');
    scrollFx.stop()
  }); 
    
}

function scrollBox(){

  this.set = function(el){
    this.el = el;
    this.timer = false;
    this.limit = {
      'Top':(el.scrollHeight)-(el.offsetHeight),
      'Left':(el.scrollWidth)-(el.offsetWidth)
    }
  };
  
  this.init = function(move){
 
    this.slide = [];
    this.increase = [];    
    for(var pos in move){
      this.increase[pos] = move[pos];
      this.slide[pos] = this.el["scroll"+pos];
    }
  
    if(!this.timer){
      this.timer = setInterval(function(fnc) {
        return function(){
          for(var pos in move) fnc.move(pos);
        }
      }(this),30);
    }
  };
  
  this.move = function(pos){
    if (this.slide[pos]+this.increase[pos] < 0){
      this.stop();
      this.el['scroll'+pos] = 0;
      this.slide[pos] = 0;
    }else if (this.slide[pos]+this.increase[pos] > this.limit[pos]){
      this.stop();
      this.slide[pos] = this.limit[pos];
      this.el['scroll'+pos] = this.limit[pos];
    }else{        
      this.slide[pos] += this.increase[pos];
      this.el['scroll'+pos] = this.slide[pos];    
    }  
  }
  
  this.stop = function(){
    clearInterval(this.timer);
    this.timer = false;
  }

}

