//*****************************************************************************
//
//	IdleAnimator.h.  	
//
//		Controls Idle panel animation
//
//			by	Felipe A. Rodriguez		
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//*****************************************************************************

#import "IdleAnimator.h"
#import "CommandScroll.h"
#import "Coordinator.h"
#import "SpaceView.h"




@interface IdleAnimator (Private)


static void IdleAnimate();						// called by timed entry events
	static DPSTimedEntry aniTimeTag;


@end
@implementation IdleAnimator

//*****************************************************************************
//
// 		designated initializer
//
//*****************************************************************************
 
- awakeFromNib
{
	imageList = [[List alloc] init];	// produce an array of image objects
	[imageList insertObject: [NXImage findImageNamed: "nomail"] at: 0];
	[imageList insertObject: [NXImage findImageNamed: "newmail1"] at: 1];
	[imageList insertObject: [NXImage findImageNamed: "newmail2"] at: 2];
				
    return self;
}
//*****************************************************************************
//
// 		cancel target -- do not end ppp session 
//
//*****************************************************************************

- cancel:sender
{
	return [[NXApp delegate] idleFinished];
}
//*****************************************************************************
//
// 		continue target -- end ppp session 
//
//*****************************************************************************

- continue:sender
{
	if([[[NXApp mainMenu] findCellWithTag:1] isEnabled])	// if ppp is up
		{
		[[[NXApp delegate] commandView] appendString:
								[[NXApp delegate] localString:
								"Dropping ppp Link due to idle timeout\n"]];
		[[NXApp delegate] disconnect:self];
		}

	return [[NXApp delegate] idleFinished];
}
//*****************************************************************************
//
// 		cleanup prior to zone destruction 
//
//*****************************************************************************

- cleanUp
{
	[self removeTimedEntry];
	[ITPanel close];
	if(wasHidden)										// if app was hidden
		[NXApp perform:@selector(hide:) with:[NXApp delegate] 
								  afterDelay:500 
							  cancelPrevious:YES];				

	return self;
}
//*****************************************************************************
//
// 		countdown to idle timeout
//
//*****************************************************************************
 
- countdown
{
int seconds;
static time_t ltime;					// time in seconds
static time_t preltime;					// previous time in seconds

	time(&ltime);						// Get time and place in time_t

	if(ltime > preltime)
		{
		if((seconds = [secTillTimeoutField intValue]) == 0)
			[self continue:self];
		else
			{
			[secTillTimeoutField setIntValue:(seconds - 1)];
			[secTillTimeoutField display];
			}
		preltime = ltime;
		}
		
    return self;
}
//*****************************************************************************
//
// 		set animation into motion
//
//*****************************************************************************
 
- startAnimTimer
{
	wasHidden =	[NXApp isHidden];							// app hidden?
	if(strcmp(NXGetDefaultValue([NXApp appName],"sound"), "YES") == 0) 
		[[[Sound findSoundFor: "Gong"] play: nil] free];
	if([[NXApp delegate] mailInQueue])
		mailIcon = YES;
	if(!aniTimeTag)  
		aniTimeTag = DPSAddTimedEntry(			// register function Animate
			0.08, 								// to be called every period of 		
			(DPSTimedEntryProc)IdleAnimate, 	// arg0
			(id)self, 
			NX_BASETHRESHOLD);
	[ITPanel center];
	[ITPanel makeKeyAndOrderFront:self];
	[NXApp perform:@selector(unhide:) with:self 			// unhide
								afterDelay:500 
							cancelPrevious:YES];				

	return self;
}
//*****************************************************************************
//
// 		icon animation
//
//*****************************************************************************
 
- _animate
{
static int Index = 0, divCntr = 0;
static BOOL reverse = NO;

	[self countdown];
	[sView display];		
	if(mailIcon && divCntr++ == 3)
		{
		divCntr = 0;						// slow mail animation down by 1/3
		[mailView setImage:[imageList objectAt:Index]]; 
		if(Index >= 2)						// reverse anim seq
			reverse = YES;
		if(Index <= 0)						// reverse anim seq
			reverse = NO;
		if(reverse)
			Index--;
		else
			Index++;
		}
			
	return self;
}
//*****************************************************************************
//
// 		remove the timed entry
//
//*****************************************************************************
 
- removeTimedEntry
{
	if(aniTimeTag)  
		DPSRemoveTimedEntry(aniTimeTag);
	aniTimeTag = 0;

	return self;
}
//*****************************************************************************
//
// 		free
//
//*****************************************************************************
 
- free
{
	NXPing();
	NXDestroyZone([self zone]);

	return self;
}

@end
@implementation IdleAnimator (Private)

//*****************************************************************************
//
//		This fucntion is registered by DPSaddtimedentry.
//		It is subsequently called every period t as registered  
//		in arg 0 of DPSaddtimedentry.
//
//*****************************************************************************
 
static void IdleAnimate(DPSTimedEntry time_tag, double now, id self)
{
	[self _animate];
}
@end
