Ext.onReady(function(){
  var selectedRow;

  Ext.BLANK_IMAGE_URL = '/system/Components/ext-2/resources/images/default/s.gif';

  Ext.QuickTips.init();
  Ext.form.Field.prototype.msgTarget = 'under';
  Ext.form.Field.prototype.height = 20;

  var ds = new Ext.data.JsonStore({
		totalProperty:'totalRows'
		,root:'rows'
		,url:'/'+hive['mnu015l']+'/mnu015l.asp'
		,remoteSort: true
	});

  var grid = new Ext.grid.GridPanel({
    renderTo:Ext.getBody()
    ,autoScroll : true
    ,store: ds
    ,columns:
      [
        {header: menu.I18N['application'], width:400, sortable:true, dataIndex: 'WSVAPP'}
      ]
    ,sm: new Ext.grid.RowSelectionModel({singleSelect:true})
    ,loadMask:false
		,tbar:
			[
        {
          id:'addNew'
          ,text: menu.I18N['addNew']
          ,tooltip: menu.I18N['addNew']
          ,cls:'x-btn-text-icon mnu-tb-add'
          ,handler: onButtonClick
        }
			]
    ,autoHeight:true
  });


  // Row system
  var rowMenu = new Ext.menu.Menu({
    id: 'rowMenu',
    items: [
      {id: 'edit', text: menu.I18N['edit'], cls:'mnu-icon-edit smd-default', handler:onRowRightClick}
    ]
  });

  grid.on('rowcontextmenu', onRowMenu);
  grid.on('rowclick', function(grid, rowIndex, e) {
    selectedRow = ds.data.items[rowIndex].id;
  });
  grid.on('rowdblclick', function(grid, rowIndex, e) {
    selectedRow = ds.data.items[rowIndex].id;
    processRow(selectedRow, 'update');
  });

  ds.load({
    params: {}
  });

  // handler for grid toolbar
  function onButtonClick(btn){
    switch (btn.id) {
    case 'addNew':
      processRow(0, 'create');
      break;
    }
  }

  function onRowMenu(grid, rowIndex, e) {
    e.stopEvent();
    var coords = e.getXY();
    rowMenu.showAt([coords[0], coords[1]]);
    selectedRow = ds.data.items[rowIndex].id;
  }

  function onRowRightClick(item) {
    switch (item.id) {
    case 'edit':
      processRow(selectedRow, 'update');
      break;
    }
  }

  var windowCount = 0;
  function processRow(wsID, func) {
    // Loading store with detail data
    var action = (func == 'create') ? 'init' : 'get';
    var row_ds = new Ext.data.JsonStore({
      url:'/'+hive['mnu015s']+'/mnu015s.asp?action='+action+'&WSVID='+wsID
    });			
    row_ds.on('load', function() {
      var data = row_ds.getAt(0).data;
      // Building window & form
      var frm = new Ext.form.FormPanel({
        labelAlign: 'left'
        ,labelWidth:100
        ,border:false
        ,bodyStyle:'padding:5px 5px 0'
        ,defaults:{width:300}
        ,layout:'form'
        ,items:[
          {xtype:'textfield',fieldLabel:menu.I18N['application'],name:'WSVAPP',value:data['WSVAPP'],allowBlank:false}
        ]
      });

      var windowID = 'mnu015-window-' + windowCount++;
      var title = (func == 'create') ? menu.I18N['addNew'] : menu.I18N['update'];
      if (!win) {
        var win = new Ice.Window({
          id: windowID
          ,title: title
          ,width: 450
          ,height: 130
          ,minWidth: 50
          ,minHeight: 50
          ,autoScroll : true
          ,bodyStyle:'padding:5px;'
          ,buttonAlign:'left'
          ,layout: 'fit'
          ,plain:true
          ,modal: false
          ,items: frm
          ,buttons: [{
            text: menu.I18N['save']
            ,type: 'submit'
            ,handler: function() {	
              if (frm.form.isValid()) {
                frm.form.submit({
                  url:'/'+hive['mnu015s']+'/mnu015s.asp?WSVID='+wsID+'&action='+func
                  ,success: function() {
                    win.hide();
                    ds.load();
                  }
                });
              }
            }
            ,key:'ENTER'
          },{
            text: menu.I18N['delete'],
            handler: function(){
              Ext.MessageBox.confirm(menu.I18N['message'], menu.I18N['confirmDelete'] , doDelete);
            }
          },{
            text: menu.I18N['cancel']
            ,handler: function() {
              win.hide();
            }
          }]
        })
      }
      win.show();

      if (func == 'create') {
        win.buttons[1].disable();
      }

      frm.form.findField('WSVAPP').focus();

      function doDelete(btn) {
        if (btn == 'yes') {
          frm.form.submit({
            url:'/'+hive['mnu015s']+'/mnu015s.asp?WSVID='+wsID+'&action=delete'
            ,success: function() {
              win.hide();
              ds.load();
            }
            ,waitMsg:menu.I18N['deletingData']
          });
        }
      }
    })
    row_ds.load()
  }
});