var rsCarousel = function (params) {

    var myself = this;

    this.slideShowId = params.slideShowId;
    this.slideWidth = params.slideWidth;
    this.slideHeight = params.slideHeight;
    this.autoplay = params.autoplay;
    this.autoplayInterval =  (params.autoplayInterval != undefined) ? params.autoplayInterval : 2000;
    this.showButtons =  (params.showButtons != undefined) ? params.showButtons : true;
    this.showBullets =  (params.showBullets != undefined) ? params.showBullets : true;
    this.bottomTabs =  (params.bottomTabs != undefined) ? params.bottomTabs : false;
    this.slidePath =  (params.slidePath != undefined) ? params.slidePath : "";
    this.fadeTransition =  (params.fadeTransition != undefined) ? params.fadeTransition : false;
	if (params.thumbPath != undefined) {
		this.thumbPath=params.thumbPath;
		this.showThumbs = true;
	    this.thumbWidth = params.thumbWidth;
    	this.thumbHeight = params.thumbHeight;
	} else { 
		this.thumbPath="";
		this.showThumbs = false;
	    this.thumbWidth = 0;
    	this.thumbHeight = 0;
	}
	this.playing=false;
	this.playTimeout=null;
	this.currentPosition=0;

    selector = '#' + this.slideShowId;
    $(selector).css( {
            'width' : this.slideWidth,
            'height': this.slideHeight+this.thumbHeight
            });
	// add the interface elements
	if (this.bottomTabs) $(selector).append('<div class="tabs"></div>');
	else  $(selector).prepend('<div class="tabs"></div>');

	if (this.showThumbs) {
		$(selector).append('<div class="thumbContainer"><div class="thumbs"></div></div>');
	    selector = '#' + this.slideShowId + ' .thumbContainer';
	    $(selector).css( {'width' : this.slideWidth});
	}
	
    selector = '#' + this.slideShowId + ' .slideshow';
	$(selector).prepend('<div class="buttonContainer"><div class="buttons"><span class="leftControl control">previous</span> <span class="rightControl control">next</span><span class="playControl control"></span></div><div class="bullets"></div></div>');

    selector = '#' + this.slideShowId + ' .slideContainer';
    $(selector).css( {
            'width' : this.slideWidth,
            'height': this.slideHeight
            });
    selector = '#' + this.slideShowId + ' .slide';
    this.slides = $(selector);
    this.numberOfSlides = this.slides.length;

// control visibility
selector = '#' + this.slideShowId + ' .buttons';
if (!this.showButtons) $(selector).css('display','none');
selector = '#' + this.slideShowId + ' .bullets';
if (!this.showBullets) $(selector).css('display','none');

selector = '#' + this.slideShowId + ' .buttonContainer';
if ((!this.showButtons)&&(!this.showBullets)) $(selector).css('display','none');
else $(selector).css({'top':this.slideHeight-60,'width':this.slideWidth});

//selector = '#' + this.slideShowId + ' p.copy';
//if (!this.showButtons) $(selector).css({'left' : 0,'top':(this.slideHeight-130)+'px'});
//else $(selector).css({'left' : 110+'px','top':(this.slideHeight-130)+'px'});
  // Wrap all .slides with #slideInner div
  this.slides
    .wrapAll('<div class="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({'float' : 'left','width' : this.slideWidth,'height':this.slideHeight});

  // Set .slideInner width equal to total width of all slides
selector = '#' + this.slideShowId + ' .slideInner';
  $(selector).css('width', this.slideWidth * this.numberOfSlides);

  // are tabs required?
selector = '#' + this.slideShowId + ' .tabs';
var tabcount=0;
for (i=0;i<this.slides.length;i++){
  	if (this.slides[i].title.length!=0){
		$(selector).append('<span class="tab lolight" id="'+i+'_'+myself.slideShowId+'_tab">'+this.slides[i].title+'</span>');
		tabcount++;
	} else $(selector).append('<span class="tab lolight" id="'+i+'_'+myself.slideShowId+'_tab" style="display:none;">'+i+'</span>');
}

if (tabcount<=1){ // show tab container
	// if thumbnail images are required, load them
	if (this.showThumbs) {
		selector = '#' + this.slideShowId + ' .thumbs';
		var $loopElement=null;
		for (i=0;i<this.slides.length;i++){
			$loopElement=this.slides.eq(i).clone().html().replace(this.slidePath,this.thumbPath);
			// here I stopped
			$(selector).append('<span class="thumb lolight" id="'+i+'_'+myself.slideShowId+'_thumb">'+$loopElement+'</span>');

		}
	    selector = '#' + this.slideShowId + ' .thumb';
    	this.thumbs = $(selector);
  		this.thumbs
    		.wrapAll('<div class="thumbInner"></div>')
    		// Float left to display horizontally
			.css({'float' : 'left'});

  		// Set .thumbInner width equal to total width of all thumbs
		selector = '#' + this.slideShowId + ' .thumbInner';
		$(selector).css('width', (this.thumbWidth+4) * this.numberOfSlides);

	}
	
	selector = '#' + this.slideShowId + ' .tabs';
	if (!this.showButtons) $(selector).css('display','none');
}

  // add bullets and set width of container
selector = '#' + this.slideShowId + ' .bullets';
for (i=0;i<this.slides.length;i++){
  $(selector).append('<span class="bullet" id="'+i+'_'+this.slideShowId+'">'+i+'</span>');
}
if (this.numberOfSlides>9) $(selector).css('width',this.numberOfSlides*6+'px');
else  $(selector).css('width',this.numberOfSlides*12+'px');

// define the function which sets the position and manages control visibility
 this.setPosition = function (position){
 	// grey old position bullet
	this.setBGPosition($('#'+this.currentPosition+'_'+this.slideShowId),'-90px -42px');
	this.clearHilightTab($('#'+this.currentPosition+'_'+this.slideShowId+'_tab'));
	if (this.showThumbs) this.clearHilightTab($('#'+this.currentPosition+'_'+this.slideShowId+'_thumb'));
	this.currentPosition=position;
    // grey left arrow if position is first slide
	selector = '#' + this.slideShowId + ' .leftControl';
	if(this.currentPosition==0) this.setBGPosition($(selector),'0 -42px');
	else this.setBGPosition($(selector),'0 0');
	// grey right arrow if position is last slide
	selector = '#' + this.slideShowId + ' .rightControl';
    if(this.currentPosition==this.numberOfSlides-1)this.setBGPosition($(selector),'-30px -42px');
	else this.setBGPosition($(selector),'-30px 0');
	// highlight new position bullet
	this.setBGPosition($('#'+this.currentPosition+'_'+this.slideShowId),'-90px 0');
	this.hilightTab($('#'+this.currentPosition+'_'+this.slideShowId+'_tab'));
	if (this.showThumbs) {
		this.hilightTab($('#'+this.currentPosition+'_'+this.slideShowId+'_thumb'));
		// if thumbs are showing, move them too if you can do that without showing blank space
		selector = '#' + this.slideShowId + ' .thumbInner';
		if (this.currentPosition<2){
		    $(selector).animate({'marginLeft' : 0}); 	
		} else if ((this.thumbWidth+3)*(this.numberOfSlides-this.currentPosition+1)>this.slideWidth){
		    $(selector).animate({'marginLeft' : (this.thumbWidth+3)*(1-this.currentPosition)-5}); 	
		}
	}
	selector = '#' + this.slideShowId + ' .slideInner';
	// actually make the move
	if (this.fadeTransition){
		$(selector).css({opacity : 0,'marginLeft' : this.slideWidth*(-this.currentPosition)}); 
		$(selector).animate({opacity : 1},1000); 
	} else {
		if (this.currentPosition!=0) $(selector).animate({'marginLeft' : this.slideWidth*(-this.currentPosition)}); 	
		else $(selector).css({'marginLeft' : this.slideWidth*(-this.currentPosition)}); 
	}
 }
this.setBGPosition = function (ctrl,newposition){
	ctrl.css('background-position',newposition);
}
this.clearHilightTab = function (ctrl){
	ctrl.removeClass('hilight');
	ctrl.addClass('lolight');
}
this.hilightTab = function (ctrl){
	ctrl.removeClass('lolight');
	ctrl.addClass('hilight');
}
  // Create event listeners for .controls clicks
  selector = '#' + this.slideShowId + ' .control';
  $(selector).bind('click', function(){
    // Determine new position
	if ($(this).hasClass('rightControl')) {
		if (myself.playing) myself.pause();
		if(myself.currentPosition<myself.numberOfSlides-1) myself.setPosition(myself.currentPosition+1);
	} else if ($(this).hasClass('leftControl')) {
		if (myself.playing) myself.pause();
		if(myself.currentPosition>0) myself.setPosition(myself.currentPosition-1);
	} else if ($(this).hasClass('playControl')){
		// handle play/pause
		if (myself.playing) myself.pause();
		else myself.play();
	}
  });

  // Create event listeners for .tab clicks
  selector = '#' + this.slideShowId + ' .tab';
  $(selector).bind('click', function(){
    // Determine new position
	var i=1*this.id.substring(0,this.id.indexOf('_'));
	if (myself.playing) myself.pause();
	myself.setPosition(i);
  });

  // Create event listeners for .bullet clicks
  selector = '#' + this.slideShowId + ' .bullet';
  $(selector).bind('click', function(){
    // Determine new position
	var i=1*this.id.substring(0,this.id.indexOf('_'));
	if (myself.playing) myself.pause();
	myself.setPosition(i);
  });
  
if (this.showThumbs){
  // Create event listeners for .thumb clicks
  selector = '#' + this.slideShowId + ' .thumb';
  $(selector).bind('click', function(){
    // Determine new position
	var i=1*this.id.substring(0,this.id.indexOf('_'));
	if (myself.playing) myself.pause();
	myself.setPosition(i);
  });

}
  // Hide left arrow control on first load
  this.setPosition(this.currentPosition);
  this.play=function(){
  	this.playing=true;
  	selector = '#' + this.slideShowId + ' .playControl';
  	this.setBGPosition($(selector),'-60px -42px');
	this.playTimeout=setTimeout(function(){
		if (myself.currentPosition<myself.numberOfSlides-1) {
			myself.setPosition(myself.currentPosition+1);
		} else {
			myself.setPosition(0);
		}
		myself.play();
	}
	,this.autoplayInterval);
  }	
  this.pause=function () {
	this.playing=false;
  	selector = '#' + this.slideShowId + ' .playControl';
	this.setBGPosition($(selector),'-60px 0');
	clearTimeout(this.playTimeout);
	}
// handle autoplay
if (this.autoplay) this.play();
}
