#define main_m
//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#pragma mark Includes
#pragma mark -
#endif
//----------------------------------------------------------------------------------------------
#include "Common.h"
//----------------------------------------------------------------------------------------------

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>

#import "main.h"

//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#pragma mark Definitions
#pragma mark -
#endif
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#pragma mark Globals
#pragma mark -
#endif
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#pragma mark Prototypes
#pragma mark -
#endif
//----------------------------------------------------------------------------------------------

CFPropertyListRef CreateMyPropertyListFromFile( CFURLRef fileURL );

//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#endif
//----------------------------------------------------------------------------------------------

@implementation MyController

//----------------------------------------------------------------------------------------------

- (id) init
{
	if (self = [super init]) self.title = @"iFuntastic /dev/101";
	return self;
}

//----------------------------------------------------------------------------------------------

- (void) playSound: (int) which
{
	SystemSoundID	theSound = nil;
	NSString		*path, *name;
	char			cname[256];
	
	sprintf(cname, "sound%d", which);
	name = [[NSString alloc] initWithCString:cname];
	path = [[NSBundle mainBundle] pathForResource:name ofType:@"wav" inDirectory:@"/"];
	if (path) AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &theSound);

	if (theSound) // make it keep playing, even after the app terminates
	{
		UInt32	propertyData = 1;
		
		AudioServicesSetProperty 
		(
		   kAudioServicesPropertyCompletePlaybackIfAppDies,
		   sizeof(theSound),
		   &theSound,
		   sizeof(propertyData),
		   &propertyData
		);			
	}
	
	if (theSound)
		AudioServicesPlaySystemSound(theSound);
}

//----------------------------------------------------------------------------------------------

- (void) loadView
{
	contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	self.view = contentView;
    [contentView release];

	{
		id					path = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist" inDirectory:@"/"];
		CFURLRef			fileURL = (CFURLRef) [[NSURL alloc] initFileURLWithPath:path];
		CFPropertyListRef	propList = nil;
		
		if (fileURL)
		{
			propList = CreateMyPropertyListFromFile( fileURL );
			CFRelease(fileURL);
			
			if (propList)
			{
				NSDictionary	*dict = (NSDictionary *) propList;
				NSString		*name;
				
				name = [dict objectForKey:@"CFBundleDisplayName"];
				if (name)
				{
					self.title = name;
					//[name release];
				}
				
				CFRelease(propList);
			}
		}
	}
		
	stage = -1;
	[self changeBackground];
	
	self.navigationItem.leftBarButtonItem =	[[[UIBarButtonItem alloc]
											   initWithTitle:@"Tap" 
											   style:UIBarButtonItemStylePlain 
											   target:self 
											   action:@selector(changeBackground)] autorelease];
}

//----------------------------------------------------------------------------------------------

- (void) changeBackground
{
	++stage;

	if (stage == 0)			[contentView setImage:[UIImage imageNamed:@"image1.png"]];
	else if (stage == 1)	[contentView setImage:[UIImage imageNamed:@"image2.png"]];
//	else if (stage == 2)	[contentView setImage:[UIImage imageNamed:@"image3.png"]];
	else					{stage = -1; [self changeBackground];}

	[self playSound:stage];
}

@end

//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#endif
//----------------------------------------------------------------------------------------------

@implementation MyAppDelegate

//----------------------------------------------------------------------------------------------

- (void) applicationDidFinishLaunching:(UIApplication *) application 
{	
	UIWindow				*window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds] ];
	UINavigationController	*nav = [[UINavigationController alloc] initWithRootViewController:[[MyController alloc] init] ];
	
	[window addSubview:nav.view];
	[window makeKeyAndVisible];
}

@end

//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#pragma mark Main
#pragma mark -
#endif
//----------------------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
	NSAutoreleasePool	*pool = [[NSAutoreleasePool alloc] init];
	int					retVal = UIApplicationMain(argc, argv, nil, @"MyAppDelegate");
	
	[pool release];
	return retVal;
}

//----------------------------------------------------------------------------------------------
#if 0 
#pragma mark -
#pragma mark Utilities
#pragma mark -
#endif
//------------------------------------------------------------------------------------------

CFPropertyListRef CreateMyPropertyListFromFile( CFURLRef fileURL ) 
{
   CFPropertyListRef propertyList;
   CFStringRef       errorString;
   CFDataRef         resourceData;
   Boolean           status;
   SInt32            errorCode;
 
   // Read the XML file.
   status = CFURLCreateDataAndPropertiesFromResource(
               kCFAllocatorDefault,
               fileURL,
               &resourceData,            // place to put file data
               NULL,
               NULL,
               &errorCode);
 
   // Reconstitute the dictionary using the XML data.
   propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
               resourceData,
               kCFPropertyListMutableContainersAndLeaves, //kCFPropertyListImmutable,
               &errorString);
 
	if (resourceData) 
	{
        CFRelease( resourceData );
	}
    else 
	{
        CFRelease( errorString );
    }
   
   return propertyList;
}

