var Gallery = Class.create({
  initialize: function(options) {
    options = (options)?options:{};
    
    this.id = ++Gallery.uid;
    this.started = false;
    this.currentImage = null;
    this.images = [];
    this.container = null;
    
    if (Object.isArray(options.images)) { this.setImages(options.images); }
    if (options.container) { this.container = $(options.container); }
  },
  
  setImages: function(images) {
    this.images = [];
    if (!Object.isArray(images)) {
      return;
    }
    for (var i=0; i<images.length; ++i) {
      if (this.isValidImage(images[i])) {
        if (!images[i].title) {
          var s = images[i].file.lastIndexOf('/');
          var l = images[i].file.lastIndexOf('.') - startIndex;
          images[i].title = images[i].file.substr(s, l);
        }
        images[i].shrinkThumb = false;
        if (!images[i].thumbFile) {
          images[i].thumbFile = images[i].file;
          images[i].shrinkThumb = true;
        }
        
        this.images.push(images[i]);
      }
    }
  },
  
  start: function(location) {
    if (!location && !this.container) { return; }
    if (this.started || this.images.length < 1) { return; }
    
    if (location != null) { this.container = $(location); }
    if (!this.container) { return; }
    this.started = true;
    
    var galleryNode = new Element('div', {'id':'gallery'+this.id, 'class':'gallery'});
    
    for (var i=0; i<this.images.length; ++i) {
      var img = new Element('img', {'src':this.images[i].thumbFile, 'alt':this.images[i].title});
      if (this.images[i].shrinkThumb) {
        img.width = Gallery.maxThumbWidth;
        img.height = Gallery.maxThumbHeight;
      }
      galleryNode.appendChild(img);
    }
    
    this.container.appendChild(galleryNode);
  },
  
  showImage: function(imageId) {
    if (this.currentImage != null) {
      this.cancelImage();
    }
    
  },
  
  cancelImage: function() {
    
  },
  
  isValidImage: function(image) {
    if (!image || !Object.isString(image.file)) { return false; }
    return true;
  }
  
  
});
Gallery.maxThumbHeight = 200;
Gallery.maxThumbWidth = 200;
Gallery.uid = 0;