var gallery = {
    index: 0,
    zIndex: 2,
    pictures: [],
    to: null,
    durationMSec: 7000, //wird überschrieben
    durationFadeMSec: 750,
    fadeCount: 0,
    bannerTemplate: null,
    currentBanner: null,
    current: function() {
        return this.pictures[this.index];
    },
    init: function() {
        this.pictures = vGalleryPics;
        if(this.pictures.length <= 1)
            return;

        var _this = this;
        this.currentBanner = $('#contentLeft img');
        this.bannerTemplate = this.currentBanner.clone();
        this.bannerTemplate.attr('src', '');
        $('#imgPrev').click(function() {
            _this.prev();
        });
        $('#imgNext').click(function() {
            _this.next();
        });
        $('#imgStop').click(function() {
            _this.stop();
        });
        this.nextTO();
    },
    next: function() {
        this.index = (this.index + 1) % this.pictures.length;
        this.fadeIn(this.current());
        this.nextTO();
    },
    nextTO: function() {
        clearTimeout(this.to);
        var _this = this;
        this.to = setTimeout(function() {
            _this.next();
        }, this.durationMSec);
    },
    prev: function() {
        if(--this.index < 0)
            this.index = this.pictures.length - 1;
        this.fadeIn(this.current());
        this.prevTO();
    },
    prevTO: function() {
        clearTimeout(this.to);
        var _this = this;
        this.to = setTimeout(function() {
            _this.prev();
        }, this.durationMSec);
    },
    stop: function() {
        clearTimeout(this.to);
    },
    fadeIn: function(pSrc) {
        /*
        pItem.css('z-index', ++this.zIndex);
        pItem.css('display', 'none');
        pItem.fadeIn(this.durationFadeMSec);
        */
        var vNewBanner = this.bannerTemplate.clone();
        vNewBanner.attr('src', 'hi23');
        vNewBanner.hide();
        var _this = this;
        $('#contentLeft').addClass('loading');
        vNewBanner.load(function() {
            vNewBanner.appendTo('#contentLeft');
            $('#contentLeft').removeClass('loading');
            vNewBanner.fadeIn(_this.durationFadeMSec, function() {
                vPrevBanner.remove();
                if(vFadeCount != _this.fadeCount)
                    return;

                vNewBanner.css('z-index', 1);
                _this.zIndex = 1;
            });
        });
        vNewBanner.attr('src', '/images/photos/gallery/' + pSrc);
        vNewBanner.css('z-index', ++this.zIndex);
        var vPrevBanner = this.currentBanner;
        this.currentBanner = vNewBanner;
        var vFadeCount = ++this.fadeCount;
    }
};

$(window).load(function() { gallery.init(); });