
	MainPanel = {};

	
	Nav = {};
	
	
	
	
	function orderWin(cust){
	
    	OrderWin = new Ext.Window({
    		title: 'Bestellung f&uuml;r: ' + cust.customer_no + ' ' + cust.customer,
    		closable:true,
            closeAction:'destroy',
            modal:true,
            height:600,
            layout:'fit',
            width:800,
            x:50,
            y:20,
            items:[
	            new Ext.Panel({
	            	regiuon:'center',
	            	layout:'border',
		             items:[AddressFld(cust)
			            ,new Ext.Panel({
			            	id: 'orderTpl',
			            	region:'center',
			            	html: 'grid'
			            })
			            ,new Ext.Panel({
			            	id: 'sumaryTpl',
			            	region:'south',
			            	height: '80',
			            	html: 'sumaryTPL'
			            
			            })
			           ]
		            })
			],
            tbar: [{
	              text: 'Bestellung parken',
	              handler: function(){
	                  OrderWin.destroy();
	                  //getWin();
	              }
            	},{
	              text: 'Bestellung senden',
	              handler: function(){
	                  OrderWin.destroy();
	                  //getWin();
	              }
            	},'->',{
            		text: 'Schlie&szlig;en',
		              handler: function(){
	               		OrderWin.destroy();
	                  //getWin();
	              		}
            	}],
            	bbar:new Ext.StatusBar({
		            defaultText: 'Status: ' + _lang.status_default,
		            id: 'orderStatusBar',
		            defaultIconCls: '',
			        items: ['', new Date().format('l, d.m.Y'), ' ']
		        })
    		
    	});
//    	addrTpl.overwrite('addrTpl');
//    	orderGrid.overwrite('orderTpl');
//    	sumaryTPL.overwrite('sumaryTpl');
    	OrderWin.show();
	   
	   
    }
	
	var customer_list_grid = new Ext.grid.GridPanel({
	        store: customer_list_grid_store,
			autoScroll:true,
			disabled:false,
			loadMask: true,
			frame:false,
			bodyStyle:'border:1px solid #D0D0D0;',
			disableSelection:false,
	        columns: [
	        	{
	            	id:'customer_no',
	            	header: 'Kunden-Nr.', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'customer_no',
	            	width:60
	            },
	            {
	            	id:'lead_salutation',
	            	header: 'Anrede', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'lead_salutation',
	            	hidden:true,
	            	hide:true,
	            	width:60
	            },
	            {
	            	id:'first_name',
	            	header: 'Vorname', 
		        	menuDisabled:false,
		        	sortable:true,
	            	hidden:true,
	            	hide:true,
	            	dataIndex: 'first_name'
	            },
	            {
	            	id:'last_name',
	            	header: 'Nachname', 
		        	menuDisabled:false,
		        	sortable:true,
	            	hidden:true,
	            	hide:true,
	            	dataIndex: 'last_name'
	            },
	            {
	            	id:'customer',
	            	header: 'Kunde', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'customer'
	            },
	            {
	            	id:'street',
	            	header: 'Strasse', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'street'
	            },
	            {
	            	id:'street_no',
	            	header: 'Str. Nr.', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'street_no',
	            	width:60
	            },
	            {
	            	id:'country',
	            	header: 'Land', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'country'
	            },
	            {
	            	id:'zip',
	            	header: 'Plz', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'zip',
	            	width:60
	            },
	            {
	            	id:'city',
	            	header: 'Ort', 
		        	menuDisabled:false,
		        	sortable:true,
	            	dataIndex: 'city'
	            }
	        ],
	        stripeRows: true,
	        layout: 'fit',
	        region: 'ceneter',
	        viewConfig: {
            	forceFit:true
				//,emptyText: 'Bitte wählen Sie eine Benutzergruppe. <br>Ihre Suche hat keine Ergebnisse geliefert. Bitte verallgemeinern Sie Ihre Suche.'
	        },
//	        tbar: [
//	            _lang.fast_search + ': ',  
//	            new Ext.app.SearchField_for_Lead_search({
//	                store: customer_list_grid_store,
//	                id:'search_string_lead',
//	                paramName:'search_string_lead',
//	                emptyText:'sämtliche Daten',
//	                width:240,
//	                listeners: {
//	                	'blur': function(){
//	                		Ext.getCmp('search_string_lead').emptyText = '';
//							Ext.getCmp('search_string_lead').applyEmptyText();
//	                	}
//	                }
//	            })
//	        ],
	        bbar: new Ext.PagingToolbar({
	            pageSize: 25,
	            id:'customer_list_grid_store_search_res_bbar',
	            store: customer_list_grid_store,
	            displayInfo: true,
	            displayMsg: ' {0} - {1} von {2}',
	            emptyMsg: _lang.no_data
//	            ,
//	            plugins: [new Ext.ux.PageSizePlugin()]
	        }),
	        listeners: {
				'rowcontextmenu': function(grid,rowIndex,e) {
					if (!this.contextMenu) {
						this.contextMenu = new Ext.menu.Menu({
							id: 'gridCtxMenu',
							items: [{
								 text: '<b>neue Bestellung</b>'
								,scope:this
								,handler: function(){
									orderWin(this.ctxRecord.data);
//									alert("neue Bestellung f&uuml;r " + this.ctxRecord.data.customer_no);
								}
							},{
								text: 'Kundendaten anzeigen'
							}]
						})
						
						
					}
					e.stopEvent();
					var xy = e.getXY();
					this.ctxRecord = this.store.getAt(rowIndex);
					this.contextMenu.showAt(xy);
				}
				,'rowdblclick': function(grid,rowIndex,e) {
					this.ctxRecord = this.store.getAt(rowIndex);
//					 alert("neue Bestellung f&uuml;r " + this.ctxRecord.data.customer_no);
					orderWin(this.ctxRecord.data);
				}
	        }
	    }); 
	    customer_list_grid.on('render', function(sm, rowIdx, r) {
			//alert("asd");
	    	customer_list_grid_store.on('beforeload', function() {
			     customer_list_grid_store.baseParams = {
			          'sort':'last_name'
			          ,'dir':'ASC'
			          ,search_string:''
			          ,user_id:_loggedInuserId 
			          ,role_id:_loggedInRight_id
			          ,consult_id:_loggedInConsultant_no
			          ,prog_lang: params.lang
			          ,todo:'getMyCustomers'
				};
			});
			customer_list_grid_store.load();
		});
		
		
	
		
	Nav.TreeData = {
		 	"id":"leads"
			,"iconCls":"icon-docs"
			,"text":"Bestellung"
			,"singleClickExpand":true
			,"children":[{
				 "id":"orderHist"
				,"text":"Bestellhistorie"
				,"leaf":true
				},{
				 "id":"newOrder"
				,"text":"neue Bestellung"
				,"leaf":true
				,listeners: {
					'click': function(){
							var TabPan = Ext.getCmp('doc-body');
							var tab = Ext.getCmp('custList');
							if(tab){
								TabPan.setActiveTab(tab);
							}else{
								var p = TabPan.add({
									title: 'Bestandskunden',
									closable:false,
									id:'custList',
									region:'center',
		        					layout: 'fit',
									items:[customer_list_grid]
								});
								TabPan.setActiveTab(p);
							}
//							Ext.getCmp('custList').doLayout();
						}
					
					}
				}]
			},{
		 		'id':'cust'
		 		,'text':'Kunden'
		 		,"singleClickExpand":true
		 		,"leaf":true
		 		
			};
		
	
		
	NavPanel = function(){
    	NavPanel.superclass.constructor.call(this, {
    		id:'api-tree',
	        region:'west',
	        split:true,
	        minSize: 175,
	        maxSize: 500,
	        frame:false,
	        collapsible: true,
	        margins:'0 0 5 5',
	        cmargins:'0 0 0 0',
	        rootVisible:false,
	        lines:false,
	        autoScroll:true,
	        animCollapse:false,
	        animate: false,
	        collapseMode:'mini',
	        loader: new Ext.tree.TreeLoader({
				preloadChildren: true,
				clearOnLoad: false
			}),
	        root: new Ext.tree.AsyncTreeNode({
	            text:'Baum',
	            id:'root',
	            expanded:true,
	            children:[Nav.TreeData]
	         }),
	        collapseFirst:true,
	        listeners: {
	        	afterlayout: { 
	        		fn: function(){
						this.expandAll();
					}
				}
			}
    	});
    	
    	
    };
    
    Ext.extend(NavPanel, Ext.tree.TreePanel, {
    	
    });
		
	var win;
	var treePanel = new Ext.tree.TreePanel({
			id: 'tree-panel',
			collapsible: true,
	    	collapseMode: 'mini',
	    	width:200,
	    	minWidth: 150,
			title: 'Produktgruppen',
			region:'west',
			split: true,
			width:200,
			autoScroll: true,
			bodyStyle: 'padding-bottom:15px;background:#FFF;',
			// tree-specific configs:
			rootVisible: false,
			lines: true,
			singleExpand: true,
			useArrows: true,
			
			loader: new Ext.tree.TreeLoader({
				dataUrl:'/php_bin/modules/order_system/treedata.php'
			}),
        	root: new Ext.tree.AsyncTreeNode()
    });
	treePanel.on('click', function(node){
//    	Ext.get.insertHtml('beforeBegin','textFld',node.id);
//		Ext.Msg.alert('Multiverse', node.text + ' ' + node.id);
		productGrid.store.load({params:{id_cat:node.id}})
	});
	
	
	
	
	
	var productGrid = new Ext.grid.GridPanel({
		store: store,
		region:'center',
		columns: [
			{id:'orderno', header: "Bestell Nr.", width: 40, sortable: true, dataIndex: 'order_number'},
			{header: "Bezeichnung",  sortable: true, dataIndex: 'article_name'},
			{header: "Packungseinheit", align:'right', width:35,dataIndex: 'article_size'},
			{header: "Einheit", width: 35, sortable: true, dataIndex: 'article_quantity_unit'},
			{header: "Preis", width: 35, align:'right', sortable: true, dataIndex: 'article_vk_price'}
		],
		viewConfig: {
		    forceFit: true
		},
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		width:800,
		height:600
	});
	
	var productWin = function(){
	    if(!win){
	    	win = new Ext.Window({
	    		title:'Produkte',
	    		closeable:true,
	    		layout: 'border',
                closeAction:'hide',
                plain: true,
                modal:true,
                height:600,
                width:800,
                x:50,
                y:20,
                items: [treePanel, productGrid]
	    	});
	    }
	    win.show(this);
    };
	
	
	
	OrderPanel = function(){
		OrderPanel.superclass.constructor.call(this, {
			id:'api-tree',
			region:'west',
			split:true,
			width: 280,
			minSize: 175,
			maxSize: 500,
			collapsible: true,
			margins:'0 0 5 5',
			collapseMode:'mini',
	        autoScroll:true,
	        animCollapse:true,
	        animate: true,
			cmargins:'0 0 0 0',
			items:[new NavPanel]
		});
	};
	
	Ext.extend(OrderPanel, Ext.Panel, {
		
		initEvents : function(){
	        OrderPanel.superclass.initEvents.call(this);
//	        this.body.on('click', this.onClick, this);
	    }
	});
	
	
	
	MainPanel = function(){
		
		MainPanel.superclass.constructor.call(this, {
			id:'doc-body',
	        region:'center',
//	        margins:'0 5 5 0',
	        layoutOnTabChange: true,
	        resizeTabs: true,
	        plugins: new Ext.ux.TabCloseMenu(),
	        enableTabScroll: true,
	        activeTab: 0,
	        items: {
	            id:'welcome-panel',
	            title: 'Bestellung Online',
	            autoLoad: {url: '/bin/modules/order_system/welcome.html', scope: this},
	            iconCls:'icon-docs',
	            autoScroll: true,
				tbar: [
					'Search: ', ' ', {
						text:'Hinzuf&uuml;gen',
						iconCls: 'add',
						handler:productWin
					}
	            ]
        	}
		});
	};
	
	Ext.extend(MainPanel, Ext.TabPanel, {
		initEvents : function(){
	        MainPanel.superclass.initEvents.call(this);
//	        this.body.on('click', this.onClick, this);
	    }
	});
	
	var orderpanel = new OrderPanel();
	var mainPanel = new MainPanel();
	var order_system_panel = new Ext.Panel({
        layout:'border',
        items:[ orderpanel, mainPanel ]
    });
	
//var order_system_panel = new Ext.Panel({
//	title:'/modules/order_system/order_system.js'
//	,region:'center'
//	,layout:'fit'
//	,frame:true
//});