FuguGallery = new AJS.Class({
  imageContainer : new Array(),
  imageSources   : new Array(),
  imageElement   : null,
  imageDuration  : 3000,
  currentIndex   : 0,
  showTimer      : null,
  currentBg      : '',
  standardBg     : '',
  loadFinished : true,
  resetted : true,
  paused: false,
  
  // Constructor
  init : function(element, images, duration, standardBg) {
    this.imageElement  = element;
    this.imageElement.style.display = 'none';
    this.imageSources  = images;
    this.imageDuration = duration || this.imageDuration;
    this.standardBg    = standardBg;
    
    for (key in this.imageSources) {
      this.imageSources[key]['image'] = this.imageSources[key]['image'].replace(/\&amp;/g, '&');
    }
  },
  // Starts the timer
  startTimer : function() {
    this.stopTimer();
    this.showTimer = setTimeout(AJS.$b(this.showNext, this), this.imageDuration);
  },
  // Stops the timer
  stopTimer : function() {
    try {
      clearTimeout(this.showTimer);
      this.showTimer = null;
    } catch(e) { }
  },
  
  reset : function() {
    this.pause();
    this.resetted = true;
    if(this.currentIndex!=0){
      this.currentIndex = 0;
      this.handleImage();
    }
  },
  play : function() {
    this.paused = false;
    this.showNext();
  },
  pause : function(){
    this.stopTimer();
    this.paused = true;
  },
  isPaused : function(){
    return this.paused;
  },
  next : function() {
    this.showNext();
    this.pause();
  },
  previous : function(){
    this.showPrevious();
    this.pause();
  },
  // Show previous image if ready
  showPrevious : function() {
    if (!this.loadFinished) { return; }
    
    if(!this.resetted){
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.imageSources.length - 1;
      }
    }
    this.resetted = false;
    this.handleImage();
  },
  
  // Show next image if ready
  showNext : function() {
    if (!this.loadFinished) { return; }
    
    if(!this.resetted){
      this.currentIndex++;
      if (this.currentIndex >= this.imageSources.length) {
        this.currentIndex = 0;
      }
    }
    this.resetted = false;
    this.handleImage();
  },
  // Fade out the current image and go to this.showImage()
  handleImage : function() {
    var url = this.imageSources[this.currentIndex]['image'];

    //New url is equal to the currently visible url
    if(this.visibleURL == url){
      if (!this.paused) {
        if(this.showTimer==null){
          this.startTimer(); 
        }
        else {
          this.showNext();
        }
      }
      return; 
    }

    this.stopTimer();
    this.loadFinished = false;
       
    if(this.imageContainer[url]!=null){
      // Direct (is already loaded)
      this.hideImage(url);
    }
    else {
      // Load
      this.imageContainer[url] = new Image();
      AJS.AEV(this.imageContainer[url], 'load', AJS.$b(this.loadImageEvent, this, [url]));
      this.imageContainer[url].src = url;
    }
  },
  loadImageEvent : function(evt,url) {
    this.imageContainer[url].loaded = true;
    this.hideImage(url);
  },
  //Fade in the new image
  hideImage : function(url) {
    var bgColor = this.imageSources[this.currentIndex]['fade_color'];
    if(bgColor==null || bgColor.length==0){
      bgColor = this.standardBg;
    }
    this.imageElement.parentNode.style.backgroundColor = bgColor;
    AJS.fx.fadeOut(this.imageElement, {onComplete: AJS.$b(this.showImageEvent, this, [url])});
  },
  // Fade in the new image
  showImageEvent : function(evt,url,url1) {
    url = url1 || url;
    //The loaded url is not the current url!
    if(url!=this.imageSources[this.currentIndex]['image']){
      //return;
    }
    this.imageElement.src = this.imageContainer[this.imageSources[this.currentIndex]['image']].src;
    this.imageElement.style.display = 'block';
    AJS.fx.fadeIn(this.imageElement, {onComplete: AJS.$b(
        function (a,b,c) {
          var url = c || b;
          this.visibleURL = url;
          this.loadFinished = true;
          if (!this.paused) { this.startTimer(); }
    }, this, [url])});
  }
});