﻿// JScript File

function CatalogShowcase(frameId, items) {
    this.frame = document.getElementById(frameId);
    this.items = items;
    this.animSpeed = 1000;
    window.catShowcase = this;
    
    this.activeItem = null;
    this.activateItem = function (itemKey) {
        this.stopped = true; //stop animation
        if (this.activeItem) {
            if (this.activeItem.key == itemKey) return;
            $(this.activeItem).fadeOut(this.animSpeed);
        }        
        this.activeItem = document.getElementById('csc_'+itemKey);
        $(this.activeItem).fadeIn(this.animSpeed);
        this.activeItem.key = itemKey;        
    }
    
    this.log = function (message) {
        if (this.logger) {
            this.logger.innerText += '\n'+message
        }
    }
    
    this.timer = null;
    this.start = function () {
        this.stopped = false;
        if (this.timer==null)
            this.switchNextItem();
    }
    
    this.stop = function () {
        this.stopped = true;
    }
    
    this.activeItemIndex = -1;
    this.switchNextItem = function() {
        if (!this.stopped) {
            this.activeItemIndex++;
            if (this.activeItemIndex > this.items.length-1)
                this.activeItemIndex = 0;
            this.activateItem(this.items[this.activeItemIndex].key);
            this.stopped = false; //run animation
        }
        this.timer = setTimeout(switchItem, 3000);
    }
}

function switchItem() {
    window.catShowcase.switchNextItem();
}


var showTimerId;
var itemKeyToShow;
function showItem(key) {
    if (showTimerId)
        clearTimeout(showTimerId);
    
    itemKeyToShow = key;
    showTimerId = setTimeout(reallyShowItem, 500);
}

function reallyShowItem() {
    if (window.catShowcase) {
        window.catShowcase.activateItem(itemKeyToShow);
    }
}

function continueAnimation() {
    if (showTimerId)
        clearTimeout(showTimerId);
        
    if (window.catShowcase) {
        window.catShowcase.start();
    }
}