var iDexSlider = function(autoDrag, scrollCont, scrollTrack, scrollBut, className){
	this.scrollCont = scrollCont;
	this.scrollTrack = scrollTrack;
	this.scrollBut = scrollBut;	
	this.scrollDirection = 'right';	
	this.autoDrag = autoDrag;
	this.arrowClass = '.aDragStop';
	
	
	//инициализируем слайдер
	this.SliderInit = function(){
		var thisObj = this;
		 
		if(this.autoDrag){
			this.MakeAutoDrag();
			this.StopAutoDrag();
		}
		 
		//узнаем ширину содержимого блока с прокручиваемыми элементами
		this.scrollContWidth = $(this.scrollCont).width();		
		//узнаем ширину блока с полосой прокрутки
		this.scrollTrackWidth = $(this.scrollTrack).width();		
		//вычисляем коэфициент отношения 
		this.scrollCoeff = parseFloat(this.scrollContWidth / this.scrollTrackWidth);
		//вычисляем размер бегунка			
		if(this.scrollCoeff > 1){		
			this.scrollSize = (this.scrollTrackWidth / this.scrollCoeff) + Math.round(23 / this.scrollCoeff);		
		} else {
			this.scrollSize = 0;
		}
		//устанавливаем ширину бегунка прокрутки
		$(this.scrollBut).width(this.scrollSize);
		
		$(thisObj.scrollBut).draggable({
			axis:'x',
			drag: function(event, ui){				
				if(ui.position.left+thisObj.scrollSize > thisObj.scrollTrackWidth){
					ui.position.left = thisObj.scrollTrackWidth - thisObj.scrollSize;
				}
				
				if(ui.position.left >= 0){
					$(thisObj.scrollCont).css('left', ui.position.left*thisObj.scrollCoeff*-1);
					thisObj.SlideLeft = ui.position.left;
				} else {
					thisObj.SlideLeft = 0;
					ui.position.left = 0;
					$(thisObj.scrollCont).css('left', 0);
				}								
			}, 
			stop: function(){				
				var LeftMargin = thisObj.SlideLeft*thisObj.scrollCoeff;	
				var SetLeft = Math.round(LeftMargin / thisObj.scrollStep)*thisObj.scrollStep;				
				thisObj.SlideLeft = SetLeft / thisObj.scrollCoeff;
				
				$(thisObj.scrollBut).css({'left': thisObj.SlideLeft});
				$(thisObj.scrollCont).animate({'left': SetLeft*-1});
			}
		});
	}	
	
	//функция для перемещения вправо
	this.RightArrowClick = function(inAuto){
		if(this.scrollCoeff > 1){
			//прерываем анимации
			$(this.scrollCont).stop(true, true);
			$(this.scrollBut).stop(true, true);			
			
			//получаем текущий отступ слева
			if(parseFloat(this.SlideLeft) < 0 || isNaN(parseFloat(this.SlideLeft))) this.SlideLeft = 0;			

			var dec = this.scrollStep / this.scrollCoeff;
			var newLeft = dec + this.SlideLeft;
			
			if(newLeft+this.scrollSize > this.scrollTrackWidth ){
				newLeft	= this.scrollTrackWidth-this.scrollSize;
			}
			
			//перемещаем слайдер
			$(this.scrollBut).animate({'left': newLeft});
			$(this.scrollCont).animate({'left': newLeft*this.scrollCoeff*-1});

			//сохраняем новый отсуп слева
			this.SlideLeft = newLeft;
			
			if(newLeft == this.scrollTrackWidth-this.scrollSize){
				return false;	
			} else {
				return true;	
			}
		}
	}
	
	//функция для перемещения влево
	this.LeftArrowClick = function(inAuto){	
		if(this.scrollCoeff > 1){
			//прерываем анимации
			$(this.scrollCont).stop(true, true);
			$(this.scrollBut).stop(true, true);	
			
			//получаем текущий отступ слева
			if(parseFloat(this.SlideLeft) < 0 || isNaN(parseFloat(this.SlideLeft))) this.SlideLeft = 0;	
			
			var dec = this.scrollStep / this.scrollCoeff;
			var newLeft = this.SlideLeft - dec;
			
			if(newLeft < 0){ newLeft = 0; }
			
			//перемещаем слайдер
			$(this.scrollBut).animate({'left': newLeft});
			$(this.scrollCont).animate({'left': newLeft*this.scrollCoeff*-1});
			
			//сохраняем новый отсуп слева
			this.SlideLeft = newLeft;
			
			if(newLeft == 0){
				return false;
			} else {
				return true;	
			}
		}
	}
	
	//функция инициализирующая автоклики
	this.MakeAutoDrag = function(){	
		var thisObj = this;
		this.dragTimer = setInterval("window['"+className+"'].MoveScroll()", this.autoDragTime);		
	}
	
	//функция нажимающая на кнопки
	this.MoveScroll = function(){
		if(this.scrollDirection == 'right'){
			if(this.RightArrowClick(true)){
				this.scrollDirection = 'right';
			} else {
				this.scrollDirection = 'left';
				//this.LeftArrowClick(true);
			} 
		} else if(this.scrollDirection == 'left'){
			if(this.LeftArrowClick(true)){
				this.scrollDirection = 'left';
			} else {
				this.scrollDirection = 'right';
				//this.RightArrowClick(true);
			}
		}
	}
	
	//функция остановки автолисталки при наведении курсора на стрелку
	this.StopAutoDrag = function(){
		var thisObj = this;	
		
		if(this.autoDrag){			
			$(this.arrowClass+', '+this.scrollBut).hover(function(){					
				clearInterval(thisObj.dragTimer);				
			}, function(){
				thisObj.MakeAutoDrag();
			});
		}
	}
}
