/*
 * AppController.j
 * CappuccinoBrowser
 *
 * Created by __Me__ on __Date__.
 * Copyright 2008 __MyCompanyName__. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import "ArticleCollectionView.j"
@import "ArticleCell.j"

@implementation AppController : CPObject
{
	CPSplitView verticalSplitter;
    ArticleCollectionView articleCollectionView;
	CPWebView webView;
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
    var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
    var contentView = [theWindow contentView];
    var bounds = [contentView bounds];

	[self populateWindowWithSplitters:contentView];
	
	var defaultCenter = [CPNotificationCenter defaultCenter];
	[defaultCenter addObserver:self selector:@selector(articleChanged:) name:@"ArticleChanged" object:articleCollectionView];
	

    [theWindow orderFront:self];

	[articleCollectionView LoadArticles];
}

-(void) populateWindowWithSplitters:(CPView)contentView
{
    var bounds = [contentView bounds];

    verticalSplitter = [[CPSplitView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([contentView bounds]), CGRectGetHeight([contentView bounds]))];
	[verticalSplitter setDelegate:self];
	[verticalSplitter setVertical:YES]; 
	[verticalSplitter setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; 
	[verticalSplitter setIsPaneSplitter:NO];
	[contentView addSubview:verticalSplitter];

	var leftView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 200, CGRectGetHeight([verticalSplitter bounds]))];
	[leftView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; 
	
	var rightView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([verticalSplitter bounds]) - 200, CGRectGetHeight([verticalSplitter bounds]))];
	[rightView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; 

	[verticalSplitter addSubview:leftView];
	[verticalSplitter addSubview:rightView];

	
	[self populateLeftView:leftView];
	[self populateRightView:rightView];
}

-(void) populateLeftView:(CPView)leftView
{
    var bounds = [leftView bounds];

    var collectionViewItem = [[CPCollectionViewItem alloc] init];
    [collectionViewItem setView:[[ArticleCell alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]];

    var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];

    articleCollectionView = [[ArticleCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
    [articleCollectionView setItemPrototype:collectionViewItem];
    [articleCollectionView setMinItemSize:CGSizeMake(200, 50)];
    [articleCollectionView setMaxItemSize:CGSizeMake(200, 50)];
	[articleCollectionView setAllowsMultipleSelection:NO];
    [articleCollectionView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
    
    [scrollView setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
    [scrollView setDocumentView:articleCollectionView];
    [scrollView setAutohidesScrollers:YES];

    [[scrollView contentView] setBackgroundColor:[CPColor colorWithCalibratedWhite:0.25 alpha:1.0]];
    
    [leftView addSubview:scrollView];    
}

-(void) populateRightView:(CPView)rightView
{
    var bounds = [rightView bounds];

	webView = [[CPWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
	[webView setAutoresizingMask: CPViewWidthSizable | CPViewHeightSizable]; 
	// [webView setDrawsBackground:YES]; 

	[rightView addSubview:webView]; 	
}

-(void)articleChanged:(CPNotification)notification
{
	// debugger;
	var userinfo = [notification userInfo];
	var articleUrl = [userinfo objectForKey:@"articleUrl"];
	
	[webView setMainFrameURL:articleUrl];
}


@end
