//
//  TenMinuteBrowserAppDelegate.m
//  TenMinuteBrowser
//
//  Created by Chris Adamson on 7/9/08.
//  Copyright Subsequently and Furthermore, Inc. 2008. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "TenMinuteBrowserViewController.h"

@implementation TenMinuteBrowserViewController

/*
 Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
 */

/*
 Implement viewDidLoad if you need to do additional setup after loading the view.
- (void)viewDidLoad {
	[super viewDidLoad];
}
 */


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return ((interfaceOrientation == UIInterfaceOrientationPortrait) ||
			(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
			(interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
			(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}


- (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 {
	[super dealloc];
}

-(void) loadURL {
	NSRange aboutRange = [urlField.text rangeOfString: @"about:"];
	NSURL *url = NULL;
	if (aboutRange.location == 0) {
		// find the about page in bundle
//START:code.TenMinuteBrowser.findabout
		NSString *aboutPath =
			[[NSBundle mainBundle] pathForResource:@"aboutbook"
						ofType:@"html"];
		url = [[NSURL alloc] initFileURLWithPath: aboutPath];
//END:code.TenMinuteBrowser.findabout
	} else {
		url = [[NSURL alloc] initWithString: urlField.text];
	}
	if (url != NULL) {
		NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
		[webView loadRequest: request];
		[request release];
		[url release];

	}
}

/* this is the book version, which doesn't know about the "about:" stuff
//START:code.TenMinuteBrowser.loadurl
-(void) loadURL {
	NSURL *url = [[NSURL alloc] initWithString: urlField.text];
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
	[webView loadRequest: request];
	[request release];
	[url release];
}	
//END:code.TenMinuteBrowser.loadurl
 */

//START:code.TenMinuteBrowser.textfieldshouldreturn
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	if (textField == urlField) { 
		[self handleGoTapped];
	}
	return YES;
}
//END:code.TenMinuteBrowser.textfieldshouldreturn


//START:code.TenMinuteBrowser.handlegotapped
-(IBAction) handleGoTapped {
	[urlField resignFirstResponder];
	[self loadURL];
}
//END:code.TenMinuteBrowser.handlegotapped


@end
