Ext.namespace("Ext.hansub.naverAPI");

Ext.hansub.naverAPI.NaverIMGOpenAPIPanel = function(config) { 
	
	this.config = config;
	
	
	this.store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url : './src/api/OpenAPI.php',
	       	method : 'POST'
        }),  
        baseParams:{
				'class':'OpenAPI',
				'method':'getNaverXML',
				target:this.config.data.target,
				keyword:this.config.data.keyword,
				start:1,
				display:30
		},

        reader: new Ext.data.XmlReader(
            {
            	record: 'item',
            	totalProperty: 'total'
            },
            
            [
            	{name: 'title', mapping:'title', type: 'string'},
            	{name: 'link', mapping:'link', type: 'string'},
            	{name: 'thumbnail', mapping:'thumbnail', type: 'string'}
            	]
//
        )
    });
    
    this.store.load();
  
    var tpl = new Ext.XTemplate(
		'<tpl for=".">',
            '<br><div class="thumb-wrap2" id="{title}">',
		    '<div class="thumb2" align=center><img src="{link}" title="{title}"></div>',
		    '</div>',
        '</tpl>',
        '<div class="x-clear"></div>'
	);
   
  
	Ext.hansub.naverAPI.NaverIMGOpenAPIPanel.superclass.constructor.call(this, {
		
		id:this.config.id,
		border:false,
		iconCls:this.config.iconCls,
       	region:this.config.region,
		//title:this.config.data.keyword+ " 이미지 검색결과",
		split: true,

        items: new Ext.DataView({
            store: this.store,
            loadMask: {msg:'화면 구성 중'},
          	scope:this,
            tpl: tpl,
            autoHeight:true,
            multiSelect: true,
            overClass:'x-view-over',
            itemSelector:'div.thumb-wrap',
            //emptyText: '<center>이미지 정보가 없습니다.</center>',

            prepareData: function(data){
            	
                data.s_title = Ext.util.Format.ellipsis(data.title, 8);
                return data;
            },
            //click : ( Ext.DataView this, Number index, HTMLElement node, Ext.EventObject e )
            listeners: {
            	scope:this,
            	click: {
            		fn: function(dv,index){
            			
            			records = this.store.getAt(index);
            			
            			title= Ext.util.Format.ellipsis(records.data.title, 15);
            			if(Ext.getCmp('imgFrameID')){
            				Ext.getCmp('imgFrameID').setSrc(records.data.link);
							Ext.getCmp('centerRightTabPanelID').activate(Ext.getCmp(records.data.link));
							Ext.getCmp('imgFrameID').setTitle(title);
						}else{
							
							var iframe = new Ext.ux.ManagedIframePanel({
									id:'imgFrameID',
									iconCls:'img_icon',
									title:title,
									loadMask:true,
									cls:'scroll',
									closable:true,
									defaultSrc:records.data.link,
									loadMask : false,
						    		animCollapse:true
							});
							
							Ext.getCmp('centerCenterPanel').add(iframe);
							Ext.getCmp('centerCenterPanel').activate(iframe);
							
						}
						Ext.getCmp('mainToolbarID').layout('main_right2');
            			
            			/**
            			var l = nodes.length;
            			var s = l != 1 ? 's' : '';
            			panel.setTitle('Simple DataView ('+l+' item'+s+' selected)');
            			**/
            		}
            	}
            }
        })

        
							        
									
	});
	
	
	this.on('activate', function(){Ext.getCmp('mainToolbarID').layout('main_right2');},this);
	//this.view.target = this.config.target;
	/**
	this.boardGridSM = this.getSelectionModel();

    this.boardGridSM.on('rowselect', function(sm, index, record){
    	
    	this.selectedRecord = record;
    	if(index == 1){
    		this.linkLoad();
    	}
    	
    	
    }, this)
    **/

    
    
    
 
};

Ext.extend(Ext.hansub.naverAPI.NaverIMGOpenAPIPanel, Ext.Panel, {
	
	onTriggerClear:function() {
        this.searchTextField.setValue('');
        this.field.focus();
        
    },
    
    search:function(){
		
		this.keyword = this.searchTextField.getValue();
		
		this.store.load(
				{
					params:{
						keyword:this.keyword
					}
				}
			);

	}
	
	/**
	
	setKeyword:function(keyword){
		
		this.isloaded = false;
		this.config.data.keyword = keyword.replace(" ", "+");
		this.config.data.keyword = "결혼+"+this.config.data.keyword
		
	},
	
	setViewType:function(viewtype){
		
		this.config.data.viewtype = viewtype
		this.view.showView = this.config.data.viewtype;
		this.view.refresh();

	},
	
	loadAPI : function(){
		if(!this.isloaded){
			this.store.load(
				{
					params:{
						keyword:this.config.data.keyword
					}
				}
			);
			this.isloaded = true;
		}
		
		
		
	},
	
	onDbClick:function(grid,index,e){
		this.addTab(null,this.store.getAt(index),null,null);
	},
	
	addTab:function(grid, records, action, groupId){
		
		if(records && records.data.link ){
			
			records.data.link = Ext.util.Format.htmlDecode( records.data.link) 
			
			//title = Ext.util.Format.ellipsis(this.selectedRecord.data.title, 20);
			title = records.data.title;
			
			
			if(Ext.getCmp(records.data.link)){
				Ext.getCmp('centerTopPanelID').activate(Ext.getCmp(records.data.link));
			}else{
				title= Ext.util.Format.ellipsis(title, 15);
				var iframe = new Ext.ux.ManagedIframePanel({
						id:records.data.link,
						title:title,
						cls:'scroll',
						closable:true,
						defaultSrc:records.data.link,
						loadMask : false,
			    		animCollapse:true
				});
				
				iframe.on('activate', function(){Ext.getCmp('centerRightPanelID').collapse(false);},this);
				
				Ext.getCmp('centerTopPanelID').add(iframe);
				Ext.getCmp('centerTopPanelID').activate(iframe);
				
				
				
			}
			
			
		}
	},
	/**
	linkLoad:function(selectedRecord){
		
		if(selectedRecord && selectedRecord.data.link ){
			
			selectedRecord.data.link = Ext.util.Format.htmlDecode( selectedRecord.data.link) 
			
			//title = Ext.util.Format.ellipsis(this.selectedRecord.data.title, 20);
			title = selectedRecord.data.title;
			
			if(Ext.getCmp(selectedRecord.data.title)){
				Ext.getCmp('centerTopPanelID').activate(Ext.getCmp(selectedRecord.data.title));
			}else{
				
				var iframe = new Ext.ux.ManagedIframePanel({
						id:selectedRecord.data.title,
						title:title,
						cls:'scroll',
						closable:true,
						defaultSrc:selectedRecord.data.link,
						loadMask : false,
			    		animCollapse:true
				});
				
				Ext.getCmp('centerTopPanelID').add(iframe);
				Ext.getCmp('centerTopPanelID').activate(iframe);
				
			}
			
			
		}
		
	},
	
	applyRowClass: function(record, rowIndex, p, ds) {
		
        if(this.showView == 'title'){			
        	return 'x-grid3-row-collapsed';     	
        }else if(this.showView == 'summary'){
        	content = Ext.util.Format.stripTags(record.data.description);
        	if(content == ""){
        		return 'x-grid3-row-collapsed';
        	}
        	p.body = '<div class=boardsummary>' + content + '</div>';
            return 'x-grid3-row-expanded';
        }else if(this.showView == 'full'){
			p.body = '<div class=boardcontent>' + record.data.description + '</div>';
            return 'x-grid3-row-expanded';
        }else{
        	return 'x-grid3-row-collapsed';
        }
        
    },
    
    formatDate : function(date) {
        if (!date) {
            return '';
        }
        var now = new Date();
        var d = now.clearTime(true);
        var notime = date.clearTime(true).getTime();
        if (notime == d.getTime()) {
            return '오늘 ' + date.dateFormat('g:i a');
        }
        d = d.add('d', -6);
        if (d.getTime() <= notime) {
            return date.dateFormat('D g:i a');
        }
        return date.dateFormat('n/j g:i a');
    },
    

    
    titleFormat: function(value, p, record,rowIndex, colIndex, store) {
    	
    	return String.format(
                '<div class="'+store.baseParams.target+'">{0}<span class="author">{1}</span></div>',
                value, record.data.author, record.id, record.data.forumid
                );
    },
    
    imageFormat: function(value, p, record) {
    	return String.format('<img src={0} height=100>',value);
    },
    
    korMoney :  function(v, params, record){
		
		var usMoney = Ext.util.Format.usMoney(v);
		if(usMoney == '$NaN.00'){
			hanMoney = '0';
			record.data.expected = 0;
		}else{
			hanMoney = usMoney.substr(1,usMoney.length-4);
		}
		return hanMoney + ' 원';
                 
	},
    
    onContextClick : function(grid,index,e){
    	
    	this.getSelectionModel().selectRow(index);
    	this.selectedRecord = this.store.getAt(index);
		
        this.addMenu.showAt(e.getXY());
      
    },
    onTriggerClear:function() {
        this.searchTextField.setValue('');
        this.field.focus();
        
    },
    
    search:function(){
		
		keyword = this.searchTextField.getValue();

		if(keyword){
			
			this.setKeyword(keyword);
			this.loadAPI();
						
		}
	},
	
	setListLayout:function(viewtype){
    	   	
  
		if(viewtype == 'title'){
			
    		this.layoutItemButton.setText('간략하게 보기');
    		this.layoutItemButton.setIconClass('view_summary');
    		this.titleLayoutButton.disable();
    		this.summaryLayoutButton.enable();
				
    	}else if(viewtype == 'summary'){
    		
    		this.layoutItemButton.setText('제목만 보기');
    		this.layoutItemButton.setIconClass('view_title');
    		this.titleLayoutButton.enable();
    		this.summaryLayoutButton.disable();
			
    	}else{
    		
    		if(this.viewtype == 'title'){
    			this.layoutItemButton.setText('제목만 보기');
    			this.layoutItemButton.setIconClass('view_title');
    			this.titleLayoutButton.enable();
    			this.summaryLayoutButton.disable();
    			viewtype = "summary";
    		}else{
    			this.layoutItemButton.setText('간략하게 보기');
    			this.layoutItemButton.setIconClass('view_summary');
    			this.titleLayoutButton.disable();
    			this.summaryLayoutButton.enable();
    			viewtype = "title";
    		}
 		
    	}
		
    	this.viewtype = viewtype;
    	
		this.setViewType(viewtype);

		

    }
	**/

});

