define(function(n,o,e){"use strict";var s=n("../util"),t=n("../base"),r=function(n){r.superclass.constructor.call(this,n),this.userConfig=s.mix({snapColIndex:0,snapRowIndex:0,snapDuration:500,snapEasing:"ease",snapOffsetLeft:0,snapOffsetTop:0,autoStep:!1},n)};if(s.extend(r,t,{pluginId:"snap",pluginInitializer:function(n){var o=this;o.xscroll=n.render(),o.snapColIndex=o.userConfig.snapColIndex,o.snapRowIndex=o.userConfig.snapRowIndex,o.render()},pluginDestructor:function(){var n=this,o=n.xscroll;o.on("panend",o._onpanend,o),o.off("panend",n._snapAnimate,n)},snapTo:function(n,o,e,s,t){return this.snapToCol(n,e,s,t),this.snapToRow(o,e,s,t),this},snapToCol:function(n,o,e,s){var t=this,r=t.xscroll,a=t.userConfig,o=o||a.snapDuration,e=e||a.snapEasing,i=a.snapWidth,l=a.snapColsNum,p=a.snapOffsetLeft;n=n>=l?l-1:n<0?0:n,t.prevColIndex=t.snapColIndex,t.snapColIndex=n;var d=t.snapColIndex*i+p;return d>r.containerWidth-r.boundry.width&&(d=r.containerWidth-r.boundry.width),r.scrollLeft(d,o,e,s),t},_colChange:function(n){var o=this;return o.prevColIndex!=o.snapColIndex&&o.trigger("colchange",s.mix(n,{type:"colchange",curColIndex:o.snapColIndex,prevColIndex:o.prevColIndex})),o},snapToRow:function(n,o,e,s){var t=this,r=t.xscroll,a=t.userConfig,o=o||a.snapDuration,e=e||a.snapEasing,i=a.snapHeight,l=a.snapRowsNum,p=a.snapOffsetTop;n=n>=l?l-1:n<0?0:n,t.prevRowIndex=t.snapRowIndex,t.snapRowIndex=n;var d=t.snapRowIndex*i+p;return d>r.containerHeight-r.boundry.height&&(d=r.containerHeight-r.boundry.height),t.xscroll.scrollTop(d,o,e,s),t},_rowChange:function(n){var o=this;return o.prevRowIndex!=o.snapRowIndex&&o.trigger("rowchange",s.mix(n,{type:"rowchange",curRowIndex:o.snapRowIndex,prevRowIndex:o.prevRowIndex})),o},_snapAnimate:function(n){var o=this,e=o.userConfig,s=e.snapWidth,t=e.snapHeight;o.xscroll.__topstart=null,o.xscroll.__leftstart=null;var r=n.direction;if(Math.abs(n.velocity)<=.2){var a=o.xscroll.getScrollLeft(),i=o.xscroll.getScrollTop(),l=Math.round(a/s),p=Math.round(i/t);o.snapTo(l,p)}else if(e.autoStep){var d=o.xscroll.computeScroll("x",n.velocityX),u=o.xscroll.computeScroll("y",n.velocityY),l=d&&d.pos?Math.round(d.pos/s):o.snapColIndex,p=u&&u.pos?Math.round(u.pos/t):o.snapRowIndex,c=Math.ceil(d&&d.duration,u&&u.duration);d&&"inside"==d.status?o.snapToCol(l,c,d&&d.easing,function(){o.xscroll.boundryCheckX()}):d&&o.xscroll.scrollLeft(d.pos,d.duration,d.easing,function(){o.xscroll.boundryCheckX(),o.prevColIndex=o.snapColIndex,o.snapColIndex=Math.round(Math.abs(o.xscroll.getScrollLeft())/s)}),u&&"inside"==u.status?o.snapToRow(p,c,u&&u.easing,function(){o.xscroll.boundryCheckY()}):u&&o.xscroll.scrollTop(u.pos,u.duration,u.easing,function(){o.xscroll.boundryCheckY(),o.prevRowIndex=o.snapRowIndex,o.snapRowIndex=Math.round(Math.abs(o.xscroll.getScrollTop())/t)})}else 2==r?o.snapColIndex++:4==r?o.snapColIndex--:void 0,8==r?o.snapRowIndex++:16==r?o.snapRowIndex--:void 0,o.snapTo(o.snapColIndex,o.snapRowIndex)},render:function(){var n=this,o=n.xscroll;return n.userConfig.snapWidth=n.userConfig.snapWidth||o.width||100,n.userConfig.snapHeight=n.userConfig.snapHeight||o.height||100,n.userConfig.snapColsNum=n.userConfig.snapColsNum||Math.max(Math.round(o.containerWidth/o.width),1),n.userConfig.snapRowsNum=n.userConfig.snapRowsNum||Math.max(Math.round(o.containerHeight/o.height),1),o.off("panend",o._onpanend),o.on("panend",n._snapAnimate,n),n._bindEvt(),n},_bindEvt:function(){var n=this,o=n.xscroll;n._isEvtBinded||(n._isEvtBinded=!0,o.on("scrollend",function(e){"y"!=e.zoomType||o.isBoundryOutTop()||o.isBoundryOutBottom()||n._rowChange(e)}),o.on("scrollend",function(e){"x"!=e.zoomType||o.isBoundryOutLeft()||o.isBoundryOutRight()||n._colChange(e)}))}}),"object"==typeof e&&e.exports)e.exports=r;else if(window.XScroll&&window.XScroll.Plugins)return XScroll.Plugins.Snap=r});