﻿

Ext.namespace('FinnKodeWeb');

FinnKodeWeb.SearchSources = function(config) {
    this.config = config || {};
    this.addEvents('searchRequested');
    this.on('specialkey', function(f, e) {
        if (e.getKey() == e.ENTER) {
            this.fireEvent('searchRequested');
        }
    });
    FinnKodeWeb.SearchSources.superclass.constructor.call(this, config);
    Ext.apply(this, {
        createSearchSources: function(settings) {
            this.removeAll();
            // for each source in config
            // add check box
            var bookIndex;
            for (bookIndex = 0; bookIndex < settings.Books.length; bookIndex++) {
                var book = settings.Books[bookIndex];
                var bookSectionIndex;
                for (bookSectionIndex = 0; bookSectionIndex < book.BookSections.length; bookSectionIndex++) {
                    var bookSection = book.BookSections[bookSectionIndex];
                    if (bookSection.IsSearchable) {
                        var checkBox = new Ext.form.Checkbox({
                            id: NamingService.createId(bookSection.BookSectionId, 'checkbox'),
                            name: bookSection.BookSectionId,
                            boxLabel: bookSection.Description + ' - ' + bookSection.BookSectionName,
                            checked: bookSection.Checked || false,
                            listeners: {
                                specialkey: function(f, e) {
                                    if (e.getKey() == e.ENTER) {
                                        this.fireEvent('searchRequested');
                                    }
                                },
                                scope: this
                            }
                        });
                    }
                    this.add(checkBox);
                }
            }
        },
        getCheckedSearchSources: function() {
            var selectedSources = new String();
            var index = 0;
            for (index = 0; index < this.items.length; index++) {
                var checkBox = this.items.itemAt(index);
                if (selectedSources.length > 0) {
                    selectedSources = selectedSources.concat(',');
                }
                selectedSources = selectedSources.concat(checkBox.getName() + ':');
                selectedSources = selectedSources.concat(checkBox.checked ? 'true' : 'false');
            }
            return '{' + selectedSources.toString() + '}';
        },
        setCheckOnSearchSources: function(sourceConfig) {
            if (typeof (sourceConfig) == 'string') {
                sourceConfig = eval(sourceConfig);
            }
            for (var property in sourceConfig) {
                var checkBoxId = NamingService.createId(property, 'checkbox')
                var checkBox = this.getComponent(checkBoxId);
                if (checkBox != null) {
                    checkBox.setValue(sourceConfig[property]);
                }
            }
        },
        autoCheckSources: function(sourceConfig) {
            if (typeof (sourceConfig) == 'string') {
                sourceConfig = eval('(' + sourceConfig + ')');
            }
            var isSourceChecked = false;
            for (var property in sourceConfig) {
                isSourceChecked = sourceConfig[property];
                if (isSourceChecked) break;
            }
            if (!isSourceChecked) {
                // default search in all sources if not any of them are selected
                for (var property in sourceConfig) {
                    sourceConfig[property] = true;
                }
                this.setCheckOnSearchSources(sourceConfig);
            }
            return this.getCheckedSearchSources();
        }
    });
};

Ext.extend(FinnKodeWeb.SearchSources, Ext.form.FormPanel, {
    hideLabels: true,
    hideLabel: true,
    border: false
});
