//
//  hellowebviewViewController.m
//  hellowebview
//
//  Created by Bess Ho on 8/5/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

// Define layout
#define kLeftMargin				20.0
#define kTopMargin				20.0
#define kRightMargin			20.0
#define kTweenMargin			10.0
#define kTextFieldHeight		30.0

#import "hellowebviewViewController.h"

@implementation hellowebviewViewController

@synthesize myWebView;

- (void)dealloc {
	myWebView.delegate = nil;
	[myWebView release];
	
    [super dealloc];
}

 - (void)viewDidLoad {
	[super viewDidLoad];
	 
	 CGRect webFrame = CGRectMake(0, kTweenMargin * 2.0 + kTextFieldHeight,
								self.view.bounds.size.width, self.view.bounds.size.height);
	 self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
	 self.myWebView.backgroundColor = [UIColor whiteColor];
	 self.myWebView.scalesPageToFit = YES;
	 self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	 self.myWebView.delegate = self;
	 [self.view addSubview: self.myWebView];

	 CGRect textFieldFrame = CGRectMake(kLeftMargin, kTweenMargin,
										self.view.bounds.size.width - (kLeftMargin * 2.0), kTextFieldHeight);
	 UITextField *urlField = [[UITextField alloc] initWithFrame:textFieldFrame];
	 urlField.borderStyle = UITextBorderStyleBezel;
	 urlField.textColor = [UIColor blackColor];
	 urlField.delegate = self;
	 urlField.placeholder = @"<enter a URL>";
	 urlField.text = @"http://www.apple.com";
	 urlField.backgroundColor = [UIColor whiteColor];
	 urlField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	 urlField.returnKeyType = UIReturnKeyGo;
	 urlField.keyboardType = UIKeyboardTypeURL;	// Makes the keyboard more friendly for typing URLs
	 urlField.autocapitalizationType = UITextAutocapitalizationTypeNone;	// Disable Autocapitalization
	 urlField.autocorrectionType = UITextAutocorrectionTypeNo;	// Disable Autocompletion
	 urlField.clearButtonMode = UITextFieldViewModeAlways; // Always show clear button
	 [urlField setAccessibilityLabel:NSLocalizedString(@"URLTextField", @"")];
	 [self.view addSubview:urlField];
	 [urlField release];
	 
	 [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];
 }

- (void)viewDidUnload {
	[super viewDidUnload];
	
	// Release any retained subviews of the main view and set to nil
	self.myWebView = nil;
	
}

#pragma mark -
#pragma mark UIViewController delegate methods

- (void)viewWillAppear:(BOOL)animated {
	// Set the delegate if the web view is displayed
	self.myWebView.delegate = self;	
}

- (void)viewWillDisappear:(BOOL)animated {
	// Stop loading if the view view is hidden
    [self.myWebView stopLoading];
	
	// Disconnect the delegate if the web view is hidden
	self.myWebView.delegate = nil;
	
	// Hide Activity Indicator if the web view is hidden
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	// Dismiss the keyboard when "Done" button is selected
	[textField resignFirstResponder];
	[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[textField text]]]];
	
	return YES;
}

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Support rotation in this view controller
	 return YES;
 }

#pragma mark -
#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView {
	// Display activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
	// Hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
	// Hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
	// Report the error inside the webview
	NSString* errorString = [NSString stringWithFormat:
							 @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
							 error.localizedDescription];
	[self.myWebView loadHTMLString:errorString baseURL:nil];
}

- (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.
}

/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */

@end
