"use strict";
var Premium = Premium || {};

Premium.creative = {
    init: function() {
        /* START OF CUSTOM JS */

        switch (document.body.id) {
            case "body_top":

                break;

            case "body_left":
                // The left is the auto-show timer master
                Premium.communicator.init(2);
                var carouselOb = JPCarousel.getObject("#carousel");
                var ref;

                var setAutoShowTimer = function(time) {
                    time = time || 3000;
                    clearInterval(ref);
                    ref = setInterval(function() {
                        carouselOb.showItem(carouselOb.getCurrentIndex() + 1)
                    }, time)
                }
                carouselOb.on("showitem", function(index) {
                    Premium.communicator.api.sendMessage({ action: "jp-carousel-sync", from: document.body.id, index: index });
                    setAutoShowTimer();
                })
                Premium.communicator.api.receiveMessage(function(mess) {
                    if (mess.from !== document.body.id && mess.action === "jp-carousel-sync" && mess.index !== carouselOb.getCurrentIndex()) {
                        carouselOb.showItem(mess.index)
                    }
                })
                setAutoShowTimer();

                break;

            case "body_right":

                Premium.communicator.init(2);
                var carouselOb = JPCarousel.getObject("#carousel");
                carouselOb.on("showitem", function(index) {
                    Premium.communicator.api.sendMessage({ action: "jp-carousel-sync", from: document.body.id, index: index });
                })

                Premium.communicator.api.receiveMessage(function(mess) {
                    if (mess.from !== document.body.id && mess.action === "jp-carousel-sync" && mess.index !== carouselOb.getCurrentIndex()) {
                        carouselOb.showItem(mess.index)
                    }
                })

                break;
        }

        /* END OF CUSTOM JS */
    }
}