var BS_NEXT=1;var BS_PREV=-1;var BS_BY_BUTTONS=2;var BS_BY_TIMER=3;function BlocksScroller(data){if("undefined"==typeof(data)){alert("Не заданы данные");return;}this.container=$(data.container);this.scrolledObj=$(data.scrolledObj);this.scrolledObj[0].obj=this;this.pos=0;this.blocksCount=data.blocksCount;this.blockWidth=data.blockWidth;this.visibleBlocks=data.visibleBlocks;this.scrollingStarted=false;this._visAreaWidth=this.blockWidth*this.visibleBlocks;this._allAreaWidth=this.blockWidth*this.blocksCount;if(data.scrollType){this.scrollType=data.scrollType;}else{this.scrollType=BS_BY_BUTTONS;}if(BS_BY_BUTTONS==this.scrollType){this.prevButton=data.prevButton;this.nextButton=data.nextButton;this.initButtons();}else{this.timerSpeed=data.timerSpeed;this.initTimer();}if(data.speed){this.speed=data.speed;}else{this.speed="slow";}}BlocksScroller.prototype.initButtons=function(){var _obj=this;$(this.prevButton).click(function(){if(!_obj.scrollingStarted){_obj.scrollIt(BS_PREV);}return false;});$(this.nextButton).click(function(){if(!_obj.scrollingStarted){_obj.scrollIt(BS_NEXT);}return false;});}
BlocksScroller.prototype.timers=new Array();BlocksScroller.prototype.initTimer=function(){BlocksScroller.prototype.timers.push(this);var _currIndex=BlocksScroller.prototype.timers.length-1;window.setInterval("(BlocksScroller.prototype.timers["+_currIndex+"]).scrollIt(BS_NEXT)",this.timerSpeed);}
BlocksScroller.prototype.scrollIt=function(direction){if((direction>0&&((Math.abs(this.pos)+this._visAreaWidth)>=this._allAreaWidth))){this.scrolledObj.css("left",-(this._allAreaWidth-this._visAreaWidth-this.blockWidth)+"px");$(">:first-child",this.container).remove().insertAfter($(">:last-child",this.container));}else if(direction<0&&this.pos==0){this.scrolledObj.css("left",-this.blockWidth+"px");$("> :last-child",this.container).remove().insertBefore($(">:first-child",this.container));}else{this.pos-= direction*this.blockWidth;}this.scrollingStarted=true;var _obj=this;this.scrolledObj.animate({left:this.pos },this.speed,null,function(){ _obj.clearStartedFlag();});}
BlocksScroller.prototype.clearStartedFlag=function(){this.scrollingStarted=false;}
$(window).load(function(){new BlocksScroller({container:$("#preview .panel-content:first"),scrolledObj:$("#preview .panel-content:first"),blocksCount:6,blockWidth:244,visibleBlocks:4,prevButton:$("#banners .prev:first"),nextButton:$("#banners .next:first")});});
