Ext.namespace("Ext.hansub.camping");

Ext.hansub.camping.CampingDetailPortalPanel = function(config) {
	
	this.config = config;
	this.config.title = this.config.name;
	this.config.closable = true;
	
	this.reservationButton = new Ext.Button({
		text:'예약',
		iconCls:'board_modify',
		handler:this.reservation,
		scope:this
	});
	
	this.regReservationSysButton = new Ext.Button({
		text:'예약시스템 신청',
		iconCls:'board_modify',
		hidden : true,
		handler:this.regReservationSys,
		scope:this
	});

	this.regBasicButton = new Ext.Button({
		text:'캠핑장 정보 수정하기',
		iconCls:'board_modify',
		handler:this.regCamping,
		scope:this
	});
	
	
	
	this.regImgButton = new Ext.Button({
		text:this.config.name + '에서 찍은 사진올리기',
		iconCls:'img_icon',
		handler:function(){
			this.inlinePanelBlog.upload();
		},
		scope:this
	});

	this.regMyNextCampingjangButton = new Ext.Button({
		text: '다음  캠핑지로 설정',
		iconCls:'flag',
		handler:this.setNextCampingjang,
		scope:this
	});

	this.regConquerorMarkButton = new Ext.Button({
		text: '정복지 마크',
		iconCls:'flag',
		handler:this.setConquerorMark,
		scope:this
	});
	
	
	
	this.config.tbar = [" ",this.reservationButton,this.regReservationSysButton,"-",this.regBasicButton,'-',this.regImgButton,'-',this.regMyNextCampingjangButton,'-',this.regConquerorMarkButton];
	
	
	
	
	
	
	
	
	
	this.infoPanel = new Ext.hansub.camping.CampingHeaderPanel({
							id: 'price_infoPanel' + this.config.id,
							style:'border-bottom:1px solid #ccc;',
							autoHeight:true,
							border:false,
							camping_uid:this.config.record_data.camping_list_uid,
							data:this.config.record_data
							
						});
	
	
	this.naverBlog = new Ext.hansub.naverAPI.NaverOpenAPIPanel({
		id: this.config.name+"BLOG",
		//title: this.config.name + ' 블러그',
		target: 'blog',
		region:'center',
		keyword: this.config.name,
		viewtype: 'summary',
		border: false,
		start: 1,
		display: 15
	
	});
	
	this.mapPanel = new Ext.hansub.camping.CampingMapPanel({
		id: this.config.name+"MAP",
		//region:'west',
		x: this.config.x,
		y: this.config.y,
		camping_list_uid:this.config.record_data.camping_list_uid,
		border: true
	
	});
	
	this.inlinePanelBlog = new Ext.hansub.panel.InlineBlog({
		id:'inlineBlog_ID_CAM_'+this.config.record_data.camping_list_uid,
		title:this.config.name,
		camping_user_uid : '',
		camping_list_uid : this.config.record_data.camping_list_uid,
		region:'center',
		autoWidth:true,
		border : true,
		closable:false,
		fieldWidth : 150,
		imageable : true,
		inputdisable : false
	});
	

	
	this.daumImage = new Ext.hansub.daumAPI.DaumIMGOpenAPIPanel({
		id:this.config.name+"IMAGE",
		region:'south',
   		border:false,
   		start:1,
   		display:18,
		data:{
				isSearch:true,
				viewtype:'summary',
				target:'image',
				keyword:this.config.name,
		camping_list_uid:this.config.record_data.camping_list_uid
			}
		
		});

	this.campingUser = new Ext.hansub.camping.CampingUserGridPanel({
		id:'CampingUserGridPanel1_ID'+this.config.record_data.camping_list_uid,
		camping_list_uid : this.config.record_data.camping_list_uid,
		autoHeight:true,
		autoWidth:true,
 		border:false
	});

	
	var tools = [{
        id:'gear',
        handler: function(){
            Ext.Msg.alert('Message', 'The Settings tool was clicked.');
        }
    },{
        id:'close',
        handler: function(e, target, panel){
            panel.ownerCt.remove(panel, true);
        }
    }];
	
	/**
	this.mapPortalPanel = new Ext.ux.Portal({
		//title:'위치',
		tools: tools,
		layout:'fit',
		//style:'padding:10px 0 10px 10px',
		items : [this.mapPanel]
	});
	
	
	this.naverPortalPanel = new Ext.ux.Portal({
		//title:'블러그',
		tools: tools,
		layout:'fit',
		//style:'padding:10px 0 10px 10px',
		items : [this.naverBlog]
	});
	**/
	
	this.inlinePortalPanel = new Ext.ux.Portal({
		title:this.config.name+" 한줄 후기",
		tools: tools,
		layout:'fit',
		//style:'padding:10px 10px 10px 10px',
		items : [this.inlinePanelBlog]
	});
	
	
	/**
	this.daumImgPortalPanel = new Ext.ux.Portal({
		title:"이미지",
		tools: tools,
		layout:'fit',
		//style:'padding:10px 10px 10px 10px',
		items : [this.daumImage]
	});
	
	this.campingImgPortalPanel = new Ext.ux.Portal({
		title:"이미지",
		tools: tools,
		layout:'fit',
		//style:'padding:10px 10px 10px 10px',
		items : [this.campingImage]
	});
	
	*/
	
/**
	
	this.homepageGrid = new Ext.mobile.grid.HomepageGrid({
		border:false,
		pageSize :40,
		height:200
		
	});
	
	this.brokerGrid = new Ext.mobile.grid.BrokerGrid({
		border:false,
		pageSize :40,
		height:200
		
	});
	
	this.commentGrid = new Ext.mobile.grid.CommentGrid({
		border:false,
		pageSize :40,
		height:200
		
	});
	
	this.boardGrid = new Ext.mobile.board.BoardGridPanel({
							iconCls:'community_icon',
							border:false,
							region:'center',
							data:{
								mobile_boardtype_uid: '2',
								viewtype: 'summary',
								mobile_boardcat_uid:'4',
								includeTotal:false
							}
						})

	
	
	var tools = [{
        id:'gear',
        handler: function(){
            Ext.Msg.alert('Message', 'The Settings tool was clicked.');
        }
    },{
        id:'close',
        handler: function(e, target, panel){
            panel.ownerCt.remove(panel, true);
        }
    }];

	
	this.homePortalPanel = new Ext.mobile.panel.PortalPanel({
		title:'Ȩ������ ����',
		tools: tools,
		layout:'fit',
		style:'padding:10px 0 10px 10px',
		items : [this.homepageGrid]
	});
	
	
	this.brokerPortalPanel = new Ext.mobile.panel.PortalPanel({
		title:'������� ����',
		tools: tools,
		layout:'fit',
		style:'padding:10px 10px 10px 10px',
		items : [this.brokerGrid]
	});
	
	this.commentPortalPanel = new Ext.mobile.panel.PortalPanel({
		title:'�?�ǰ� ����Ʈ',
		tools: tools,
		layout:'fit',
		style:'padding:0px 0 10px 10px',
		items : [this.commentGrid]
	});

	this.boardPortalPanel = new Ext.mobile.panel.PortalPanel({
		title:'ȸ���� �Խ���',
		tools: tools,
		layout:'fit',
		style:'padding:0px 10px 10px 10px',
		items : [this.boardGrid ]
	});

	
	this.config.items = [{
		
			                columnWidth:1,
							items:[
								this.infoPanel
							]
						},{ 
							columnWidth:.50,
							items:[
								this.homePortalPanel,
								this.commentPortalPanel
							]
						},{ 
							columnWidth:.50,
							items:[
								this.brokerPortalPanel,
								this.boardPortalPanel 
							]
						}];
	**/
	this.config.items = [
		                     {
								columnWidth:1,
								items:[
									this.infoPanel
								]
							},{ 
								columnWidth:.68,
								style:'padding:7px 0 7px 7px',
								items:[
								    this.mapPanel,
								    this.naverBlog,
									this.daumImage,
									this.campingUser
								]
							},{ 
								columnWidth:.32,
								style:'padding:7px 7px 7px 7px',
								items:[
								       	this.inlinePanelBlog
								]
							}/**,{ 
								columnWidth:.25,
								style:'padding:5px 5px 5px 5px',
								items:[
								       	this.inlinePanelBlog
								       	
								]
							}**/
						];
	
	Ext.hansub.camping.CampingDetailPortalPanel.superclass.constructor.call(this, this.config);
	

	this.on('afterlayout', this.init, this);
	    
};

Ext.extend(Ext.hansub.camping.CampingDetailPortalPanel , Ext.ux.Portal, {
	
	init : function(){
		
		
		this.mapPanel.init();
		
		this.campingUser.showPanel();
		
		// 예약 타입 확인
		Ext.Ajax.request({
    		
			url: './src/api/Interface.php',
			params:{
				'class':'Reservation',
				'method':'getReservationType',
				'camping_list_uid': this.config.record_data.camping_list_uid
			},
			scope:this,
			success:function(response, options){
				
				var json = Ext.decode(response.responseText);
				
				this.reservationType = json.type;
				this.reservationValue = json.value;
				
				if(this.reservationType != "reservationsystem"){
					
					this.regReservationSysButton.setVisible(true);
					
				}
				
				
			},
			failure:function(response, options){
				Ext.MessageBox.alert('ERROR', '예약정보를 로딩에 오류가 발생하였습니다.');
			}
			            	
					
		});
		
	},
	
	regReservationSys : function(){
		
		if(IS_LOGIN){
			
			this.RegReservationSysWindow = new RegReservationSysWindow({
						id:'RegReservationSysWindowID',
						title: this.config.name+ ' 예약 시스템 신청  - 캠핑장 관리자만  신청이 가능합니다.-',
						iconCls:'add',
				        width:500,
				        resizable:true,
				        height: 170,
						data:{
							parentID : this.config.id,
							camping_list_uid : this.config.record_data.camping_list_uid,	
							camping_list_name : this.config.name
													
						}
					        				        
			});
		
			
		
				
			this.RegReservationSysWindow.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();
			
		}
		
		
	},
	
	
	loginState: function(){
		/**
		if (IS_LOGIN) {
			this.regBasicButton.setVisible(true);
			this.regImgButton.setVisible(true);
		}else{
			this.regBasicButton.setVisible(false);
			this.regImgButton.setVisible(false);
		}
		**/
	},
	
	setNextCampingjang : function() {
		if(IS_LOGIN){		
  		Ext.Ajax.request({
  			url: './src/api/Interface.php',
  			params:{
  					'class':'Camping',
      			method:'setNextCampingjang',
      			camping_list_uid:this.config.record_data.camping_list_uid
  					},
  			scope:this,
  			success:function(response, options){
  				var json = Ext.decode(response.responseText);
  				Ext.MessageBox.alert('확인', '나의 Next 캠핑지로 설정되었습니다.')
  			},
  			failure:function(response, options){
  				Ext.MessageBox.alert('오류', '서버에 응답이 없습니다. 잠시 후 다시 접근해 주세요.')
  			}
  		});	
    }//end if (IS_LOGIN)
		else{
				if(!Ext.getCmp('loginWindowID')){
					this.loginWin = new Ext.hansub.user.LoginWindow();
        }
        Ext.getCmp('loginWindowID').reset();
	    	Ext.getCmp('loginWindowID').show();
		}
    
    
	},
	setConquerorMark : function() {
		if(IS_LOGIN){		
  		addCamingConquerorInfoWindow = new AddCampingConquerorInfoWindow({
  		  camping_name:this.config.record_data.name,
  		  camping_list_uid:this.config.record_data.camping_list_uid,
  		  grid_index:500
  		  });
  		addCamingConquerorInfoWindow.show();
    }//end if (IS_LOGIN)
		else{
				if(!Ext.getCmp('loginWindowID')){
					this.loginWin = new Ext.hansub.user.LoginWindow();
        }
        Ext.getCmp('loginWindowID').reset();
	    	Ext.getCmp('loginWindowID').show();
		}
	},
	
	upload : function(){
		
		if(IS_LOGIN){		
			
			this.descField =  new Ext.form.TextField({
				emptyText: '업로드 이미지에 대한 간략한 설명을 입력해 주세요.',
			    fieldLabel: '설명',
			    name: 'content'			
			});
			
			this.fileUploadField =  new Ext.form.FileUploadField({
				emptyText: '업로드 파일을 선택해 주세요.(이미지 파일만 가능합니다)',
			    fieldLabel: '이미지',
			    name: 'file',
			    buttonCfg: {
				    text: '',
				    iconCls: 'upload-icon'
			    }			
			});
	
			
			var fp = new Ext.FormPanel({
			        fileUpload: true,
			        width: 500,
			        frame: true,
			        autoHeight: true,
			        bodyStyle: 'padding: 10px 10px 0 10px;',
			        labelWidth: 65,
			        defaults: {
			            anchor: '100%',
			            allowBlank: false,
			            msgTarget: 'side'
			        },
			        items: [this.fileUploadField,this.descField],
			        buttons: [{
			            text: '업로드',
			            scope: this,
			            handler: function(){
			                if(fp.getForm().isValid()){
				                fp.getForm().submit({
				                    waitMsg: '이미지 업로드 중 입니다....',
									url:'./src/api/Interface.php',
									params :{
												'class':'Camping',
												method:'uploadFile',
												camping_list_uid:this.config.record_data.camping_list_uid
												
											},
									scope:this,
				                    success: function(fp, o){
				                    	
										this.campingImage.load();
										
								        this.uploadWindow.close();
										
				                    },
				                    failure:function(fp,o){
				                    	Ext.Msg.show({
								            title: '실패', 
								            msg:o.result.realname,
								            minWidth: 200,
								            modal: true,
								            icon: Ext.Msg.INFO,
								            buttons: Ext.Msg.OK
								        });
				                    }
				                    
				                });
			                }
			            }
			        }]
			    });
			//this.uploadWindow.add(fp);
			
			this.uploadWindow = new Ext.Window({
				title:'이미지 업로드',
				modal:true,
				width:520,
				model:true,
				items:fp
				
			});
			
			this.uploadWindow.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();
				
			}
		
		
	},
	regCamping : function(){
		
			if(IS_LOGIN){
		
				this.addCampingContentWindow = new Ext.hansub.camping.AddCampingContentWindow({
							id:'addCampingContentWindowID',
							title: '캠핑장 기본정보 수정',
							iconCls:'add',
					        width:800,
					        resizable:true,
					        height: 570,
							data:{
								parentID : this.config.id,
								camping_list_uid : this.config.record_data.camping_list_uid	
														
							}
						        				        
				});
			
				
			
					
				this.addCampingContentWindow.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();
				
			}
		
		
	},
	
	closed:function(){
		
		/**		
		this.tabPanel.items.each(function(item){
                        
                        this.tabPanel.remove(item);
                        
                    },this);
        **/
	},
	
	initLayout : function(){

		this.mapPanel.init();
		//this.inlinePanelBlog.checkServer();
		//Ext.getCmp('InlineBlog').setData(this.config.name,this.config.record_data.uid);
		this.campingUser.showPanel();
		
	},
	
	
	
	reservation : function(){
		
		
		if(this.reservationType == 'tel'){
			
			Ext.MessageBox.alert(this.config.name+' 예약', '<br><p>현재 예약시스템을 제공하지 않습니다.</p><br><p><b>'+this.config.name+'</b>예약은  전화  또는 홈페이지를 이용해 주십시오.</p><br><p>#캠핑지도  예약시스템은 무료로 제공되며, 예약 시스템 신청 버튼을 통해 등록이 가능합니다.</p>');
			
		}else if(this.reservationType == 'url'){
		
			Ext.MessageBox.alert(this.config.name+' 예약', '<br><p>'+this.config.name+'은 자체  예약시스템을 제공하며, 예약 문의는 '+this.config.name+'으로 연락하십시오.</p><br><p>#캠핑지도  예약시스템은 무료로 제공되며, 예약 시스템 신청 버튼을 통해 등록이 가능합니다.</p>');
			
			var iframe = new Ext.ux.ManagedIframePanel({
				id:this.reservationValue,
				title:this.config.name+' 예약',
				cls:'scroll',
				width:100,
				height:100,
				closable:true,
				defaultSrc:this.reservationValue,
				loadMask : false,
	    		animCollapse:true
			});
			
			iframe.on('activate', function(){
				Ext.getCmp('layout').doLayout();
			},this);
			
			Ext.getCmp('centerCenterPanel').add(iframe);
			Ext.getCmp('centerCenterPanel').activate(iframe);
			
			
		}else if(this.reservationType == 'reservationsystem'){
			
			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.config.reservation  = Ext.decode(response.responseText);
					
					this.reservationWrapperPanel = new ReservationWrapperPanel(this.config);
					Ext.getCmp('centerCenterPanel').add(this.reservationWrapperPanel);
					Ext.getCmp('centerCenterPanel').activate(this.reservationWrapperPanel);
					
					
					
					
				},
				failure:function(response, options){
					Ext.MessageBox.alert('ERROR', '예약정보를 로딩에 오류가 발생하였습니다.');
				}
				            	
						
			});
			
			
			
		}
	}

	

});

