$(document).ready(function() {
  
  
  
  
  
  
  
  /* Gallery Switcher */
  
  var running = false;
  var galleryswitcher = null;
  
  $('#tpl_cont_wrapper .right .HP-pageblock.gallery.HP_owncontent').each(function(i, data) {
    
      $('<a class="button next " shape="rect"></a><a class="button prev" shape="rect"></a>').appendTo('.HP-pageblock-content',this);
    
    $('.BG_gallery', this).append(galleryswitcher);
    
    var gallery = {
      items:   $('<div class="gallery-items" />').append($('.BG_gallery a.BG-picture', this))
    };
    
    // 64 breite der Bilder
    gallery.items.css({
      'position': 'absolute',
      'width': 150 * parseInt(gallery.items.children('a.BG-picture').length)
    });
    
    $('.BG_gallery', this).append(gallery.items);
    
  });
  
  
  
  
  
  
  /* Button Next */
  $('#tpl_cont_wrapper .right .button.next').click(function() {
    
    if(running) return;
    
    var pos     = $(this).parent().find('.BG_gallery .gallery-items').position();
    var element = $(this).parent().find('.BG_gallery .gallery-items');
    
    if(element.width() >= (-1*pos.left) + (2*150)) {
      running = true;
      element.animate({'left': pos.left - 150}, function() { running = false });
    } else if(element.width() > (-1*pos.left) + 150) {
      running = true;
      element.animate({'left': (-1*(element.width() - 150))}, function() { running = false });
    }
  });
  
  
  
  
  
  
  
  /* Button Prev */
  $('#tpl_cont_wrapper .right .button.prev').click(function() {
      
    if(running) return;
    
    var pos     = $(this).parent().find('.BG_gallery .gallery-items').position();
    var element = $(this).parent().find('.BG_gallery .gallery-items');
    
    if((pos.left + 150) < 0) {
      running = true;
      element.animate({'left': pos.left + 150}, function() { running = false });
    } else if(pos.left != 0) {
      running = true;
      element.animate({'left': 0}, function() { running = false });
    }
  });
  
  
  
  
  
  
  
  
});

