pcwa.core.scope('pcwa.ext.model');
pcwa.ext.model.EventMgr = function EventMgr()
{
	this.loadMessage =
	{
		showLoadMsg : false,
		showLoadQue : "",
		currLoading : 0,
		loading : false,

		showLoadWaitMsg : function showLoadWaitMsg(bShow)
		{
			if ( this.loading ){
				this.showLoadQue = bShow == true ? bShow : false;
			} else {
				this.showLoadMsg = bShow == true ? bShow : false;
			}
		},
		
		startLoading : function startLoading()
		{
			this.currLoading++;
			if ( this.currLoading == 1 ){
		    	Ext.Msg.wait("", "Getting Data");
				this.loading = true;
			}
			Ext.Msg.updateText(this.loadMsg());
		},
		doneLoading : function doneLoading()
		{
			if ( this.currLoading > 0 ){
				this.currLoading--;
				if ( Ext.Msg.isVisible() ){
					if ( this.currLoading == 0 ){
						Ext.Msg.hide();
						if ( this.showLoadQue != "" ){
							this.showLoadWaitMsg(this.showLoadQue);
							this.showLoadQue = "";
						}
						this.loading = false;
					} else {
						Ext.Msg.updateText(this.loadMsg());
					}
				}
			}
		},
		loadMsg : function loadMsg()
		{
			var msg = "";
			if ( this.currLoading == 1 ){
				msg = "Please wait...(1 set remaining)";
			}else{
				msg = "Please wait..." + "(" + this.currLoading + " sets remaining)";
			}
			return msg;
		}
	};
	
    this.addEvents(
    				"add",
					"beforeload",
    				"clear",
    				"datachanged",
    				"load",
    				"loadexception",
    				"metachange",
    				"remove",
    				"update"
    			  );

    // The following event handlers process Ext.StoreMgr events
    this.onaddstore = function(idx, store, key){
    	store.on({
    		"add": this.onadd,
    		"beforeload": this.onbeforeload,
			"clear": this.onclear,
			"datachanged": this.ondatachanged,
			"load": this.onload,
			"loadexception": this.onloadexception,
			"metachange": this.onmetachange,
			"remove": this.onremove,
			"update": this.onupdate,
			scope: this
    	});
    };
    this.onclearstores = function(){
    	return;
    };
    this.onremovestore = function(o, key){
    	return;
    };
    this.onreplacestore = function(key, oldO, newO){
    	return;
    };
    Ext.StoreMgr.on({	'add'		: this.onaddstore,
						'clear'		: this.onclearstores,
						'remove'	: this.onremovestore,
						'replace'	: this.onreplacestore,
						scope		: this
				   });
    pcwa.ext.model.EventMgr.superclass.constructor.call(this);
};

Ext.extend(pcwa.ext.model.EventMgr, Ext.util.Observable,
{
    // The following event handlers are used to
    // expose the store events at the page level
    onadd : function(store, records, idx){
    	this.fireEvent("add", store, records, idx);
    },
    onbeforeload : function(store, options){
    	if ( !(store.proxy.conn.allowAutoEvents === false) ){
    		if ( this.loadMessage.showLoadMsg ){ this.loadMessage.startLoading();}
    	}
    	this.fireEvent("beforeload", store, options);
    },
    onclear : function(store){
    	this.fireEvent("clear", store);
    },
    ondatachanged : function(store){
    	if (store.proxy && !(store.proxy.conn.allowAutoEvents === false) ){
	    	if ( this.loadMessage.showLoadMsg ){ this.loadMessage.doneLoading();}
    	}
    	this.fireEvent("datachanged", store);
    },
    onload : function(store, records, options){
    	this.fireEvent("load", store, records, options);
    },
    onloadexception : function(proxy, options, callback, error){
    	if ( !(proxy.conn.allowAutoEvents === false) ){
    		if ( this.loadMessage.showLoadMsg ){ this.loadMessage.doneLoading();}
    	}
    	this.fireEvent("loadexception", proxy, options, callback, error);
    },
    onmetachange : function(store, meta){
    	this.fireEvent("metachange", store, meta);
    },
    onremove : function(store, record, idx){
    	this.fireEvent("remove", store, record, idx);
    },
    onupdate : function(store, record, operation){
    	this.fireEvent("update", store, record, operation);
    }
});
    

var dataEventManager = new pcwa.ext.model.EventMgr();


