function MediaGallery(_options) {
  var options = {
    container_id          : _options.container_id || "media-gallery",
    item_class            : _options.item_class || "media-gallery-item",
    thumb_container_id    : _options.thumb_container_id || "media-gallery-thumbs",
    thumb_class           : _options.thumb_class || "media-gallery-thumb",
    selected_thumb_class  : _options.selected_thumb_class || "media-gallery-thumb-selected",
    left_button_class     : _options.left_button_class || "media-gallery-thumbs-left",
    right_button_class    : _options.right_button_class || "media-gallery-thumbs-right",
    slideshow_delay       : _options.slideshow_delay || 6000
  };
  
  var fadein_delay = 500;
  var fadeout_delay = 200;
  var items_jq;
  var thumbs_jq;
  var pos, length;
  var self = this;
  var in_transit = false;
  var manual = false;

  // Private

  var showItem = function(new_pos) {
    if (in_transit)
      return;
    in_transit = true;

    $(thumbs_jq).removeClass(options.selected_thumb_class);
    $(thumbs_jq[new_pos]).addClass(options.selected_thumb_class);

    $(items_jq[pos]).fadeOut(fadeout_delay, function() {
	$(items_jq[new_pos]).fadeIn(fadein_delay);
	in_transit = false;
	pos = new_pos;
      });
  };

  // Public

  this.init = function() {
    items_jq = $("#" + options.container_id).find("div[class~='" + options.item_class + "']");
    thumbs_jq = $("#" + options.thumb_container_id).find("div[class~='" + options.thumb_class + "']");
    $(thumbs_jq[0]).addClass(options.selected_thumb_class);

    pos = 0;
    length = items_jq.length;

    thumbs_jq.each(function(index, node) {
	$(node).bind("click", function(event) {
	    manual = true;
	    self.show(index);
	  });
      });

    $("." + options.left_button_class).bind("click", function(event) {
	manual = true;
	self.prev();
      });

    $("." + options.right_button_class).bind("click", function(event) {
	manual = true;
	self.next();
      });

    return this;
  };

  this.show = function(new_pos) {
    if (pos == new_pos)
      return;
    showItem(new_pos);
  };

  this.next = function() {
    showItem((pos + 1) % length);
  };

  this.prev = function() {
    var next_pos = pos - 1;
    if (next_pos < 0)
      next_pos = length - 1;

    showItem(next_pos);
  };

  this.play = function() {
    function next_repeat() {
      if (manual)
	return;

      self.next();
      setTimeout(next_repeat, options.slideshow_delay);
    }

    setTimeout(next_repeat, options.slideshow_delay);

    return this;
  };

  // Used in startpage galleries.
  this.stop = function () {
    manual = true;
  };
}


