; window.swipe = function (element, options) { if (!element) return null; var _this = this; this.options = options || {}; this.index = this.options.startslide || 0; this.speed = this.options.speed || 300; this.callback = this.options.callback || function () {}; this.delay = this.options.auto || 0; this.container = element; this.element = this.container.children[0]; this.container.style.overflow = 'hidden'; this.element.style.liststyle = 'none'; this.element.style.margin = 0; this.setup(); this.begin(); if (this.element.addeventlistener) { this.element.addeventlistener('touchstart', this, false); this.element.addeventlistener('touchmove', this, false); this.element.addeventlistener('touchend', this, false); this.element.addeventlistener('touchcancel', this, false); this.element.addeventlistener('webkittransitionend', this, false); this.element.addeventlistener('mstransitionend', this, false); this.element.addeventlistener('otransitionend', this, false); this.element.addeventlistener('transitionend', this, false); window.addeventlistener('resize', this, false) } }; swipe.prototype = { setup: function () { this.slides = this.element.children; this.length = this.slides.length; if (this.length < 2) return null; this.width = math.ceil(("getboundingclientrect" in this.container) ? this.container.getboundingclientrect().width : this.container.offsetwidth); if (this.width === 0 && typeof window.getcomputedstyle === 'function') { this.width = window.getcomputedstyle(this.container, null).width.replace('px', '') } if (!this.width) return null; var origvisibility = this.container.style.visibility; this.container.style.visibility = 'hidden'; this.element.style.width = math.ceil(this.slides.length * this.width) + 'px'; var index = this.slides.length; while (index--) { var el = this.slides[index]; el.style.width = this.width + 'px'; el.style.display = 'table-cell'; el.style.verticalalign = 'top' } this.slide(this.index, 0); this.container.style.visibility = origvisibility }, slide: function (index, duration) { var style = this.element.style; if (duration == undefined) { duration = this.speed } style.webkittransitionduration = style.moztransitionduration = style.mstransitionduration = style.otransitionduration = style.transitionduration = duration + 'ms'; style.moztransform = style.webkittransform = 'translate3d(' + -(index * this.width) + 'px,0,0)'; style.mstransform = style.otransform = 'translatex(' + -(index * this.width) + 'px)'; this.index = index }, getpos: function () { return this.index }, prev: function (delay) { this.delay = delay || 0; cleartimeout(this.interval); if (this.index) this.slide(this.index - 1, this.speed); else this.slide(this.length - 1, this.speed) }, next: function (delay) { this.delay = delay || 0; cleartimeout(this.interval); if (this.index < this.length - 1) this.slide(this.index + 1, this.speed); else this.slide(0, this.speed) }, begin: function () { var _this = this; this.interval = (this.delay) ? settimeout(function () { _this.next(_this.delay) }, this.delay) : 0 }, stop: function () { this.delay = 0; cleartimeout(this.interval) }, resume: function () { this.delay = this.options.auto || 0; this.begin() }, handleevent: function (e) { switch (e.type) { case 'touchstart': this.ontouchstart(e); break; case 'touchmove': this.ontouchmove(e); break; case 'touchcancel': case 'touchend': this.ontouchend(e); break; case 'webkittransitionend': case 'mstransitionend': case 'otransitionend': case 'transitionend': this.transitionend(e); break; case 'resize': this.setup(); break } }, transitionend: function (e) { if (this.delay) this.begin(); this.callback(e, this.index, this.slides[this.index]) }, ontouchstart: function (e) { this.start = { pagex: e.touches[0].pagex, pagey: e.touches[0].pagey, time: number(new date()) }; this.isscrolling = undefined; this.deltax = 0; this.element.style.moztransitionduration = this.element.style.webkittransitionduration = 0; e.stoppropagation() }, ontouchmove: function (e) { if (e.touches.length > 1 || e.scale && e.scale !== 1) return; this.deltax = e.touches[0].pagex - this.start.pagex; if (typeof this.isscrolling == 'undefined') { this.isscrolling = !! (this.isscrolling || math.abs(this.deltax) < math.abs(e.touches[0].pagey - this.start.pagey)) } if (!this.isscrolling) { e.preventdefault(); cleartimeout(this.interval); this.deltax = this.deltax / ((!this.index && this.deltax > 0 || this.index == this.length - 1 && this.deltax < 0) ? (math.abs(this.deltax) / this.width + 1) : 1); this.element.style.moztransform = this.element.style.webkittransform = 'translate3d(' + (this.deltax - this.index * this.width) + 'px,0,0)'; e.stoppropagation() } }, ontouchend: function (e) { var isvalidslide = number(new date()) - this.start.time < 250 && math.abs(this.deltax) > 20 || math.abs(this.deltax) > this.width / 2, ispastbounds = !this.index && this.deltax > 0 || this.index == this.length - 1 && this.deltax < 0; if (!this.isscrolling) { this.slide(this.index + (isvalidslide && !ispastbounds ? (this.deltax < 0 ? 1 : -1) : 0), this.speed) } e.stoppropagation() } };