/*
$Id: ngmp.js 380 2008-06-22 16:21:32Z lukejee $
*/

Ext.Ajax = new NGMP.data.Connection({
	autoAbort:false,
	serializeForm:function(A) {
		return Ext.lib.Ajax.serializeForm(A);
	}
});


NGMP.jsonAuthHandler=function(A,C,B){
	if(A.authError){
		switch(A.authError){
			case"UNAUTHORIZED":
				Ext.Msg.alert(LANG_unauthorized,LANG_unauthorized_tip);
 				return false;
 				break;
 			case"NOTLOGGEDIN":
				if(C){
					NGMP.loginDialog.addCallback(C,B);
				}
				NGMP.loginDialog.show();
 				return false;
				break;
			default:
				break;
			}
	}
	return true;
};

NGMP.deleteItems=function(A){
	switch(A.count){
		case 0:
			Ext.MessageBox.alert(LANG_error,LANG_no_item_selected);
 			return false;
 			break;
 	    case 1:
			var C=LANG_delete_selected_item;
 			break;
 		default:
			var B=new Ext.Template(LANG_delete_selected_items);
 			C=B.applyTemplate({count:A.count} );
			break;
	}
	Ext.MessageBox.confirm(LANG_confirm,C,function(D){
		if(D=="yes"){
			if(A.store){
				for(var E in A.params){
					A.store.baseParams[E]=A.params[E];
				}
				A.store.reload({
					callback:function(){
						if(!this.reader.jsonData.deleteSuccess){
							if(A.failure){
								callback=A.failure.createDelegate(A.scope);
 								callback.call();
							}
							Ext.MessageBox.alert(LANG_error,this.reader.jsonData.deleteFeedback);
						} else {
							if(A.success){
								callback=A.success.createDelegate(A.scope);
 								callback.call();
							}
						}
						if(A.callback){
							callback=A.callback.createDelegate(A.scope);
 						   	callback.call();
						}
					}
			  });
 			  for(E in A.params){
			    delete A.store.baseParams[E];
			  }
			} else {
				Ext.Ajax.request({
					url:A.url,
					params:A.params,
					callback:function(G,I,F){
						if(!I){
							Ext.MessageBox.alert(LANG_error,LANG_could_not_send_request);
						} else {
							var H=Ext.decode(F.responseText);
 							if(!H.success){
								if(A.failure){
									callback=A.failure.createDelegate(A.scope);
 									callback.call();
								}
								Ext.MessageBox.alert(LANG_error,H.feedback);
							} else {
								if(A.success){
									callback=A.success.createDelegate(A.scope);
 									callback.call();
								}
							}
							if(A.callback){
								callback=A.callback.createDelegate(A.scope);
 						   		callback.call();
							}
				   		}
			  		}
				});
	 	 	}	
    	}
	});
	return true;
};



NGMP.state.HttpProvider=function(A){
	NGMP.state.HttpProvider.superclass.constructor.call(this);
 	this.url=BaseHref+"ngmp.php";
 	Ext.apply(this,A);
 	this.state=this.readValues();
};

Ext.extend(NGMP.state.HttpProvider,Ext.state.Provider,{
	set:function(A,B){
		if(A.substr(0,4)!="ext-"){
			if(typeof B=="undefined"||B===null){
				this.clear(A);
				return;
			}
		this.setValue(A,B);
		NGMP.state.HttpProvider.superclass.set.call(this,A,B);
		}
	},
	clear:function(A){
		this.clearValue(A);
 		NGMP.state.HttpProvider.superclass.clear.call(this,A);
    },
	readValues:function(){
		var B={};
		//for(var A in NGMP.settings.state){
		//	if(A!="remove"){
		//		B[A]=this.decodeValue(NGMP.settings.state[A])
		//	}
		//}
		return B;
	},
	setValue:function(A,C){
		var B=new Ext.data.Connection();
		Ext.Ajax.request({
			url:this.url,
			params:{
				func:"stateSet",
				name:A,
				value:this.encodeValue(C)
			//	index:NGMP.settings.state_index
		    }
	    });
	},
	clearValue:function(A){
		var B=new Ext.data.Connection();
 		B.request({
			url:this.url,
			params:{
				func:"set",
				name:A,
				value:"null"
			}
	   });
	}
});

NGMP.moduleManager={
	modulePanels:{},
	panelConfigs:{},
	sortOrder:Array(),
	adminModulePanels:{},
	adminPanelConfigs:{},
	adminSortOrder:Array(),
	addModule:function(B,A,C){
		C.moduleName=B;
		this.modulePanels[B]=A;
		this.panelConfigs[B]=C;
		this.sortOrder.push(B);
	},
	addAdminModule:function(B,A,C){
		C.moduleName=B;
		this.adminModulePanels[B]=A;
		this.adminPanelConfigs[B]=C;
		this.adminSortOrder.push(B);
	},
	getAdminPanel:function(A){
		return new this.adminModulePanels[A](this.adminPanelConfigs[A]);
	},
	getAllAdminPanels:function(){
		var A=[];
		for(var B=0; B<this.adminSortOrder.length;B++){
			A.push(this.getAdminPanel(this.adminSortOrder[B]));
		}
		return A;
	},
	getAllAdminPanelConfigs:function(){
		var B=[];
	    for(var A=0; A<this.adminSortOrder.length;A++){
	    	B.push(this.adminPanelConfigs[this.adminSortOrder[A]]);
	   	}
		return B;
	},
	getPanel:function(A){
		return new this.modulePanels[A](this.panelConfigs[A]);
	},
	getAllPanels:function(){
		var A=[];
		for(var B=0; B<this.sortOrder.length;B++){
			A.push(this.getPanel(this.sortOrder[B]));
		}
		return A;
	}
};

var BaseHref = '';
Ext.state.Manager.setProvider(new NGMP.state.HttpProvider({
	//url: BaseHref + 'state.php'
}));

Ext.app.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
	initComponent:function() {
		Ext.app.SearchField.superclass.initComponent.call(this);
		this.on("specialkey",function(A,B){
			if(B.getKey()==B.ENTER){
				this.onTrigger2Click();
			}
		},this);
	},
	validationEvent:false,
	validateOnBlur:false,
	trigger1Class:"x-form-clear-trigger",
	trigger2Class:"x-form-search-trigger",
	hideTrigger1:true,
	width:180,
	hasSearch:false,
	paramName:"query",
	onTrigger1Click:function(){
		if(this.hasSearch){
			this.store.baseParams.start=0;
			this.store.baseParams[this.paramName]="";
		 	this.store.reload();
		  	this.el.dom.value="";
		   	this.triggers[0].hide();
		    this.hasSearch=false;
		}
	},
	onTrigger2Click:function(){
		var A=this.getRawValue();
		if(A.length<1){
			this.onTrigger1Click();
	  		return;
		}
		this.store.baseParams.start=0;
		this.store.baseParams[this.paramName]=A;
		this.store.reload();
		this.hasSearch=true;
		this.triggers[0].show();
	}
});
