//
//  OptionsViewController.m
//  LostClient
//
//  Created by Webster Ross III on 4/1/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "OptionsViewController.h"
#import "Constants.h"

@implementation OptionsViewController

@synthesize searchRadius, maxResults, maxResultsSlider, searchRadiusSlider, maxResultsLabel,
			searchRadiusLabel, maxResultsEnabled, searchRadiusEnabled, directedSearchEnabled, distanceControl,
			searchMinLabel, searchMaxLabel;

- (void)viewDidLoad {
	self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];	
	maxResultsSlider.enabled = maxResultsEnabled;
	searchRadiusSlider.enabled = searchRadiusEnabled;
    maxResultsSlider.value = maxResults; 
	maxResultsLabel.text = [NSString stringWithFormat:@"%.0f %@", maxResults, @"Results"];
	
    if(useKm){
        searchRadiusSlider.value = searchRadius;
        searchRadiusLabel.text = [NSString stringWithFormat:@"%.1f %@", searchRadiusSlider.value, @"km"];
    }
    
    else {
        searchRadiusSlider.value = searchRadius * 0.621371192;
        searchRadiusLabel.text = [NSString stringWithFormat:@"%.1f %@", searchRadiusSlider.value, @"mi"];
    }
    
    
	//searchRadiusLabel.text = [NSString stringWithFormat:@"%.1f %@", searchRadius, @"mi"];
}

- (IBAction)maxResultsSliderValueChanged:(id)sender {
	double value = [maxResultsSlider value];
	maxResultsLabel.text = [NSString stringWithFormat:@"%.0f %@", value, @"Results"];
	maxResults = value;
}

- (IBAction)searchRadiusValueChanged:(id)sender {
	double value = [searchRadiusSlider value];
	
	if(useKm)
	{
		searchRadiusLabel.text = [NSString stringWithFormat:@"%.1f %@", value, @"km"];
	}
	else{
		searchRadiusLabel.text = [NSString stringWithFormat:@"%.1f %@", value, @"mi"];
	}
	
	if(!useKm){searchRadius = value*1.609344;}
	else{searchRadius = value;}
	optionsUpdated = TRUE;
}

- (IBAction)maxResultsSwitchAction:(id)sender {
	if(!maxResultsEnabled) {
		maxResultsEnabled = TRUE;
		maxResultsSlider.enabled = TRUE;
	}
	else {
		maxResultsEnabled = FALSE;
		maxResultsSlider.enabled = FALSE;
	}
}

- (IBAction)searchRadiusSwitchAction:(id)sender {
	if(!searchRadiusEnabled) {
		searchRadiusEnabled = TRUE;
		searchRadiusSlider.enabled = TRUE;
	}
	else {
		searchRadiusEnabled = FALSE;
		searchRadiusSlider.enabled = FALSE;
	}
}

- (IBAction)directedSearchSwitchAction:(id)sender {
	if(!directedSearchEnabled) {
		directedSearchEnabled = TRUE;
	}
	else {
		directedSearchEnabled = FALSE;
	}	
	optionsUpdated = TRUE;
}

- (IBAction)distanceTypeChanged:(id)sender {
	if(distanceControl.selectedSegmentIndex == 0)
	{
		useKm = FALSE;
		searchRadiusSlider.value = searchRadiusSlider.value * 0.621371192;
		searchMinLabel.text = @"1mi";
		searchMaxLabel.text = @"5mi";
		searchRadiusSlider.minimumValue = 1;
		searchRadiusSlider.maximumValue = 5;
		searchRadiusLabel.text = [NSString stringWithFormat:@"%.1f %@", searchRadiusSlider.value, @"mi"];
	}
	
	else{
		useKm = TRUE;
		searchMinLabel.text = @"1km";
		searchMaxLabel.text = @"8km";
		searchRadiusSlider.minimumValue = 1;
		searchRadiusSlider.maximumValue = 8;
		searchRadiusSlider.value = searchRadiusSlider.value * 1.609344;
		searchRadiusLabel.text = [NSString stringWithFormat:@"%.1f %@", searchRadiusSlider.value, @"km"];
	}
}

- (void)dealloc {
	[maxResultsSlider release];
	[searchRadiusSlider release];
	[maxResultsLabel release];
	[searchRadiusLabel release];
    [super dealloc];
}

@end
