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

@import <Foundation/CPObject.j>

@import <AppKit/CPCollectionView.j>

@import "PhotoCell.j"
@import "PLCollectionView.j"

@implementation AppController : CPObject
{
    CPCollectionView photosCollectionView1;
    CPCollectionView photosCollectionView2;
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
	CPLogRegister(CPLogPopup)

    var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
    var contentView = [theWindow contentView];
    var bounds = [contentView bounds];

    var photoItem = [[CPCollectionViewItem alloc] init];
    [photoItem setView:[[PhotoCell alloc] initWithFrame:CGRectMake(0, 0, 150, 150)]];

    var scrollView1 = [[CPScrollView alloc] initWithFrame:CGRectMake(200, 0, CGRectGetWidth(bounds) - 200, CGRectGetHeight(bounds) / 2 - 5)];

    photosCollectionView1 = [[CPCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds) - 200, 0)];

    [photosCollectionView1 setDelegate:self];
    [photosCollectionView1 setItemPrototype:photoItem];
    
    [photosCollectionView1 setMinItemSize:CGSizeMake(150, 150)];
    [photosCollectionView1 setMaxItemSize:CGSizeMake(150, 150)];
    [photosCollectionView1 setAutoresizingMask:CPViewWidthSizable];
    
    [scrollView1 setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
    [scrollView1 setDocumentView:photosCollectionView1];
    [scrollView1 setAutohidesScrollers:YES];

    [[scrollView1 contentView] setBackgroundColor:[CPColor colorWithCalibratedWhite:0.25 alpha:1.0]];
    
    [contentView addSubview:scrollView1];    

	var mainBundle = [CPBundle mainBundle];
	var path1 = [mainBundle pathForResource:@"PLL_0018_550_368.jpg"];
	var path2 = [mainBundle pathForResource:@"PLL_0033_550_368.jpg"];
	var path3 = [mainBundle pathForResource:@"DSC6881_550_368.jpg"];
	var path4 = [mainBundle pathForResource:@"DSC6888_550_368.jpg"];
	var path5 = [mainBundle pathForResource:@"PLL_0087_368_550.jpg"];
	var path6 = [mainBundle pathForResource:@"PLL_9873_368_550.jpg"];
	var path7 = [mainBundle pathForResource:@"PLL_9929_368_550.jpg"];
	var array = [[CPArray alloc] initWithObjects:path1, path2, path3, path4, path5, path6, path7, nil];
	[photosCollectionView1 setContent:array];
	[photosCollectionView1 setAllowsMultipleSelection:YES];
//	[photosCollectionView1 setMaxNumberOfRows:1];
//	[photosCollectionView1 setMaxNumberOfColumns:1];




    var scrollView2 = [[CPScrollView alloc] initWithFrame:CGRectMake(200, CGRectGetHeight(bounds) / 2 + 5, CGRectGetWidth(bounds) - 200, CGRectGetHeight(bounds) / 2)];

    photosCollectionView2 = [[PLCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds) - 200, 0)];

    [photosCollectionView2 setDelegate:self];
    [photosCollectionView2 setItemPrototype:photoItem];
    
    [photosCollectionView2 setMinItemSize:CGSizeMake(150, 150)];
    [photosCollectionView2 setMaxItemSize:CGSizeMake(150, 150)];
    [photosCollectionView2 setAutoresizingMask:CPViewWidthSizable];
    
    [scrollView2 setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
    [scrollView2 setDocumentView:photosCollectionView2];
    [scrollView2 setAutohidesScrollers:YES];

    [[scrollView2 contentView] setBackgroundColor:[CPColor colorWithCalibratedWhite:0.5 alpha:1.0]];
    
    [contentView addSubview:scrollView2];    

	var mainBundle = [CPBundle mainBundle];
	var path1 = [mainBundle pathForResource:@"PLL_0018_550_368.jpg"];
	var path2 = [mainBundle pathForResource:@"PLL_0033_550_368.jpg"];
	var path3 = [mainBundle pathForResource:@"DSC6881_550_368.jpg"];
	var array = [[CPArray alloc] initWithObjects:path1, path2, path3, nil];
	[photosCollectionView2 setContent:array];
	[photosCollectionView2 setAllowsMultipleSelection:YES];



	[theWindow makeKeyAndOrderFront:self];
	[theWindow makeFirstResponder:photosCollectionView2];

   // [theWindow orderFront:self];
}



- (void)adjustImageSize:(id)sender
{
    var newSize = [sender value];
    
    [photosCollectionView setMinItemSize:CGSizeMake(newSize, newSize)];
    [photosCollectionView setMaxItemSize:CGSizeMake(newSize, newSize)];
}


// CPCollectionView delegate
// index start at 0
-(void)collectionView:(CPCollectionView)collectionView didDoubleClickOnItemAtIndex:(int)index
{
	if (collectionView == photosCollectionView1)
	{
		//debugger;
	}
}

-(void)collectionViewDidChangeSelection:(CPCollectionView)collectionView
{
	if (collectionView == photosCollectionView1)
	{
		//debugger;
	}
}

-(CPData)collectionView:(CPCollectionView)collectionView dataForItemsAtIndexes:(CPIndexSet)indexes forType:(CPString)aType
{
	// debugger;
	
	if (aType == PhotoCellDragType)
	{
		var pathArray = [CPArray array];

		// Get the array of selected 
		var selectedItems = [[collectionView items] objectsAtIndexes:indexes];
		var enumerator = [selectedItems objectEnumerator];
		
		var selectedItem;
		while (selectedItem = [enumerator nextObject])
		{
			var path = [selectedItem representedObject];
			[pathArray addObject:path];
		}

		// CPLog.trace(@"pathArray:" + [pathArray description]);
		
		var data = [CPKeyedArchiver archivedDataWithRootObject:pathArray];
		return data;
	}
	
	// Empty data
	return [CPData data];
}

-(CPArray)collectionView:(CPCollectionView)collectionView dragTypesForItemsAtIndexes:(CPIndexSet)indices
{
	// debugger;
	
	var array = [CPArray array];		// Empty array

	if (collectionView == photosCollectionView1)
	{
		array = [CPArray arrayWithObject:PhotoCellDragType];
	}
	
	// CPLog.trace(@"collectionView:dragTypesForItemsAtIndexes:" + [array description]);
	return array;
}






@end
