﻿var image = new Class({
    _HMUID: "i",
    _call: function (e) {
        switch (e.a) {
            case "show":
                this._showImage(e);
                break;
            case "next":
                this._nextImage(e);
                break;
            case "previous":
                this._prevImage(e);
                break;
            case "history":
                var t = e.v.split(",");
                this._showImage({ c: t[0], p: parseInt(t[1]) });
                break;
        }
    },
    initialize: function () {
        site.addHistoryEvent("image", this._HMUID);
        site.addEvent("image", function (e) { this._call(e); } .bind(this));
    },
    _nextImage: function (e) {
        var container = $(e.c);
        var currentPosition = container.retrieve("pos", 0);
        var maxPos = container.retrieve("max", container.getElements(".imagePanelThumbs a").length);
        var newPos = (currentPosition + 1) % maxPos;
        var thumbContainer = this._getThumbContainer(e.c, newPos);
        e.p = newPos;
        e.l = thumbContainer.get("title");
        this._showImage(e);
    },
    _prevImage: function (e) {
        var container = $(e.c);
        var currentPosition = container.retrieve("pos", 0);
        var maxPos = container.retrieve("max", container.getElements(".imagePanelThumbs a").length);
        var newPos = (currentPosition - 1 + maxPos) % maxPos;
        var thumbContainer = this._getThumbContainer(e.c, newPos);
        e.p = newPos;
        e.l = thumbContainer.get("title");
        this._showImage(e);
    },
    _showImage: function (e) {
        var container = $(e.c);
        var currentPosition = container.retrieve("pos", 0);
        if (e.p == currentPosition)
            return;
        var thumbContainer = this._getThumbContainer(e.c, e.p);

        this._resetActive(e.c);
        thumbContainer.addClass("active");
        var srcToShow = thumbContainer.get("rel");
        container.store("pos", e.p);
        Asset.image(srcToShow, { onLoad: function (img) { container.getElement(".imagePanelPreview").empty().adopt(img);} });
//        container.getElement(".imagePanelPreview").empty().adopt(new Element("img", { "src": srcToShow })); // setStyle("background-image", "url(" + srcToShow + ")");

        if (container.getElement(".imagePanelLegends")) {
            if (container.getElement(".imagePanelLegends .imagePanelControlsNumbersCurrent"))
                container.getElement(".imagePanelLegends .imagePanelControlsNumbersCurrent").set("html", (e.p + 1));
            if (thumbContainer.getElement("div.imageLegend"))
                container.getElement(".imagePanelLegends .imagePanelLegend").set("html", thumbContainer.getElement("div.imageLegend").get("html"));
        }
        site.HM.set(this._HMUID, e.c + "," + e.p);
    },
    _getThumbContainer: function (c, p) {

        var container = $(c);
        return container.getElements(".imagePanelThumbs a")[p];
    },
    _resetActive: function (c) {
        $(c).getElements(".imagePanelThumbs a").removeClass("active");
    }
});
