//
//  countdownViewController.m
//  countdown
//
//  Created by Bess Ho on 9/5/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "countdownViewController.h"

@implementation countdownViewController

@synthesize countdownLabel;
@synthesize countdownTimer;

- (void)dealloc {
	[countdownLabel release];
	[countdownTimer release];
    [super dealloc];
}

- (void)viewDidUnload {
	countdownLabel = nil;
	[countdownTimer invalidate];
	countdownTimer = nil;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
	[super viewDidLoad];
	// Enter the count down integer value
	[self showCountdown:10];
}

- (void) showCountdown:(NSInteger) timeRemaining{
	// Hide Label
    self.countdownLabel.hidden = NO;
	
	// Display Integer in String in Label value
    self.countdownLabel.text = [NSString stringWithFormat:@"%d", timeRemaining];
	
	// Set Label alpha to Alpha 100% opaque
    self.countdownLabel.alpha = 1;
	
	// Transform Label
    self.countdownLabel.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
    
	// Start animation with number in Integer
    [UIView beginAnimations:nil context:[NSNumber numberWithInt:timeRemaining]];
	
	// Set animation
    [UIView setAnimationDelegate:self];
	
	// Stop animation in condition
    [UIView setAnimationDidStopSelector:@selector(countdownDidStop:finished:context:)];
	
	// Set animation duration to seconds
    [UIView setAnimationDuration:2];
    
	// Transform Label
    self.countdownLabel.transform = CGAffineTransformScale(CGAffineTransformIdentity, .5, .5);
	
	// Set Label alpha to Alpha 0% transparent
    self.countdownLabel.alpha = 0;
    
	// Run animation
    [UIView commitAnimations];
}

- (void) countdownDidStop:(NSString *)name finished:(BOOL)finished context:(void*)context{
    NSNumber *lastNumber = (NSNumber *)context;
    if ([lastNumber intValue] > 1) 
    {
        [self showCountdown:[lastNumber intValue] - 1];
    }
    else 
    {
        self.countdownLabel.hidden = YES;
		[self startTimer];
    }
}

- (void)startTimer {	
	countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
													 target: self
												   selector: @selector(tick)
												   userInfo: nil
													repeats: YES];
}

- (void) tick{
	// Create a counter
	static int counter = 0;
	counter += 1;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

@end
