//gallery functions
function initGallery() {
  gallery = {actX:0, container : '', actImage: '', bgTween:''};
  $('gallery_arrowleft').set('tween', {duration:200, transition: Fx.Transitions.Quart.easeOut});
  $('gallery_arrowleft').onmouseover = function() {this.tween("left",22)}
  $('gallery_arrowleft').onmouseout = function() {this.tween("left",27)}
  $('gallery_arrowleft').onclick = function() {
    gallery.container.set('tween',{transition: Fx.Transitions.Quart.easeOut, duration: 500});
    gallery.actX = gallery.actX + 684 > 0 ? 0 : gallery.actX + 684;
    gallery.container.tween("left",gallery.actX);
  }
  
  $('gallery_arrowright').set('tween', {duration:200, transition: Fx.Transitions.Quart.easeOut});
  $('gallery_arrowright').onmouseover = function() {this.tween("right",23)}
  $('gallery_arrowright').onmouseout = function() {this.tween("right",29)}
  $('gallery_arrowright').onclick = function() {
    gallery.container.set('tween',{transition: Fx.Transitions.Quart.easeOut, duration: 500});
    gallery.actX = gallery.actX - 684 < -gallery.container.getStyle('width').toInt() + 684 ? -gallery.container.getStyle('width').toInt() + 684 : gallery.actX - 684;
    gallery.container.tween("left",gallery.actX);
  }
  
  $('gallery_thumbnails_container').getElements('img').each(function(img) {
    img.onclick = function() { loadGalleryImage(this); }
  });
  
  gallery.container = $('gallery_thumbnails_container').getElement('.thumbnails');
  gallery.container.setStyle('left',0);
  gallery.container.tween('opacity',0,1);
  loadGalleryImage();
}
  
function loadGalleryImage(thumb) {
  if (!$defined(thumb)) {
    thumb = gallery.container.getElements('img')[0];
  }
  if (gallery.actImage == thumb) {
    return;
  };
  if (gallery.actImage != "") {
    var n = gallery.actImage.get('alt').split("_")[1];
    $("gallery_image_"+n).get('tween').cancel();
    $("gallery_image_"+n).set('opacity',0);
  }
  gallery.actImage = thumb;
  var num = thumb.get('alt').split("_")[1];
  if ($defined($("gallery_image_"+num))) {
    $("gallery_image_"+num).inject('gallery_images');
    $("gallery_bg").tween('height',$("gallery_image_"+num).offsetHeight.toInt()+ 120);
    $("gallery_image_"+num).set('opacity',0);
    setTimeout(function(){showGalleryImage($("gallery_image_"+num))},500);
  } else {
    var div = new Element('div');
    div.inject('gallery_images');
    
    var imgsrc = thumb.get('src').split("images/")[1];
    var myImage = new Asset.image('images/'+imgsrc, {
      onload: function() {
          div.set("id","gallery_image_"+num);
          div.addClass('gallery_image');
          this.inject(div);
          div.set('opacity',0);
          $("gallery_bg").tween('height',$("gallery_image_"+num).offsetHeight.toInt()+ 92);
          div.onclick = function() {
        }
        div.setStyle('cursor','pointer');
          setTimeout(function(){showGalleryImage(div)},500);
        }
    });
  }
  gallery.container.getElement(".gallery_selector").set('tween',{duration:500, transition: Fx.Transitions.Quart.easeOut});
  gallery.container.getElement(".gallery_selector").tween("left",thumb.offsetLeft.toInt());
}
  
function showGalleryImage(obj) {
  var n = gallery.actImage.get('alt').split("_")[1];
  if ("gallery_image_"+n == obj.get('id')) obj.tween('opacity', 0, 1);
}
