@implementation PakageAppDelegate

@synthesize window, tabBarController;

- (void) applicationDidFinishLaunching:(UIApplication *) application 
{   
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	[[PackageDatabase database] addNotification:self];
	
	HomeViewController *homeTab = [[HomeViewController alloc] init];
	homeTab.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Pakage" image:[UIImage imageNamed:@"home.png"] tag:0];
	homeTab.title = @"Pakage";
	homeTab.navigationController = [[UINavigationController alloc] initWithRootViewController:homeTab];
	
	BrowseViewController *browseTab = [[BrowseViewController alloc] init];
	browseTab.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Browse" image:[UIImage imageNamed:@"sections.png"] tag:1];
	browseTab.title = @"Browse";
	browseTab.navigationController = [[UINavigationController alloc] initWithRootViewController:browseTab];
	
	RecentViewController *recentTab = [[RecentViewController alloc] init];
	recentTab.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Recent" image:[UIImage imageNamed:@"recent.png"] tag:2];
	recentTab.title = @"Recent";
	recentTab.navigationController = [[UINavigationController alloc] initWithRootViewController:recentTab];
	if([[[PackageDatabase database] needUpdates] count] > 0)
		recentTab.tabBarItem.badgeValue = [NSString stringWithFormat:@"%i", [[[PackageDatabase database] needUpdates] count]];
	
	ManageViewController *manageTab = [[ManageViewController alloc] init];
	manageTab.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Manage" image:[UIImage imageNamed:@"install.png"] tag:3];
	manageTab.title = @"Manage";
	manageTab.navigationController = [[UINavigationController alloc] initWithRootViewController:manageTab];
	
	SourceViewController *sourceTab = [[SourceViewController alloc] init];
	sourceTab.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Sources" image:[UIImage imageNamed:@"sources.png"] tag:4];
	sourceTab.title = @"Sources";
	sourceTab.navigationController = [[UINavigationController alloc] initWithRootViewController:sourceTab];
	
	// Preload views
	
	[recentTab view];
	[browseTab view];
	[manageTab view];
	[sourceTab view];
	
	tabBarController = [[UITabBarController alloc] init];  
	//tabBarController.viewControllers = [NSArray arrayWithObjects:homeTab.navigationController, recentTab.navigationController, browseTab.navigationController, manageTab.navigationController, sourceTab.navigationController, nil];  
	
	[browseTab release];
	//[homeTab release];
	
	[window addSubview:tabBarController.view];
	[window makeKeyAndVisible];
	
	// TODO: refresh from sources (based on prefs)
	[NSThread detachNewThreadSelector:@selector(refreshFromCache) toTarget:[PackageDatabase database] withObject:nil];
}

- (void) applicationWillTerminate:(UIApplication *) application
{
	// nothing to see here
}

- (void) databaseDidUpdate
{
	// nothing to see here
}

- (void) dealloc 
{
	[[PackageDatabase database] removeNotification:self];
    [window release];
    [super dealloc];
}

@end
