

#import <Foundation/Foundation.h> // can remove?? *****

#import <UIKit/CDStructures.h>
#import <UIKit/UIKit.h>

#import "GreenstoneApplication.h"

#import "PrefTable.h"
#import "PrefView.h"



@implementation PrefView

- (id)initWithFrame:(struct CGRect)frame withParent:(UIView*)parent 
        withCollection:(NSString*)collection withTitle:(NSString*)title
{
  self = [super initWithFrame:frame];

  _parent = parent;
  _collection = collection;

  // Create the storage array.
  // _cellResultList = [[NSMutableArray alloc] init]; // ****

  int frame_x_dim = CGRectGetMaxX(frame)+1;
  int frame_y_dim = CGRectGetMaxY(frame)+1;
  
  int nav_x_org = 0.0f;
  int nav_y_org = 0.0f;
  int nav_x_dim = frame_x_dim;
  int nav_y_dim = TOP_NAVBAR_HEIGHT;
  CGRect navrect = CGRectMake(nav_x_org,nav_y_org,nav_x_dim,nav_y_dim);

  // Create the navigation bar.
  _navbar = [[UINavigationBar alloc] initWithFrame: navrect];
  //[_navbar showLeftButton:@"Back" withStyle:2 rightButton:nil withStyle:0]; // 2 = arrow left. // *** do enum for "2"
  [_navbar showButtonsWithLeftTitle:@"Back" rightTitle:nil leftBack:YES];
  [_navbar enableAnimation];
  [_navbar setBarStyle: UIBarStyleDefault]; 
  [_navbar setDelegate:self];

  [self setTitle:title];


  CGRect pref_rect = frame;
  pref_rect.origin.y += 45;
  pref_rect.size.height -= 45;

  _prefTable = [ [ PrefTable alloc ] initWithFrame: pref_rect ];
  [_prefTable reloadData];

  // [self setTitle:collection]; // ***** can't seem to be this to work!!!! but does in MenuCascadeView


  [self addSubview: _navbar];
  [self addSubview: _prefTable];

  return self;
}


-(void) setTitle:(NSString*)title
{
  UINavigationItem* navtitle = [UINavigationItem alloc];
  [_navbar pushNavigationItem: [navtitle initWithTitle: title]];
}

-(NSString*) languagePref
{
  return [_prefTable languagePref];
}


//***********
//* Delegates 
//***********

- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button
{
  switch (button) 
  {
    case 1:  // left
      [GreenstoneApplication setInCollection:nil];
      [GreenstoneApplication transitionView:UITransitionBothShiftRight 
          fromView:self toView:_parent];
      break;

    default:
      NSLog(@"Unrecognised navbar button = %d",button);
      break;
  }

}


-(void) dealloc
{
  [_collection release];

  [super dealloc];
}

@end

