﻿
function formatDate(value){
    return value ? value.dateFormat('m/d/Y') : '';
};

function formatIP(value){
    return("<a href='http://www.dnsstuff.com/tools/whois.ch?ip=" + value + "' target='_blank'>" + value + "</a>")
}
    


// ChatLog Layout Dialog
var ChatLog = function(){

    // shorthand alias
    var fm = Ext.form, Ed = Ext.grid.GridEditor;    
    
    // define some private variables
    var dialog,showBtn;   
    var msgCm, Message;
    var grid;
    var msgDs;
    var txtFromDate, txtToDate;
    

    
    // return a public interface
    return {
        init : function(){


            // DataStore configuration for: Messages
            msgCm = new Ext.grid.ColumnModel([
                {
                   header: localize('timestamp') //"Timestamp"
                   ,dataIndex: 'msg_ts'
                   ,width: 100
                   ,renderer: formatDate
                }
                ,{
                   header: localize('ip') //"IP"
                   ,dataIndex: 'ip_addr'
                   ,width: 125
                   ,align:'left'
                   ,renderer:formatIP
                }
                ,{
                   header: localize('from') //"From"
                   ,dataIndex: 'nick'
                   ,width: 125
                   ,align:'left'
                   ,renderer:escapeHTML
                }
                ,{
                   header: localize('chat_text') //"Text"
                   ,dataIndex: 'msg_text'
                   ,width: 425
                   ,align:'left'
                   ,renderer:localize_svr_msg

                }

            ]);
            // by default columns are sortable
            msgCm.defaultSortable = true;
            
            Message = Ext.data.Record.create([
                {name:'msg_id',type:'int'},
                {name:'msg_ts',type:'date'},
                {name:'ip_addr',type:'string'},
                {name:'msg_from_chat_userid',type:'int'},
                {name:'nick',type:'string'},
                {name:'msg_to_chat_userid',type:'int'},
                {name:'msg_color',type:'string'},
                {name:'msg_text',type:'string'},
                {name:'msg_type',type:'int'}
            ]);
            msgDs = new Ext.data.Store({
                proxy: new Ext.data.AjaxProxy(SWIRL.DNN.Modules.SWIRLChat.AJAX, "GetMessageLog"),
                reader: new Ext.data.JsonReader({id: 'msg_id'},Message)
            });
            

            
                      
        },
        
        showDialog : function(){
        
        if(!dialog){ // lazy initialize the dialog and only create it once
            
                // create the editor grid
                var grid = new Ext.grid.Grid('chatlog_grid', {
                    ds: msgDs,
                    cm: msgCm,
                    //autoSizeColumns:true,
                    mode: 'local',
                    enableColLock:false

                });
           
                
                dialog = new Ext.LayoutDialog("chatlog_container", { 
                        modal:false,
                        width:675,
                        height:400,
                        shadow:true,
                        minWidth:300,
                        minHeight:300,
                        proxyDrag: false,
	                    center: {
	                        autoScroll:true,
	                        tabPosition: 'top',
	                        closeOnTab: false,
	                        alwaysShowTabs: false
	                    }
                });
                dialog.addKeyListener(27, dialog.hide, dialog);

                dialog.addButton(localize('rm_close_button'), dialog.hide, dialog);
                
                
                var layout = dialog.getLayout();
                layout.beginUpdate();
	            layout.add('center', new Ext.GridPanel(grid, {title: 'Log', closable:false}));
                
	            layout.endUpdate();
	            //grid.autoSize();
	            grid.render();
	            
	            txtFromDate=new Ext.form.TextField({
                    selectOnFocus:true,
                    width:110
                })
                txtToDate=new Ext.form.TextField({
                    selectOnFocus:true,
                    width:110
                })
	            var gridHead = grid.getView().getHeaderPanel(true);
                var tb = new Ext.Toolbar(gridHead, []);
                tb.add(localize('from')+':');
                tb.addField(txtFromDate);
                tb.add({
                    icon:_modulePath+'images/icons/calendar.png',
                    cls: 'x-btn-icon',
                    menu: new Ext.menu.DateMenu({
                        handler : function(dp, date){
                            txtFromDate.setValue(formatDate(date));
                        }})
                });
                tb.addSeparator();
                tb.add(localize('to')+':');
                tb.addField(txtToDate);
                tb.add({
                    icon:_modulePath+'images/icons/calendar.png',
                    cls: 'x-btn-icon',
                    menu: new Ext.menu.DateMenu({
                        handler : function(dp, date){
                            txtToDate.setValue(formatDate(date));
                        }})
                }); 
                tb.addSeparator();
                tb.add({
                    cls: 'x-btn-text'
                    ,id: 'btnShowLog'
                    ,text: localize('display')
                    ,handler: ChatLog.renderLog
                });
                                
                if(checkPerm('PERM_VIEW_LOG',false)){
                    tb.addSeparator();
                    tb.add({
                        cls: 'x-btn-text'
                        ,id: 'btnClearLog'
                        ,text: localize('clear_log') //'Clear Log'
                        ,handler: ChatLog.clearLog
                    });                 
                }      
	            
            }   

            var now=new Date();
                       		    
            dialog.show(Ext.get('chatlog_btn').dom);
            var dtFrom = new Date(formatDate(now));
            var dtTo =   new Date(formatDate(now));
            txtFromDate.setValue(formatDate(now));
            txtToDate.setValue(formatDate(now));

            msgDs.load({params: {p1:room_id,p2:chat_userid,p3:dtFrom,p4:dtTo,p5:security_token}});
            
        },
        renderLog : function(){
            var dtFrom = new Date(txtFromDate.getValue());
            var dtTo =   new Date(txtToDate.getValue());
            msgDs.load({params: {p1:room_id,p2:chat_userid,p3:dtFrom,p4:dtTo,p5:security_token}});
        },
        clearLog : function(){
            Ext.Msg.buttonText.yes=localize("yes");
            Ext.Msg.buttonText.no=localize("no");
            Ext.Msg.show({
                title: localize('clear_log'),
                msg: localize('clear_log_confirm'),
                modal: false,
                prompt: false,
                closable: true,
                buttons: Ext.Msg.YESNO,
                fn: function(btn){
                    if(btn=="yes"){
                        SWIRL.DNN.Modules.SWIRLChat.AJAX.ClearMessageLog(room_id,chat_userid,security_token,function(res){ChatLog.renderLog();});
                    }
                }
            });
        }
    };
}();




// using onDocumentReady instead of window.onload initializes the application
// when the DOM is ready, without waiting for images and other resources to load
//Ext.EventManager.onDocumentReady(ChatLog.init, ChatLog, true);
Ext.onReady(
//    function(){
//    if(typeof SWIRL!='undefined'){
        ChatLog.init,ChatLog,true
//    }
//    }
);






