Yext.view = {};

Yext.view.base = function(){
	var opt = arguments[0];	
	if(!opt || !opt.url || !opt.ctrl){ 
		Yext.util.error('NO_ARGS','No url/ctrl is given');
		return;
	}
	
	this.panel = null;
	this.url = opt.url;	
	this.ctrl = opt.ctrl;
	this.observers = [];
	this.components = {};
	
	this.conn = new Ext.data.Connection({
		url: opt.url,
		disableCaching: true,
		method: 'GET',
		autoAbort: true,
		extraParams: { out_format: 'json', ctrl: opt.ctrl }
	});
	
	this.initialize(opt);
};

Ext.extend(Yext.view.base, Ext.util.Observable, {
	initialize	: function(opt){
		if(opt.cfg){
			if(opt.cfg.auth){ this.auth = opt.cfg.auth; }
			this.view(opt.cfg);
		}else{
			this.conn.request({
				params	:	{
					action: Yext.cons.ACTIONS.INIT
				},
				scope	:	this,
				failure : 	function(r,o){
					Yext.util.error('AJAX_FAIL');
				},
				success	:	function(r,o){
					var res = Yext.util.responseTextHandler(r.responseText);
					if(res){				
						if(!res.auth){ Yext.util.error('NO_AUTH'); }
						else if(!res.parts){ Yext.util.error('NO_PARTS'); }
						else{
							this.auth = res.auth;
							this.view(res);
							
							if(opt.callback){
								var obj = opt.scope?opt.scope:this;
								opt.callback.call(obj);
							}
						}
					}
				} // end success
			}); // end request
		} // end else
	}, // end initialize
	
	// To be implemented at subclass
	view: Ext.emptyFn,
	
	getPanel: function(){
		return this.panel;
	},
	
	doLayout: function(){
		this.panel.doLayout();
	}
});

