﻿///<reference path="../../Lib/Extjs_Intellisense.js" />

Ext.namespace('LUL');

LUL.FileView = Ext.extend(Ext.DataView, {

    multiSelect: true,

    overClass: 'x-view-over',
    itemSelector: 'div.thumb-wrap',
    emptyText: '<span style="position: relative; top: 10px; left: 10px">Inga dokument i mappen</span>',

    store: new Ext.data.JsonStore({
        url: dataManager.fileBrowserFiles,
        root: 'rows',
        fields: ['filePath', 'icon', 'title', 'hasThumb', 'thumb']
    }),

    tpl: new Ext.XTemplate(
        '<tpl for=".">',
            '<div class="thumb-wrap">',
            '<div class="thumb"><img src="{icon}" title="{title}"></div>',
            '<span>{title}</span></div>',
        '</tpl>',
        '<div class="x-clear"></div>'
    ),

    //    plugins: [
    //        new Ext.DataView.DragSelector()/*,
    //        new Ext.DataView.LabelEditor({ dataIndex: 'title' })*/
    //    ],

    constructor: function(c) {
        LUL.FileView.superclass.constructor.call(this, Ext.apply(c, {}));
        //this.store.on('load', this.onLoad);
    },

    /*onLoad: function(store, records, options) {

        store.each(function(r) {
    if (r.data.hasThumb) r.data.icon = r.data.thumb;
    });

    },*/

    prepareData: function(data) {
        if (data.hasThumb) data.icon = data.thumb; else data.icon = 'Lib/FileBrowser/Icons/' + data.icon + '.gif';
        return data;
    },

    getSelectionText: function() {
        var nodes = this.getSelectedNodes();
        var l = nodes.length;
        if (l > 1) return l + ' filer markerade, storlek ' + this.calcSize(nodes);
        if (l == 1) return l + ' fil markerad, storlek ' + this.calcSize(nodes);
        if (l == 0) {
            var s = this.store.getCount() != 1 ? 'er' : '';
            return this.store.getCount() + ' fil' + s;
        }
    },

    getSelectedData: function() {
        var sel = this.getSelectedRecords();
        if (sel.length > 0) {
            var data = [];
            for (var i = 0; i < sel.length; i++) {
                data.push(sel[i].data);
            }
            return data;
        }
        return null;
    },

    calcSize: function(nodes) {
        var units = ['byte', 'kB', 'MB', 'GB', 'TB']
        var records = this.getRecords(nodes);
        var size = 0, unit = 'byte';
        for (var i = 0; i < records.length; i++) size += records[i].data.size;
        for (var j = 0; j < units.length; j++) {
            if (size < Math.pow(1024, j + 1)) {
                size = Math.round(size / Math.pow(1024, j));
                unit = units[j];
                break;
            }
        }
        return size + ' ' + unit;
    },

    loadFiles: function(path) {
        //        this.plugins[0].update();
        this.store.load({ params: { path: path, type: this.type } });
    }

    /*loadFiles: function(folderId) {
    this.plugins[0].update();
    this.store.module = this.module;
    this.store.load({ params: { folder: folderId, property: this.module.property.id } });        
    }*/
});
