  GalleryViewer = function(id, options) {
    this.id = '#' + id;
    this.galleries;
    this.max;
    this.cIndex;
    this.cGallery;
    
    if (options != undefined) {
      jQuery.extend(this, options);
      this.init();
    }
  };
  
  GalleryViewer.prototype = {
    updateCurrentImage: function(img) {
      if (img.image == undefined) {
        img.image = jQuery('<img/>').attr('src', img.url + '360.240.jpg');
      }
      
      jQuery(this.id + ' .gallery_image').attr('src', img.image.attr('src'));
      jQuery(this.id + ' .gallery_image_caption').text(img.caption);
    },
    
    updateCurrentGallery: function(thumb) {
      var index = thumb.attr('rel');
      if (index >= 0 && index < this.max) {
        this.removeSelected();
        this.cIndex = index;
        this.cGallery = this.galleries[this.cIndex];
      
        jQuery(this.id + ' .gallery_title').text(this.cGallery.title);
        jQuery(this.id + ' .gallery_headline').text(this.cGallery.headline);
        jQuery(this.id + ' .gallery_text').html(this.cGallery.text);
        thumb.addClass('selected');
      
        if (this.cGallery.images.length > 0) {
          this.updateCurrentImage(this.cGallery.images[0]);
        }
      
        var gIndexe = jQuery(this.id + ' .gallery_index').empty();
        for (i = 0; i < this.cGallery.images.length; i++) {
          if (i > 0) {
            gIndexe.append('&nbsp;|&nbsp;');
          }
          
          var current = this;
          gIndexe.append(
            jQuery('<a/>').text(i + 1).attr('href', '#').attr('rel', i).click(function(e) {
              e.preventDefault();
              current.updateCurrentImage(current.cGallery.images[jQuery(this).attr('rel')]);
            })
          );
        }
      }
    },

    removeSelected: function() {
      jQuery(this.id + ' .thumb').removeClass('selected');
    },
    
    init: function() {
      this.max = Math.min(this.max, this.galleries.length);
      
      var current = this;
      jQuery(this.id + ' .thumb').click(function(e) {
        e.preventDefault();
        current.updateCurrentGallery(jQuery(this));
      });

      if (this.galleries.length > 0) {
        this.updateCurrentGallery(jQuery(this.id + ' .thumb[rel=0]'));
      }
    }
  };