/*
 * AppController.j
 *
 * Created by Philippe Laval on 2009/03/19.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>
@import <Foundation/CPNotificationCenter.j>
@import <Foundation/CPNotification.j>

@import <AppKit/CPCollectionView.j>
@import <AppKit/CPToolbar.j>
@import <AppKit/CPToolbarItem.j>

@import "CPString_Base64.j"
@import "CPString_JSON.j"

@import "ImageCell.j"
@import "ImagetteCell.j"
@import "CategoryCollectionView.j"
@import "CategoryCell.j"

@import "PLCollectionView.j"
@import "PhotoSlideshow.j"
@import "ImageResizeView.j"
@import "ImagetteResizeView.j"

@import "AboutController.j"


var ImagetteSliderToolbarItemIdentifier = "ImagetteSliderToolbarItemIdentifier",
	ImageSliderToolbarItemIdentifier = "ImageSliderToolbarItemIdentifier",
    AddToolbarItemIdentifier = "AddToolbarItemIdentifier",
    RemoveToolbarItemIdentifier = "RemoveToolbarItemIdentifier",
    AboutToolbarItemIdentifier = "AboutToolbarItemIdentifier",
    SlideShowToolbarItemIdentifier = "SlideShowToolbarItemIdentifier";




@implementation AppController : CPObject
{
    CPWindow theWindow;
    CPWindow aboutWindow;


	CPSplitView verticalSplitter;
	CPSplitView horizontalSplitter;

    CategoryCollectionView categoryCollectionView;
    CPCollectionView photosCollectionView1;
    PLCollectionView photosCollectionView2;
    
    CPURLConnection postConnection;
    CPData receivedJsonData;
}

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

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

	// Create and display th emenu bar
	//[self setupMainMenu];
  	//[CPMenu setMenuBarVisible:YES];

	// Create the toolbar and set its delegate to be the AppController instance.
    var toolbar = [[CPToolbar alloc] initWithIdentifier:"Photos"];    
    [toolbar setDelegate:self];
	[toolbar setVisible:YES];
	// Associate the toolbar with the window
	[theWindow setToolbar:toolbar];


	[self populateWindowWithSplitters:contentView];

	var defaultCenter = [CPNotificationCenter defaultCenter];
	[defaultCenter addObserver:self selector:@selector(categoryChanged:) name:@"CategoryChanged" object:categoryCollectionView];

   // [theWindow orderFront:self];
	[theWindow makeKeyAndOrderFront:self];
	[theWindow makeFirstResponder:photosCollectionView2];
	
	[categoryCollectionView LoadCategories];
}

-(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]; 
	[horizontalSplitter setIsPaneSplitter:NO];
	[contentView addSubview:verticalSplitter];

	var leftView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 200, CGRectGetHeight([verticalSplitter bounds]))];
	[bottomView setAutoresizingMask: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];

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

	[rightView addSubview:horizontalSplitter];

	var topView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([horizontalSplitter bounds]), CGRectGetHeight([horizontalSplitter bounds]) / 2.0)];
	[bottomView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; 
	
	var bottomView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([horizontalSplitter bounds]), CGRectGetHeight([horizontalSplitter bounds]) / 2.0)];
	[bottomView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; 

	[horizontalSplitter addSubview:topView];
	[horizontalSplitter addSubview:bottomView];
	
	[self populateLeftView:leftView];
	[self populateTopView:topView];
	[self populateBottomView:bottomView];
}


-(void) populateLeftView:(CPView)leftView
{
	/*
	var button = [[CPButton alloc] initWithFrame: CGRectMake(20, 20, 100, 18)];
	[button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
	[button setTitle:"Slide show"];
	[button setTarget:self];
	[button setAction:@selector(startSlideShow:)];                
				  
	[leftView addSubview:button];

	*/
	
    var bounds = [leftView bounds];

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

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

    categoryCollectionView = [[CategoryCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
    [categoryCollectionView setItemPrototype:collectionViewItem];
    [categoryCollectionView setMinItemSize:CGSizeMake(150, 150)];
    [categoryCollectionView setMaxItemSize:CGSizeMake(150, 150)];
	[categoryCollectionView setAllowsMultipleSelection:YES];
    [categoryCollectionView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
    
    [scrollView setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
    [scrollView setDocumentView:categoryCollectionView];
    [scrollView setAutohidesScrollers:YES];

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

	var mainBundle = [CPBundle mainBundle];
	
	/*
	var dict1 = [CPDictionary dictionaryWithObject:[mainBundle pathForResource:@"PLL_0018_550_368.jpg"] forKey:@"imagePath"];
	[dict1 setObject:@"Category 1" forKey:@"title"];
	[dict1 setObject:@"subtitle 1" forKey:@"subtitle"];
	[dict1 setObject:@"category1.php" forKey:@"jsonUrl"];
	var dict2 = [CPDictionary dictionaryWithObject:[mainBundle pathForResource:@"PLL_0033_550_368.jpg"] forKey:@"imagePath"];
	[dict2 setObject:@"Category 2" forKey:@"title"];
	[dict2 setObject:@"subtitle 2" forKey:@"subtitle"];
	[dict2 setObject:@"category2.php" forKey:@"jsonUrl"];
	var dict3 = [CPDictionary dictionaryWithObject:[mainBundle pathForResource:@"DSC6881_550_368.jpg"] forKey:@"imagePath"];
	[dict3 setObject:@"Category 3" forKey:@"title"];
	[dict3 setObject:@"subtitle 3" forKey:@"subtitle"];
	[dict3 setObject:@"category1.php" forKey:@"jsonUrl"];
	var dict4 = [CPDictionary dictionaryWithObject:[mainBundle pathForResource:@"DSC6888_550_368.jpg"] forKey:@"imagePath"];
	[dict4 setObject:@"Category 4" forKey:@"title"];
	[dict4 setObject:@"subtitle 4" forKey:@"subtitle"];
	[dict4 setObject:@"category2.php" forKey:@"jsonUrl"];
	var dict5 = [CPDictionary dictionaryWithObject:[mainBundle pathForResource:@"PLL_0087_368_550.jpg"] forKey:@"imagePath"];
	[dict5 setObject:@"Category 5" forKey:@"title"];
	[dict5 setObject:@"subtitle 5" forKey:@"subtitle"];
	[dict5 setObject:@"category1.php" forKey:@"jsonUrl"];
	var dict6 = [CPDictionary dictionaryWithObject:[mainBundle pathForResource:@"PLL_9873_368_550.jpg"] forKey:@"imagePath"];
	[dict6 setObject:@"Category 6" forKey:@"title"];
	[dict6 setObject:@"subtitle 6" forKey:@"subtitle"];
	[dict6 setObject:@"category2.php" forKey:@"jsonUrl"];
	var dict7 = [CPDictionary dictionaryWithObject:[mainBundle pathForResource:@"PLL_9929_368_550.jpg"] forKey:@"imagePath"];
	[dict7 setObject:@"Category 7" forKey:@"title"];
	[dict7 setObject:@"subtitle 7" forKey:@"subtitle"];
	[dict7 setObject:@"category1.php" forKey:@"jsonUrl"];
	
	
	// Associate the content array with the collection view
	var array = [[CPArray alloc] initWithObjects:dict1, dict2, dict3, dict4, dict5, dict6, dict7, nil];
	[categoryCollectionView setContent:array];
	*/
}


-(void) populateTopView:(CPView)topView
{
    var bounds = [topView bounds];

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

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

    photosCollectionView1 = [[CPCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
    [photosCollectionView1 setDelegate:self];
    [photosCollectionView1 setItemPrototype:photoItem];
    [photosCollectionView1 setMinItemSize:CGSizeMake(150, 150)];
    [photosCollectionView1 setMaxItemSize:CGSizeMake(150, 150)];
	[photosCollectionView1 setAllowsMultipleSelection:YES];
    [photosCollectionView1 setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
    
    [scrollView1 setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
    [scrollView1 setDocumentView:photosCollectionView1];
    [scrollView1 setAutohidesScrollers:YES];

    [[scrollView1 contentView] setBackgroundColor:[CPColor colorWithCalibratedWhite:0.25 alpha:1.0]];
    
    [topView 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];
*/
}

-(void) populateBottomView:(CPView)bottomView
{
    var bounds = [bottomView bounds];

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

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

    photosCollectionView2 = [[PLCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
    [photosCollectionView2 setDelegate:self];
    [photosCollectionView2 setItemPrototype:photoItem];
    [photosCollectionView2 setMinItemSize:CGSizeMake(250, 250)];
    [photosCollectionView2 setMaxItemSize:CGSizeMake(250, 250)];
    [photosCollectionView2 setAllowsMultipleSelection:YES];
    [photosCollectionView2 setAutoresizingMask:CPViewWidthSizable];

    [scrollView2 setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
    [scrollView2 setDocumentView:photosCollectionView2];
    [scrollView2 setAutohidesScrollers:YES];

    [[scrollView2 contentView] setBackgroundColor:[CPColor colorWithCalibratedWhite:0.5 alpha:1.0]];
    
    [bottomView 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];
*/
}



// 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 == ImagetteCellDragType)
	{
		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 dict = [selectedItem representedObject];
			var path = [dict objectForKey:@"url"];
			[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:ImagetteCellDragType];
	}
	
	// CPLog.trace(@"collectionView:dragTypesForItemsAtIndexes:" + [array description]);
	return array;
}


-(void)startSlideShow:(id)sender
{
	debugger;
	var application = [CPApplication sharedApplication];
	
	var photoSlideShow = [[PhotoSlideshow alloc] init];
    [photoSlideShow showWindow:self];
    [[photoSlideShow window] makeKeyAndOrderFront:self];
	
	/*
	var mainBundle = [CPBundle mainBundle];
	var path = [mainBundle pathForResource:@"PLL_0018_550_368.jpg"];
	var image1 = [[CPImage alloc] initWithContentsOfFile:path size:CGSizeMake(550, 368)];

	path = [mainBundle pathForResource:@"PLL_0033_550_368.jpg"];
	var image2 = [[CPImage alloc] initWithContentsOfFile:path size:CGSizeMake(550, 368)];

	var images = [[CPArray alloc] initWithObjects:image1, image2, nil];
	*/
	
	var images = [[photosCollectionView2 content] copy];
	
	var objects = [[CPArray alloc] initWithObjects:[CPNumber numberWithLong:0], images, nil];
	var keys = [[CPArray alloc] initWithObjects:@"currentIndex", @"images", nil];
	var dict = [[CPDictionary alloc] initWithObjects:objects forKeys:keys];


	//var timer = [CPTimer scheduledTimerWithTimeInterval:2 target:photoSlideShow selector:@selector(switchImage:) userInfo:dict repeats:YES];
	var timer = [[CPTimer alloc] initWithFireDate:[CPDate dateWithTimeIntervalSinceNow:0.1] interval:2 target:photoSlideShow 
									selector:@selector(switchImage:) userInfo:dict repeats:YES];
	// add the timer to the runloop
	[[CPRunLoop currentRunLoop] addTimer:timer forMode:CPDefaultRunLoopMode];

	// Start in modal mode (the user can choose to use the "esc" key or to close the window to escape this modal mode)
	[application runModalForWindow:[photoSlideShow window]];
}





/*

-(void)endAction:(id)sender
{
	debugger;
}

-(void)startSheet:(id)sender
{
	debugger;
	
	var application = [CPApplication sharedApplication];

	var aSheet = [[CPWindow alloc] initWithContentRect:CGRectMake(0, 0, 150, 150) styleMask:CPHUDBackgroundWindowMask | CPClosableWindowMask];
    var contentView = [aSheet contentView];
    var bounds = [contentView bounds];


	var button = [[CPButton alloc] initWithFrame: CGRectMake(20, 20, 100, 18)];
	[button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
	[button setTitle:"OK"];
	[button setTarget:self];
	[button setAction:@selector(checkJSON:)];                
				  
	[contentView addSubview:button];

	// PLL (2009/03/17) : cappuccino 0.6 (seems not to be implemented)
	[application beginSheet:aSheet modalForWindow:[application mainWindow] modalDelegate:self didEndSelector:@selector(endAction:) contextInfo:nil];
}

*/

- (void)add:(id)sender
{
	var pathArray = [CPArray array];

	// Get the array of selected items
	var selectedItems = [[photosCollectionView1 items] objectsAtIndexes:[photosCollectionView1 selectionIndexes]];
	var enumerator = [selectedItems objectEnumerator];
	
	var selectedItem;
	while (selectedItem = [enumerator nextObject])
	{
		var dict = [selectedItem representedObject];
		var path = [dict objectForKey:@"url"];
		[pathArray addObject:path];
	}

	[photosCollectionView2 AddPictures:pathArray];
}

// Remove the selected pictures in the 
- (void)remove:(id)sender
{
	[photosCollectionView2 removeSelectedPictures:self];
}


- (void)adjustImagetteSize:(id)sender
{
    var newSize = [sender doubleValue];

    [photosCollectionView1 setMinItemSize:CGSizeMake(newSize, newSize)];
    [photosCollectionView1 setMaxItemSize:CGSizeMake(newSize, newSize)];
}

- (void)adjustImageSize:(id)sender
{
    var newSize = [sender doubleValue];

    [photosCollectionView2 setMinItemSize:CGSizeMake(newSize, newSize)];
    [photosCollectionView2 setMaxItemSize:CGSizeMake(newSize, newSize)];
}


// **************************** Toolbar delegate code ****************************

// Return an array of toolbar item identifier (all the toolbar items that may be present in the toolbar)
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
   return [CPToolbarFlexibleSpaceItemIdentifier, ImageSliderToolbarItemIdentifier, ImagetteSliderToolbarItemIdentifier, 
   				AddToolbarItemIdentifier, RemoveToolbarItemIdentifier,
   				AboutToolbarItemIdentifier, SlideShowToolbarItemIdentifier];
}

// Return an array of toolbar item identifier (the default toolbar items that are present in the toolbar)
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
   return [SlideShowToolbarItemIdentifier, AddToolbarItemIdentifier, RemoveToolbarItemIdentifier, 
   		CPToolbarFlexibleSpaceItemIdentifier, ImagetteSliderToolbarItemIdentifier, ImageSliderToolbarItemIdentifier, AboutToolbarItemIdentifier];
}

// Create the toolbar item that is requested by the toolbar.
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
	// Create the toolbar item and associate it with its identifier
    var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier:anItemIdentifier];

    if (anItemIdentifier == ImagetteSliderToolbarItemIdentifier)
    {
    	// The toolbar is using a custom view (of class PhotoResizeView)
        [toolbarItem setView:[[ImagetteResizeView alloc] initWithFrame:CGRectMake(0, 0, 180, 50)]];
        
        // We don't associate a target/action with the toolbar item. 
        // This will be done in the slider contained in the PhotoResizeView.
        [toolbarItem setLabel:"Scale Thumbnail"];

        [toolbarItem setMinSize:CGSizeMake(180, 32)];
        [toolbarItem setMaxSize:CGSizeMake(180, 32)];
    }
    else if (anItemIdentifier == ImageSliderToolbarItemIdentifier)
    {
    	// The toolbar is using a custom view (of class PhotoResizeView)
        [toolbarItem setView:[[ImageResizeView alloc] initWithFrame:CGRectMake(0, 0, 180, 50)]];
        
        // We don't associate a target/action with the toolbar item. 
        // This will be done in the slider contained in the PhotoResizeView.
        [toolbarItem setLabel:"Scale Picture"];

        [toolbarItem setMinSize:CGSizeMake(180, 32)];
        [toolbarItem setMaxSize:CGSizeMake(180, 32)];
    }
    else if (anItemIdentifier == AddToolbarItemIdentifier)
    {
	    var mainBundle = [CPBundle mainBundle];
	    
        var image = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"add.png"] size:CPSizeMake(30, 25)];
        var highlighted = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"addHighlighted.png"] size:CPSizeMake(30, 25)];
            
        [toolbarItem setImage:image];
        [toolbarItem setAlternateImage:highlighted];
        
        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(add:)];
        [toolbarItem setLabel:"Add Picture"];

        [toolbarItem setMinSize:CGSizeMake(32, 32)];
        [toolbarItem setMaxSize:CGSizeMake(32, 32)];
    }
    else if (anItemIdentifier == RemoveToolbarItemIdentifier)
    {        
    	var mainBundle = [CPBundle mainBundle];

        var image = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"remove.png"] size:CPSizeMake(30, 25)];
        var highlighted = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"removeHighlighted.png"] size:CPSizeMake(30, 25)];
            
        [toolbarItem setImage:image];
        [toolbarItem setAlternateImage:highlighted];

        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(remove:)];
        [toolbarItem setLabel:"Remove Picture"];
        
        [toolbarItem setMinSize:CGSizeMake(32, 32)];
        [toolbarItem setMaxSize:CGSizeMake(32, 32)];
    }
    else if (anItemIdentifier == AboutToolbarItemIdentifier)
    {        
    	var mainBundle = [CPBundle mainBundle];

        var image = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"remove.png"] size:CPSizeMake(30, 25)];
        var highlighted = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"removeHighlighted.png"] size:CPSizeMake(30, 25)];
            
        [toolbarItem setImage:image];
        [toolbarItem setAlternateImage:highlighted];

        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(toggleAboutWindow:)];
        [toolbarItem setLabel:"About"];
        
        [toolbarItem setMinSize:CGSizeMake(32, 32)];
        [toolbarItem setMaxSize:CGSizeMake(32, 32)];
    }
    else if (anItemIdentifier == SlideShowToolbarItemIdentifier)
    {        
    	var mainBundle = [CPBundle mainBundle];

        var image = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"remove.png"] size:CPSizeMake(30, 25)];
        var highlighted = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"removeHighlighted.png"] size:CPSizeMake(30, 25)];
            
        [toolbarItem setImage:image];
        [toolbarItem setAlternateImage:highlighted];

        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(startSlideShow:)];
        [toolbarItem setLabel:"Start Slideshow"];
        
        [toolbarItem setMinSize:CGSizeMake(32, 32)];
        [toolbarItem setMaxSize:CGSizeMake(32, 32)];
    }
    
    return toolbarItem;
}

// **************************** Menu code ****************************


-(void)setupMainMenu
{
	var mainMenu = [[CPMenu alloc] initWithTitle:@"MainMenu"];
	[[CPApplication sharedApplication] setMainMenu:mainMenu];
	
	// Action Menu
	var actionMenuItem = [[CPMenuItem alloc] initWithTitle:@"Action" action:@selector(doNothing:) keyEquivalent:@""];
	
	var actionMenu = [[CPMenu alloc] initWithTitle:@"Action"];
	buyMenuItem = [[CPMenuItem alloc] initWithTitle:@"Buy pictures" action:@selector(buyPictures:) keyEquivalent:@"B"];
	createAccountMenuItem = [[CPMenuItem alloc] initWithTitle:@"Create account" action:@selector(createAccountAction:) keyEquivalent:@""];
	[actionMenu addItem:buyMenuItem];
	[actionMenu addItem:createAccountMenuItem];
	
	[actionMenuItem setSubmenu:actionMenu];
		
	// Action Menu	
	var aboutMenuItem = [[CPMenuItem alloc] initWithTitle:@"About" action:@selector(toggleAboutWindow:) keyEquivalent:@"A"];
	
	// Arrange menu order
	[mainMenu addItem:actionMenuItem];
	[mainMenu addItem:[CPMenuItem separatorItem]];
	[mainMenu addItem:aboutMenuItem];
}


-(void)doNothing:(id)sender 
{
}

-(void)buyPictures:(id)sender 
{
}

-(void)createAccountAction:(id)sender 
{
}

-(void)toggleAboutWindow:(id)sender 
{
	// debugger;
	var sharedAboutController = [AboutController sharedAboutController];
	[sharedAboutController toggleAboutWindow:sender];
}

-(void)categoryChanged:(CPNotification)notification
{
	// debugger;
	var userinfo = [notification userInfo];
	var picturesArray = [userinfo objectForKey:@"pictures"];
	
	// Empty selection
	[photosCollectionView1 setSelectionIndexes:[CPIndexSet indexSet]];
	
	// Display the pictures for the category
	[photosCollectionView1 setContent:picturesArray];
}

@end