﻿Ext.ns("Ext.ux");

Ext.ux.Combo = function(config) {
    var fields = config.fields;
    if(!fields) {
        fields = ['LookupId', 'DisplayValue'];
    }
    delete config.fields;

    if(config.data) {
        var data = config.data;
        delete config.data;
        
        if(Ext.isArray(data)) {
            config.store = new Ext.data.SimpleStore({data: data, fields: fields});
        } else {
            config.store = new Ext.data.JsonStore({data: data, fields: fields});
        }
        Ext.applyIf(config, {
            mode: 'local'
        });
    }
    if(config.storeConfig) {
        var storeConfig = config.storeConfig;
        delete config.storeConfig;
        
        Ext.applyIf(storeConfig, {
            totalProperty: 'recordCount',
            root: 'records',
            fields: fields
        });
        
        var value = config.value;
        if(!storeConfig.data && typeof(value) == 'object') {
            Ext.apply(storeConfig, {
                data: {}
            });
            storeConfig.data[storeConfig.totalProperty] = 1;
            storeConfig.data[storeConfig.root] = [value];
            delete config.value;
            config.value = config.valueField || value[fields[0]];
        }

        config.store = new Ext.data.JsonStore(storeConfig);
        Ext.applyIf(config, {
            queryDelay: 25,
            minChars: 0,
            mode: 'local'
        });
        
    }
    Ext.applyIf(config, {
        forceSelection: true,
        triggerAction: config.mode == 'local' ? 'all' : undefined,
        valueField: fields[0],
        displayField: fields[1]
    });
    Ext.apply(this, config);
    Ext.ux.Combo.superclass.constructor.call(this);
};

Ext.extend(Ext.ux.Combo, Ext.form.ComboBox);

Ext.reg('xcombo', Ext.ux.Combo);

Ext.ux.SetCascading = function(parent, child) {
    child.setDisabled(!parent.isValid());
    
    parent.on('change', function() {
        child.lastQuery = null;
        child.clearValue();
        child.setDisabled(!parent.isValid());
    });

    child.on('focus', function() {
        if(!child.disabled) {
            var parentValue = parent.getValue();
            var childParams = child.store.baseParams;
            if(parentValue != childParams.ScopeId) {
                childParams.ScopeId = parentValue;
                child.clearValue();
                child.lastQuery = null;
                if(child.mode != 'local') {
                    child.store.removeAll();
                }
            }
        }
    });
}

Ext.override(Ext.form.ComboBox, {
    doQuery : function(q, forceAll){
        if(q === undefined || q === null){
            q = '';
        }
        var qe = {
            query: q,
            forceAll: forceAll,
            combo: this,
            cancel:false
        };
        if(this.fireEvent('beforequery', qe)===false || qe.cancel){
            return false;
        }
        q = qe.query;
        forceAll = qe.forceAll;
        if(forceAll === true || (q.length >= this.minChars)){
            if(this.lastQuery !== q){
                this.lastQuery = q;
                if(this.mode == 'local'){
                    this.selectedIndex = -1;
                    if(forceAll){
                        this.store.clearFilter();
                    }else{
                        this.store.filter(this.displayField, q);
                    }
                    this.onLoad();
                }else{
                    // change: Durlabh's change start
                    var storeCount;
                    if(this.store.snapshot) {
                        storeCount = this.store.snapshot.length;
                    } else {
                        storeCount = this.store.getCount();
                    }
                    if(this.store.lastOptions && this.store.lastOptions.params) {
                        var lastQuery = this.store.lastOptions.params[this.queryParam];
                    }
                    if(lastQuery != null
                            && q.length >= lastQuery.length
                            && q.substr(0, lastQuery.length) == lastQuery
                            && this.store.getTotalCount() == storeCount) {

                        this.selectedIndex = -1;
                        this.store.filter(this.displayField, q);                        
                        this.onLoad();
                    }
                    else {
                        this.store.baseParams[this.queryParam] = q;
                        this.store.load({
                            params: this.getParams(q)
                        });
                    }
                    this.expand();
                }
            }else{
                this.selectedIndex = -1;
                this.onLoad();
            }
        }
    }
});

Ext.ns("DA");
DA.swf = function(options) {
    Ext.applyIf(options, {
        version: "9.0.0",
        expressInstallSwfurl: false,
        params: {wmode: 'transparent'}
    });
    
    swfobject.embedSWF(options.swfUrl, options.id, options.width, options.height, options.version, options.expressInstallSwfurl, options.flashVars, options.params, options.attributes);
    delete options;
};

Ext.ux.SlidingWindow = function(){
    var msgCt;

    function createBox(t, s){
        return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
    }
    return {
        msg : function(title, format){
            if(!msgCt){
                msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
            }
            msgCt.alignTo(document, 't-t');
            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
            m.slideIn('t').pause(3).ghost("t", {remove:true});
        },

        init : function(){
        }
    };
}();
