var comments = {};

$(document).ready(function() { 
   
          
    comments.expanderRenderer = function(text,row)
    {	
    	var html="";
    	text = nl2br(text);
    	html += "<b>"+lang("User")+"</b>: "+ row.commentUser+"<br/>";
    	html += "<b>"+lang("E-mail")+"</b>: "+ row.commentEmail+"<br/>";
    	html += "<b>"+lang("Comment")+"</b>: " + text;

    	return html;
    }
          
          
    // ** grid definition   
    comments.grid = new RichHTML.grid({
        el: 'KBcomments-grid',
        pagingEl: 'KBcomments-grid-pagination',
        metaEl: 'KBcomments-grid-meta',
        root: "data",
        totalProperty: "totalcount",
        url: 'index.php?fuse=knowledgebase&action=GetKBCommentsList',
		baseParams: { filterComents:'NonApproved',sort: 'commentAddedOn', dir: 'desc', limit: clientexec.records_per_view},
        columns: [{
        		dataIndex:	"comment",
        		xtype:	    "expander",
        		renderer:	comments.expanderRenderer
	        },{
                id:         "cb",
                dataIndex:  "articleId",                
                xtype:      "checkbox"
            },{
            	id: 		"articleId",
            	dataIndex:	"articleId",
            	text:		lang("Art"),
            	align:		"center",
            	sortable:	true,
            	width:		50,
            	renderer:   function(text) {
            		var str = text.split('/');
            		return str[0];
            	}           	
            },{
            	id: 		"articleTitle",
            	dataIndex:	"articleTitle",
            	text:		lang("Title"),
            	align:		"left"
            },{
            	id:			"commentAddedOn",
            	dataIndex:	"commentAddedOn",
            	text:		lang("Added"),
            	align:		"center",
            	sortable:	true,
            	width:		110
            },{
                id:         'commentIsApproved',
                text:     lang("Approved"),
                width:      80,
                dataIndex:  "commentIsApproved",
                align: 'center'
            }
        ], 	
    });
    comments.grid.render();
    // **** start click binding


    $('#KBcomments-grid-filter').change(function(){
    	comments.grid.reload({params:{start:0,limit:$(this).val()}});
    });

    $('#KBcomments-grid-filterby').change(function(){
    	if ($(this).val()!=="articleId") {
    		comments.grid.reload({params:{start:0,filterComents:$(this).val()}});
			$('#kb-comment-label').text(lang("Viewing")+" "+$(this).find('option:selected').text()+" "+lang("Comments"));				    		
    		$('#KBcomments-grid-filterby-artid-td').hide();    		
    	} else {
    		$('#KBcomments-grid-filterby-artid-td').show();
    	}
    });	

    // **** listeners to grid
    $(comments.grid).bind({    
	    "load" : function(event,data) {
        	$('#delComments').attr('disabled','disabled');	      
        	$('#appComments').attr('disabled','disabled');	              	
	    },
	    "rowselect": function(event,data) {
	        if (data.totalSelected > 0) {
	        	$('#delComments').removeAttr('disabled');
	        	$('#appComments').removeAttr('disabled');	        	
	        } else {
	        	$('#delComments').attr('disabled','disabled');
	        	$('#appComments').attr('disabled','disabled');
	        }
	    }
    });

	$('#KBcomments-grid-filterby-artid-td').keydown(function(e){
		if (e.keyCode === 13) {
			if (trim($(this).find('input').val()) === ""){
				comments.grid.reload({params:{start:0,filterComents:"all"}});
				$('#kb-comment-label').text(lang("Viewing All Comments"));				
			} else {
    			comments.grid.reload({params:{start:0,filterComents:"articleId",articleId:$(this).find('input').val()}});
				$('#kb-comment-label').text(lang("Viewing Comments for Article "+$(this).find('input').val()));
    		}
		}
	});

    // **** lets bind our buttons
    $('#delComments').click(function () { 
    	if ($(this).attr('disabled')) { return false; }
    	RichHTML.msgBox(lang('Are you sure you want to delete the selected comment(s)'),
    		{
    			type:"prompt",
    			buttons: {  button1:{text:"yes"}, button2:{text:"no"}, button3:{text:"cancel",type:"cancel"} }
    		}, function(result) {
				if(result.btn === "yes") {
					$.post("index.php?action=KB_DeleteComments&fuse=knowledgebase", {articleId:comments.grid.getSelectedRowIds()},
					   function(data){
	   						comments.grid.reload({params:{start:0}});
					});
				}
			});
    });
    
	$('#appComments').click(function () {
		if ($(this).attr('disabled')) { return false; }
		$.post("index.php?action=KB_ApproveComments&fuse=knowledgebase", {articleId:comments.grid.getSelectedRowIds()},
					   function(data){
	   						comments.grid.reload();
					});
	});


});