/*!
   * Lectric v0.2.2
   * http://github.com/mckinney/lectric
   *
   * Copyright 2010, McKinney
   * Licensed under the MIT license.
   * http://github.com/mckinney/lectric/blob/master/LICENSE
   *
   * Author: Brett C. Buddin
   */
(function (g) {
    var i = {},
        h = {},
        j = navigator.userAgent.toLowerCase();
    h.isWebkit = !! j.match(/applewebkit/i);
    h.isChrome = !! j.match(/chrome/i);
    h.isIE = !! j.match(/msie/i);
    h.isFirefox = j.match(/firefox/);
    try {
        document.createEvent("TouchEvent");
        h.supportsTouch = true
    } catch (l) {
        h.supportsTouch = false
    }
    var f = function () {};
    f.prototype.init = function (a, b) {
        this.opts = jQuery.extend({
            next: undefined,
            previous: undefined,
            limitLeft: false,
            limitRight: false,
            init: undefined
        }, b);
        this.currentY = this.currentX = 0;
        var e = $(a);
        e.find(".item").wrapAll('<div class="items">');
        e.css("overflow", "hidden");
        this.element = e.find(".items");
        this.element.css("width", "1000000px");
        this.element.find(".item").css("float", "left");
        this.structure = this.structure(this.element);
        var c = this;
        this.opts.next && $(this.opts.next).bind("click", function () {
            var k = c.currentX;
            c.currentX = c.limitXBounds(c.nextPageX(c.currentX));
            c.currentX !== k && c.update();
            return false
        });
        this.opts.previous && $(this.opts.previous).bind("click", function () {
            var k = c.currentX;
            c.currentX = c.limitXBounds(c.previousPageX(c.currentX));
            c.currentX !== k && c.update();
            return false
        });
        this.opts.init !== undefined && this.opts.init(this)
    };
    f.prototype.update = function (a) {
        a = jQuery.extend({
            animate: true,
            triggerMove: true
        }, a);
        var b = this,
            e = function () {
                b.element.trigger("lectric.animationEnd");
                $(this).dequeue()
            };
        a.animate ? this.element.animate({
            "margin-left": this.currentX + "px"
        }).queue(e) : this.element.css({
            "margin-left": this.currentX + "px"
        }).queue(e);
        a.triggerMove && this.element.trigger("lectric.move")
    };
    f.prototype.subscribe = function (a, b) {
        var e = this;
        return this.element.bind("lectric." + a, function (c) {
            c.target == e.element[0] && b(e)
        })
    };
    f.prototype.structure = function (a) {
        var b = {},
            e = function () {
                return a.find(".item").eq(0)
            };
        b.itemCount = function () {
            return a.find(".item").size()
        };
        b.itemHeight = function () {
            return parseInt(e().height(), 10)
        };
        b.itemSpacing = function () {
            var c = e().css("marginRight");
            return c !== undefined ? parseInt(c.replace("px", ""), 10) : 0
        };
        b.itemWidth = function () {
            return b.itemSpacing() + parseInt(e().width(), 10)
        };
        return b
    };
    f.prototype.page = function (a) {
        return Math.abs(Math.round(a / this.structure.itemWidth()))
    };
    f.prototype.nearestPageX = function (a) {
        return Math.round(a / this.structure.itemWidth()) * this.structure.itemWidth()
    };
    f.prototype.pageX = function (a) {
        return (this.opts.reverse ? 1 : -1) * a * this.structure.itemWidth()
    };
    f.prototype.nextPageX = function (a) {
        if (this.page(a) + 1 <= this.structure.itemCount() - 1) a -= this.structure.itemWidth();
        return a
    };
    f.prototype.previousPageX = function (a) {
        if (this.page(a) >= 0) a += this.structure.itemWidth();
        return a
    };
    f.prototype.limitXBounds = function (a) {
        var b = this.structure.itemWidth() * this.structure.itemCount();
        if (this.opts.reverse) {
            a = a > b - this.structure.itemWidth() ? b - this.structure.itemWidth() : a;
            a = a < 0 ? 0 : a
        } else {
            a = a < -b + this.structure.itemWidth() ? -b + this.structure.itemWidth() : a;
            a = a > 0 ? 0 : a
        }
        if (this.currentX - a > 0 && this.opts.limitRight || this.currentX - a < 0 && this.opts.limitLeft) a = this.currentX;
        return a
    };
    var d = function () {};
    d.prototype = new f;
    d.superobject = f.prototype;
    d.prototype.init = function (a, b, e) {
        d.superobject.init.call(this, a, b, e);
        this.element.parent().addClass("lectric-slider-touch");
        this.gesturing = false;
        a = $(a);
        a[0].addEventListener("touchstart", this, false);
        a[0].addEventListener("webkitTransitionEnd", this, false)
    };
    d.prototype.update = function (a) {
        a = jQuery.extend({
            animate: true,
            triggerMove: true
        }, a);
        a.animate && this.decayOn();
        this.element.css({
            "-webkit-transform": "translate3d(" + this.currentX + "px, 0, 0)"
        });
        a.triggerMove && this.element.trigger("lectric.move")
    };
    d.prototype.handleEvent = function (a) {
        this[a.type](a)
    };
    d.prototype.click = function (a) {
        this.moved && a.preventDefault();
        this.element[0].removeEventListener("click", this, false);
        return false
    };
    d.prototype.touchstart = function (a) {
        this.currentTarget = a.currentTarget;
        this.startX = a.touches[0].pageX - this.currentX;
        this.startY = a.touches[0].pageY - this.currentY;
        this.moved = false;
        g.addEventListener("gesturestart", this, false);
        g.addEventListener("gestureend", this, false);
        g.addEventListener("touchmove", this, false);
        g.addEventListener("touchend", this, false);
        this.element[0].addEventListener("click", this, false);
        this.decayOff();
        this.element.trigger("lectric.start")
    };
    d.prototype.touchmove =

    function (a) {
        if (this.gesturing) return false;
        if (!this.moved) {
            Math.abs(a.touches[0].pageY - this.startY) < 15 && a.preventDefault();
            this.element.trigger("lectric.firstMove")
        }
        this.moved = true;
        this.lastX = this.currentX;
        this.lastMoveTime = new Date;
        this.currentX = this.limitXBounds(a.touches[0].pageX - this.startX);
        this.update()
    };
    d.prototype.touchend = function () {
        g.removeEventListener("gesturestart", this, false);
        g.removeEventListener("gestureend", this, false);
        g.removeEventListener("touchmove", this, false);
        g.removeEventListener("touchend", this, false);
        if (this.moved) {
            this.currentX = this.nearestPageX(this.limitXBounds(this.currentX + (this.currentX - this.lastX) * 100 / (new Date - this.lastMoveTime + 1)));
            this.update();
            this.element.trigger("lectric.end")
        } else this.element.trigger("lectric.endNoMove");
        this.currentTarget = undefined
    };
    d.prototype.webkitTransitionEnd = function () {
        this.element.trigger("lectric.animationEnd")
    };
    d.prototype.gesturestart = function () {
        this.gesturing = true
    };
    d.prototype.gestureend = function () {
        this.gesturing = false
    };
    d.prototype.decayOff =

    function () {
        this.element.css({
            "-webkit-transition-duration": "0s"
        });
        this.element.css({
            "-webkit-transition-property": "none"
        })
    };
    d.prototype.decayOn = function () {
        this.element.css({
            "-webkit-transition-duration": "0.4s"
        });
        this.element.css({
            "-webkit-transition-property": "-webkit-transform"
        })
    };
    i.Slider = function () {
        return h.supportsTouch && h.isWebkit ? new d : new f
    };
    i.BaseSlider = f;
    i.TouchSlider = d;
    g.Lectric = i
})(window);
