SlideShow = function (){
	this.init = function(qtdSlides,imagesName,controller,counter){
		// set the starting image.
		this.i = 0;			
		
		// The number of images in the array.
		this.numOfImages = qtdSlides;
			
		// The time to wait before moving to the next image. Set to 4 seconds by default.
		this.wait = 4000;
		
		this.imagesName = imagesName;
		this.controller = controller;
		this.counter = counter;
		
		this.initImages();
		
		// Start Slide Show
		this.startSlideShow();
	}
	
	// The Fade Function
	this.swapImage = function (x,y) {		
		$(this.image_slide[x]).appear({ duration: 0.5 });
		$(this.image_slide[y]).fade({duration: 0.5});
	}
	
	this.goNext = function() {
		clearInterval(this.play);
		
		if(this.controller){
			$('bt_js_play').hide();
			$('bt_js_pause').appear({ duration: 0});
		}else{
			this.startSlideShow();
		}
		
		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.goPrevious = function () {
		clearInterval(this.play);
		
		if(this.controller){
			$('bt_js_play').hide();
			$('bt_js_pause').appear({ duration: 0});
		}else{
			this.startSlideShow();
		}
		
		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--;
		}
		
		if(this.counter){
			this.updateCounter(this.i+1);
		}
	}
	
	this.goTo = function(to) {
		if((to-1)==this.i)
			return false;
			
		this.stop();
		
		if(this.controller){
			$('bt_js_pause').hide();
			$('bt_js_play').appear({ duration: 0});
		}
		
		var imageShow, imageHide;
		
		imageHide = this.i;
		this.i = to-1;
		imageShow = this.i;
		
		if (imageShow == this.numOfImages) {
			this.swapImage(0,imageHide);	
			i = 0;					
		} else {
			this.swapImage(imageShow,imageHide);			
		}
	
		if(this.counter){
			this.updateCounter(this.i+1);
		}
	}

	this.initSlide = 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++;
		}
		
		if(this.counter){
			this.updateCounter(this.i+1);
		}
	}
	
	this.stop = function () {
		clearInterval(this.play);	
		//
		if(this.controller){
			$('bt_js_pause').hide();
			$('bt_js_play').appear({ duration: 0});
		}
	}
	
	this.initImages = function() {
		this.image_slide = new Array();
		
		for(k=0;k<this.numOfImages;k++)
			this.image_slide[k] = (this.imagesName+'-'+(k+1));
	}
	
	// the onload event handler that starts the fading.
	this.startSlideShow = function () {
		var escope = this;
		//
		this.play = setInterval(function(){ escope.initSlide(); },this.wait);
		//
		if(this.controller){
			$('bt_js_play').hide();
			$('bt_js_pause').appear({ duration: 0});
		}
		if(this.counter){
			this.updateCounter(this.i+1);
		}
	}
	
	this.updateCounter = function (bt) {
		for(cn=1;cn<=this.numOfImages;cn++){
			$('bt_js_'+cn).style.background = "#B11116";
		}
		//
		$('bt_js_'+bt).style.background = "#000000";
	}
}
