var App_Banner_Light = qs.create();
Shadowbox.init({skipSetup: true});

App_Banner_Light.prototype = {
    __instances: [],
    index: -1,
    fadeInterval: 1500,
    interval: 5000,
    stopTimer: false,
    timer: null,
    idItem: null,
    image: null,

    init: function (options)
    {
        if (options.isDouble && options.list.length > 1) {
            var list = [];
            var list2 = [];
            for (var i = 0; i< options.list.length; i++) {
                if (i%2 == 0) {
                    list.push(options.list[i]);
                } else {
                    list2.push(options.list[i]);
                }
            }
            options.list = list;
            var options2 = {};
            $.extend(options2, options, {idItem:options.idItem + '-2', list: list2});
            new App_Banner_Light(options);
            new App_Banner_Light(options2);
        } else {
            new App_Banner_Light(options);
        }
    },
    
    initialize: function (options)
    {
        for (var i in options) {
            this[i] = options[i];
        }
        this.idContainer = 'banner_container_' + this.idItem;
        App_Banner_Light.prototype.__instances[this.idItem] = this;
        this.image = document.getElementById('banner_image_' + this.idItem);
        this.prevImage = document.getElementById('banner_image_prev_' + this.idItem);
        if (!this.image) {
            alert('Banner initialization failed\nCannot find image with id=banner_image_' + this.idItem);
            return false;
        }
        this.image.idItem = this.idItem;
        this.image.onclick = function () {
            App_Banner_Light.prototype.getInstance(this.idItem).imageOnClick(this);
        }
        this.index = 0;
        this.prevIndex = 0;
        this.show();
    },
    
    getInstance: function (id)
    {
        if (typeof App_Banner_Light.prototype.__instances[id] != 'undefined') {
            return App_Banner_Light.prototype.__instances[id];
        }
        return false;
    },
    
    show: function ()
    {
        if (typeof this.list[this.index] == 'undefined') {
            return false;
        }
        if (typeof this.list[this.index].imageObj == 'undefined') {
            this.list[this.index].imageObj = new Image();
            this.list[this.index].imageObj.idItem = this.idItem;
            this.list[this.index].imageObj.onabort = function () {};
            this.list[this.index].imageObj.onerror = function () {};
            this.list[this.index].imageObj.onload = function () {
                App_Banner_Light.prototype.getInstance(this.idItem).imageOnLoad(this);
            }
            this.list[this.index].imageObj.src = qs.imageFs(this.list[this.index].image, this.width, this.height);
        } else {
            this.imageOnLoad(this.list[this.index].imageObj);
        }
    },
    
    next: function()
    {
        this.prevIndex = this.index;
		this.index++;
        if (this.index > this.list.length -1) {
            this.index = 0;
        }
        this.show();
    },
    
    stopAll: function()
    {
        App_Banner_Light.prototype.stopTimer = true;
        for (var id in App_Banner_Light.prototype.__instances) {
            var obj = App_Banner_Light.prototype.__instances[id];
            clearTimeout(obj.timer);
        }
    },
    
    resumeAll: function()
    {
        App_Banner_Light.prototype.stopTimer = false;
        for (var id in App_Banner_Light.prototype.__instances) {
            var obj = App_Banner_Light.prototype.__instances[id];
            obj.timer = setTimeout('App_Banner_Light.prototype.getInstance(\'' + obj.idItem + '\').next()', obj.interval);
        }
    },
    
    imageOnLoad: function (image)
    {
        this.prevImage.src = this.image.src;
        this.image.src = image.src;
        $(this.prevImage).show();
        $(this.prevImage).fadeOut(this.fadeInterval);
        $(this.image).hide();
        $(this.image).fadeIn(this.fadeInterval);
		
        $('#banner_title_prev_' + this.idItem).text(this.list[this.prevIndex].title);
        $('#banner_title_' + this.idItem).text(this.list[this.index].title);

		$('#banner_title_prev_' + this.idItem).show();
        $('#banner_title_prev_' + this.idItem).fadeOut(this.fadeInterval);
        $('#banner_title_' + this.idItem).hide();
        $('#banner_title_' + this.idItem).fadeIn(this.fadeInterval);
        if (!App_Banner_Light.prototype.stopTimer && this.list.length > 1) {
            this.timer = setTimeout('App_Banner_Light.prototype.getInstance(\'' + this.idItem + '\').next()', this.interval);
        }
    },
    
    imageOnClick: function(image)
    {
        App_Banner_Light.prototype.stopAll();
        Shadowbox.open({
            content:    qs.imageFs(this.list[this.index].image),
            player:     "img",
            title:      this.list[this.index].title,
            options: {
                onClose: App_Banner_Light.prototype.resumeAll
            }
        });
    }
}
