var IcaviaScroller = function($scrollingContainer, _galleryItemCount, _sliderWidth, options){

    var _settings = {
	    sliderArrows: null,
		scrollOffset : 0      ,
		postScrollCallback : null,  
		dataAttributeName: 'data-slide',
		autoRotateInterval:0,
		directNavContainer:null        
	
	};
	
	//console.log(options);
	var _autorotateHandle = {};
	var _currentGalleryIndex=0;
	
	if(options != null){
		$.extend(_settings, options);
	}
	   
	var gotoSlide = function(index){


		_currentGalleryIndex = index;               
		//calculate next index
		if(index<0){
			_currentGalleryIndex=_galleryItemCount-1};
		
		if(index>=_galleryItemCount){
			_currentGalleryIndex=0};        
			                                    
		//calculate index position in px
		var px = parseInt(_sliderWidth) * parseInt(_currentGalleryIndex)    ;       

		px = px * -1 + _settings.scrollOffset;        
        

		//console.log(_sliderWidth);
                  		
		$scrollingContainer.animate({left:px});
		

		if(_settings.sliderArrows != null){ 

			var $sliderArrows=_settings.sliderArrows;
			
		   	if(_currentGalleryIndex == 0 && _settings.btnsSlideshow.eq(0).hasClass('sprite-work-video-nav')){
				$sliderArrows.hide();
			}else{
				$sliderArrows.show();				
			}  
			
		   
		}       
		
		if(_settings.btnsSlideshow != null){ //update nav buttons                      
			_settings.btnsSlideshow.removeClass('current');    
			_settings.btnsSlideshow.eq(_currentGalleryIndex).addClass('current');      
		}
		
		if(_settings.postScrollCallback != null){
			_settings.postScrollCallback();
			
		}             
		
	};
	           
	

	var getCurrentIndex = function(){
	  return _currentGalleryIndex;  
	};
	
	var handleSliderArrowsClick = function(e){  
		//this will be the arrow that was clicked.

		var $sender = $(e.target);  

        //console.log();

		var nextIndex = parseInt(_currentGalleryIndex) + parseInt($sender.attr(_settings.dataAttributeName));



		gotoSlide(nextIndex);
		
	};
	
	var doAutoRotate = function(){
		
		
		if(_settings.autoRotateInterval>0){
		    var nextIndex = parseInt(_currentGalleryIndex) + 1;
			gotoSlide(nextIndex);          
		}else{
		   clearInterval(_autorotateHandle) ;
			
		}	
			
		
	};       
	
	var pauseRotation = function(){
		
		_settings.autoRotateInterval=0;
	};
	
	if(_settings.autoRotateInterval >0){
		_autorotateHandle = setInterval(doAutoRotate, _settings.autoRotateInterval);		
	}                        
  
	return  {handleSliderArrowsClick : handleSliderArrowsClick,  gotoSlide:gotoSlide, getCurrentIndex:getCurrentIndex, pauseRotation:pauseRotation} ;
	
}
