jQuery.fn.imageScroller = function(params){
	var p = params || {
		next:"buttonNext",
		prev:"buttonPrev",
		parent:"viewer",
		frame:"viewerFrame",
		width:100,
		child:"a",
		speed:800,
		speedWheel:100,
		typeAction:"click"
}; 
	
	var _typeAction=p.typeAction;
	var _btnNext = $("#"+ p.next);
	var _btnPrev = $("#"+ p.prev);
	var _imgFrameParent = $("#"+ p.parent);
	var _imgFrame = $("#"+ p.frame);
	var _width = p.width;
	var _child = p.child;
	var _auto = p.auto;
	var _speed = p.speed;
	var _speedWheel = p.speedWheel;
	var _itv;

	var left=function() {
		_imgFrame.find(_child+":first").appendTo( _imgFrame );
		_imgFrame.css("marginLeft", 0);
	};
	
	var right=function() {
		_imgFrame.find(_child+":last").clone().show().prependTo( _imgFrame );
		_imgFrame.css("marginLeft", -_width);
	}
	
	var turnLeft = function(){
		_btnPrev.unbind("click",turnLeft);
		_imgFrame.animate( {marginLeft:-_width}, _speed, '', function(){
			left();
			_btnPrev.bind("click", turnLeft);
			$('#viewerFrame a').lightBox();
		});
	};
	
	var turnRight = function(){
		_btnNext.unbind("click",turnRight);
		right();
		_imgFrame.animate( {marginLeft:0}, _speed, '', function(){
			_imgFrame.find(_child+":last").remove();
			_btnNext.bind("click", turnRight);
			$('#viewerFrame a').lightBox();
		});
	};	
		
	var  mouse=function (event, delta) {
		_imgFrame.unbind("mousewheel", mouse);	
	
		if (delta > 0) {
			right();
			_imgFrame.animate( {marginLeft:0}, _speedWheel, '', function(){
				_imgFrame.find(_child+":last").remove();
				_imgFrame.bind("mousewheel", mouse);
				$('#viewerFrame a').lightBox();
			});
		} else if (delta < 0) {
				_imgFrame.animate( {marginLeft:-_width}, _speedWheel, '', function(){
					left();
					_imgFrame.bind("mousewheel", mouse);
					$('#viewerFrame a').lightBox();
			});	
		}};
	
		var  mousePos=function (event) {
			event.stopPropagation();
			event.preventDefault();
		};
		
	if(_typeAction=='click') {
		_btnPrev.click( turnLeft );
		_btnNext.click( turnRight );
	
	} else if(_typeAction=='hover') {
		_btnPrev.hover(
        	function () { autoPlay ('left'); }, 
        	function () { autoStop();
      	});
		_btnNext.hover(
        	function () { autoPlay ('right');}, 
        	function () { autoStop();
      	});
	}
	
	var selectedObj=function() {
		_imgFrame.css("width",$("#"+ p.frame+" "+_child).length*_width+"px"); // set width with hidden element

		$("#"+ p.frame+" "+_child).each(function(i){
   			if($(this).is('#selected')) {
			   var _selectedObj=i+1;
			   var _centralObj=Math.round((parseInt(_imgFrameParent.css("width"))/_width)/2);
			   var _diffMove=_centralObj-_selectedObj; // если отриц, то назад, то если положит то вперёд
			   
			   if(_diffMove>0) {
				  	for(i=0; i<_diffMove+1; i++) {		
						right();
						_imgFrame.find(_child+":last").remove();
					}
			   }
			   			   
			   if(_diffMove<0) {
				   _diffMove=Math.abs(_diffMove);
					for(i=0; i<_diffMove; i++)	
						left();
			   }	
			}
		});
	}; // конец функции
		
	selectedObj(); // start search selected element
	_imgFrame.mousewheel(mouse); // mouse events
	_imgFrame.mousewheel(mousePos); // mouse events
		
	var autoPlay = function(type){
		if(type=='right')
	  		_itv = window.setInterval(turnRight, _speed+200);
		if(type=='left')
	  		_itv = window.setInterval(turnLeft, _speed+200);
	};
	
	var autoStop = function(){
		window.clearInterval(_itv);
	};
};