if (!Ascribe) {
    var Ascribe = {};
}

if (typeof(Ascribe.Gallery)  == "undefined") {
  Ascribe.Gallery = function(config) {
    this.thumbsVisible;
    this.largeImages = [];
    this.xlargeImages = [];
    this.imagePos = 0;
    this.toggleInProcess;
    this.defaultPhotoContainer = config.defaultPhotoContainer || 'large-image-container';
  }

  Ascribe.Gallery.prototype = {
    showThumbs: function(b) {
  	  if(!this.toggleInProcess) {
    	  new Effect.Move($('gallery-thumbnails-container'), {
          x: 0,
          y: $('gallery-thumbnails-container').getDimensions().height,
          mode: 'relative',
          beforeStart: function() { this.toggleInProcess = true }.bind(this),
          afterFinish: function() { this.toggleInProcess = false; this.thumbsVisible = b; }.bind(this)
        });
      }
    },

    hideThumbs: function(b) {
      if(!this.toggleInProcess) {
        new Effect.Move($('gallery-thumbnails-container'), {
          x: 0,
          y: -$('gallery-thumbnails-container').getDimensions().height,
          mode: 'relative',
          beforeStart: function() { this.toggleInProcess = true }.bind(this),
          afterFinish: function() { this.toggleInProcess = false; this.thumbsVisible = b; }.bind(this)
        });
      }
    },

    toggleThumbs: function() {
      if (this.thumbsVisible == true) {
        this.hideThumbs(false);
      }
      else {
        this.showThumbs(true);
      }
    },

    loadImage: function(largePath, xlargePath) {
      this.imageFadeOut();
      $('default-photo').src = largePath;
      $(this.defaultPhotoContainer).href = xlargePath;
    },

    nextImage: function() {
      this.imagePos++;
      if (this.imagePos == this.largeImages.length) {
        this.imagePos = 0;
      }
      this.imageFadeOut();
    },

    prevImage: function() {
      this.imagePos--;
      if (this.imagePos < 0) {
        this.imagePos = this.largeImages.length - 1;
      }
      this.imageFadeOut();
    },

    loadingIndicator: function() {
      /*new Effect.Morph(this.defaultPhotoContainer, {
  			style: 'background-color: #000;',
        duration: 0.2
      });*/
      $('loading').show();
      if ($('view-full-size')) {
        $('view-full-size').update('&nbsp;');
      }
      this.imageDomUpdate();
      Event.observe('default-photo', 'load', this.imageFadeIn);
    },

    imageFadeOut: function() {
      new Effect.Opacity('default-photo', {
        from: 1,
        to: 0,
        duration: 0
      });
      this.loadingIndicator();
    },

    imageFadeIn: function(event) {
      $('loading').hide();
      /*new Effect.Morph(this.defaultPhotoContainer, {
  			style: 'background-color: transparent;',
        duration: 0.2
      });*/
      new Effect.Opacity('default-photo', {
        from: 0,
        to: 1.0,
        duration: 1
      });
      if ($('view-full-size')) {
        $('view-full-size').update('<img src="http://assets.ascribehq.com/integrations/images/misc/view_full_size.png" alt="View Full Size" />');
      }
      x = $('default-photo').getDimensions().height;
      $('prev-container').style.height = x + 'px';
      $('next-container').style.height = x + 'px';
    },

    imageDomUpdate: function() {
      $('default-photo').src = this.largeImages[this.imagePos];
      $(this.defaultPhotoContainer).href = this.xlargeImages[this.imagePos];
    },

    showViewFullSize: function() {
      $('view-full-size').show();
    },

    hideViewFullSize: function() {
      $('view-full-size').hide();
    }

  } // Ascribe.Gallery.prototype
} // if (typeof(Ascribe.Gallery)
