//
//  TuveAppDelegate.m
//  Tuve
//
//  Created by Philippe Hausler on 9/8/08.
//  Copyright Philippe Hausler 2008. All rights reserved.
//
#import <MediaPlayer/MediaPlayer.h>
#import "TuveAppDelegate.h"
#import "TuveConnection.h"
@implementation TuveAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
	connection = [[TuveConnection alloc] init];
	flipped = NO;
    [window makeKeyAndVisible];
	[window addSubview:nav.view];
}

- (IBAction)toggleView {
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1];
	[UIView setAnimationTransition:(flipped ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:window cache:YES];
	if(!flipped)
	{
		[flipside viewWillAppear:YES];
		[nav viewWillDisappear:YES];
		[nav.view removeFromSuperview];
		[window addSubview:flipside.view];
		[nav viewDidDisappear:YES];
		[flipside viewDidAppear:YES];
		flipped = YES;
	}
	else
	{
		[nav viewWillAppear:YES];
		[flipside viewWillDisappear:YES];
		[flipside.view removeFromSuperview];
		[window addSubview:nav.view];
		[flipside viewDidDisappear:YES];
		[nav viewDidAppear:YES];
		flipped = NO;
	}
	[UIView commitAnimations];
}
- (IBAction)sendChat
{
	[connection sendChat:chatBox.text toChannel:@"#TUvé"];
	chatBox.text = @"";
}
- (void)dealloc {
	[window release];
	[super dealloc];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
	CGRect rect = window.frame;
	[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
	rect.origin.y = 35-window.frame.size.height+textField.superview.frame.origin.y-textField.superview.frame.size.height;
	window.frame = rect;
    [UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField endEditing:YES];
	return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
	
	CGRect rect = window.frame;
	[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
	rect.origin.y = 0;
	window.frame = rect;
    [UIView commitAnimations];
	return YES;
}
-(void)playMovieAtURL:(NSURL*)theURL

{
    MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:theURL];
    theMovie.scalingMode=MPMovieScalingModeAspectFill;
    theMovie.movieControlMode=MPMovieControlModeHidden;
	
    // Register for the playback finished notification.
	
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(movieFinished:)
												 name:MPMoviePlayerPlaybackDidFinishNotification
											   object:theMovie];
	
    // Movie playback is asynchronous, so this method returns immediately.
    [theMovie play];
}

// When the movie is done,release the controller.
-(void)movieFinished:(NSNotification*)aNotification
{
    MPMoviePlayerController* theMovie=[aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:theMovie];
	
    // Release the movie instance created in playMovieAtURL
    [theMovie release];
} 
@end
