Ext.ns('App');

Ext.form.Field.prototype.msgTarget = 'side';
Ext.QuickTips.init();

Ext.BLANK_IMAGE_URL = base_url + '/library/Extjs/resources/images/default/s.gif';


/* registrační formulář **************************************************** */
App.SearchFormLight = Ext.extend(Ext.form.FormPanel, {
    split                               : true,
    frame                               : true,
    url                                 : base_url + '/index/ext-action',
    urlResults                          : base_url + '/vysledky',
    urlSearch                           : base_url + '/vyhledavani',
    
    initComponent : function() {
        this.specializationStore = new Ext.data.Store({
            proxy   : new Ext.data.HttpProxy({
                url                     : this.url,
                method                  : 'post'
            }),
            baseParams : {
                cmd                     : 'getSpecializations'
            },
            reader : new Ext.data.JsonReader({
                    root                : 'results',
                    id                  : 'spec_id'
                },[
                  {name                 : 'spec_id'},
                  {name                 : 'name'}
                ]
              ),
             autoLoad                   : true
        });
        
        this.regionStore = new Ext.data.Store({
            proxy : new Ext.data.HttpProxy({
                url                     : this.url,
                method                  : 'post'
            }),
            baseParams : {
                cmd                     : 'getRegions'
            },
            reader : new Ext.data.JsonReader({
                    root                : 'results',
                    id                  : 'city_id'
                },[
                    {name               : 'city_id'},
                    {name               : 'name'}
                ]
            ),
            autoLoad                    : true
        });
        
        this.cityStore = new Ext.data.Store({
            proxy : new Ext.data.HttpProxy({
                url                     : this.url,
                method                  : 'post'
            }),
            baseParams : {
                cmd                     : 'getCities',
                region                  : null
            },
            reader : new Ext.data.JsonReader({
                    root                : 'results',
                    id                  : 'city_id'
                },[
                    {name               : 'city_id'},
                    {name               : 'name'}
                ]
            )
        });
        
        Ext.apply(this, {
            layout                      : 'form',
            frame                       : false,
            border                      : false,
            bodyStyle                   : 'background: none;',
            labelAlign                  : 'right',
            buttonAlign                 : 'right',
            labelWidth                  : 60,
            defaultType                 : 'combo',
            defaults : {
                anchor                  : '-10',
                labelStyle              : 'width: 62px; color: #183f46;'
            },
            items : [{
                    id                  : 'specialization',
                    fieldLabel          : 'Zaměření',
                    store               : this.specializationStore,
                    displayField        : 'name',
                    valueField          : 'spec_id',
                    hiddenName          : 'spec_id',
                    triggerAction       : 'all',
                    mode                : 'local',
                    editable            : false,
                    lazyRender          : true,
                    forceSelection      : true,
                    listeners : { 
                        select : {
                            scope       : this,
                            fn          : this.activateButton
                        }
                    }
                }, {
                    id                  : 'region',
                    fieldLabel          : 'Region',
                    store               : this.regionStore,
                    displayField        : 'name',
                    valueField          : 'city_id',
                    hiddenName          : 'region_id',
                    triggerAction       : 'all',
                    mode                : 'local',
                    editable            : false,
                    lazyRender          : true,
                    forceSelection      : true,
                    listeners : { 
                        select : {
                            scope       : this,
                            fn : function(combo, record, index) {
                                this.activateCity(combo, record, index);
                                this.activateButton();
                            }
                        }
                    }
                }, {
                    id                  : 'city',
                    fieldLabel          : 'Město',
                    disabled            : true,
                    store               : this.cityStore,
                    displayField        : 'name',
                    valueField          : 'city_id',
                    hiddenName          : 'city_id',
                    triggerAction       : 'all',
                    mode                : 'remote',
                    editable            : false,
                    lazyRender          : true,
                    forceSelection      : true,
                    emptyText           : 'vyberte nejdříve region',
                    listEmptyText       : 'nenalezena žádná advokátní kancelář',
                    listeners : {
                        beforequery : function(qe) {
                            delete qe.combo.lastQuery;
                        }
                    }
            }],
            buttons: [{
                    text                : 'Rozšířené vyhledávání',
                    scope               : this,
                    scale               : 'medium',
                    handler : function() {
                        window.location = this.urlSearch;
                    }
                }, {
                    text                : 'Vyhledat',
                    disabled            : true,
                    scope               : this,
                    scale               : 'medium',
                    handler             : this.commitForm
            }]
        });
        
        App.SearchFormLight.superclass.initComponent.call(this);
    },
    
    commitForm : function() {
        this.getForm().standardSubmit = true;
        this.getForm().getEl().dom.action = this.urlResults;
        this.getForm().getEl().dom.method = 'get';
        this.getForm().submit();
    },
    
    activateCity : function(combo, record, index) {
        var city = this.findById('city');
        city.getStore().baseParams.region = record.id;
        city.setRawValue('');
        city.setDisabled(false);
    },
    
    activateButton : function() {
        if (this.findById('specialization').getValue() || 
            this.findById('city').getValue() || 
            this.findById('region').getValue()) {
            this.buttons[1].setDisabled(false);
        } else {
            this.buttons[1].setDisabled(true);
        }
    }
    
});
Ext.reg('searchformlight', App.SearchFormLight);


/* onReady ***************************************************************** */
Ext.onReady(function() {
    var panel = new App.SearchFormLight({
        iconCls                         : 'icon-zoom',
        renderTo                        : 'search_form_light'
    });
});

