// Copyright (c) 2009 Brian Edwards

Ext.ns('App');

App.main = function () {

var type_combo_config = {
  xtype: 'combo',
  id: 'type_combo',
  fieldLabel: 'Type',
  displayField: 'type',
  typeAhead: true,
  mode: 'local',
  triggerAction: 'all',
  emptyText: 'Select a type...',
  forceSelection: true,
  store: new Ext.data.ArrayStore({
      fields: ['type'],
      data : [['Organizer'], ['Class']]
  })
};

var id_textfield_config = {
  xtype: 'textfield',
  id: 'id_textfield',
  fieldLabel: 'ID',
  allowBlank: false
};

var form_window_config = {
  xtype: 'window',
  id: 'form_window',
  title: 'Add a Node',
  layout: 'form',
  padding: 10,
  items: [type_combo_config, id_textfield_config],
  x: 10,
  y: 0,
  shadow: false,
  buttons: [
    {
      text: 'Submit',
      handler: function() {
        var type = Ext.getCmp('type_combo').getValue();
        var id = Ext.getCmp('id_textfield').getValue();
        Ext.Msg.alert('Submit Clicked', 'Type is "' + type + '". ID is "' + id + '".');
      }
    }
  ]
};

var user_formpanel_config = {
  xtype: 'form',
  id: 'user_formpanel',
  border: false,
  buttonAlign: 'left',
  disabled: true,
  padding: 8,
  api: {
    load: App.api.AppUserRouter.load,
    submit: App.api.AppUserRouter.submit
  },
  items: [
    {
      xtype: 'textfield',
      id: 'zip_code',
      fieldLabel: '5-digit ZIP Code',
      msgTarget: 'side',
      maskRe: /[0-9]/,
      minLength: 5,
      maxLength: 5
    }
  ],
  buttons: [
    {
      text: 'Submit',
      handler: function() {
        Ext.getCmp('user_formpanel').getForm().submit({
          success: function(form, action) {
            Ext.Msg.alert('Success', action.result.msg);
          },
          failure: function(form, action) {
            switch (action.failureType) {
              case Ext.form.Action.CLIENT_INVALID:
                Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                break;
              case Ext.form.Action.CONNECT_FAILURE:
                Ext.Msg.alert('Failure', 'Ajax communication failed');
                break;
              case Ext.form.Action.SERVER_INVALID:
                Ext.Msg.alert('Failure', action.result.msg);
             }
          }
        });
      }
    }
  ]
};

var nav_panels = [
  {
    xtype: 'app_navigationpanel',
    title: 'Public',
    button_configs: [
      {
        id: 'main_page_button',
        text: 'Main Page',
        pressedHandler: function(panel) {
          panel.add(form_window_config);
          Ext.getCmp('form_window').show();
        }
      }, {
        text: 'Page 2',
        pressedHandler: function(panel) {
          
        }
      }
    ]
  }, {
    xtype: 'app_navigationpanel',
    title: 'User',
    button_configs: [
      {
        text: 'Weather',
        pressedHandler: new App.FragmentLoader('/fragments/weather.html').load
      }
    ]
  }, {
    xtype: 'app_navigationpanel',
    title: 'Admin',
    button_configs: [
      {
        
        text: 'Edit User Info',
        
        pressedHandler: function(panel) {
          
          panel.add(user_formpanel_config);
          
          var user_formpanel = Ext.getCmp('user_formpanel');
          var user_form = user_formpanel.getForm();
          user_form.api.load({}, function(result) {
            if (result.success) {
              user_form.setValues(result.data);
              user_formpanel.enable();
            } else {
              Ext.Msg.alert('Failure', result.msg);
            }
          });
          
        }
      }
    ]
  }, {
    xtype: 'app_navigationpanel',
    title: 'About',
    button_configs: [
      {
        text: 'About This Site',
        pressedHandler: new App.FragmentLoader('/fragments/about.html').load
      }, {
        text: 'Contact Us',
        pressedHandler: function(panel) {
          
        }
      }
    ]
  }
];

new Ext.Viewport({
  layout: 'border',
  defaults: {
    border: false
  },
  items: [
    {
      region: 'north',
      id: 'northRegion',
      height: 40,
      padding: 4,
      defaults: {
        height: 21,
        border: false
      },
      items: [
        {
          width: 107,
          bodyCssClass: 'header_logo',
          bodyStyle: 'position: absolute; top: 50%; margin-top: -10;'
        }, {
          autoLoad: {url: '/fragments/login.html'},
          bodyStyle: 'position: absolute; top: 50%; margin-top: -10; right: 4;'
        }
      ]
    }, {
      region: 'west',
      id: 'westRegion',
      width: 160,
      layout: 'accordion',
      collapsible: true,
      title: 'Navigation',
      layoutConfig: {fill: false},
      items: nav_panels
    }, {
      region: 'center',
      id: 'centerRegion',
      autoScroll: true
    }, {
      region: 'south',
      id: 'southRegion',
      height: 40,
      padding: 4,
      items: {
        html: 'Copyright &copy; 2009 Brian Edwards',
        border: false,
        height: 21,
        bodyStyle: 'position: absolute; top: 50%; margin-top: -10;'
      }
    }
  ]
});

Ext.getCmp('main_page_button').toggle();
  
};
