var VION = VION || {};
VION.ImageLoader = function (a) {
    this.subscribers = [];
    this.img = a
};
VION.ImageLoader.prototype = {
    subscribe: function (a) {
        this.subscribers.push(a)
    },
    unsubscribe: function (a) {
        for (var b = 0; b < this.subscribers.length; b++) this.subscribers[b] === a && delete this.subscribers[b]
    },
    publish: function () {
        for (var a = 0; a < this.subscribers.length; a++) if (typeof this.subscribers[a] === "function") this.subscribers[a]()
    },
    load: function () {
        var a = this;
        this.img.addEventListener ? this.img.addEventListener("load", function (b) {
            a.onLoad(b)
        }, !1) : this.img.attachEvent && this.img.attachEvent("onload", function (b) {
            a.onLoad(b)
        });
        if (this.img.complete || this.img.complete === void 0 || this.img.readyState === "loading") {
            var b = this.img.src;
            this.img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
            this.img.src = b
        }
    },
    onLoad: function () {
        this.img.height > 1 && this.publish()
    }
};
VION.GalleryItem = function (a) {
    this.subscribers = {
        load: [],
        click: []
    };
    this.element = a
};
VION.GalleryItem.prototype = {
    subscribe: function (a, b) {
        this.subscribers[a].push(b)
    },
    unsubscribe: function (a, b) {
        for (var c = 0; c < this.subscribers[a].length; c++) this.subscribers[a][c] === b && delete this.subscribers[a][c]
    },
    publish: function (a, b) {
        for (var c = 0; c < this.subscribers[a].length; c++) if (typeof this.subscribers[a][c] === "function") this.subscribers[a][c](b)
    },
    addSelected: function () {
        this.element.addClass("selected")
    },
    removeSelected: function () {
        this.element.removeClass("selected")
    },
    fadeIn: function (a, b) {
        this.element.stop().animate({
            opacity: 1
        }, a, b)
    },
    fadeOut: function (a, b, c) {
        this.element.stop().animate({
            opacity: b
        }, a, c)
    },
    getX: function () {
        return this.element.position().left
    },
    index: function () {
        return this.element.index()
    },
    width: function () {
        return this.element.outerWidth(!0)
    }
};
VION.Slide = function (a) {
    VION.GalleryItem.call(this, a);
    this.info = a.children("div.info");
    this.imageHolder = a.children("div.image-holder");
    this.loaded = !1;
    this.init()
};
VION.Slide.prototype = new VION.GalleryItem;
VION.Slide.prototype.constructor = VION.Slide;
VION.Slide.prototype.init = function () {
    var a = this;
    this.imageHolder.bind("mousedown", function () {
        a.publish("click", a)
    })
};
VION.Slide.prototype.load = function () {
    var a = this,
        b = new VION.ImageLoader(this.imageHolder.children("img").get(0));
    b.subscribe(function () {
        a.onLoadComplete()
    });
    b.load()
};
VION.Slide.prototype.fadeInfoIn = function (a, b) {
    this.info.stop().animate({
        opacity: 1
    }, a, b)
};
VION.Slide.prototype.fadeInfoOut = function (a, b) {
    this.info.stop().animate({
        opacity: 0
    }, a, b)
};
VION.Slide.prototype.fadeImageIn = function (a) {
    var b = this;
    this.imageHolder.children("img").animate({
        opacity: 1
    }, 800, function () {
        b.imageHolder.css("background", "none");
        a && a()
    })
};
VION.Slide.prototype.setOpacity = function (a) {
    this.element.css({
        opacity: a,
        filter: "alpha(opacity=" + a * 100 + ")"
    })
};
VION.Slide.prototype.onLoadComplete = function () {
    this.loaded = !0;
    this.element.addClass("loaded");
    this.publish("load", this)
};
VION.Slide.prototype.getX = function () {
    var a = parseInt(this.element.css("margin-left"));
    return this.element.position().left + a
};
VION.Thumbnail = function (a) {
    VION.GalleryItem.call(this, a);
    this.img = this.element.children("img");
    this.loaded = !1;
    this.init()
};
VION.Thumbnail.prototype = new VION.GalleryItem;
VION.Thumbnail.prototype.constructor = VION.Thumbnail;
VION.Thumbnail.prototype.init = function () {
    var a = this;
    this.element.bind("mousedown", function () {
        a.publish("click", a)
    })
};
VION.Thumbnail.prototype.load = function () {
    var a = this,
        b = new VION.ImageLoader(this.img.get(0));
    b.subscribe(function () {
        a.onLoadComplete()
    });
    b.load()
};
VION.Thumbnail.prototype.onLoadComplete = function () {
    this.loaded = !0;
    this.element.addClass("loaded");
    this.publish("load", this)
};
VION.Thumbnail.prototype.addIndicator = function () {
    $("<div class='indicator'></div>").insertBefore(this.img).css({
        width: this.img.width(),
        height: this.img.height()
    })
};
VION.Thumbnail.prototype.removeIndicator = function () {
    this.element.children(".indicator").remove()
};
VION.ThumbnailScroller = function (a, b) {
    this.element = a;
    this.options = b;
    this.thumbnailContainer = this.element.children("ul");
    this.thumbnailElements = this.thumbnailContainer.children("li");
    this.subscribers = {
        select: []
    };
    this.loadIndex = 0;
    this.selectedIndex = -1;
    this.thumbnails = [];
    this.init()
};
VION.ThumbnailScroller.prototype = {
    init: function () {
        for (var a = this, b = 0; b < this.thumbnailElements.size(); b++) {
            var c = new VION.Thumbnail(this.thumbnailElements.eq(b));
            c.subscribe("load", function (b) {
                a.onLoad(b)
            });
            c.subscribe("click", function (b) {
                a.select(b)
            });
            this.thumbnails.push(c)
        }
        this.updateContainerWidth();
        this.element.scrollLeft(0);
        this.thumbnails[0].load();
        this.select(0)
    },
    subscribe: function (a, b) {
        this.subscribers[a].push(b)
    },
    unsubscribe: function (a, b) {
        for (var c = 0; c < this.subscribers[a].length; c++) this.subscribers[a][c] === b && delete this.subscribers[a][c]
    },
    publish: function (a, b) {
        for (var c = 0; c < this.subscribers[a].length; c++) if (typeof this.subscribers[a][c] === "function") this.subscribers[a][c](b)
    },
    onLoad: function (a) {
        a.index() != this.selectedIndex && a.fadeOut(this.options.thumbnailTransitionSpeed, this.options.unselectedThumbnailOpacity);
        this.loadIndex < this.thumbnails.length - 1 && this.thumbnails[++this.loadIndex].load()
    },
    updateContainerWidth: function () {
        for (var a = 0, b = 0; b < this.thumbnails.length; b++) a += this.thumbnails[b].width();
        this.thumbnailContainer.width(a)
    },
    select: function (a) {
        if (typeof a == "number") var b = this.thumbnails[a];
        else typeof a == "object" && (b = a);
        if (b.index() != this.selectedIndex) this.selectedIndex != -1 && (a = this.thumbnails[this.selectedIndex], a.removeSelected(), a.removeIndicator(), a.fadeOut(this.options.thumbnailTransitionSpeed, this.options.unselectedThumbnailOpacity)), this.selectedIndex = b.index(), b.addSelected(), b.addIndicator(), b.fadeIn(this.options.thumbnailFadeInSpeed), a = b.getX() - this.element.width() / 2 + b.width() / 2, this.element.stop().animate({
            scrollLeft: a
        }, this.options.thumbnailTransitionSpeed), this.publish("select", b)
    }
};
VION.ImageGallery = function (a, b) {
    this.settings = {
        unselectedSlideOpacity: 0.45,
        slideTransitionSpeed: 500,
        slideFadeInSpeed: 300,
        unselectedThumbnailOpacity: 0.4,
        thumbnailTransitionSpeed: 500,
        thumbnailFadeInSpeed: 300,
        enableKeyboard: !0,
        autoplay: !1,
        loop: !0,
        slideDuration: 5,
        pauseOnHover: !1
    };
    b && $.extend(this.settings, b);
    this.element = a;
    this.mask = a.children("div.gallery-mask");
    this.slideContainer = this.mask.children("div.slides");
    this.slideElements = this.slideContainer.children("div.slide");
    this.playing = !1;
    this.intervalID = 0;
    this.selectedIndex = -1;
    this.loadIndex = 0;
    this.slides = [];
    if (this.element.children(".thumbnails").size() > 0) this.thumbnailScroller = new VION.ThumbnailScroller(this.element.children(".thumbnails"), this.settings);
    this.init()
};
VION.ImageGallery.prototype = {
    init: function () {
        for (var a = this, b = 0; b < this.slideElements.size(); b++) {
            var c = new VION.Slide(this.slideElements.eq(b));
            c.setOpacity(this.settings.unselectedSlideOpacity);
            c.subscribe("load", function (b) {
                a.onLoad(b)
            });
            c.subscribe("click", function (b) {
                a.select(b)
            });
            this.slides.push(c)
        }
        this.mask.scrollLeft(0);
        this.initResizeListener();
        this.initKeyboardNavigation();
        this.updateContainerWidth();
        this.thumbnailScroller != void 0 && this.thumbnailScroller.subscribe("select", function (b) {
            a.onSelectThumbnail(b)
        });
        this.slides[0].load();
        this.select(0);
        this.settings.autoplay && this.startAutoplay();
        this.settings.autoplay && this.settings.pauseOnHover && (this.mask.mouseover(function () {
            a.stopAutoplay()
        }), this.mask.mouseout(function () {
            a.startAutoplay()
        }))
    },
    initResizeListener: function () {
        var a = this;
        $(window).resize(function () {
            a.updateContainerWidth()
        })
    },
    initKeyboardNavigation: function () {
        var a = this;
        this.settings.enableKeyboard && $(document).keydown(function (b) {
            if (b.target.type !== "textarea" && b.target.type !== "text") switch (b.keyCode || b.which) {
            case 37:
                a.selectPrevious();
                break;
            case 39:
                a.selectNext()
            }
        })
    },
    updateContainerWidth: function () {
        for (var a = 0, b = 0; b < this.slides.length; b++) a += this.slides[b].width();
        this.slideContainer.width(a + this.mask.width())
    },
    onLoad: function (a) {
        var b = this;
        a.fadeImageIn(function () {
            b.playing && a.index() == b.selectedIndex && a.loaded && b.resetTimer();
            b.loadIndex < b.slides.length - 1 && b.slides[++b.loadIndex].load()
        })
    },
    select: function (a) {
        var b = this;
        if (typeof a == "number") var c = this.slides[a];
        else typeof a == "object" && (c = a);
        if (c.index() != this.selectedIndex) clearInterval(b.intervalID), this.selectedIndex != -1 ? (a = this.slides[this.selectedIndex], a.removeSelected(), a.fadeOut(this.settings.slideTransitionSpeed, this.settings.unselectedSlideOpacity), a.fadeInfoOut(this.settings.slideTransitionSpeed), this.mask.stop().animate({
            scrollLeft: c.getX() - this.slideContainer.position().left
        }, this.settings.slideTransitionSpeed, function () {
            c.fadeIn(b.settings.slideFadeInSpeed, function () {
                b.playing && c.index() == b.selectedIndex && c.loaded && b.resetTimer()
            })
        })) : c.fadeIn(this.settings.slideFadeInSpeed, function () {
            b.playing && c.index() == b.selectedIndex && c.loaded && b.resetTimer()
        }), c.fadeInfoIn(this.settings.slideTransitionSpeed), this.selectedIndex = c.index(), c.addSelected(), this.thumbnailScroller != void 0 && this.thumbnailScroller.select(c.index())
    },
    selectNext: function () {
        this.selectedIndex < this.slides.length - 1 && this.select(this.selectedIndex + 1)
    },
    selectPrevious: function () {
        this.selectedIndex > 0 && this.select(this.selectedIndex - 1)
    },
    onSelectThumbnail: function (a) {
        this.select(a.index())
    },
    getSelectedIndex: function () {
        return this.selectedIndex
    },
    onTimer: function () {
        this.playing && (this.selectedIndex < this.slides.length - 1 ? this.selectNext() : this.settings.loop && this.select(0))
    },
    resetTimer: function () {
        var a = this;
        clearInterval(this.intervalID);
        this.intervalID = setInterval(function () {
            a.onTimer()
        }, this.settings.slideDuration * 1E3)
    },
    startAutoplay: function () {
        this.playing = !0;
        this.resetTimer()
    },
    stopAutoplay: function () {
        this.playing = !1;
        clearInterval(this.intervalID)
    }
};
(function (a) {
    a.fn.vion = function (b) {
        var c = [];
        this.each(function () {
            var d = a(this);
            d.data("vion") || d.data("vion", new VION.ImageGallery(d, b));
            c.push(d.data("vion"))
        });
        return c.length > 1 ? c : c[0]
    }
})(jQuery);
