

#import "TwoXibsAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation TwoXibsAppDelegate

@synthesize window, firstViewController, secondViewController, helloWorldColor;   //synthesize all our getters and setters

- (void)applicationDidFinishLaunching:(UIApplication *)application {	
	
	// Override point for customization after app launch
	
	helloWorldColor = [UIColor darkGrayColor];					//initialize our color variable to dark gray
	
	//set up firstViewController ivar 
	FirstViewController *aFirstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]];
	[self setFirstViewController:aFirstView];
	[aFirstView release];
	
	//add firstViewController's view to the window
	[window addSubview:[firstViewController view]];	
	
	//set onFront equal to YES since firstViewController's view is showing
	onFront = YES;
	
    [window makeKeyAndVisible];
	
	//tell the notification center to notify the app delegate if a @"DelegateFlipViewNotification" is received
	//Nothing will happen as a result of this unless you uncomment the indicated lines in SecondViewController.m
	NSNotificationCenter *dc = [NSNotificationCenter defaultCenter];
	[dc addObserver:self selector:@selector(flipViews:) name:@"DelegateFlipViewNotification" object:nil];
}

- (void)flipViews:(id)sender {
	//animation block to flip the views in and out of the screen
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1.5];
	if(onFront) {
		if(!secondViewController) {
			SecondViewController *aSecondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
			[self setSecondViewController:aSecondView];
			[aSecondView release];
		}
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES];
		[[firstViewController view] removeFromSuperview];
		[window addSubview:[secondViewController view]];
		onFront = NO;
	} else {
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
		[[secondViewController view] removeFromSuperview];
		[window addSubview:[firstViewController view]];
		onFront = YES;
	}
	[UIView commitAnimations];
}
 

- (void)dealloc {
	[helloWorldColor release];
	[firstViewController release];
	[secondViewController release];
	[window release];
	[super dealloc];
}


@end
