Ext.define('TradePortal.store.SlideShow', {
    extend: 'Ext.data.Store',
    model: 'TradePortal.model.Widget',
    storeId: 'SlideShow',
    proxy: {
        type: 'jsonajax',
        url: BASE_URL + '/fluid/FluidAppServerProxy?ipApi=presenter&ipService=Frameworks.Interfaces.Mobile.TradePortal&ipFunction=getWidgetConfiguration',
        reader: {
            type: 'json',
            root: 'dataset.widgetConfiguration'
        },
        extraParams: {
            tpa: 'tportal'
        }
    },
    listeners: {
        load: {
            fn: function (store, records, options) {
                Ext.getCmp('viewport').setLoading(false);

                function getRandomInt(min, max) {
                    return Math.floor(Math.random() * (max - min + 1)) + min;
                }

                if (store.data.items.length > 0) {
                    var task = {
                        run: function () {
                            rand = getRandomInt(0, store.data.items.length - 1);
                            if (store.data.items[rand].data.object_value_2 == '') {
                                Ext.getCmp('slideshow_carousel').update('<img src="' + store.data.items[rand].data.object_value + '" width="960" height="400">');
                            } else {
                                Ext.getCmp('slideshow_carousel').update('<a href="http://' + store.data.items[rand].data.object_value_2 + '" target="_blank"><img src="' + store.data.items[rand].data.object_value + '" width="960" height="400"><a/>');
                            }
                        },
                        interval: 8000
                    };
                    Ext.TaskManager.start(task);
                } else {
                }
            }
        }
    }
});
