ReservationWrapperPanel = function(config) {
		
	this.config = config;
	/**
	 * this.accountData  = Ext.decode(response.responseText);
				
				this.telButton.setText(this.config.name +" 연락처 : "+this.accountData.tel);
				this.accountButton.setText(" 계좌번호 :"+this.accountData.accountnumber+" ("+this.accountData.bank+"/"+this.accountData.accountowner+")");
				
				if(this.accountData.isowner){
					
					this.managerButton.setVisible(true);
					
					
				}
	 */
	
	this.editButton = new Ext.Button({
    	text:this.config.name +" 예약 시스템 정보 변경 ",
    	iconCls : 'check_icon',
    	scope : this,
    	handler: this.editReservationSys
    		
    });
	
	this.telButton = new Ext.Button({
    	text:this.config.name +" 연락처 : "+this.config.reservation.tel,
    	iconCls : 'check_icon',
    	scope : this,
    	pressed : true
    		
    });
	this.accountButton = new Ext.Button({
    	text:" 계좌번호 :"+this.config.reservation.accountnumber+" ("+this.config.reservation.bank+"/"+this.config.reservation.accountowner+")",
    	iconCls : 'check_icon',
    	scope : this,
    	pressed : true
    });
	
	
	
	
	
	
	
	
	if(this.config.reservation.isowner){
		// 관리자
		this.userReservationPanel = new UserReservationManagementPanel(
				{	
					id:"UserReservaion_"+this.config.record_data.camping_list_uid,
					title:this.config.name+" 예약관리(관리자전용)",
					border:false,
					defaults: {autoScroll:true},
					autoHeight:true,
					region:'center',
					autoWidth:true,
					split: true,
					collapsible: true,
					hideCollapseTool : true,
					collapseMode: "mini",
					camping_list_uid:this.config.record_data.camping_list_uid
				}
			);
		
		this.calendarWidth = "600";
		this.editButton.setVisible(true);
		
	}else{
		// 일반 사용자
		this.userReservationPanel = new UserReservationPanel(
				{	
					id:"UserReservaion_"+this.config.record_data.camping_list_uid,
					title:"예약자 현황",
					border:false,
					defaults: {autoScroll:true},
					split: true,
					collapsible: true,
					hideCollapseTool : true,
					collapseMode: "mini",
					region:'center',
					autoWidth:true,
					autoHeight:true,
					camping_list_uid:this.config.record_data.camping_list_uid
					//autoHeight:true
				}
			);
		
		this.calendarWidth = "600";
		this.editButton.setVisible(false);
	}
	

	this.calendarStore = new Ext.data.JsonStore({
        storeId: 'calendarStore',
        root: 'calendars',
        idProperty: 'id',
        data: {
            "calendars":[{
                "id":1,
                "title":"Home"
            },{
                "id":2,
                "title":"Work"
            },{
                "id":3,
                "title":"School"
            }]
        },
        proxy: new Ext.data.MemoryProxy(),
        autoLoad: true,
        fields: [
            {name:'CalendarId', mapping: 'id', type: 'int'},
            {name:'Title', mapping: 'title', type: 'string'}
        ],
        sortInfo: {
            field: 'CalendarId',
            direction: 'ASC'
        }
    });

    // A sample event store that loads static JSON from a local file. Obviously a real
    // implementation would likely be loading remote data via an HttpProxy, but the
    // underlying store functionality is the same.  Note that if you would like to 
    // provide custom data mappings for events, see EventRecord.js.
	this.eventStore = new Ext.data.JsonStore({
	       id: 'eventStore',
	       root: 'evts',
	       //data: eventList, // defined in event-list.js
	       proxy: new Ext.data.MemoryProxy(),
	       fields: Ext.calendar.EventRecord.prototype.fields.getRange(),
	       sortInfo: {
	           field: 'StartDate',
	           direction: 'ASC'
	       }
	});
	
	
	
	this.calendarPanel = new Ext.calendar.CalendarPanel({
		split: true,
		collapsible: true,
		hideCollapseTool : true,
		collapseMode: "mini",
	  
	   isowner : this.config.isowner,
       eventStore: this.eventStore,
       calendarStore: this.calendarStore,
       region:'west',
       width:this.calendarWidth,
       height:400,
       monthText:"",
       border: true,
       showWeekView : false,
       showDayView : false,
       showTime : false,
       todayText : this.config.name,
       id:'app-calendar_'+this.config.record_data.camping_list_uid,

      // activeItem: 2, // month view
       
       // CalendarPanel supports view-specific configs that are passed through to the 
       // underlying views to make configuration possible without explicitly having to 
       // create those views at this level:
       monthViewCfg: {
           showHeader: true,
           showWeekLinks: true,
           showWeekNumbers: true,
           //showTodayText : false,
           todayText:this.config.name
       },
       
       // Some optional CalendarPanel configs to experiment with:
       //showDayView: false,
       //showWeekView: false,
       //showMonthView: false,
       //showNavBar: false,
       //showTodayText: false,
       //showTime: false,
       //title: 'My Calendar', // the header of the calendar, could be a subtitle for the app
       
       // Once this component inits it will set a reference to itself as an application
       // member property for easy reference in other functions within App.
       initComponent: function() {
           //App.calendarPanel = this;
           this.constructor.prototype.initComponent.apply(this, arguments);
       },
       
       listeners: {
           'eventclick': {
               fn: function(vw, rec, el){
            	   //alert(rec.Title);
                   this.showEditWindow(rec, el);
                   this.clearMsg();
               },
               scope: this
           },
           'eventover': function(vw, rec, el){
               //console.log('Entered evt rec='+rec.data.Title+', view='+ vw.id +', el='+el.id);
           },
           'eventout': function(vw, rec, el){
               //console.log('Leaving evt rec='+rec.data.Title+', view='+ vw.id +', el='+el.id);
           },
           'eventadd': {
               fn: function(cp, rec){
                   this.showMsg('Event '+ rec.data.Title +' was added');
               },
               scope: this
           },
           'eventupdate': {
               fn: function(cp, rec){
                   this.showMsg('Event '+ rec.data.Title +' was updated');
               },
               scope: this
           },
           'eventdelete': {
               fn: function(cp, rec){
                   this.showMsg('Event '+ rec.data.Title +' was deleted');
               },
               scope: this
           },
           'eventcancel': {
               fn: function(cp, rec){
                   // edit canceled
               },
               scope: this
           },
           'viewchange': {
               fn: function(p, vw, dateInfo){
                   if(this.editWin){
                       this.editWin.hide();
                   };
                   if(dateInfo !== null){
                       // will be null when switching to the event edit form so ignore
                      // Ext.getCmp('app-nav-picker').setValue(dateInfo.activeDate);
                       this.updateTitle(dateInfo.viewStart, dateInfo.viewEnd);
                   }
               },
               scope: this
           },
           'dayclick': {
               fn: function(vw, dt, ad, el){
            	   this.showEditWindow({
                       StartDate: dt,
                       IsAllDay: ad
                   }, el);
                   this.clearMsg();
               },
               scope: this
           },
           'rangeselect': {
               fn: function(win, dates, onComplete){
                   this.showEditWindow(dates);
                   this.editWin.on('hide', onComplete, this, {single:true});
                   this.clearMsg();
               },
               scope: this
           },
           'eventmove': {
               fn: function(vw, rec){
                   rec.commit();
                   var time = rec.data.IsAllDay ? '' : ' \\a\\t g:i a';
                   this.showMsg('Event '+ rec.data.Title +' was moved to '+rec.data.StartDate.format('F jS'+time));
               },
               scope: this
           },
           'eventresize': {
               fn: function(vw, rec){
                   rec.commit();
                   this.showMsg('Event '+ rec.data.Title +' was updated');
               },
               scope: this
           },
           'eventdelete': {
               fn: function(win, rec){
                   this.eventStore.remove(rec);
                   this.showMsg('Event '+ rec.data.Title +' was deleted');
               },
               scope: this
           },
           'initdrag': {
               fn: function(vw){
                   if(this.editWin && this.editWin.isVisible()){
                       this.editWin.hide();
                   }
               },
               scope: this
           }
       }
   });

	/**
    this.commentGridPanel = new Ext.ux.hansub.board.CommentGridPanel({
				//title: "댓글 목록",
				id: "CommentGridPanel_"+this.config.id,
				region:'east',
				//width:300,	
				//autoHeight:100,
				//autoWidth:true,
				autoHeight:true,
				closable:false,
				loadMask: {msg:'댓글화면 구성 중'},
				//defaults: {autoScroll:true},
				split: true,
				collapsible: true,
				collapseMode: "mini",
				hideCollapseTool:true,
		        border:false,
				istoolbar:true,

		        hideHeaders:true
			});

    	**/
	
	this.myInfoPanel = new Ext.hansub.camping.MyCampingReservationInfoPanel({
		camping_user_uid : this.config.reservation.owner_uid,
		autoWidth:true,
		border:false,
		autoHeight:true,
		camping_list_name:this.config.name
		//frame:true,
		//padding: 7 //,
		//height:180
		
	})
   
	this.inlinePrivateBlog = new Ext.hansub.panel.InlineBlog({
		id:'inlineBlog_ID_OWNER_USER_'+this.config.reservation.camping_user_uid,
		title:"관리자에게  문의",
		camping_user_uid : this.config.reservation.owner_uid,
		camping_list_uid : '',
		pageSize : 10,
		iconCls:'memo_icon',
		autoWidth:true,
		autoHeight:true,
		fieldWidth : 200,
		'private' : true,
		private_name : "관리자",
		private_uid : this.config.reservation.owner_uid
		
	});

	this.tabPanel = new Ext.TabPanel({
		autoWidht:true,
		plain:true,
		defaults:{autoHeight: true},
		resizeTabs:false, // turn on tab resizing
        minTabWidth: 50,
        activeItem: 0,
        items:[this.userReservationPanel ]
	});
	
	this.tabPanel2 = new Ext.TabPanel({
		autoWidht:true,
		plain:true,
		defaults:{autoHeight: true},
		resizeTabs:false, // turn on tab resizing
        minTabWidth: 50,
        activeItem: 0,
        items:[this.inlinePrivateBlog ]
	});
	
	

			
	ReservationWrapperPanel.superclass.constructor.call(this, {
		id:'ReservationWrapperPanel_'+this.config.id,
		title:this.config.name+' 예약 시스템',
		closable:true,
		border:false,
		height:700,
		autoWidth:true,
    	defaults: {autoScroll:true},
    	tbar : ['->',this.editButton,'-', this.telButton,'-', this.accountButton,' '],
    	items:[{ 
					columnWidth:1,
					//style:'padding:7px 7px 7px 7px',
					items:[
					       this.myInfoPanel
					]
				},{ 
					columnWidth:.7,
					style:'padding:7px 7px 7px 7px',
					items:[
					       this.calendarPanel
					]
				},{ 
					columnWidth:.3,
					style:'padding:7px 7px 7px 0px',
					items:[
					       this.tabPanel2
					]
				},{ 
					columnWidth:.7,
					style:'padding:7px 7px 7px 7px',
					items:[
					       this.tabPanel
					]
				}/**,{ 
					columnWidth:.25,
					style:'padding:5px 5px 5px 5px',
					items:[
					       	this.inlinePanelBlog
					       	
					]
				}**/
			]
    	//this.myInfoPanel
        
	});
	
	this.on('activate', this.init, this);
	this.on('deactivate', this.undoLayout, this);
	this.on('close', this.undoLayout, this);
	
	//this.on('render', this.init, this);
	
	//this.commentGridPanel.on('render',function(){this.add(this.commentGridPanel);},this);
	
	
	/**
	this.on('afterlayout', this.init, this);
	this.on('activate', this.initLayout, this);
	this.on('close', this.closed, this);
	
	this.on('render',this.loginState,this);
   **/
};

Ext.extend(ReservationWrapperPanel, Ext.ux.Portal, {
	
	init : function(){
		
		// 관리자일 경우 (layout 변경)
		if(this.config.reservation.isowner){
			//Ext.getCmp("leftPanelID").collapse(false);
		}
		
	},
	
	undoLayout : function(){
		
		// 관리자일 경우 (layout 초기화)
		if(this.config.reservation.isowner){
			//Ext.getCmp("leftPanelID").expand(false);
		}
		
	},
	
	editReservationSys : function(){
		
		if(this.config.reservation.isowner){
			
			
			this.editReservationSysWindow = new EditReservationSysWindow({
						id:'editReservationSysWindowID',
						title: this.config.name+ ' 예약 시스템 신청  - 캠핑장 관리자만  신청이 가능합니다.-',
						iconCls:'add',
				        width:650,
				        resizable:true,
				        height:400,
						data:{
							parentID : this.config.id,
							camping_list_uid : this.config.record_data.camping_list_uid,	
							camping_list_name : this.config.name
						}
					        				        
			});
		
			this.editReservationSysWindow.show();
		
		}else{
			if(!Ext.getCmp('loginWindowID')){
				this.loginWin = new Ext.hansub.user.LoginWindow();
			    //this.loginWin.on('login', this.loginState, this);
	        }
	        Ext.getCmp('loginWindowID').reset();
	    	Ext.getCmp('loginWindowID').show();
			
		}
		
	},
	/**
	openReservationManager : function(){
		
		if(!Ext.getCmp("UserReservationManagementPanel_"+this.config.id)){
			
			this.userReservationManagementPanel = new UserReservationManagementPanel(
					{	
						id:"UserReservationManagementPanel_"+this.config.id,
						title:this.config.name+" 예약관리(관리자전용)",
						border:false,
						closable:true,
						defaults: {autoScroll:true},
						camping_list_uid:this.config.record_data.camping_list_uid
					}
				);
		}
		
		Ext.getCmp('centerCenterPanel').add("UserReservationManagementPanel_"+this.config.id);
		Ext.getCmp('centerCenterPanel').activate("UserReservationManagementPanel_"+this.config.id);
		
	},
	**/
	/**
	updateToolbar: function(){
		
		Ext.Ajax.request({
    		
			url: './src/api/Interface.php',
			params:{
				'class':'Reservation',
				'method':'getOwnerInformation',
				'camping_list_uid': this.config.record_data.camping_list_uid
			},
			scope:this,
			success:function(response, options){
				
				this.accountData  = Ext.decode(response.responseText);
				
				this.telButton.setText(this.config.name +" 연락처 : "+this.accountData.tel);
				this.accountButton.setText(" 계좌번호 :"+this.accountData.accountnumber+" ("+this.accountData.bank+"/"+this.accountData.accountowner+")");
				
				if(this.accountData.isowner){
					
					this.managerButton.setVisible(true);
					
					
				}
				
				
			},
			failure:function(response, options){
				Ext.MessageBox.alert('ERROR', '예약정보를 로딩에 오류가 발생하였습니다.');
			}
			            	
					
		});
		
		
		
		
	},
	
	**/
	updateTitle: function(startDt, endDt){
		
		
		this.startDt = startDt;
		
		this.endDt = endDt;
    	
    	toolbarText = this.config.name+" 예약  (";
    	
    	if(startDt.format('d') > 20){
    		month = startDt.getMonth() + 2;
    		
    	}else{
    		month = startDt.getMonth() + 1;
    	}
    	if(month == 13){
			month = 1;
		}
    	
    	
    	if(month == 1 && startDt.format('d') > 20){
    	
    		
    		year = startDt.getFullYear()+"";
    		year = year.substr(2,2) + 1;
    		
    		
    		
    	}else{
    		
    		year = startDt.getFullYear()+"";
    		year = year.substr(2,2);
    		
    	}
    	
    	
    	this.userReservationPanel.load(year, month);
    	
    	Ext.getCmp('app-calendar_'+this.config.record_data.camping_list_uid+'-tb-month').setText(toolbarText+" "+year+"년 "+month+"월)");
    	
    	this.userReservationPanel.setTitle(this.config.name+" 예약자 현황  ("+year+"년 "+month+"월)");
    	   	
    	Ext.Ajax.request({
    		
			url: './src/api/Interface.php',
			params:{
				'class':'Reservation',
				'method':'getReservation',
				'camping_list_uid': this.config.record_data.camping_list_uid,
				'year': year,
				'month': month
			},
			waitMsg: month+'월 예약정보를 로딩합니다.',
			scope:this,
			success:function(response, options){
				
				var json = Ext.decode(response.responseText);
				
				this.calendarPanel.eventStore.loadData(json);		
				
				this.calendarPanel.doLayout();
				//this.calendarPanel.fireViewChange();
				
				
			},
			failure:function(response, options){
				Ext.MessageBox.alert('ERROR', '예약정보를 로딩에 오류가 발생하였습니다.');
			}
			            	
					
		});
    	
    	
    },
    
    showEditWindow : function(rec, animateTarget){
    	
    	
    	if(IS_LOGIN){
    		
    		if(!this.editWin){
                this.editWin = new Ext.calendar.EventEditWindow({
                	id:"editWindowID",
                    calendarStore: this.calendarStore,
                    accountData : this.accountData,
                    rec:rec,
    				listeners: {
    					'eventadd': {
    						fn: function(win, rec){
    							
    							this.updateTitle(this.startDt, this.endDt);
    							win.hide();
    							Ext.MessageBox.alert('안내', '입금이 완료되어야 정상 예약이 가능합니다.<br> 계좌번호 :'+this.config.reservation.accountnumber+' ('+this.config.reservation.bank+'/'+this.config.reservation.accountowner+')');
    							/**
    							rec.data.IsNew = false;
    							this.eventStore.add(rec);
    							**/
                               // this.showMsg('입금이 완료되어야 정상 예약이 가능합니다.');
    						},
    						scope: this
    					},
    					'eventupdate': {
    						fn: function(win, rec){
    							this.updateTitle(this.startDt, this.endDt);
    							win.hide();
    							Ext.MessageBox.alert('안내', '입금이 완료되어야 정상 예약이 가능합니다.<br> 계좌번호 :'+this.config.reservation.accountnumber+' ('+this.config.reservation.bank+'/'+this.config.reservation.accountowner+')');
    						},
    						scope: this
    					},
    					'eventdelete': {
    						fn: function(win, rec){
    							this.updateTitle(this.startDt, this.endDt);
    							win.hide();
    							Ext.MessageBox.alert('안내', '입금이 완료되어야 정상 예약이 가능합니다.<br> 계좌번호 :'+this.config.reservation.accountnumber+' ('+this.config.reservation.bank+'/'+this.config.reservation.accountowner+')');
    						},
    						scope: this
    					},
                        'editdetails': {
                            fn: function(win, rec){
                            	this.updateTitle(this.startDt, this.endDt);
    							win.hide();
    							Ext.MessageBox.alert('안내', '입금이 완료되어야 정상 예약이 가능합니다.<br> 계좌번호 :'+this.config.reservation.accountnumber+' ('+this.config.reservation.bank+'/'+this.config.reservation.accountowner+')');
                            }
                        }
    				}
                });
            }
            this.editWin.show(rec, animateTarget, this.config.record_data.camping_list_uid);
		
		}else{
			if(!Ext.getCmp('loginWindowID')){
				this.loginWin = new Ext.hansub.user.LoginWindow();
			    //this.loginWin.on('login', this.loginState, this);
	        }
	        Ext.getCmp('loginWindowID').reset();
	    	Ext.getCmp('loginWindowID').show();
			
		}
    	
        
	},
	
	howMsg: function(msg){
        Ext.fly('app-msg').update(msg).removeClass('x-hidden');
    },
    
    clearMsg: function(){
        //Ext.fly('app-msg').update('').addClass('x-hidden');
    }
	
});

