//
//  BrowserViewController.m
//  Bookmarks
//
//  Created by Lieven Dekeyser on 10/12/09.
//  Copyright 2009 Lieven Dekeyser. All rights reserved.
//

#import "BrowserViewController.h"
#import "BookmarksViewController.h";


@implementation BrowserViewController

- (id)initWithURLString:(NSString *)inURL
{
	if (self = [super initWithNibName:nil bundle:nil])
	{
		UIWebView * webView = [[[UIWebView alloc] init] autorelease];
		webView.delegate = self;
		
		
		NSURLRequest * request = [[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:inURL]] autorelease];
		
		[webView loadRequest:request];
		
		self.view = webView;
		
		self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
			target:self
			action:@selector(addBookmark)] autorelease];
		
	}
	return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)addBookmark
{
	NSLog(@"TODO: add bookmark");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	NSLog(@"Web view loaded something: %@", webView.request);
	
	NSString * link = [[webView.request URL] absoluteString];
	
	NSString * title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
	
	NSLog(@"%@ - %@", title, link);
	
	NSDictionary * historyItem = [NSDictionary dictionaryWithObjectsAndKeys:
		title, @"name",
		link, @"link",
		nil];
	
	[[BookmarksViewController currentInstance] addHistoryItem:historyItem];
	
	
	self.title = title;
	
}


@end
