// PHP
/*
	<div class=\"scroller\" _n=\"".$n."\" _w=\"600\">
							<div class=\"scroller_l\"></div>
							<div class=\"scroller_r\"></div>
							<div class=\"scroller_container\">
								<div class=\"scroller_container_mov\">
									
									<div class=\"scroller_1\"><div class=\"scroller_2\">
										Содержимое кадра
									</div></div>
						
								</div>
							</div>
						</div>
*/


$(document).ready(function ()
{
	$(".scroller").scroller();
});

var scroller_speed = 200;

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 _scroller_1 = $(_container).find(".scroller_1");
		//var _w1 = _w / _n;
		var _w1 = Number($(_this).attr("_w"));
		var _scroller_2 = $(_container).find(".scroller_2");
		var _scroller_l = $(this).find(".scroller_l");
		var _scroller_r = $(this).find(".scroller_r");
		var _count = $(_scroller_2).length;
		var _scroller_width = $(this).parent(".scroller_width");
		var _dots = Number($(this).attr("_dots"));
		var _dots_container;
		
		// I
			$(_scroller_1).each(function (i)
			{
				$(this).addClass("scroller_i_"+i).attr("_i", i);
			});
		
		// Ширина
			if ($(_scroller_width).length)
			{
				$(_this).css("position", "absolute");
				
				var scroller_resize = function ()
				{
					_w1 = $(_scroller_width).width();
					$(_this).width(_w1 - 40);
					$(_container_mov).width(_w1);
					$(_container).scroller_1_pos({w1:_w1, view_n:_view_n});
					$(_scroller_2).width(_w1);
					if (_count > 1)
					{
						$(_container_mov).css("left", -_w1);
					}
					$(_scroller_r).css("margin-left", (_w1 - 52) + 'px');
						$(_dots_container).width(_w1);
				};
				
				scroller_resize();
				$(window).resize(scroller_resize);
				$(document).oneTime(100, scroller_resize).oneTime(500, scroller_resize).oneTime(1000, scroller_resize);
			}
			else
			{
				$(_this).width(_w1);
				$(_container_mov).width(_w1);
			}
		
		// Высота
			var _h = Number($(this).attr("_h"));
			if (_h > 0)
			{
				$(_container).height(_h);
				$(_scroller_width).height(_h);
			}
		
		
		// Количество кадров сразу
			var _view_n = $(this).attr("_view_n");
			if ((_view_n == undefined) || (_view_n == ''))
			{
				_view_n = 1;
			}
			_w1 /= _view_n;
			$(_scroller_2).width(_w1);
			
			
		// Active I
			var get_active_i = function ()
			{
				return Number($(_this).find(".scroller_active").attr("_i"));
			};
		
		
		// Update Dots Active
			var update_dots_active = function ()
			{
				if (und(_dots_container))
				{
					$(_dots_container).find(".scroller_dot").removeClass("scroller_dot_active");
					var active_i = get_active_i();
					$(_dots_container).find(".scroller_dot_"+active_i).addClass("scroller_dot_active");
				}
			};
			
			
			// Элементы не помещаются
				if (_view_n < _count)
				{
					// Последнюю картинку влево
						var _last=$(_container_mov).find(".scroller_1:last");
						$(_container_mov).prepend(_last).css("left", -_w1);
					// Последнюю картинку влево End
					
					/*$(_scroller_1).each(function (i)
					{
						var _l=i*_w1;
						$(this).css("margin-left", _l+"px").addClass("scroller_i_"+i);
					});*/
					
					var _move=false;
					
					// R Click
						var move_r = function (n)
						{
							if (!_move)
							{
								if (n)
								{
									_move=true;
									$(_container_mov).animate({left:-_w1*2}, scroller_speed, 'linear', function ()
									{
										var _o=$(_container).find(".scroller_1:first");
										$(_container_mov).css("left", -_w1).append(_o);
										$(_container).scroller_1_pos({w1:_w1, view_n:_view_n});
										update_dots_active();
										_move=false;
										move_r(n-1);
									});
								}
							}
						};
						
						$(_scroller_r).click(function ()
						{
							move_r(1);
							
							return false;
						}).hoverClass("scroller_r_over").attr("onmousedown", "return false").mousedown(function () { return false; });
					// R Click End
					
					// L Click
						var move_l = function (n)
						{
							if (!_move)
							{
								if (n)
								{
									_move=true;
									$(_container_mov).animate({left:0}, scroller_speed, 'linear', function ()
									{
										var _o=$(_container).find(".scroller_1:last");
										$(_container_mov).css("left", -_w1).prepend(_o);
										$(_container).scroller_1_pos({w1:_w1, view_n:_view_n});
										update_dots_active();
										_move=false;
										move_l(n-1);
									});
								}
							}
						};
						
						$(_scroller_l).click(function ()
						{
							move_l(1);
							
							return false;
						}).hoverClass("scroller_l_over").attr("onmousedown", "return false").mousedown(function () { return false; });
					// L Click End
					
					
					// Dots
						if (_dots)
						{
							var _dots_container2 = $("<div class=\"scroller_dots_container2\"></div>");
							_dots_container = $("<div class=\"scroller_dots_container\"></div>").append(_dots_container2);
							$(_this).append(_dots_container);
							//$(_dots_container).css("margin-top", _h+'px');
							
							var add_dots = function ()
							{
								var _w = 0;
								for(i = 0; i<_count; i++)
								{
									var _dot = $("<div class=\"scroller_dot scroller_dot_"+i+"\" _i=\""+i+"\"></div>").click(function ()
									{
										var active = get_active_i();
										var I = Number($(this).attr("_i"));
										if (I < active)
										{
											//alert(active +'-'+ I);
											move_l(active - I);
										}
										else if (I > active)
										{
											//alert(I +'-'+ active);
											move_r(I - active);
										}
									});
									$(_dots_container2).append(_dot);
									_w += 35;
								}
								
								$(_dots_container2).width(_w);
							};
							
							add_dots();
						}
				}
			
			
			// Элементы помещаются
				else
				{
					$(_scroller_l).remove();
					$(_scroller_r).remove();
					
					$(_container_mov).css("left", 0);
				}
			
			
			$(_container).scroller_1_pos({w1:_w1, view_n:_view_n});
			update_dots_active();
	});
	
	return this;
};



// Scroller_1_pos
	jQuery.fn.scroller_1_pos=function (params)
	{
		if (1)
		{
			$(this).find(".scroller_1").removeClass("scroller_active").each(function (i)
			{
				var _l = i*params.w1;
				if (i == 1)
				{
					$(this).addClass("scroller_active");
				}
				$(this).css("margin-left", _l+"px")/*.addClass("scroller_i_"+i).attr("_i", i)*/;
			});
		}
		
		return this;
	};
// Scroller_1_pos End

