


Ext.namespace('TJ.Admin');

TJ.Admin.Viewport = Ext.extend(Ext.Viewport,{
    layout:'border',
	username: 'Guest',
	userTextTemplate: 'Logged in as <b>{0}</b>',
	guestTextTemplate: 'Not logged in',
	userInfo: null,
	disabled: true,

	loginWindow: null,
	setUser: function(user){
		this.setDisabled(false);
		if(!user.userid)
			user.userid=0;
//		console.log(user);
		this.userInfo=user;

		if(user.userid >0) {
			if(user.username =='system') {
				Ext.fly(this.userText.getEl()).update(String.format(this.userTextTemplate,user.username) + ' ExtJS ' + Ext.version);  

			}
			else
				Ext.fly(this.userText.getEl()).update(String.format(this.userTextTemplate,user.username) + Ext.version);  


		}
		else
			Ext.fly(this.userText.getEl()).update(String.format(this.guestTextTemplate));  

		if(user.userid===0){
			TJ.Admin.MainTreePanel.getRootNode().collapse();
//			TJ.Admin.MainTabPanel.setActiveTab(0);
			if(! this.loginWindow){
				this.loginWindow=new TJ.Admin.LoginForm({
					callback: function(action){ TJ.Admin.MainWindow.setUser(action.result.userinfo);}
				});
			}
			this.loginWindow.show();

		} else {
			TJ.Admin.MainTreePanel.getRootNode().expand();

		}


	},
	render: function(ct,pos){
		TJ.Admin.Viewport.superclass.render.call(this,ct,pos);
		TJ.CallbackManager.GetInfo({url: TJ.LANGUAGESS + 'user:getinfo',callback: this.setUser,scope:this});

	},
	initComponent: function(){

		this.logoutButton = new Ext.Button({ text: 'Logout',
											 iconCls: 'icon_user_delete',
											scope: this,
											 handler: function(){ 
												 TJ.CallbackManager.Execute({url: TJ.LANGUAGESS + 'admin:logout',callback:this.setUser,scope:this});
								}
		});


	
		this.userText = new Ext.Toolbar.TextItem ({text: String.format('Loading...')});

				TJ.Admin.MainTreePanel = new Ext.tree.TreePanel({
                    region:'west',
                    title:'Navigation',
                    split:true,
					autoScroll: true,
                    width: 150,
                    minSize: 120,
                    maxSize: 400,
                    collapsible: true,
						tools: [ { id: 'refresh',
								   handler: function(){ 
											TJ.Admin.MainTreePanel.getLoader().load(TJ.Admin.MainTreePanel.getRootNode(),
												function(node){  node.expand();  });},
									scope: this
							}],

//						autoScroll:true,
//						animate:true,
//						enableDD:false,
//						border: false,
//						containerScroll: true ,
						loader: new Ext.tree.TreeLoader({
							dataUrl:'/en/admin:getuiactionchildren',
							listeners: { beforeload : function(treeLoader,node) { treeLoader.dataUrl = '/en/' +node.id+':getuiactionchildren';}}
						}),

						listeners: {
								beforeclick: function(node) { return !node.disabled;},
								click: function(node) { if(!node.disabled) TJ.Admin.LoadMainTabFromNode( node);
													}
//								,	 render: function(tp) { tp.getRootNode().expand();}
							},
						root: new Ext.tree.AsyncTreeNode({
						text: 'Admin Home',
						id:'admin',
						cls: 'cardenabled',
						iconCls: 'icon_application_home'
						})
					});


		this.items=
			[
                   new Ext.Toolbar({
					region: 'north',
					height: 24,
					items:
					[  new TJ.AltiusBtn(),'-',						new TJ.comp.FontSizeButton(),			
					'->', 
					this.userText,
					'-', 
					this.logoutButton
					]
				}),
					TJ.Admin.statusGrid.init(
					{
                    region:'south',
                    split:true,
                    height: 200,
                    minSize: 100,
                    maxSize: 500,
                    collapsible: true,
					margins:'0 0 0 0',
					layout: 'fit'
					}
						),		
					TJ.Admin.MainTreePanel,					
/*                 {
                    region:'west',
                    title:'Navigation',
                    split:true,
                    width: 200,
                    minSize: 150,
                    maxSize: 400,
					hideMode: 'offsets',
                    collapsible: true,

					border: false,
						tools: [ { id: 'refresh',
								   handler: function(){ 
											TJ.Admin.MainTreePanel.getLoader().load(TJ.Admin.MainTreePanel.getRootNode());},
									scope: this
							}],

					layout:'fit',
						items: 
//							new Ext.Panel({title: 'nav'})
					

                },
*/				{
                    region:'center',
					layout:'fit',
					items: TJ.Admin.MainTabPanel = new Ext.TabPanel ({

				        enableTabScroll: true,
		                activeTab:0,
						border: false,
						items:[{
							items : new TJ.Admin.HomePanel(),
							id: 'admin',
							layout: 'fit',
							title: 'Admin Home',
							iconCls: 'icon_cog',
							closable:false,
							autoScroll:true
						}],
						listeners: { tabchange: TJ.Admin.MainTabChange						,
									 contextmenu: function(tp,tab){
									TJ.Admin.ClearPanel(tab);
											tab.getUpdater().refresh();
															}
							}
						
	                })
				}
             ];
		TJ.Admin.Viewport.superclass.initComponent.call(this);
	}


});


	TJ.Admin.Startup = function (username) {
		TJ.Admin.MainWindow = new TJ.Admin.Viewport({});
//		TJ.Admin.statusGrid.addMessage({message: 'Logged in as ' + username, code: 0});
	};


TJ.Admin.LoginForm = Ext.extend(Ext.Window,{
	title:'Altius:  Login',
	modal: true,
	iconCls: 'icon_altius',
	layout: 'fit',
	height: 240,
	width: 600,
	closeAction: 'hide',
	closable: false,
	callback: null,
	form: null,
	initComponent: function(){

		this.form = 	new TJ.Admin.StatusPanelForm( {
				formconfig: {
					url:   '/en/admin:login',
					items:   [{"fieldLabel":"Username","name":"username","xtype":"tjtext","desc":"Enter your username.","allowBlank":false,"minLength":3,"maxLength":48},{"fieldLabel":"Password","name":"password","xtype":"tjpassword","desc":"Enter your password.","allowBlank":false},{"fieldLabel":"Remember me","name":"persist","xtype":"tjcheckbox","desc":"Allow automatic login from this computer.","checked":false}] ,
					buttons:   [{"text":"Login","value":"login"}],
					listeners: {	actioncomplete: {scope:this, fn: function(form,action){
	//							form.reset();
								if(this.callback)
									this.callback(action);
								this.hide();
								}
							}
					} 

					
					
					}
				});
			this.items=this.form;
			TJ.Admin.LoginForm.superclass.initComponent.call(this);
			this.on('hide', function(){  this.form.form.getForm().reset();},this);
	}
});


