/*
 * file: common.js
 *
 * This file includes code common to all parts of the LL-MAP application
 */

Ext.QuickTips.init();

Ext.Ajax.disableCaching            = false;
Ext.Ajax.timeout                   = 120*1000; // 2 minutes
Ext.form.Field.prototype.msgTarget = 'side';
Ext.Updater.defaults.loadScripts   = true;

Ext.Ajax.on('requestexception', function(conn, response, options) {
    var body = response.responseText;
    var msg;

    if (body) {
        if (body.match(/^{/)) {
            body = Ext.decode(body);

            if (body && body.errors) {
                msg = body.errors;
            }
        }
        else {
            msg = body;
        }

        Ext.MessageBox.show({
            title: 'Server Error',
            msg: msg,
            buttons: Ext.MessageBox.OK,
            icon: Ext.MessageBox.ERROR,
            width: 600
        });
    }
});

Ext.namespace('LINGUIST.LLMAP');

LINGUIST.LLMAP.CountrySelector = Ext.extend(Ext.form.ComboBox, {
    displayField: 'country',
    valueField: 'id',
    triggerAction: 'all',
    hideTrigger: false,
    forceSelection: true,
    fieldLabel: 'Country Name',
    typeAhead: false,
    editable: false,
    mode: 'remote',

    constructor: function(config) {
        Ext.applyIf(config, {
            store: new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({
                    url: '/countries.json',
                    method: 'GET'
                }),
                reader: new Ext.data.JsonReader({
                    fields: [ 'id', 'country' ],
                    root: 'data'
                })
            })
        });

        LINGUIST.LLMAP.CountrySelector.superclass.constructor.call(this, config);
    }
});

Ext.ComponentMgr.registerType('countryselector', LINGUIST.LLMAP.CountrySelector);

LINGUIST.LLMAP.LanguageSelector = Ext.extend(Ext.form.ComboBox, {
    displayField: 'name',
    valueField: 'code',
    itemSelector: 'div.language-selector-item',
    hideTrigger: true,
    forceSelection: true,
    emptyText: 'Type a language name...',
    fieldLabel: 'Language Name',
    typeAhead: false,
    minChars: 2,
    queryParam: 'query',

    constructor: function(config) {
        Ext.applyIf(config, {
            tpl: new Ext.XTemplate('<tpl for="."><div class="language-selector-item">{name} ({code})</p></div></tpl>'),
            store: new Ext.data.JsonStore({
                url: '/languages/search.json',
                root: 'data',
                fields: ['name','code']
            })
        });

        LINGUIST.LLMAP.LanguageSelector.superclass.constructor.call(this, config);
    }
});

Ext.ComponentMgr.registerType('languageselector', LINGUIST.LLMAP.LanguageSelector);

LINGUIST.LLMAP.FamilySelector = Ext.extend(Ext.form.ComboBox, {
    displayField: 'pri_name',
    valueField: 'code',
    triggerAction: 'all',
    hideTrigger: true,
    forceSelection: true,
    emptyText: 'Type a family name...',
    fieldLabel: 'Family Name',
    typeAhead: true,
    minChars: 2,
    queryParam: 'query',

    constructor: function(config) {
        Ext.applyIf(config, {
            store: new Ext.data.Store({
                baseParams: { family_tree: true },
                proxy: new Ext.data.ScriptTagProxy({
                    url: 'http://multitree.linguistlist.org/nodes.json',
                    method: 'GET'
                }),
                reader: new Ext.data.JsonReader({
                    root: 'nodes',
                    id: 'code',
                    totalProperty: 'results'
                }, [ 'code', 'pri_name' ])
             })
        });

        LINGUIST.LLMAP.FamilySelector.superclass.constructor.call(this, config);
    }
});

Ext.ComponentMgr.registerType('familyselector', LINGUIST.LLMAP.FamilySelector);
