#define ALLOW_ACCESS_TO_INTERNALS_OF_MENUS

#include <PalmOS.h>
#include "SonyChars.h"
#include "SonyKeyMgr.h"
#include "T-Suite.h"

Err WbMnJogEventPreHandle (SysNotifyParamType *notifyParamsP)
{
	EventType *event = (EventType*)notifyParamsP->notifyDetailsP;

// is keyDownEvent
	if (event->eType != Swap16 (keyDownEvent))
		return errNone;

// is JogEvent
	switch (Swap16 (event->data.keyDown.chr))
	{
	case vchrJogUp:
	case vchrJogDown:
	case vchrJogPushRepeat:
	case vchrJogPushedUp:
	case vchrJogPushedDown:
	case vchrJogPush:
	case vchrJogRelease:
	case vchrJogBack:
	case vchrJogLeft:
	case vchrJogRight:
		break;
	default:
		return errNone;
	}

// get Feature Value and Set Temp Feature Value
	UInt32 ftrValue;
	FtrGet (appFileCreator, ftr_appStatus, &ftrValue);
	TSuitePreferenceType *prefsP = (TSuitePreferenceType*)ftrValue;

	TSuitePreferenceType prefs;
		MemMove (&prefs, prefsP, sizeof (TSuitePreferenceType));

// check JogPush. if true then update Feature Value (FiveWayPushDetected)
	switch (Swap16 (event->data.keyDown.chr))
	{
	case vchrJogUp:
	case vchrJogDown:
	case vchrJogPush:
	case vchrJogBack:
	case vchrJogLeft:
	case vchrJogRight:
		prefs.JogPushDetected = IsOn;

		if (!prefsP->JogPushDetected)
			DmWrite (prefsP, 0, &prefs, sizeof (TSuitePreferenceType));

		break;
	}

	// if Jog Push was not detected. change FiveWayEvent to nilEvent.
	if (!prefs.JogPushDetected)
	{
		MemSet (event, sizeof (EventType), 0);
		return errNone;
	}

// check JogConbination.
	MenuBarType *menuP;

	menuP = MenuGetActiveMenu();

	if (event->data.keyDown.chr == vchrJogPush)
	{
		if (menuP != NULL && menuP->attr.visible)
			return errNone;

		if (MenuCmdBarGetButtonData (0, NULL, NULL, NULL, NULL))
			return errNone;

		MemSet (event, sizeof (EventType), 0);
		EvtFlushKeyQueue ();
		return errNone;
	}

	if (event->data.keyDown.chr == vchrJogRelease)
	{
		if (menuP != NULL && menuP->attr.visible)
			return errNone;

		if (MenuCmdBarGetButtonData (0, NULL, NULL, NULL, NULL))
			return errNone;

		event->data.keyDown.chr = vchrJogPush;
		EvtFlushKeyQueue ();
		EvtEnqueueKey (vchrJogRelease, 0, commandKeyMask);
		goto SelectLeftHandled;
	}

//	if (event->data.keyDown.chr == vchrJogBack)
//	{
//		prefs.JogPushDetected = IsOff;
//		DmWrite (prefsP, 0, &prefs, sizeof (TSuitePreferenceType));
//		return;
//	}

	// if JogPushedUp, then CommandToolBar Open.
	if (Swap16 (event->data.keyDown.chr) == vchrJogPushedDown)
	{
		if (menuP != NULL && menuP->attr.visible)
			return errNone;

		if (MenuCmdBarGetButtonData (0, NULL, NULL, NULL, NULL))
			return errNone;

		UInt32 prvTimeOut = TimGetTicks () + (SysTicksPerSecond () / 5);
		do
		{
		}while (prvTimeOut > TimGetTicks ());

		EvtFlushKeyQueue ();

		EvtEnqueueKey (vchrMenu, 0, commandKeyMask);
		goto SelectUpDownHandled;
	}

	// if JogPushedUp, then CommandToolBar Open.
	if (Swap16 (event->data.keyDown.chr) == vchrJogPushedUp)
	{
		menuP = MenuGetActiveMenu();

		if (menuP != NULL && menuP->attr.visible)
			return errNone;

		UInt32 prvTimeOut = TimGetTicks () + (SysTicksPerSecond () / 5);
		do
		{
		}while (prvTimeOut > TimGetTicks ());

		EvtFlushKeyQueue ();

		EvtEnqueueKey (vchrCommand, 0, commandKeyMask);
		goto SelectUpDownHandled;
	}

	return errNone;

SelectUpDownHandled:
	MemSet (event, sizeof (EventType), 0);

SelectLeftHandled:
	prefs.JogPushDetected = IsOff;
	DmWrite (prefsP, 0, &prefs, sizeof (TSuitePreferenceType));

	return errNone;
}