
(function($){$(function(){$.each($(".gridScroller"),function(sind,_scroller){var scroller=$(_scroller);var $$=function(){return scroller.find.apply(scroller,arguments);}
var slider=$$(".scroller");var bLeft=$$(".bLeft");var bRight=$$(".bRight");var brLeft=$$(".lButton");var brRight=$$(".rButton");var iul=$$("ul");var buttonsEnabled={left:0,right:0};slider.scrollLeft(0);var showLRButton=function(button){if(button.css('display')=='none'){button.show().css({opacity:0}).stop().animate({opacity:1},400,function(){button.css({'opacity':''});})}}
var hideLRButton=function(button){if(button.css('display')!='none'){button.stop().animate({opacity:0},400,function(){button.css({'opacity':''});button.hide();})}}
var checkNavigation=function(){var scrollLeft=slider.scrollLeft();var effectiveWidth=scroller.width();var ulWidth=iul.width();if(scrollLeft>0){buttonsEnabled.left=1;showLRButton(bLeft);}else{buttonsEnabled.left=0;hideLRButton(bLeft);}
if(ulWidth-scrollLeft>effectiveWidth){buttonsEnabled.right=1;showLRButton(bRight);}else{buttonsEnabled.right=0;hideLRButton(bRight);}}
var tm_scroll=0;var LI_index=0;var stopRequests=$$("ul li").length<3;var pauseRequests=0;function requestMore(){if(stopRequests){return;}
if(pauseRequests){return;}
pauseRequests=1;var liCount=$$("ul li").length-1;$.ajax({url:'ajax/slider-requiery/',data:{set:$$(".query").html(),counter:liCount},dataType:'json',complete:function(){pauseRequests=0;},error:function(){},success:function(data){stopRequests=data.cnt<6;if(data.cnt){var $tmp=$("<ul>"+data.html+"</li>");$.each($("li",$tmp),function(i,j){$(this).appendTo(iul);});checkNavigation();setTimeout(function(){tryRequestMore();},1);}}})}
function tryRequestMore(){var ulWidth=iul.width();var effectiveWidth=scroller.width();if((ulWidth-effectiveWidth)<0){requestMore();}}
function scroll(dir){clearInterval(tm_scroll);if(!dir){return false;}
var functor=function(){checkNavigation();if(!buttonsEnabled[dir>0?'right':'left']){return false;}
var scrollNow=slider.scrollLeft();var ulWidth=iul.width();var effectiveWidth=scroller.width();var liCount=$$("ul li").length-1;LI_index+=dir;if(LI_index<0){clearInterval(tm_scroll);}
LI_index=Math.max(0,Math.min(liCount,LI_index));var scrollTo=$$("ul li:eq("+LI_index+")").position().left;scrollTo=Math.min(Math.max(0,scrollTo),ulWidth);if((ulWidth-effectiveWidth)-scrollTo<300){requestMore();}
if(scrollNow!=scrollTo){slider.animate({scrollLeft:scrollTo},300,function(){checkNavigation();});}};tm_scroll=setInterval(functor,350);functor();return false;};$(window).load(function(){scroll(-1);});brLeft.click(function(){return false;}).mousedown(function(){return buttonsEnabled.left&&scroll(-1);}).mouseup(function(){return scroll(0);}).mouseleave(function(){return scroll(0);});brRight.click(function(){return false;}).mousedown(function(){return buttonsEnabled.right&&scroll(1);}).mouseup(function(){return scroll(0);}).mouseleave(function(){return scroll(0);});checkNavigation();tryRequestMore();});});})(jQuery);
