Ext.ns('Hdv.lot.detail');

Hdv.lot.detail.Manager = {

    /**
     *----------------------------------------------------------------------
     *              Gestion du cache
     *----------------------------------------------------------------------
     */

    cache: [],

    getCache: function() {
        return Hdv.lot.detail.Manager.cache;
    },

    setCacheElement: function(records) {
        var record = records[0];
        Hdv.lot.detail.Manager.cache[record.get('lot_id') + '_' + record.get('sale_id')] = records;
    },

    isInCache: function(lotId, saleId) {
        return Hdv.lot.detail.Manager.cache[lotId + '_' + saleId] || null;
    },




    /**
     *----------------------------------------------------------------------
     *              Affichage des fenetres
     *----------------------------------------------------------------------
     */

    windows: [],

    show: function(lot, sale, animEl) {
        if (!Hdv.lot.detail.Manager.windows[lot]) {
            Hdv.lot.detail.Manager.windows[lot] = new Hdv.lot.detail.Window({
                // le config vient de la TPL
                config      : config,
                animEl      : animEl,
                storeParams : {
                    lot_id         : lot,
                    sale_id        : sale,
                    fetch_all_info : 1
                }
            });
        }
        Hdv.lot.detail.Manager.windows[lot].show(lot, sale);
    },




    /**
     *----------------------------------------------------------------------
     *              Navigation clavier
     *----------------------------------------------------------------------
     */

    current: null,

    enableKeyNav: function(current) {
        Hdv.lot.detail.Manager.current = current;
        Ext.fly(document).on('keydown', Hdv.lot.detail.Manager.keyNavAction);
    },

    disableKeyNav: function(current) {
        Hdv.lot.detail.Manager.current = current;
        Ext.fly(document).un('keydown', Hdv.lot.detail.Manager.keyNavAction);
    },

    keyNavAction: function(ev) {
        var win = Hdv.lot.detail.Manager.current;
        var keyCode = ev.getKey();
        var config = win.getLotConfig(win.currentLot);
        var stop = false;
        if (keyCode == ev.LEFT || keyCode == ev.PAGE_DOWN) {
            if (keyCode == ev.LEFT) {
                if (win.getPanelGallery().previousThumb()) stop = true;
            }
            if (config.lot_previous_id && !stop) win.navigateTo(config.lot_previous_id, config.sale_previous_id);
            stop = true;
        }
        else if (keyCode == ev.RIGHT || keyCode == ev.PAGE_UP) {
            if (keyCode == ev.RIGHT) {
                if (win.getPanelGallery().nextThumb()) stop = true;
            }
            if (config.lot_next_id && !stop) win.navigateTo(config.lot_next_id, config.sale_next_id);
            stop = true;
        }
        else if (keyCode == ev.M) {
            win.maximizePhoto();
            stop = true;
        }
        if (stop) {
            ev.stopEvent();
            ev.stopPropagation()
        }
    }

};