function Slideshow(target, options)
{ 
 this.slideshowArea =  $(target);
 this.slides        =  options.element ? this.slideshowArea.find(options.element) : this.slideshowArea.find('img');
 this.actualSlide   =  (options.actualSlide >= 0 && options.actualSlide < this.slides.length && options.actualSlide != null) ? options.actualSlide : 0;
 this.timeInterval  =  options.interval || 3000;  
 this.fadeIn        =  options.fadeIn || 2000;
 this.fadeOut    =  options.fadeOut || 3000 
 this.autoRun    =  options.autoRun == null ? true : options.autoRun ;
 this.intervalId    =  0;
 this.easing = options.animation || 'swing';

 this.slides.eq(this.actualSlide).css('z-index', 2).siblings().css({'opacity': 0, 'z-index': 1});} 

Slideshow.prototype.run = function()
{    
 var scope = this;

 if(this.slides.length > 0){

  if(this.autoRun == true){
   this.intervalId = setTimeout(function(){ scope.slide()}, this.timeInterval);
  }
 }
}

Slideshow.prototype.next = function(b)
{
 if(this.actualSlide < (this.slides.length - 1))
 { 
  this.slide(this.actualSlide + 1)
 } else {
  if(b) this.slide(0);
 }
}

Slideshow.prototype.prev = function(b)
{
 if(this.actualSlide > 0)
 { 
  this.slide(this.actualSlide - 1)
 } else {
  if(b) this.slide((this.slides.length - 1));
 }
}

Slideshow.prototype.pause = function()
{    
 this.autoRun = !this.autoRun; 
}

Slideshow.prototype.slide = function(slideNumber)
{ 
 if(this.actualSlide == slideNumber){ return false;}

 clearInterval(this.intervalId);
 var scope = this;

 if(slideNumber == null)
 { 
  slideNumber =  (this.actualSlide + 1) % this.slides.length;

 } else if(slideNumber < 0){

  slideNumber = 0;

 } else if(slideNumber >  this.slides.length){

  slideNumber = (this.slides.length - 1)
 }

 this.slides.eq(this.actualSlide).css('z-index', 1).stop(true).animate({opacity: 0}, this.fadeOut, this.easing)

 this.slides.eq(slideNumber).css({'z-index': 2, 'opacity': 0}).stop(true).animate({opacity: 1}, this.fadeIn, this.easing, function(){ 
   if(scope.autoRun)
   { 
    clearInterval(scope.intervalId);
    scope.intervalId = setTimeout(function(){ scope.slide()}, scope.timeInterval);   
   }});

 this.actualSlide = slideNumber; 
}

// GETTERS
Slideshow.prototype.isPaused = function()
{
 return !this.autoRun;
}

Slideshow.prototype.children = function()
{ 
 return this.slides;  
}
