#import "HowObject.h"

@implementation HowObject

	// Global Varibales
	int TOTAL = 0;
	int i; // dummy variable 
	int X; // X screen coordinate
	int Y; // Y screen coordinate


        

- (void)applicationDidFinishLaunching:(NSNotification *)notification
// In order for your object to receive this notification make sure that
// your object is a delegate or "File's Owner" in IB.
// Connected from "File's Owner" to HowObject in this case.
{

    NSTimer *aTimer;

    // Activate the application
    [NSApp activateIgnoringOtherApps:YES];

    // Displays the Info Panel
    [InfoPanel center];
    [InfoPanel makeKeyAndOrderFront:NULL];


    // Registers a new timer with the NSRunLoop object
    // DisplayTime is the amount of time the panel will be displayed
    // CloseInfoPanel is the method executed when the timer "rings"
    // userInfo:nil is NOT used at this time
    aTimer = [NSTimer scheduledTimerWithTimeInterval:(10) target:self  selector:@selector(CloseInfoPanel:)  userInfo:nil  repeats:NO]; 

    	// Selection Card
        [Image1 setImage:[NSImage imageNamed:@"Image1"]];
        [Image2 setImage:[NSImage imageNamed:@"Image2"]];
        [Image3 setImage:[NSImage imageNamed:@"Image3"]];
        [Image4 setImage:[NSImage imageNamed:@"Image4"]];
        [Image5 setImage:[NSImage imageNamed:@"Image5"]];
        [Image6 setImage:[NSImage imageNamed:@"Image6"]];
        [Image7 setImage:[NSImage imageNamed:@"Image7"]];
        [Image8 setImage:[NSImage imageNamed:@"Image8"]];
        [Image9 setImage:[NSImage imageNamed:@"Image9"]];
        [Image10 setImage:[NSImage imageNamed:@"Image10"]];
        [Image11 setImage:[NSImage imageNamed:@"Image11"]];
        [Image12 setImage:[NSImage imageNamed:@"Image12"]];
        [Image13 setImage:[NSImage imageNamed:@"Image13"]];
        [Image14 setImage:[NSImage imageNamed:@"Image14"]];
        [Image15 setImage:[NSImage imageNamed:@"Image15"]];

        // Card One
        [Image1_C1 setImage:[NSImage imageNamed:@"Image1"]];
        [Image3_C1 setImage:[NSImage imageNamed:@"Image3"]];
        [Image5_C1 setImage:[NSImage imageNamed:@"Image5"]];
        [Image7_C1 setImage:[NSImage imageNamed:@"Image7"]];
        [Image9_C1 setImage:[NSImage imageNamed:@"Image9"]];
        [Image11_C1 setImage:[NSImage imageNamed:@"Image11"]];
        [Image13_C1 setImage:[NSImage imageNamed:@"Image13"]];
        [Image15_C1 setImage:[NSImage imageNamed:@"Image15"]];

        // Card Two
        [Image15_C2 setImage:[NSImage imageNamed:@"Image15"]];
        [Image2_C2 setImage:[NSImage imageNamed:@"Image2"]];
        [Image14_C2 setImage:[NSImage imageNamed:@"Image14"]];
        [Image6_C2 setImage:[NSImage imageNamed:@"Image6"]];
        [Image7_C2 setImage:[NSImage imageNamed:@"Image7"]];
        [Image10_C2 setImage:[NSImage imageNamed:@"Image10"]];
        [Image11_C2 setImage:[NSImage imageNamed:@"Image11"]];
        [Image3_C2 setImage:[NSImage imageNamed:@"Image3"]];

        // Card Three
        [Image14_C3 setImage:[NSImage imageNamed:@"Image14"]];
        [Image5_C3 setImage:[NSImage imageNamed:@"Image5"]];
        [Image6_C3 setImage:[NSImage imageNamed:@"Image6"]];
        [Image7_C3 setImage:[NSImage imageNamed:@"Image7"]];
        [Image15_C3 setImage:[NSImage imageNamed:@"Image15"]];
        [Image13_C3 setImage:[NSImage imageNamed:@"Image13"]];
        [Image4_C3 setImage:[NSImage imageNamed:@"Image4"]];
        [Image12_C3 setImage:[NSImage imageNamed:@"Image12"]];

        // Card Four
        [Image10_C4 setImage:[NSImage imageNamed:@"Image10"]];
        [Image9_C4 setImage:[NSImage imageNamed:@"Image9"]];
        [Image8_C4 setImage:[NSImage imageNamed:@"Image8"]];
        [Image15_C4 setImage:[NSImage imageNamed:@"Image15"]];
        [Image12_C4 setImage:[NSImage imageNamed:@"Image12"]];
        [Image14_C4 setImage:[NSImage imageNamed:@"Image14"]];
        [Image13_C4 setImage:[NSImage imageNamed:@"Image13"]];
        [Image11_C4 setImage:[NSImage imageNamed:@"Image11"]];

        // Displays the Selection Window
        [Select center];
        [Select makeKeyAndOrderFront:NULL];

}




- (void)IntroAndHelp:(id)sender
{
    if (![NSBundle loadNibNamed:@"HelpIntro" owner:self])  {
        NSLog(@"Failed to load HelpIntro.nib");
        NSBeep();
        return;
        }

}

- (void)MoreInfo:(id)sender
{
    if (![NSBundle loadNibNamed:@"MoreInfo" owner:self])  {
        NSLog(@"Failed to load MoreInfo.nib");
        NSBeep();
        return;
        }

}


- (void)Chosen:(id)sender
// This is for the button is the Selection Window once the user has chosen a number
// he will click on this button
// Drop Select window at the bottom of the screen.
{
	// Local Variables
        NSRect frame;
    	TOTAL = 0;

        frame = [Select frame]; // Gets current window coordinates (position)
        X = NSMinX(frame); // Figures out the current X coordinates (Vertical)
        Y = NSMinY(frame); // Figures out the current Y coordinates (Horizontal)


	// Loops the window all the way down the screen out of sight
        // The windows actual size is 432 so we drop it to -435 below 0 (end of screen)
      	for (i = Y; i > -435; i = i - 10) {
            [Select setFrameOrigin:NSMakePoint((X), (i))];
        }

      	[Select close];  // Closes the window (Even if theres no close button on the window)
      	[Select center]; // Re-position the Window at the center of the screen

	// End of Drop for Selection Window

        [CardOne center]; // Repositions the Window at the center of the screen
        [CardOne makeKeyAndOrderFront:NULL]; // Display CardOne window on the screen
}

- (void)DropCardOne:(id)sender
// "Drops" CardOne window to the left of the screen.
{
    	 
    	// Local Variables
    	NSRect frame;

        frame = [CardOne frame]; // Gets current window coordinates (position)
    	X = NSMinX(frame); // Figures out the current X coordinates (Vertical)
    	Y = NSMinY(frame); // Figures out the current Y coordinates (Horizontal)
        
          // Loops the window all the way left the screen out of sight
          // The windows actual size is 354 so we drop it to -354 below 0 (edge of screen)
          for (i = X; i > -354; i = i - 10) {
              [CardOne setFrameOrigin:NSMakePoint((i), (Y))]; // Moves the window all the way to the left
                                        }
 

          [CardOne close];  // Closes the window (Even if theres no close button on the window)
          [CardOne center]; // Repositions the Window at the center of the screen

          [CardTwo center]; // Repositions the Window at the center of the screen
          [CardTwo makeKeyAndOrderFront:NULL]; // Display CardTwo window on the screen

    	 // End of Drop for CardOne Window
}

- (void)DropCardTwo:(id)sender
// Drop CardTwo window at the bottom of the screen.
{

    	// Local Variables
	NSRect windowframe;
    	NSRect screenframe = [[NSScreen  mainScreen]  frame]; 
        float height = screenframe.size.height; // Assigns the screen resolution to height


        windowframe = [CardTwo frame]; // Gets current window coordinates (position)
        X = NSMinX(windowframe);       // Figures out the current X coordinates (Vertical)
        Y = NSMinY(windowframe);       // Figures out the current Y coordinates (Horizontal)

           for (i = Y; i > -435; i=i-10) {
                [CardTwo setFrameOrigin:NSMakePoint((X), (i))];}
 // Moves the window down


            Y = -435;  // Window is at buttom of screen

           // height is the mainScreen height (screen resolution)
           for (i = Y; i < height; i=i+7) {
                [CardTwo setFrameOrigin:NSMakePoint((X), (i))];}
 // Moves the window upwards


            for (i = X; i > -440; i=i-5) {
                [CardTwo setFrameOrigin:NSMakePoint((i), (height))];}
 // Moves window to the left
           
          [CardTwo close]; // Closes the window (Even if theres no close button on the window)
          [CardTwo center]; // Repositions the Window at the center of the screen

          [CardThree center]; // Repositions the Window at the center of the screen
          [CardThree makeKeyAndOrderFront:NULL]; // Display CardThree window on the screen

    // end of Drop CardTwo panel

}

- (void)DropCardThree:(id)sender
// Drop CardThree window at the bottom of the screen.
{

      // Local Variables
      NSRect windowframe;
      NSRect screenframe = [[NSScreen  mainScreen]  frame];
      float width = screenframe.size.width; // Assigns the screen resolution to height


      windowframe = [CardThree frame]; // Gets current window coordinates (position)
      X = NSMinX(windowframe);         // Figures out the current X coordinates (Vertical)
      Y = NSMinY(windowframe);         // Figures out the current Y coordinates (Horizontal)

          [CardThree makeKeyWindow]; // Selects the window
          [CardThree orderFront:sender]; // Places the window in front

          for (i = X; i < width; i=i+20) {
                [CardThree setFrameOrigin:NSMakePoint((i), (Y))];} // Moves window to the right of the screen


         [CardThree close];  // Closes the window (Even if theres no close button on the window)
         [CardThree center]; // Repositions the Window at the center of the screen

	[CardFour center]; // Repositions the Window at the center of the screen
	[CardFour makeKeyAndOrderFront:NULL]; // Display CardThree window on the screen

    // end of Drop CardThree panel

}



- (void)DropCardFour:(id)sender
// Drop CardFour window at the bottom of the screen. 
{

          // Local Variables
          NSRect windowframe;

          windowframe = [CardFour frame]; // Gets current window coordinates (position)
          X = NSMinX(windowframe);         // Figures out the current X coordinates (Vertical)
          Y = NSMinY(windowframe);         // Figures out the current Y coordinates (Horizontal)

          for (i = Y; i > -435; i=i-5)
          {
 [CardFour setFrameOrigin:NSMakePoint((X), (i))];  // Moves window down
             [CardFour setFrameOrigin:NSMakePoint((X=X-5), (i))]; }
 // Moves window to the left
          
             [CardFour close];  // Closes the window (Even if theres no close button on the window)
             [CardFour center]; // Repositions the Window at the center of the screen

             [Result center]; // Repositions the Window at the center of the screen
             [Result makeKeyAndOrderFront:NULL]; // Display Result window on the screen

    // end of Drop CardFour panel
}

- (void)FourNo:(id)sender
// CardFour answer NO 
{
        [self DropCardFour:sender]; // Drop the card method
        [self WhichImage:sender];
}

- (void)FourYes:(id)sender
// CardFour answer YES 
{
        [self DropCardFour:sender]; // Drop the card method
        TOTAL=TOTAL + 8;
        [self WhichImage:sender];
}


- (void)OneNo:(id)sender
// CardOne answer NO
{
        [self DropCardOne:sender]; // Drop the card method
        [CardTwo orderFront:sender];
}

- (void)OneYes:(id)sender
// CardOne answer YES
{
        [self DropCardOne:sender]; // Drop the card method
        TOTAL=TOTAL + 1;
        [CardTwo orderFront:sender];
}

- (void)StartHow:(id)sender
// This is used by the menu item How
{
    [Result close]; // Close the Result panel
    [ResultImage setImage:[NSImage imageNamed:@"Empty.tiff"]]; // places an empty image to clean-up other images
    [Select orderFront:sender]; // Selection panel "Let's start"
    
}

- (void)ThreeNo:(id)sender
// CardOne answer NO
{
        [self DropCardThree:sender]; // Drop the card method
        [CardFour orderFront:sender];
}

- (void)ThreeYes:(id)sender
// CardThree answer YES
{
        [self DropCardThree:sender]; // Drop the card method
        TOTAL=TOTAL + 4;
        [CardFour orderFront:sender];
}

- (void)TwoNo:(id)sender
// CardTwo answer NO
{
        [self DropCardTwo:sender]; // Drop the card method
        [CardThree orderFront:sender];
}

- (void)TwoYes:(id)sender
// CardTwo answer YES
{
        [self DropCardTwo:sender]; // Drop the card method
        TOTAL=TOTAL + 2;
        [CardThree orderFront:sender];
}

- (void)WhichImage:(id)sender
// Assigns the number total with the proper image
{
      // Local Variables
      NSRect windowframe;
      NSRect screenframe = [[NSScreen  mainScreen]  frame];
      float width = screenframe.size.width; // Assigns the screen resolution to width

      windowframe = [Result frame]; // Gets current window coordinates (position)
      X = NSMinX(windowframe);         // Figures out the current X coordinates (Vertical)
      Y = NSMinY(windowframe);         // Figures out the current Y coordinates (Horizontal)

       [ResultImage setImage:[NSImage imageNamed:@"Eric"]];
       [ResultImage display];

      for (i = Y; i > 4; i=i-10) {
            [Result setFrameOrigin:NSMakePoint((X), (i))];
} // Moves the window down

       [ResultImage setImage:[NSImage imageNamed:@"Image5"]];
       [ResultImage display];

      for (i = X; i > 5; i=i-10) {
            [Result setFrameOrigin:NSMakePoint((i), (0))];}
 // Move window to the left
      
    [self ResultAnimation:sender];
    [self ResultAnimation:sender];
    [self ResultAnimation:sender];

    for (i = 5; i < (width/2-137); i=i+10) {
            [Result setFrameOrigin:NSMakePoint((i), (4))];} // Moves window to the right

      Y = 4; // Window is at buttom of screen
      for (i = Y; i <430; i=i+10) {
          [Result setFrameOrigin:NSMakePoint(((width/2-137)), (i))];}
// Moves the window upwards


   switch (TOTAL)  { // Start switch

   case 1:
       [ResultImage setImage:[NSImage imageNamed:@"Image1"]];
          break;

   case 2:
       [ResultImage setImage:[NSImage imageNamed:@"Image2"]];
          break;

   case 3:
       [ResultImage setImage:[NSImage imageNamed:@"Image3"]];
            break;

   case 4:
       [ResultImage setImage:[NSImage imageNamed:@"Image4"]];
            break;

   case 5:
       [ResultImage setImage:[NSImage imageNamed:@"Image5"]];
            break;

   case 6:
       [ResultImage setImage:[NSImage imageNamed:@"Image6"]];
            break;

   case 7:
            [ResultImage setImage:[NSImage imageNamed:@"Image7"]];
            break;

   case 8:
       [ResultImage setImage:[NSImage imageNamed:@"Image8"]];
            break;

   case 9:
       [ResultImage setImage:[NSImage imageNamed:@"Image9"]];
            break;

   case 10:
       [ResultImage setImage:[NSImage imageNamed:@"Image10"]];
            break;

   case 11:
       [ResultImage setImage:[NSImage imageNamed:@"Image11"]];
            break;

   case 12:
       [ResultImage setImage:[NSImage imageNamed:@"Image12"]];
            break;

   case 13:
       [ResultImage setImage:[NSImage imageNamed:@"Image13"]];
            break;

   case 14:
       [ResultImage setImage:[NSImage imageNamed:@"Image14"]];
            break;

   case 15:
       [ResultImage setImage:[NSImage imageNamed:@"Image15"]];
            break;

   default:
       [ResultImage setImage:[NSImage imageNamed:@"Image16"]]; // if the user does not answer properly
           break;

                                } // end switch

}

- (void)ResultAnimation:(id)sender
// This is the amimation for the Result panel
{

    // Local Variables
    NSRect windowframe;
    NSRect screenframe = [[NSScreen  mainScreen]  frame];
    float width = screenframe.size.width; // Assigns the screen resolution to width
    float height = screenframe.size.height; // Assigns the screen resolution to height


    windowframe = [Result frame]; // Gets current window coordinates (position)
    X = NSMinX(windowframe);         // Figures out the current X coordinates (Vertical)
    Y = NSMinY(windowframe);         // Figures out the current Y coordinates (Horizontal)

         [ResultImage setImage:[NSImage imageNamed:@"Image7"]];
         [ResultImage display];

        Y = 4; // Window is at bottom of screen
        for (i = Y; i < height; i=i+10) {
            [Result setFrameOrigin:NSMakePoint((5), (i))];} 
// Moves the window up
 
           [ResultImage setImage:[NSImage imageNamed:@"Image6"]];
           [ResultImage display];
     
           for (i = 5; i < (width-275); i=i+10) {
               [Result setFrameOrigin:NSMakePoint((i), (height))];} // Moves window to the right

           [ResultImage setImage:[NSImage imageNamed:@"Image9"]];
           [ResultImage display];

        Y = height; // Window is at the top of the screen
        for (i = Y; i > 4; i=i-10) {
            [Result setFrameOrigin:NSMakePoint(((width-275)), (i))];
} // Moves the window down

            [ResultImage setImage:[NSImage imageNamed:@"Image15"]];
            [ResultImage display];

            for (i = (width-275); i > 5; i=i-10) {
            [Result setFrameOrigin:NSMakePoint((i), (4))];}
 // Moves the window to the left
            [ResultImage setImage:[NSImage imageNamed:@"Image6"]];
            [ResultImage display];

}


- CloseInfoPanel:(id)timer
{
        // Closes the InfoPanel
        [InfoPanel orderOut:self];


    return self;
}





@end
