App.Telefonliste = function(){
    
    // begin private
    
    var _UserId;
    var _Kennung;
    
    function _testSelect(){
        App.setTrace('App.Telefonliste.testSelect');
        
        
        var select = Ext.getCmp('TelefonlisteGrid').getSelectionModel();
        
        if(select.getSelected()){
            
            var record = select.getSelected();
            var id = record.data.id;
        }
        else{
            App.information('Bitte Datensatz w&auml;hlen');
        }
        
        return id;
    }
    
    function _callNeu(){
        App.setTrace('App.Telefonliste._callNeu');
        
        App.Telefonliste.Neu.wakeup();
    }
    
    function _callLoeschen(){
        App.setTrace('App.Telefonliste._callLoeschen');
        
        var id = _testSelect();
        
        if(id){
            App.Telefonliste.Loeschen.wakeup(id);
        }
        
        
    }
    
    function _callView(){
        App.setTrace('App.Telefonliste._callView');
        
        var id = _testSelect();
        
        if(id){
            App.Telefonliste.View.wakeup(id);
        }
        
    }
    
    function _callEdit(){
        App.setTrace('App.Telefonliste._callEdit');
        
        var id = _testSelect();
        
        if(id){
            App.Telefonliste.Edit.wakeup(id);
        }
    }
    
    function _sendMail(){
        App.setTrace('App.Telefonliste._sendMail');
        
        var id = _testSelect();
        
        if(id){
           App.Telefonliste.Mail.wakeup(id);    
        }
    }
    
    function _sendSMS(){
        App.setTrace('App.Telefonliste._sendSMS');
        
       var id = _testSelect();
       
       if(id){
          App.Telefonliste.Sms.wakeup(id);  
       } 
    }
    
    function _viewInfo(){
        App.setTrace('App.Telefonliste._viewInfo');
        
        var id = _testSelect();
        
        if(id){
            App.checkNavigation('Protokoll',id);
        }
            
    }
    
    var _Store = new Ext.data.JsonStore({
        totalProperty: 'anzahl',
        root: 'data',
        url: 'frontend/controler/app.php',
        id: 'TelefonlisteStore',
        method: 'Post',
        // autoLoad: true,
        remoteSort: true,
        sortInfo: {
            field: 'name',
            direction: 'ASC'
        },
        baseParams: {
            modul: 'telefonliste',
            aktion: 'list',
            start: 0,
            limit: 10,
            suche: ''
        },
        fields: [{
            name: 'id',
            type: 'int'
        },{
            name: 'name'
        },{
            name:'vorname'
        },{
            name: 'firma'
        },{
            name: 'telefon1'
        },{
            name: 'telefon2'
        },{
            name: 'handy1'
        }]
    });
    
   var _Menu = [{
        text:"neu",
        handler: function(){
              _callNeu();
        },
        cls: 'x-btn-text-icon',
        icon: 'library/icons/application_form_add.png'
    },{
        text:"loeschen",
        handler: function(){
            _callLoeschen();
        },
        cls: 'x-btn-text-icon',
        icon: 'library/icons/delete.png'
    },{
        text:"anzeigen",
        cls: 'x-btn-text-icon',
        icon: 'library/icons/layout.png',
        handler: function(){
              _callView();
        } 
    },{
        text:"bearbeiten",
        cls: 'x-btn-text-icon',
        icon: 'library/icons/page_white_edit.png',
        handler: function(){
            _callEdit();
        }
    },{
         xtype: 'menuseparator'
    },{
         text:"Mail",
         cls: 'x-btn-text-icon',
         icon: 'library/icons/msn_newmsg.png',
         handler: function(){
             _sendMail();
         }
    },{
         text:"SMS",
         cls: 'x-btn-text-icon',
         icon: 'library/icons/transmit.png',
         handler: function(){
            _sendSMS();
         }
    },{
         xtype: 'menuseparator'
    },{
         text:"Protokoll",
         cls: 'x-btn-text-icon',
         icon: 'library/icons/protokoll.png',
         handler: function(){
            _viewInfo();
         }
    }];
    
    var _WindowYoutube = new Ext.Window({
        width: 495,
        height: 415,
        modal: true,
        draggable: false,
        resizable: false,
        closeAction: 'hide',
        id: 'Youtube1',
        items: [{                        
            html: '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/POoSxHUbc4E&hl=de_DE&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/POoSxHUbc4E&hl=de_DE&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>'
        }]
    }); // Ende Youtube
    
   var _Grid = new Ext.grid.GridPanel({
        stripeRows: true,
        id: 'TelefonlisteGrid',
        border: false,
        autoHeight: true,
        width: 890,
        
        store: _Store,
    
        sm: new Ext.grid.RowSelectionModel({
		    singleSelect: true
        }),
        
        autoExpandColumn: 'firma',
        
        pageSize: 2,
        
        "columns": [{
            "xtype": "gridcolumn",
            "header": "Id",
            "sortable": true,
            "resizable": true,
            "width": 50,
            "dataIndex": "id",
            "id": "id",
            hidden: true
        },{
            "xtype": "gridcolumn",
            "header": "Name",
            "sortable": true,
            "resizable": true,
            "width": 150,
            "dataIndex": "name",
            "id": "name"
        },{
            "xtype": "gridcolumn",
            "header": "Vorname",
            "sortable": true,
            "resizable": true,
            "width": 150,
            "dataIndex": "vorname",
            "id": "vorname"
        },{
            "xtype": "gridcolumn",
            "header": "Firma",
            "sortable": true,
            "resizable": true,
            "width": 150,
            "dataIndex": "firma",
            "id": "firma"
        },{
            "xtype": "gridcolumn",
            "header": "Telefon 1",
            "sortable": true,
            "resizable": true,
            "dataIndex": "telefon1",
            "width": 150,
            "id": "telefon1",
            renderer: App.GridCellPhonebutton
        },{
            "xtype": "gridcolumn",
            "header": "Telefon 2",
            "sortable": true,
            "resizable": true,
            "dataIndex": "telefon2",
            "width": 150,
            "id": "telefon2",
            renderer: App.GridCellPhonebutton
        },{
            "xtype": "gridcolumn",
            "header": "Handy",
            "sortable": true,
            "resizable": true,
            "dataIndex": "handy1",
            "width": 150,
            "id": "handy1",
            renderer: App.GridCellPhonebutton
        }],
        
        listeners: {
            rowcontextmenu: function(grid,showIndex,e){
                var contextMenu = new Ext.menu.Menu();
                
                contextMenu.add(_Menu);
                
                e.stopEvent();  
        		contextMenu.showAt(e.xy);
           }
        },

        bbar: [{
            xtype: 'paging',
            store: _Store,
            pageSize: 10,
            displayMsg: "Anzeige: {0} - {1} von {2} ",
            displayInfo: true
        }],
        
        tbar: [{
            xtype: 'tbspacer',
            width: 50
        },{
            xtype: 'tbseparator'
        },{
            xtype: 'tbsplit',
            text: 'Aktionen',
            icon: 'library/icons/cog.png',
            menu: _Menu
        },{
            xtype: 'tbseparator'
        },{
            xtype: 'tbfill'
        },{
            xtype: 'tbseparator'
        },{
            xtype: 'textfield',
            width: 100,
            name: 'suche',
            id: 'suche'
        },{
            xtype: 'tbbutton',
            text: 'suchen',
            cls: 'x-btn-text-icon',
            icon: 'library/icons/zoom.png',
            handler: function(){
                if(Ext.get('suche').getValue()){
                    
                    _Store.load({
                        params: {
                            suche: Ext.get('suche').getValue()
                        }
                    });
                }
                else{
                    _Store.load();
                }
            }
        },{
            xtype: 'tbseparator'
        },{
            xtype: 'tbbutton',
            text: 'Information',
            cls: 'x-btn-text-icon',
            icon: 'library/icons/information.png',
            handler: function(){
                Ext.getCmp('Youtube1').show();
            } 
        },{
            xtype: 'tbseparator' 
        },{
            xtype: 'tbspacer',
            width: 50
        }]
        
    }); // Ende Tabelle
    
    var _Window = new Ext.Window({
        title: 'Telefonliste',
        width: 895,
        x: 120,
        y: 85,
        iconCls: 'icon-telefonliste',
        id: 'Telefonliste',
        draggable: true,
        resizable: false,
        autoHeight: true,
        closeAction: 'hide',
        items: _Grid,
        shadow: false
    });
    
    // end private
    
    
    
    // begin public
    return{
        
        start: function(){
            App.setTrace('App.Telefonliste.start');
            
            App.setNavigation('Telefonliste');
            this.toolbar();

        },
        
        wakeup: function(){
            
            App.setTrace('App.Telefonliste.wakeup');
            
            Ext.StoreMgr.get('TelefonlisteStore').load();
            
            _Window.show();
            
            return;
        },
        
        viewProtokoll: function(id){
            App.setTrace('App.Telefonliste.protokollfenster');
            
            _UserId = id;
            
            Ext.Ajax.request({
                url: 'frontend/controler/app.php',
                method: 'post',
                params: {
                    modul: 'telefonliste',
                    aktion: 'kennungnotiz',
                    id: _UserId
                },
                success: function(result,request){
                    
                    var wert = Ext.util.JSON.decode(result.responseText);
                    
                    Ext.getCmp('TelefonlisteNotizKennung').setValue(wert.data.kennung);
                    
                    return;
                }
            });
            
            if(!this.WindowProtokollContent){
                this.WindowProtokollContent = true;
                this.buildProtokoll.add(this.buildForm);
            }
            
            this.buildProtokoll.show();
            
            return;
        },
        
        buildForm: {
            "xtype": "form",
            "labelWidth": 100,
            "labelAlign": "left",
            "layout": "form",
            "width": 650,
            "autoHeight": true,
            "padding": 10,
            "buttonAlign": "center",
            id: 'TelefonlisteNotizForm',
            "items": [
                {
                    "xtype": "displayfield",
                    "name": "kennung",
                    "width": 400,
                    cls: 'viewDisplayfield',
                    id: 'TelefonlisteNotizKennung'
                },
                {
                    "xtype": "textfield",
                    "fieldLabel": "Betreff",
                    "name": "betreff",
                    allowBlank: false,
                    helpText: 'Inhalt Betreffzeile',
                    "width": 400
                },
                {
                    "xtype": "textarea",
                    "fieldLabel": "Information",
                    "width": 400,
                    name: 'information',
                    allowBlank: false,
                    helpText: 'Information eingeben',
                    "height": 200
                }
            ],
            fbar: {
                "xtype": "toolbar",
                "items": [{
                        "xtype": "tbseparator"
                    },{
                        "xtype": "button",
                        "text": "eintragen",
                        cls: 'x-btn-text-icon',
                        icon: 'library/icons/button_ok.png',
                        handler: function(){
                            App.Telefonliste.sendNotiz();
                        }
                    },{
                        "xtype": "tbseparator"
                    }]
            }
        },
        
        sendNotiz: function(){
            App.setTrace('App.Telefonliste.sendNotiz');
            
            var FormPanel = Ext.getCmp('TelefonlisteNotizForm');            
            
            FormPanel.getForm().submit({
                url: 'frontend/controler/app.php',
                method: 'Post',
                params: {
                    modul: 'telefonliste',
                    aktion: 'notiz',
                    user: _UserId
                },
                success: function(form,action){
                    FormPanel.getForm().reset();
                    App.information('Notiz wurde eingetragen');
                    Ext.getCmp('TelefonlisteProtokollform').hide();
                    
                }
            });
        },
        
        WindowProtokollContent: false,
        
        buildProtokoll: new Ext.Window({
            title: 'Gespr&auml;chsnotiz Telefonanruf',
            width: 650,
            autoHeight: true,
            x: 158,
            y: 85,
            draggable: true,
            resizable: false,
            id: 'TelefonlisteProtokollform',
            modal: true,
            closeAction: 'hide',
            shadow: false
        }),
        
        reloadData: function(){
            App.setTrace('App.Telefonliste.reloadData');
            
            Ext.StoreMgr.get('TelefonlisteStore').load();
            
            return;
        },
        
        toolbar: function(){
            App.setTrace('App.Telefonliste.toolbar');
            
            var toolbar = App.getToolbar();
            toolbar.add({
                xtype: 'tbbutton',
                text: 'Telefonliste',
                cls: 'x-btn-text-icon',
                icon: 'library/icons/file_doc.png',
                handler: function(){
                    App.checkNavigation('Telefonliste',0);
                }
            });
            
            toolbar.add({
                xtype: 'tbseparator'
            });
            
            App.doLayoutToolbar();
            
            return;
        }
        
    } // end public
    
}();

Ext.onReady(App.Telefonliste.start,App.Telefonliste);
