/* TJ.Admin.ClientSide Editing Module


*/


TJ.Admin.ClientSide = function (){
	var first=true;
	var menu=false;
	var tip=false;
	var win=false;

	var AdminWindow=false;
	return {
		init: function(){
				el = Ext.get('AltiusComponent');
				if(el) {
					preview=el.getAttributeNS('','preview');

					var AltiusButton = new Ext.Toolbar({
						text: 'Tools',
						iconCls: 'icon_cog',
						renderTo: 'AltiusComponent',
						items: [ new TJ.AltiusBtn()
							,'-',
								new Ext.Button ({
									text: 'Admin',
									iconCls:'icon_application_lightning',
									handler: function(){ 
											AdminWindow=window.open(TJ.LANGUAGESS +'admin','AltiusAdmin').focus();
									
										}
									, scope: this}),
										'|',

						new Ext.Button ({
									text: 'Show Icons',
									iconCls:'icon_cog',
									enableToggle: true,
									handler: function(){TJ.Admin.ClientSide.toggle();} }),
										'|',
								new Ext.Button ({
									text: 'Preview',
									iconCls:'icon_page_white_magnify',
									enableToggle: true,
									pressed: preview,
									handler: function(){TJ.Admin.ClientSide.togglePreview();} })

						]
//										,						width: 220
					});
				}
		},
		togglePreview: function(){
			TJ.CallbackManager.GetInfo({url: TJ.LANGUAGESS + 'admin:previewmode'});
		},
		toggle: function(){
			spans = Ext.select('.InlineAdmin');
			for(var span=0; span < spans.elements.length;span++){
				if(first){
					el=Ext.get(spans.elements[span]);

					info=Ext.util.JSON.decode(el.getAttributeNS('','info'));
					el.info=info;

					el.toggle();
					el.on('contextmenu',TJ.Admin.ClientSide.display);
					el.on('click',TJ.Admin.ClientSide.display);

				}
				else {
					Ext.fly(spans.elements[span]).toggle();
					if(tip)
						tip.hide();
				}
			}
			first=false;
		},

		display: function(e,el,c){

			e.preventDefault();
			info=Ext.util.JSON.decode(Ext.fly(el).getAttributeNS('','info'));

			if(! tip){

				tip = new Ext.Tip({
				    baseCls: 'x-panel',
					draggable: true,
					closable: true,
					layout: 'fit',
					title: 'Tip here',
					iconCls: 'icon_cog',
					border: true,
					items: grid = new Ext.grid.PropertyGrid({height: 200,		startEditing: Ext.emptyFn}),
					tools: [{ id: 'gear',handler: TJ.Admin.ClientSide.handle}],
					minWidth: 200
				});
			}

			tip.setTitle(info.title);
			tip.showBy(el);
			grid.store.sortInfo = null; 
			grid.getColumnModel().config[0].sortable=false;
			grid.setSource(null);
			grid.body.mask();

			Ext.Ajax.request({ url: TJ.LANGUAGESS + 'admin/client:getpopupinfo',
								params: info,
								callback:  function (a,b,c){ 
									grid.body.unmask();
									val = TJ.Admin.ProcessJSONResponse(a,b,c);
									grid.setSource(val.data);
									grid.comp=val.comp;
								}
				
				
			});
//			menu.show(el);
		},
		handle: function(e){
			if(tip)
				tip.hide();

			if(! win){
				console.log('creating win');
				win = new Ext.Window({
					constrainHeader:true,				
					stateful:true,
					stateId: 'TJ_Admin_Client_Popup',
					maximizable : true,
					title: tip.title,
					width:  800,
					height:  600,
					layout: 'fit',
					closeAction: 'hide'

				});
			}
			if(grid && grid.comp) {
				TJ.Admin.ClearPanel(win);
				win.add(Ext.ComponentMgr.create(grid.comp));
				win.show();
			}
			else
				Ext.MessageBox.alert('Error','Editor not yet defined');
		}
	};


} ();

TJ.SubmitForm=function(e){
	if(Ext.get(e).parent('form'))
		Ext.get(e).parent('form').dom.submit();
	return false;
}

TJ.ExpandColumns=function(){
		items =Ext.select('.pm_column.expandheight');
		items.each(function(e) {
			if(e.getHeight() < e.parent().getHeight()){
				e.setHeight(e.parent().getHeight());
			}
		}
		);
		items =Ext.select('.pm_column.expandheight-child');
		items.each(function(e) {
			if(e.getHeight() < e.parent().getHeight()){
				e.setHeight(e.parent().getHeight());
			}
			child=e.child('> :last-child');
			if(child) {
				child.setHeight(e.getTop()+e.getHeight() - child.getTop());
			}
		}
		);

};