/* gallery slider */
jQuery.fn.createGallery = function() {

  return this.each(function() {
    var width = 0;
    sel = $('img', this);
    for (i in sel) {
      width += sel.eq(i).attr('width');
    }
    o = $('.gallery_container', this);
    o.css({width: width});
    o.get(0).step = 0;
  });
};

jQuery.fn.nextSlide = function() {
  return this.each(function() {
    imgs = $('img', this);
    width = imgs.first().width();
    o = $('.gallery_container', this);
    step = o.get(0).step;
    steps = imgs.length;
    step++;
    if (step >= steps) {
      step = 0;
    }
    o.get(0).step = step;
    left = -step * width;
    o.animate({left: left}, 500);
  });
};


jQuery.fn.previousSlide = function() {
  return this.each(function() {
    imgs = $('img', this);
    width = imgs.first().width();
    o = $('.gallery_container', this);
    step = o.get(0).step;
    steps = imgs.length;
    step--;
    if (step < 0) {
      step = steps - 1;
    }
    o.get(0).step = step;
    left = -step * width;
    o.animate({left: left}, 500);
  });
};


nextSlide = function() {
  $('.gallery').nextSlide();
}


$(document).ready(function(){
  $('.gallery').createGallery();

  $('.gallery_right_button').click(function() {
    $('.gallery').nextSlide();
  });

  $('.gallery_left_button').click(function() {
    $('.gallery').previousSlide();
  });

  $('.gallery').click(function() {
    clearInterval(document.timer);
  });

  document.timer = setInterval('nextSlide()', 5000);
});
