var ImageGallery = Class.create({
  initialize: function(is, w, playb, pauseb, counter) {
    this.imageSlide  = is;
	this.numOfImages = this.imageSlide.length;
	this.wait = w;
	this.playButton = playb;
	this.pauseButton = pauseb;
	this.counter = counter;
	this.scope = this;
	this.i = 0;
  },
  swapImage: function(x,y) {
	$(this.imageSlide[y]).fade({duration: 0});
	$(this.imageSlide[x]).appear({ duration: 0.5 });
  },
  goNext: function() {
	clearInterval(this.scope.play);		
	if(!this.playButton.blank() && !this.pauseButton.blank()) {
		$(this.pauseButton).hide();
		$(this.playButton).appear({ duration: 0});
	}
	
	var imageShow, imageHide;

	imageShow = this.i+1;
	imageHide = this.i;
	
	if (imageShow == this.numOfImages) {
		this.swapImage(0,imageHide);	
		this.i = 0;					
	} else {
		this.swapImage(imageShow,imageHide);			
		this.i++;
	}

	this.updateCounter();
  },
  goPrevious: function () {
	clearInterval(this.scope.play);				
	if(!this.playButton.blank() && !this.pauseButton.blank()) {
		$(this.pauseButton).hide();
		$(this.playButton).appear({ duration: 0});
	}

	var imageShow, imageHide;
				
	imageShow = this.i-1;
	imageHide = this.i;
	
	if (this.i == 0) {
		this.swapImage(this.numOfImages-1,imageHide);	
		this.i = this.numOfImages-1;					
	} else {
		this.swapImage(imageShow,imageHide);			
		this.i--;
	}
	
	this.updateCounter();
  },
  play: function() {
	var imageShow, imageHide;

	imageShow = this.i+1;
	imageHide = this.i;
	
	if (imageShow == this.numOfImages) {
		this.swapImage(0,imageHide);	
		this.i = 0;					
	} else {
		this.swapImage(imageShow,imageHide);			
		this.i++;
	}
	this.updateCounter();
  },
  stop: function() {
	clearInterval(this.scope.play);				
	if(!this.playButton.blank() && !this.pauseButton.blank()) {
		$(this.pauseButton).hide();
		$(this.playButton).appear({ duration: 0});
	}
  },
  startSlideShow: function() {
	var scope = this;
	setInterval(function () { scope.play(); }, this.wait);
	if(!this.playButton.blank() && !this.pauseButton.blank()) {
		$(this.pauseButton).hide({ duration: 0});
		$(this.playButton).appear();
	}
	
	this.updateCounter();
  },
  updateCounter: function () {
	var textIn = this.i + 1 + ' de ' + this.numOfImages;
	if(!this.counter.blank()) {
		$(this.counter).update(textIn);
	}
  }
});

