
/*****************************************************************************************************************/
/************************************************ nqBatchMemoryStore *********************************************/
/*****************************************************************************************************************/

/*   uses all batchStore
*/

App.components.nqBatchMemoryStore=function(config){
	this.batchId=0;
	this.version=2;
	this.allStore=new App.components.nqBatchStore({proxy:config.proxy,reader:config.reader,remoteProxy:config.remoteProxy,remoteReader:config.remoteReader})
	config.proxy=new Ext.ux.data.PagingMemoryProxy('');
	//config.remoteSort=false; //DNF
	this.customFilter=config.customFilter;
	this.allStore.addListener("load",function(){
		//when new data is received in allStore then proxy is reconfigured to get new filtered and sorted data from allStore
		this.batchId=this.allStore.batchId;
		var d=this.collectData(this.allStore.data.items);
		this.proxy = new Ext.ux.data.PagingMemoryProxy(d,{
			customFilter:this.customFilter
		});
		if(this.lastOptions && this.lastOptions.params && this.lastOptions.params.start && this.lastOptions.params.limit){
			this.reload();
		}else{
			this.load({params:{start:0,limit:this.limitLoad}})
		}
		
	},this,{delay:10}) //delay added so that calc records would work

	App.components.nqBatchMemoryStore.superclass.constructor.call(this, config);
}

App.components.nqBatchMemoryStore.prototype={
	loadRemote:function(catchupBatchId){
		if(typeof catchupBatchId=="undefined"){var catchupBatchId=-1;}
		this.allStore.loadRemote(catchupBatchId);
	},
	setFilter:function(fn){
		this.customFilter=fn;
		var d=this.collectData(this.allStore.data.items);
		this.proxy = new Ext.ux.data.PagingMemoryProxy(d,{
			customFilter:this.customFilter
		});
		if(this.params && this.params.start && this.params.limit){
			this.reload();
		}else{
			this.load({params:{start:0,limit:this.limitLoad}})
		}

		return;
	},
	collectData:function(records){
		var r = [];
        for(var i = 0, len = records.length; i < len; i++){
            r[r.length] = records[i].json;
        }
        return r;
	}			

}
Ext.extend(App.components.nqBatchMemoryStore, Ext.data.Store, App.components.nqBatchMemoryStore.prototype);

/*****************************************************************************************************************/
/************************************************ nqBatchStore *********************************************/
/*****************************************************************************************************************/

/*   uses batch Proxy - 
	This is the store that connects to the actual data via the proxy
	Need to listen to this and add/modify data in the actual store?
*/

App.components.nqBatchStore=function(config){
	this.batchId=0;
	this.version=2;
	this.remoteStore=new App.components.nqBatchRemoteStore({
		proxy:config.remoteProxy,reader:config.remoteReader
	});
	this.remoteStore.addListener("newremotedata",function(remoteStore,records){
		this.batchId=this.remoteStore.batchId;
		this.clearFilter();
		this._snapshot=this.data.clone();
		this.remove(records);
		//this.addSorted(records);
		this.add(records);
		if(this.catchupBatchId!=-1 && this.catchupBatchId>this.batchId){
			//more to load dont fire event
			this.remoteStore.load();
		}else{
			this.fireEvent("load", this, records);
		}
	},this)
	this.remoteStore.addListener("noremotedata",function(remoteStore,records){
		this.batchId=this.remoteStore.batchId;
		if(this.catchupBatchId!=-1 && this.catchupBatchId>this.batchId){
			this.remoteStore.load();
		}
	},this)

	App.components.nqBatchStore.superclass.constructor.call(this, config);
}
/*
App.stores.stats.cse.load();
*/
App.components.nqBatchStore.prototype={
	loadRemote:function(catchupBatchId){
		if(typeof catchupBatchId=="undefined"){var catchupBatchId=-1;}
		if(catchupBatchId!=-1 && this.batchId!=0 && catchupBatchId-this.batchId>1){
			this.catchupBatchId=catchupBatchId;
		}else{
			this.catchupBatchId=-1;
		}
		this.remoteStore.load();
	}	

}
Ext.extend(App.components.nqBatchStore, Ext.data.Store, App.components.nqBatchStore.prototype);


/*****************************************************************************************************************/
/************************************************ nqBatchRemoteStore *********************************************/
/*****************************************************************************************************************/

/*   uses batch Proxy - 
	This is the store that connects to the actual data via the proxy
	Need to listen to this and add/modify data in the actual store?
*/
App.components.nqBatchRemoteStore=function(config){
	this.batchId=0;
	App.components.nqBatchRemoteStore.superclass.constructor.call(this, config);
    this.addEvents("newremotedata");
    this.addEvents("noremotedata");
    this.addEvents("batchincrement");
    this.addEvents("batchdecrement");
	if(this.proxy){
		//increment batch id when new proxy batch id is loaded
		this.proxy.addListener("load",function(){
			var newBatchId=this.proxy.batchId;
			var oldBatchId=this.batchId;
			this.batchId=newBatchId;
			if(newBatchId>oldBatchId){
				//alert("increment")
				this.fireEvent("batchincrement",this);
			}
			if(newBatchId<oldBatchId){
				//alert("decrement") (used for init new day)
				this.fireEvent("batchdecrement",this);
			}
		},this)
		//check if new records loaded
		this.addListener("load",function(store,records,options){
			if(records.length >0){
				this.fireEvent("newremotedata",this,records);
			}else{
				this.fireEvent("noremotedata",this,records);
			}
		},this)
	}
}
App.components.nqBatchRemoteStore.prototype={

}
Ext.extend(App.components.nqBatchRemoteStore, Ext.data.Store, App.components.nqBatchRemoteStore.prototype);



/*****************************************************************************************************************/	
/*************************************************** nqBatchProxy ************************************************/
/*****************************************************************************************************************/
/*
	the batch proxy understands EOF and BNO syntax and increments its own internal batchID counter
	each call to load will load the next txt file to be loaded and will understand when to load new data.
*/
App.components.nqBatchProxy=function(config){
	this.batchId=0;
	this.filePrefix=""; //prices
	this.filetType=""; //txt
	this.incremental=false;
	App.components.nqBatchProxy.superclass.constructor.call(this, config);

}
App.components.nqBatchProxy.prototype={
	
}
Ext.extend(App.components.nqBatchProxy, Ext.data.ScriptTagProxy, App.components.nqBatchProxy.prototype);

App.components.nqBatchProxy.override={
    load : function(params, reader, callback, scope, arg){
        if(this.fireEvent("beforeload", this, params) !== false){

            var p = Ext.urlEncode(Ext.apply(params, this.extraParams));
            
			if(!this.incremental || this.batchId==0){
				var fileSuffix="";
			}else{
				var nextBatchId=this.batchId+1;
				var fileSuffix="_"+nextBatchId;
			}
            var url = this.url+this.filePrefix+fileSuffix+"."+this.fileType;
            
            url += (url.indexOf("?") != -1 ? "&" : "?") + p;
            if(this.nocache){
                url += "&_dc=" + (new Date().getTime());
            }
            var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
            var trans = {
                id : transId,
                cb : this.callbackSuffix,//"stcCallbackXXX"+transId,
                scriptId : "stcScript"+transId,
                params : params,
                arg : arg,
                url : url,
                callback : callback,
                scope : scope,
                reader : reader
            };
            var conn = this;

            window[trans.cb] = function(o){
                conn.handleResponse(o, trans);
            };

            url += String.format("&{0}={1}", this.callbackParam, trans.cb);

            if(this.autoAbort !== false ){
                this.abort();
            }

            trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);

            var script = document.createElement("script");
            script.setAttribute("src", url);
            script.setAttribute("type", "text/javascript");
            script.setAttribute("id", trans.scriptId);
            this.head.appendChild(script);

            this.trans = trans;
        }else{
            callback.call(scope||this, null, arg, false);
        }
    },
    // private
    handleResponse : function(o, trans){
	
        this.trans = false;
        this.destroyTrans(trans, true);
        var result;
        try {
            result = trans.reader.readRecords(o);
            if(result.EOF){
            	//this is called when a file has been loaded - irrespective of whether there are data or not
            	this.batchId=this.batchId+1;
            }
            if(result.batchId!=null){
            	//this only occurs when the root file is called
	            this.batchId=result.batchId;
	        }
        }catch(e){			
            this.fireEvent("loadexception", this, o, trans.arg, e);
            trans.callback.call(trans.scope||window, null, trans.arg, false);
            return;
        }
        this.fireEvent("load", this, o, trans.arg);
        trans.callback.call(trans.scope||window, result, trans.arg, true);
    },
	
	handleFailure : function(trans){
        this.trans = false;
        this.destroyTrans(trans, false);
        this.fireEvent("loadexception", this, null, trans.arg);
        trans.callback.call(trans.scope||window, null, trans.arg, false);		
    },
	

    // private
    destroyTrans : function(trans, isLoaded){
			
        this.head.removeChild(document.getElementById(trans.scriptId));
        clearTimeout(trans.timeoutId);
		//20100728 in case of failure next time prices are not loaded.
		if (1 == 2) {
			if (isLoaded) {
				window[trans.cb] = undefined;
				try {
					delete window[trans.cb];
				} 
				catch (e) {
				}
			}
			else {
				// if hasn't been loaded, wait for load to remove it to prevent script error
				window[trans.cb] = function(){
					window[trans.cb] = undefined;
					try {
						delete window[trans.cb];
					} 
					catch (e) {
					}
				};
			}
		}
    }	

}
Ext.override(App.components.nqBatchProxy, App.components.nqBatchProxy.override);

	
	App.sample=function(config){
		Ext.apply(this, config);
		this.init();
	};
	
	App.sample.prototype={
		init : function(config){
			config=config||this.config;
		}
	}

	Ext.extend(App.sample, Ext.util.Observable, App.sample.prototype);


if(typeof(NQJSScriptsLoaded)=="number"){NQJSScriptsLoaded=NQJSScriptsLoaded+1;}