| 205 | }; |
| 206 | |
| 207 | var bindTouchEvents = function() { |
| 208 | var startTouch; |
| 209 | var direction = options.touch.swipeDirection; |
| 210 | var tolerance = options.touch.swipeTolerance; |
| 211 | var listenToHorizontal = ({ both: true, horizontal: true })[direction]; |
| 212 | var listenToVertical = ({ both: true, vertical: true })[direction]; |
| 213 | |
| 214 | $container.unbind('touchstart.deck'); |
| 215 | $container.bind('touchstart.deck', function(event) { |
| 216 | if (!startTouch) { |
| 217 | startTouch = $.extend({}, event.originalEvent.targetTouches[0]); |
| 218 | } |
| 219 | }); |
| 220 | |
| 221 | $container.unbind('touchmove.deck'); |
| 222 | $container.bind('touchmove.deck', function(event) { |
| 223 | $.each(event.originalEvent.changedTouches, function(i, touch) { |
| 224 | if (!startTouch || touch.identifier !== startTouch.identifier) { |
| 225 | return true; |
| 226 | } |
| 227 | var xDistance = touch.screenX - startTouch.screenX; |
| 228 | var yDistance = touch.screenY - startTouch.screenY; |
| 229 | var leftToRight = xDistance > tolerance && listenToHorizontal; |
| 230 | var rightToLeft = xDistance < -tolerance && listenToHorizontal; |
| 231 | var topToBottom = yDistance > tolerance && listenToVertical; |
| 232 | var bottomToTop = yDistance < -tolerance && listenToVertical; |
| 233 | |
| 234 | if (leftToRight || topToBottom) { |
| 235 | $.deck('prev'); |
| 236 | startTouch = undefined; |
| 237 | } |
| 238 | else if (rightToLeft || bottomToTop) { |
| 239 | $.deck('next'); |
| 240 | startTouch = undefined; |
| 241 | } |
| 242 | return false; |
| 243 | }); |
| 244 | |
| 245 | if (listenToVertical) { |
| 246 | event.preventDefault(); |
| 247 | } |
| 248 | }); |
| 249 | |
| 250 | $container.unbind('touchend.deck'); |
| 251 | $container.bind('touchend.deck', function(event) { |
| 252 | $.each(event.originalEvent.changedTouches, function(i, touch) { |
| 253 | if (startTouch && touch.identifier === startTouch.identifier) { |
| 254 | startTouch = undefined; |
| 255 | } |
| 256 | }); |
| 257 | }); |
| 258 | }; |
| 259 | |
| 260 | var indexInBounds = function(index) { |
| 261 | return typeof index === 'number' && index >=0 && index < slides.length; |