$(document).ready(function ()
{
	$(".scroller").scroller();
});

jQuery.fn.scroller=function ()
{
	$(this).each(function ()
	{
		var _n=Number($(this).attr("_n"));
		var _this=this;
		var _container=$(this).find(".scroller_container");
		var _container_mov=$(this).find(".scroller_container_mov");
		var _w=950;
		//$(_container).width(_w);
		var _scroller_1=$(_container).find(".scroller_1");
		var _w1=_w / _n;
		var _scroller_2=$(_container).find(".scroller_2");
		var _scroller_l=$(this).find(".scroller_l").hoverClass('scroller_l_over');
		var _scroller_r=$(this).find(".scroller_r").hoverClass('scroller_r_over');
		var _h = $(_container).height();
		if (_h != 0)
		{
			var _lr_top = _h/2 - 50 - 30;
			$(_scroller_l).css("margin-top", _lr_top+"px");
			$(_scroller_r).css("margin-top", _lr_top+"px");
		}
		
		$(_scroller_2).width(_w1).each(function ()
		{
			var _h2 = Number($(this).attr("_h"));
			var _margin_top = (_h - _h2) / 2;
			$(this).css("margin-top", _margin_top);
		});
		
		// Последнюю картинку влево
			var _last=$(_container_mov).find(".scroller_1:last");
			$(_container_mov).prepend(_last).css("left", -_w1);
		// Последнюю картинку влево End
		
		$(_container).scroller_1_pos({w1:_w1});
		/*$(_scroller_1).each(function (i)
		{
			var _l=i*_w1;
			$(this).css("margin-left", _l+"px").addClass("scroller_i_"+i);
		});*/
		
		var _move=false;
		
		// Update Color
			var _update_color=function ()
			{
				var _o=$(_this).find(".scroller_1:eq(1)");
				var _background=$(_o).attr("_background");
				var _color=$(_o).attr("_color");
				
				//set_color(_background, _color);
			};
		// Update Color End
		
		// L Click
			var _l_move = function ()
			{
				if (!_move)
				{
					_move=true;
					$(_container_mov).animate({left:-_w1*2}, 150, function ()
					{
						var _o=$(_container).find(".scroller_1:first");
						$(_container_mov).css("left", -_w1).append(_o);
						$(_container).scroller_1_pos({w1:_w1});
						_move=false;
						_update_color();
					});
				}
				
				return false;
			};
			$(_scroller_r).click(_l_move);
		// L Click End
		
		// R Click
			var _r_move = function ()
			{
				if (!_move)
				{
					_move=true;
					$(_container_mov).animate({left:0}, 150, function ()
					{
						var _o=$(_container).find(".scroller_1:last");
						$(_container_mov).css("left", -_w1).prepend(_o);
						$(_container).scroller_1_pos({w1:_w1});
						_move=false;
						_update_color();
					});
				}
				
				return false;
			};
			$(_scroller_l).click(_r_move);
		// R Click End
		
		
		// Авто перелистывание
			var _auto_start = function ()
			{
				$(_this).stopTime();
				
				$(_this).oneTime(15*1000, function ()
				{
					_l_move();
					_auto_start();
				});
			};
		
		_auto_start();
		
		
		$(this).mouseover(function ()
		{
			$(_this).stopTime();
		});
		$(this).mouseleave(function ()
		{
			_auto_start();
		});
		
		_update_color();
	});
	
	return this;
};

// Scroller_1_pos
	jQuery.fn.scroller_1_pos=function (params)
	{
		$(this).find(".scroller_1").each(function (i)
		{
			var _l=i*params.w1;
			$(this).css("margin-left", _l+"px").addClass("scroller_i_"+i);
		});
		
		return this;
	};
// Scroller_1_pos End

