// MyPanel Extends Ext.Panel
fmeWorkspaceManager = Ext.extend(Object, {
    //  constructor function
    constructor: function(inUrl){
        Ext.apply(this, {
            url: inUrl, // Pass in the URL
            xmlStore: null
        });
        fmeWorkspaceManager.superclass.constructor.apply(this, arguments);
    },
    
    /**
     *
     */
    createParametersStore: function(){
    
        var recordFields = [{
            name: 'options',
            convert: function(v, n){
                var nodes = Ext.DomQuery.select('option', n);
                var s = [];
                
                for (var i = 0, len = nodes.length; i < len; i++) {
                
                    if (Ext.DomQuery.select('displayAlias', nodes[i]).length == 0) {
                                    	try{
                        s.push(Ext.DomQuery.select('value', nodes[i])[0].firstChild.nodeValue);
                                                }
                        catch (e) {
                        }
                    }
                    else {
                                                  try {
                            s.push([Ext.DomQuery.select('value', nodes[i])[0].firstChild.nodeValue, Ext.DomQuery.select('displayAlias', nodes[i])[0].firstChild.nodeValue]);
                        }
                        catch (e) {
                               s.push(['', Ext.DomQuery.select('displayAlias', nodes[i])[0].firstChild.nodeValue]);
                        }
                    }
                    
                }
                return s;
                
            }
        }, {
            name: 'name',
            mapping: 'name'
        }, {
            name: 'type',
            mapping: 'type'
        }, {
            name: 'defaultValue',
            mapping: 'defaultValue'
        }, {
            name: 'description',
            mapping: 'description'
        }];
        
        return new Ext.data.XmlStore({
            fields: recordFields,
            proxy: new Ext.data.HttpProxy({
                url: this.url,
                method: 'get'
            }),
            record: 'parameter',
            id: 'ourRemoteStore',
            autoLoad: true
        });
    },
    
    /**
     *
     * @param {Object} inStore
     */
    generateFormItems: function(inStore){
    
        var me = Ext.getCmp('upload-fieldset');
        Ext.each(inStore.data.items, function(item){
            switch (item.get('type')) {
                case "FILE_OR_URL":
                    me.add(this.createUploadPanel(item));
                    
                    break;
                    
            }
        }
.createDelegate(this));
        
        var me = Ext.getCmp('parameters-fieldset');
        Ext.each(inStore.data.items, function(item){
            switch (item.get('type')) {
                case "STRING_OR_CHOICE":
                    me.add(this.createComboBox(item));
                    break;
                case "LISTBOX_ENCODED":
                    
                    me.add(this.createRadioGroup(item));
                    
                    break;
                case "LOOKUP_CHOICE":
                    
                    me.add(this.createAliasComboBox(item));
                    
                    break;
                case "TEXT":
                    
                    me.add(this.createTextField(item));
                    
                    break;
                case "FLOAT":
                    me.add(this.createTextField(item));
                    
                    break;
                case "COORDSYS":
                    me.add(this.createTextField(item));
                    
                    break;
                    
                default:
                    
            }
            
        }
.createDelegate(this));
    },
    
    /**
     *
     * @param {Object} item
     */
    createAliasComboBox: function(item){
    
        return new Ext.form.ComboBox({
            fieldLabel: item.get('description').replace(" (FME Short Name):", ""),
            anchor: '50%',
            typeAhead: true,
            triggerAction: 'all',
            lazyRender: true,
			id : item.get('name')+'-combo',
            mode: 'local',
            allowBlank: false,
            paramField: item.get('name'),
            store: new Ext.data.ArrayStore({
                fields: ['myId', 'displayText'],
                data: item.get('options')
            }),
            listeners: {
                afterrender: function(){         
                    this.setValue(this.store.collect('myId', true)[0]);
                },
                beforerender: function(){         
					if (this.getId() === 'DestinationFormat-combo'){
						this.description = 'FME Server supports over 250 formats. The list here is a subset containing the most popular formats.';
					}
                }				
            },
            valueField: 'myId',
            displayField: 'displayText'
        
        });
        
    },
    
    /**
     *
     * @param {Object} item
     */
    createComboBox: function(item){
    
        return new Ext.form.ComboBox({
            fieldLabel: item.get('description').replace(" (FME Short Name):", ""),
            anchor: '50%',
            typeAhead: true,
            triggerAction: 'all',
            lazyRender: true,
            forceSelection: true,
            mode: 'local',
            allowBlank: false,
            paramField: item.get('name'),
            store: new Ext.data.ArrayStore({
                fields: ['displayValue'],
                data: item.get('options')
            }),
            listeners: {
                afterrender: function(){         
                    this.setValue(this.store.collect('displayValue', true)[0]);

                }
            },
            displayField: 'displayValue',
            valueField: 'displayValue'
        });
        
    },
    
    /**
     *
     * @param {Object} item
     */
    createRadioGroup: function(item){
    
        var layerItems = item.get('options');
        var array = [];
        
        for (var i = 0, len = layerItems.length; i < len; i++) {
            array.push({
                boxLabel: item.get('options')[i],
                name: item.get('options')[i],
                paramField: item.get('name'),
                paramValue: item.get('options')[i]
            });
        }
        
        return new Ext.form.CheckboxGroup({
            id: 'myGroup',
            xtype: 'checkboxgroup',
            allowBlank: false,
            fieldLabel: item.get('description').replace(" (FME Short Name):", ""),
            itemCls: 'x-check-group-alt',
            // Put all controls in a single column with width 100%
            columns: 1,
            items: array
        });
        
    },
    
    /**
     *
     * @param {Object} item
     */
    createTextField: function(item){
        return new Ext.form.TextField({
            fieldLabel: item.get('description').replace(" (FME Short Name):", ""),
            value: item.get('defaultValue'),
            allowBlank: false,
            anchor: '50%'
        });
    },
    
    /**
     *
     */
    createUploadPanel: function(){
    
        return new Ext.Container({
            layout: 'column',
            
            items: [new Ext.ux.UploadPanel({
                buttonsAt: 'tbar',
                height: 200,
                width: 300,
                id: 'uppanel',
                columnWidth: 0.35,
                url: url + '/fmedataupload/Samples/easyTranslator.fmw' + jSessionId,
                path: 'root',
                maxFileSize: 1048576
            
            }), {
                xtype: 'panel',
                html: ddHtmlTemplates.dataUploadHelp(),
                bodyStyle: 'padding:5px 0 0 5px',
                height: 200,
                columnWidth: 0.65
            
            }]
        
        });
        
    }
});

