//
//  FlipsideViewController.m
//  Taschenlampe
//
//  Created by Chad Armstrong on 3/30/09.
//  Copyright Edenwaith 2009. All rights reserved.
//

#import "FlipsideViewController.h"


@implementation FlipsideViewController
@synthesize redValue;
@synthesize greenValue;
@synthesize blueValue;
@synthesize redSlider;
@synthesize greenSlider;
@synthesize blueSlider;
@synthesize colorView;

- (void)viewDidLoad {
	self.view.backgroundColor = [UIColor whiteColor]; // set background color to white
	// Load up defaults
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	if ([defaults objectForKey: @"red"] != nil)
	{
		red = [defaults floatForKey: @"red"];
	}
	else
	{
		red = 0.5;
	}
	
	if ([defaults objectForKey: @"green"] != nil)
	{
		green = [defaults floatForKey: @"green"];
	}
	else
	{
		green = 0.5;
	}
	
	if ([defaults objectForKey: @"blue"] != nil)
	{
		blue = [defaults floatForKey: @"blue"];
	}
	else
	{
		blue = 0.5;
	}
	
	redValue.text = [NSString stringWithFormat: @"%1.2f", red];
	greenValue.text =  [NSString stringWithFormat: @"%1.2f", green];
	blueValue.text =  [NSString stringWithFormat: @"%1.2f", blue];
	redSlider.value = red;
	greenSlider.value = green;
	blueSlider.value = blue;
	colorView.backgroundColor = [UIColor colorWithRed: red green: green blue: blue alpha: 1.0f];
}

- (void) viewWillDisappear: (BOOL) animated
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setFloat: red forKey: @"red"];
	[defaults setFloat: green forKey: @"green"];
	[defaults setFloat: blue forKey: @"blue"];
	
	[super viewWillDisappear: animated];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[redValue dealloc];
	[greenValue dealloc];
	[blueValue dealloc];
	
	[redSlider dealloc];
	[greenSlider dealloc];
	[blueSlider dealloc];
	
	[colorView dealloc];
	
	[super dealloc];
}


- (IBAction) changeColor: (id) sender
{
	UISlider *slider = (UISlider *)sender;
	float sliderValue = (float)slider.value;
	
	if ([sender tag] == 0)	// red
	{
		redValue.text = [NSString stringWithFormat: @"%1.2f", sliderValue];
		red = sliderValue;
	}
	else if ([sender tag] == 1)	// green
	{
		greenValue.text = [NSString stringWithFormat: @"%1.2f", sliderValue];
		green = sliderValue;
	}
	else if ([sender tag] == 2)	// blue
	{
		blueValue.text = [NSString stringWithFormat: @"%1.2f", sliderValue];
		blue = sliderValue;
	}
	
	colorView.backgroundColor = [UIColor colorWithRed: red green: green blue: blue alpha: 1.0f];
}



@end
