/* -------------------------------------------------- *
 * jQuery CS div Scroller (horisontal)
 * version 1.0 :)
 * -------------------------------------------------- *
 * Author: Ivan Ugolev
 * URL: http://www.credosystems.by/
 * Copyright: 2011 CredoSystems
** -------------------------------------------------- */
(function($){CS_Scroll=function(cWrapper,cArea,cSpeed,cAutoScroll,cMouseScroll){if($(cWrapper)===null)return;if($(cArea)===null)return;var csTmIntr=40;var csSpeed=2;var el=$(cWrapper);cSpeed=Math.ceil(cSpeed);if(isNaN(cSpeed)==false){if((cSpeed>-1)&&(cSpeed<11)){if(cSpeed==0){csTmIntr=80;cSpeed=1;}csSpeed=cSpeed;}}el.data('scrollWraper',cWrapper);el.data('scrollArea',cArea);el.data('scrollSpeed',csSpeed);el.data('scrollTimeInterval',csTmIntr);el.data('scrollInterval',0);el.data('moveLeft',true);el.data('csLeft',0);el.data('cMouseScroll',cMouseScroll);el.data('cAutoScroll',cAutoScroll);el.data('cMouseX',-1);el.data('cScrollCounts',0);$(cWrapper).css('overflow','hidden');$(cWrapper).mouseout(function(){CS_Resume($(this).data('scrollWraper'));});$(cWrapper).mousemove(function(e){var el=$(this);clearInterval(el.data('scrollInterval'));el.data('scrollInterval',0);var csWrapWidth=$(el.data('scrollWraper')).width();var csScrollWidth=$(el.data('scrollArea')).innerWidth();if(csWrapWidth>=csScrollWidth)return;if(!el.data('cMouseScroll'))return;if(el.data('cMouseX')<0){el.data('cMouseX',e.pageX);return;}if(Math.abs(el.data('cMouseX')-e.pageX)<50)return;var csScLeft=e.pageX-$(el.data('scrollWraper')).offset().left;var csLeft=csScLeft*(csScrollWidth-csWrapWidth)/csWrapWidth;if(el.data('cMouseX')<100000){if((csScLeft<100)||(csScLeft>csWrapWidth-100)){el.data('cMouseX',100000);}else return;}el.data('csLeft',csLeft);$(el.data('scrollWraper')).scrollLeft(csLeft);});if(el.data('scrollInterval')==0){CS_Resume(cWrapper);}};CS_SetLeftScroll=function(cWrapper,cArea){var csWrapWidth=$(cWrapper).width();var el=$(cWrapper);var csScrollWidth=$(cArea).innerWidth();var csLeft=el.data('csLeft');var csCount=el.data('cScrollCounts');var cMouseX=el.data('cMouseX');if(csWrapWidth>=csScrollWidth)return;if(csCount<5){csCount++;el.data('cScrollCounts',csCount);}else{if(cMouseX>-1){el.data('cMouseX',-1);}if(!el.data('cAutoScroll')){CS_Stop(cWrapper);}}if(!el.data('cAutoScroll'))return;if(el.data('moveLeft')){csLeft=csLeft+el.data('scrollSpeed');if(csLeft>csScrollWidth-csWrapWidth){csLeft=csScrollWidth-csWrapWidth;el.data('moveLeft',false);}}else{csLeft=csLeft-el.data('scrollSpeed');if(csLeft<0){csLeft=0;el.data('moveLeft',true);}}el.data('csLeft',csLeft);$(cWrapper).scrollLeft(csLeft);};CS_Stop=function(cWrapper){var el=$(cWrapper);if(el.data('scrollInterval')!=0){clearInterval(el.data('scrollInterval'));}el.data('scrollInterval',0);};CS_Resume=function(cWrapper){CS_Stop(cWrapper);var el=$(cWrapper);el.data('cScrollCounts',0);el.data('scrollInterval',window.setInterval(function(){CS_SetLeftScroll(el.data('scrollWraper'),el.data('scrollArea'));},el.data('scrollTimeInterval')));};})(jQuery);

