baseObj = function(config){
	mj.apply(this, config);
	this.init();
};
baseObj.prototype = {
	buttons : false,
	forms : false,
	cacheParam : function(store){
		var p = store.params, cp = {};
		for(var i in p)
			if(typeof p[i] != 'function'&&i!='toJSONString')
				cp[i] = p[i];
		return cp;
	},
	setParam : function(store,params){
		store.params = {};
		return mj.apply(store.params,params);
	},
	deleteLoad : function(obj,event){
		var cp = obj.cacheParam(obj.stores.grid);
		mj.apply(obj.stores.grid.params,{methodName : (event?event:'deleteRecord'), table : obj.table, id : obj.grids.records.selectedRow && obj.grids.records.selectedRow.data ? parseInt(obj.grids.records.selectedRow.data.id) : 0,oldParams : cp.toJSONString(), mId:obj.mId}); 
		obj.stores.grid.load();
		obj.setParam(obj.stores.grid,cp);
	},
	clearForms : function(_forms){
		if(!_forms)
			_forms = this.forms;
		for(var f in _forms)
			if(typeof f!='function' && typeof f.clear == 'function')
				f.clear();
		if(this.win && typeof this.win.setTitle == 'function')
			this.win.setTitle(this.winTitle);
	},
	clearPageBar : function(g){
		var pb = g.pbar.params, p = g.store.params;
		p.current = pb.current = 1;
		p.start = pb.start = 0;
	},
	setDetailStore : function(g,id){
		g.store.params.id = id;
		this.clearPageBar(g);
		g.load();
	},
	btnDisabler : function(){
		var btns = this.buttons;
		if(btns.editBtn&&typeof btns.editBtn.setDisable == 'function')
			btns.editBtn.setDisable();
		if(btns.deleteBtn&&typeof btns.deleteBtn.setDisable == 'function')
			btns.deleteBtn.setDisable();
	},
	waitMaskResize : function(){
		var wm = this.waitMask;
		wm.css({'width':wm.renderTo.width(), 'height':wm.renderTo.height()});
	},
	waitMaskShow : function(){
		this.waitMask[0].style.display='block';
	},
	waitMaskHide : function(){
		this.waitMask[0].style.display='none';
	},
	onWindowBeforeClose : function(){
		return !this.ajaxActive;
	},
	bindWaitMask : function(str){
		if(str){
			str.on('beforeload',function(){
				this.ajaxActive = true;
				this.waitMaskShow();
			}, this);
			str.on('load',function(){
				this.waitMaskHide();
				this.ajaxActive = false;
			}, this);
		}
	},
	bindFormLoad : function(){
		var t = this, formField = false;
		if(t.forms && t.forms.main){
			var form = t.forms.main;
			for(var i=0,l=form.items.length;i<l;i++)
				if((formField = form.items[i]).isKey){
					form.pkField = formField;
					formField.on('focus', function(field){t._onKeyFieldFocus(field);}, t);
					formField.on('blur', function(field){t._onKeyFieldBlur(field);}, t);
				}
			form.store.on('load', function(){
				if(form.store.isEmpty){
					t.createNewRecord();
					if(form._beforeLoadParams)
						form.setValue(form._beforeLoadParams);
				}
				form._beforeLoadParams = false;
			}, t);
		}
	},
	_onKeyFieldBlur : function(field){
		var t = this, form = field.form, val = field.getValue();
		//db kontrol et, kayıt varsa yükle yoksa yeni kayıt moduna geç
		if(val!='' && val!=field._oldValue)
			t.editRecord(field.getValue(), field.dataIndex);
	},
	_onKeyFieldFocus : function(field){
		var t = this, form = field.form;
		field._oldValue = field.getValue();
	},
	createNewRecord : function(ev){
		var t = this;
		if(t.win && typeof t.win.setTitle == 'function')
			t.win.setTitle(t.winTitle+' - ' +mj.lng.glb.addRecord);
		t.grids.records.clearSelectedRow();
		t.btnDisabler();
		t.recordMode = ev&&ev!=''?ev:'addRecord';
		t.forms.main.clear();
		//sor!!!
		//t.clearItems(t.forms.main.items);
		t.tabPanels.main.setActive(0);
	},
	_storeOnLoad : function(data){
		if(this != arguments.callee._oScope){
			return arguments.callee.apply(arguments.callee._oScope, arguments);
		};
		this.recordMode = data.data.length>0?'editRecord':'addRecord';
		if(this.win && typeof this.win.setTitle == 'function')
			this.win.setTitle(this.winTitle+' - ' +mj.lng.glb[this.recordMode]);
	},
	editRecord : function(recordId, dataIndex){//dataIndex verilirse recordId kolonunda gelen değer ID yerine dataIndex ile eşlenecek. Ör: editRecord(65, 'sicil')
		var t = this, form = this.forms.main;
		if(typeof t.customEditHandler == 'function')
			t.customEditHandler.apply(this, [recordId, dataIndex]);
		else{
			form._beforeLoadParams = {};
			form._beforeLoadParams[dataIndex] = recordId;
			var q = {};
			q[t.table+'.'+dataIndex] = recordId;
			form.store.url = t.url;
			form.store.params = {
				className : t.stores.grid.params.className||'',
				methodName : t.stores.grid.params.methodName||'',
				event : t.formLoadEvent || t.stores.grid.params.event,
				table : t.table,
				q : q.toJSONString()
			};
			form.load();
			form.store.onOnce('load',t._storeOnLoad);
		}
	},
	_printPreviewLoadTrigger : function(store){
		store.params.limit = store._limitBackup;
		store.params.current = store._currentBackup;
		store.mon('load', this._printPreviewLoadTrigger);
		this._printPreview(this._printGrid);
	},
	_printLoadTrigger : function(){
		store.params.limit = store._limitBackup;
		store.params.current = store._currentBackup;
		store.mon('load', this._printLoadTrigger);
		this._print(this._printGrid);
	},
	printPreview : function(grid, fromPrintFn){
		if(!grid)
			grid = this.grids.records;
		if(parseInt(grid.store.recordCount)!=grid.store.data.length){
			grid.store._limitBackup = grid.store.params.limit;
			grid.store._currentBackup = grid.store.params.current;
			grid.store.params.limit = parseInt(grid.store.recordCount);
			grid.store.params.current = 1;
			this._printGrid = grid;
			grid.store.on('load', fromPrintFn ? this._printLoadTrigger : this._printPreviewLoadTrigger, this);
			grid.store.load();
		}else{
			var nw = mj.newWindow({title:'Verimot'}), nb = $(nw.document.body);
			$(nw.document.body.previousSibling).append('<link href="'+this.printCssPath+'" type="text/css" rel="stylesheet"/>');

			mj.NE(nb, {cls:'mj-heading-1', html:this.reportTitle});
			grid.copyContent(nb);
			return nw;
		}
	},
	print : function(){
		var t = this;
		var nw = t.printPreview(null, true);
		if(nw){
			nw.print();
			nw.close();
		}
	},
	_printPreview : function(grid){
		if(!grid)
			grid = this.grids.records;
		var nw = mj.newWindow({title:'Verimot'}), nb = $(nw.document.body);
		$(nw.document.body.previousSibling).append('<link href="'+this.printCssPath+'" type="text/css" rel="stylesheet"/>');

		mj.NE(nb, {cls:'mj-heading-1', html:this.reportTitle});
		grid.copyContent(nb);
		return nw;
	},
	_print : function(){
		var t = this;
		var nw = t.printPreview();
		nw.print();
		nw.close();	
	},
	grids : false,
	panels : false,
	stores : false,
	tabPanels : false,
	tabs : false,
	url : 'libs/mjRemote/router.php',
	windows : false,
	wins : false,
	gridLoaded : true,
	formLoadEvent : false,
	affectRecord : false,
	customClose : false,
	init : function(){
		if(handle.win){
			handle.win.obj=this;
			this.win = handle.win;
			this.winTitle = handle.win.title;
			this.win.addRelated(this.waitMask);
		}
		this._storeOnLoad._oScope = this;
		this.recordMode = 'addRecord';
		this.buttons = {};
		this.forms = {};
		this.grids = {};
		this.panels = {};
		this.stores = {};
		this.tabPanels = {};
		this.tabs = {};
		this.windows = {};
		this.wins = [];
		this.waitMask = $(mj.NE(handle,{
			cls:'mj-page-wait-mask mj-opacity-8',
			style:'display:none;width:100%;height:100%;',
			html:'<table width="100%" height="100%"><tr><td align="center" valign="middle"><img src="'+mj.glb.imagePath+'ajax-loader.gif"/><br/><br/><span class="mj-page-wait-title">'+mj.lng.glb.pleaseWait+'</span></td></tr></table>'
		}));
		this.waitMask.renderTo = handle;
		mj.bindResize(handle, this.waitMaskResize, this);
		this.panels.main = new mj.panel({
			renderTo : handle,
			border : false
		});
		//$(this.panels.main.getBody()).css('overflow', 'hidden');
		if(this.win){
			var c = this;
			this.contextMenu = new mj.contextmenu({
				renderTo : mj.NE(),
				parent : this.panels.main.getBody(),
				menuScope : this,
				width : 130,
				items : [
					{title:'Kapat',iconCls:'mj-menu-exit-icon',handler : function(){
						c.win.close();
					}}
				]
			});
			this.win.addRelated(this.contextMenu);
			this.win.onOnce('beforeclose', this.onWindowBeforeClose, this);
		}
		this.stores.form = new mj.store();
	}
};
