void MessageHandler::handleMoSyncMessage(
	Wormhole::MessageStream& message,
	MAWidgetHandle webViewHandle,
	Wormhole::HybridMoblet* moblet)
{
	const char* p = message.getNext();
	const char* logMessage;

	if (0 == strcmp(p, "ExitApplication"))
	{
		// Exit the application.
		moblet->exit();
	}
	else if (0 == strcmp(p, "SendToBackground"))
	{
		maSendToBackground();
	}
	else if (0 == strcmp(p, "SysLog"))
	{
		logMessage = message.getNext();
		maWriteLog(logMessage, strlen(logMessage));
	}
	else if (0 == strcmp(p, "OpenExternalURL"))
	{
		maPlatformRequest(message.getNext());
	}
	else if (0 == strcmp(p, "OpenWormhole"))
	{
		moblet->openWormhole(webViewHandle);
	}
}
void ImageScreen::keyPressEvent(int keyCode) {
	if (strcmp(Card::ORIENTATION_LANDSCAPE, card->getOrientation().c_str()) == 0) {
		//if it is a landscape card, the orientation must be switched
		switch(keyCode) {
			case MAK_UP:
			case MAK_DOWN:
				keyCode = MAK_LEFT;
				break;
			case MAK_LEFT:
				keyCode = MAK_DOWN;
				break;
			case MAK_RIGHT:
				keyCode = MAK_UP;
				break;
		}
	}

	switch (keyCode) {
		case MAK_LEFT:
		case MAK_RIGHT:
			imge->refreshWidget();
			imge->statAdded = false;
			currentSelectedStat = -1;

			flip=!flip;
			if (imge->getResource() != NULL) {
				maDestroyObject(imge->getResource());
			}
			imge->setResource(Util::loadImageFromResource(RES_LOADING1));
			imge->update();
			imge->requestRepaint();
			maUpdateScreen();
			if (flip) {
				Util::retrieveBack(imge, card, height-PADDING*2, imageCacheBack);
				Util::retrieveFront(NULL, card, height-PADDING*2, imageCacheFront);
			} else {
				Util::retrieveFront(imge, card, height-PADDING*2, imageCacheFront);
				Util::retrieveBack(NULL, card, height-PADDING*2, imageCacheBack);
			}
			currentSelectedStat = -1;
			break;
		case MAK_UP:
			if (imge->getResource() != RES_TEMP) {
				if(card->getStats().size()>0){
					selectStat(-1);
					if (currentSelectedStat == -1) {
						if (screenType == ST_NEW_CARD) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Accept":"", "Reject", "Flip", layout);
						} else if (screenType == Util::AT_SHARE) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Share":"", "Back", "Flip", layout);
						} else {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Options":"", "Back", "Flip", layout);
						}
						imge->refreshWidget();
						imge->statAdded = false;
					} else {
						if (screenType == ST_NEW_CARD) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Accept":"", "Reject", "Flip", layout);
						} else if (screenType == Util::AT_SHARE) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Share":"", "Back", "Flip", layout);
						} else {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Options":"", "Back", "Flip", layout);
						}
						imge->refreshWidget();
						if(flip==card->getStats()[currentSelectedStat]->getFrontOrBack()){
							imge->selectStat(card->getStats()[currentSelectedStat]->getLeft(),card->getStats()[currentSelectedStat]->getTop(),
									card->getStats()[currentSelectedStat]->getWidth(),card->getStats()[currentSelectedStat]->getHeight(),
									card->getStats()[currentSelectedStat]->getColorRed(), card->getStats()[currentSelectedStat]->getColorGreen(),
									card->getStats()[currentSelectedStat]->getColorBlue(), MobImage::PORTRAIT);
						}
					}
				}
			}
			break;
		case MAK_DOWN:
			if (imge->getResource() != RES_TEMP) {
				if(card->getStats().size()>0){
					selectStat(1);
					if (currentSelectedStat == -1) {
						if (screenType == ST_NEW_CARD) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Accept":"", "Reject", "Flip", layout);
						} else if (screenType == Util::AT_SHARE) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Share":"", "Back", "Flip", layout);
						} else {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Options":"", "Back", "Flip", layout);
						}
						imge->refreshWidget();
						imge->statAdded = false;
					} else {
						if (screenType == ST_NEW_CARD) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Accept":"", "Reject", "Flip", layout);
						} else if (screenType == Util::AT_SHARE) {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Share":"", "Back", "Flip", layout);
						} else {
							Util::updateSoftKeyLayout((hasConnection&&canAuction)?"Options":"", "Back", "Flip", layout);
						}
						imge->refreshWidget();
						if(flip==card->getStats()[currentSelectedStat]->getFrontOrBack()){
							imge->selectStat(card->getStats()[currentSelectedStat]->getLeft(),card->getStats()[currentSelectedStat]->getTop(),
									card->getStats()[currentSelectedStat]->getWidth(),card->getStats()[currentSelectedStat]->getHeight(),
									card->getStats()[currentSelectedStat]->getColorRed(), card->getStats()[currentSelectedStat]->getColorGreen(),
									card->getStats()[currentSelectedStat]->getColorBlue(), MobImage::PORTRAIT);
						} else {
							while((flip!=card->getStats()[currentSelectedStat]->getFrontOrBack())&&(currentSelectedStat <= card->getStats().size())){
								selectStat(1);
							}
							imge->selectStat(card->getStats()[currentSelectedStat]->getLeft(),card->getStats()[currentSelectedStat]->getTop(),
																card->getStats()[currentSelectedStat]->getWidth(),card->getStats()[currentSelectedStat]->getHeight(),
																card->getStats()[currentSelectedStat]->getColorRed(), card->getStats()[currentSelectedStat]->getColorGreen(),
																card->getStats()[currentSelectedStat]->getColorBlue(), MobImage::PORTRAIT);
						}
					}
				}
			}
			break;
		case MAK_SOFTLEFT:
			if (screenType == ST_NEW_CARD) {
				busy = true;
				acceptCard();
			} else if (screenType == Util::AT_SHARE) {
				if (next != NULL) {
					feed->remHttp();
					delete next;
				}
				next = new TradeFriendDetailScreen(this, feed, card);
				next->show();
			} else if (screenType == ST_VIEW_CARD) {

			} else {
				if (card != NULL && hasConnection && canAuction) {
					if (next != NULL) {
						feed->remHttp();
						delete next;
					}
					next = new OptionsScreen(feed,
							OptionsScreen::ST_CARD_OPTIONS, this, card);
					next->show();
				}
			}
			break;
		case MAK_BACK:
		case MAK_SOFTRIGHT:
			if (screenType == ST_NEW_CARD) {
				busy = true;
				rejectCard();
			}
			else {
				previous->show();
			}
			break;
		case MAK_FIRE:
			if (card != NULL) {
				if(((flipOrSelect && tapped)&&(currentSelectedStat == -1)) ||
						(!tapped && currentSelectedStat == -1)){
					flip=!flip;
					imge->refreshWidget();
					imge->statAdded = false;
					currentSelectedStat = -1;
					if (imge->getResource() != NULL) {
						maDestroyObject(imge->getResource());
					}
					imge->setResource(Util::loadImageFromResource(RES_LOADING1));
					imge->update();
					imge->requestRepaint();
					maUpdateScreen();
					if (flip) {
						if ((imageCacheBack != NULL)&&(imge != NULL)) {
							Util::retrieveBack(imge, card, height-PADDING*2, imageCacheBack);
							Util::retrieveFront(NULL, card, height-PADDING*2, imageCacheFront);
						}
					} else {
						if ((imageCacheFront != NULL)&&(imge != NULL)) {
							Util::retrieveFront(imge, card, height-PADDING*2, imageCacheFront);
							Util::retrieveBack(NULL, card, height-PADDING*2, imageCacheBack);
						}
					}
					flipOrSelect=0;
					currentSelectedStat = -1;
					tapped = false;
				}else{
					if (imge->getResource() != RES_TEMP) {
						if(currentSelectedStat>-1){
							if(flip==card->getStats()[currentSelectedStat]->getFrontOrBack()){
								imge->refreshWidget();
								imge->selectStat(card->getStats()[currentSelectedStat]->getLeft(),card->getStats()[currentSelectedStat]->getTop(),
										card->getStats()[currentSelectedStat]->getWidth(),card->getStats()[currentSelectedStat]->getHeight(),
										card->getStats()[currentSelectedStat]->getColorRed(), card->getStats()[currentSelectedStat]->getColorGreen(),
										card->getStats()[currentSelectedStat]->getColorBlue(), MobImage::PORTRAIT);
								Stat *stat = card->getStats()[currentSelectedStat];
								if ((strcmp(stat->getDesc().c_str(), "Mobile No") == 0)||
									(strcmp(stat->getDesc().c_str(), "Tel No") == 0) ){
									if (next != NULL) {
										feed->remHttp();
										delete next;
										next == NULL;
									}
									next = new OptionsScreen(feed, OptionsScreen::ST_NUMBER_OPTIONS, this, card, stat->getDisplay());
									next->show();
								} else if (strcmp(stat->getDesc().c_str(), "Email") == 0) {
									String email = stat->getDisplay();
									int ret = maPlatformRequest(("mailto:"+email).c_str());
									if (ret < 0 ) {
										ret = maPlatformRequest(email.c_str());
									}
									if (ret < 0) {
										MenuScreen *confirmation = new MenuScreen(RES_BLANK, "Feature currently not supported on device.");
										confirmation->setMenuWidth(170);
										confirmation->setMarginX(5);
										confirmation->setMarginY(5);
										confirmation->setDock(MenuScreen::MD_CENTER);
										confirmation->setListener(this);
										confirmation->setMenuFontSel(Util::getDefaultFont());
										confirmation->setMenuFontUnsel(Util::getDefaultFont());
										confirmation->setMenuSkin(Util::getSkinDropDownItem());
										confirmation->addItem("Ok");
										confirmation->show();
									}
								}
								else if ((strcmp(stat->getDesc().c_str(), "Web Address") == 0)||
										(strcmp(stat->getDesc().c_str(), "Social Media Link 1") == 0)||
										(strcmp(stat->getDesc().c_str(), "Social Media Link 1") == 0)||
										(strcmp(stat->getDesc().c_str(), "Social Media Link 1") == 0)||
										(strcmp(stat->getDesc().c_str(), "Social Media Link 1") == 0)||
										(strcmp(stat->getDesc().c_str(), "Social Media Link 1") == 0)) {
									String url = stat->getDisplay();
									//maPlatformRequest will only work if the url starts with http://
									//so we need to check for it, and add it if it isnt there
									if (url.find("http://") != 0) {
										url = "http://"+url;
									}
									int ret = maPlatformRequest(url.c_str());
									if (ret < 0) {
										MenuScreen *confirmation = new MenuScreen(RES_BLANK, "Feature currently not supported on device.");
										confirmation->setMenuWidth(170);
										confirmation->setMarginX(5);
										confirmation->setMarginY(5);
										confirmation->setDock(MenuScreen::MD_CENTER);
										confirmation->setListener(this);
										confirmation->setMenuFontSel(Util::getDefaultFont());
										confirmation->setMenuFontUnsel(Util::getDefaultFont());
										confirmation->setMenuSkin(Util::getSkinDropDownItem());
										confirmation->addItem("Ok");
										confirmation->show();
									}
								}
							}
						}
					}
				}
			} else {
				previous->show();
			}
			break;
	}
}
void OptionsScreen::keyPressEvent(int keyCode) {
	int ind = listBox->getSelectedIndex();
	int max = listBox->getChildren().size();
	Widget *currentSoftKeys = mainLayout->getChildren()[mainLayout->getChildren().size() - 1];
	switch(keyCode) {
		case MAK_FIRE:
			if(currentSoftKeys->getChildren()[0]->isSelected()){
				keyPressEvent(MAK_SOFTLEFT);
				break;
			}else if(currentSoftKeys->getChildren()[2]->isSelected()){
				keyPressEvent(MAK_SOFTRIGHT);
				break;
			}
		case MAK_SOFTLEFT:
			index = listBox->getSelectedIndex();
			switch(screenType) {
				case ST_TRADE_OPTIONS:
					if(index == 0) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new AuctionCreateScreen(this, feed, card);
						next->show();
					} else if(index == 1) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new TradeFriendDetailScreen(this, feed, card);
						next->show();
					}
					break;
				case ST_AUCTION_OPTIONS:
					if(index == 0) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new ShopCategoriesScreen(this, feed, ShopCategoriesScreen::ST_AUCTIONS);
						next->show();
					}
					else if (index == 1) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new AuctionListScreen(this, feed, AuctionListScreen::ST_USER);
						next->show();
					}
					break;
				case ST_PLAY_OPTIONS:
					if(index == 0 && !connError) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new AlbumLoadScreen(this, feed, AlbumLoadScreen::ST_PLAY);
						next->show();
					}
					else if (index == 1 && !connError) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new AlbumLoadScreen(this, feed, AlbumLoadScreen::ST_GAMES, album);
						next->show();
					}
					break;
				case ST_GAME_OPTIONS:
					if(index == 0) {
						origMenu->show();
					}
					else if (index == 1) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new GameDetailsScreen(feed, GameDetailsScreen::ST_GAME_LOG);
						next->show();
					}
					break;
				case ST_NEW_GAME_OPTIONS:
					if (index == 0) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new GamePlayScreen(this, feed, true, number, "1", false, deckId);
						next->show();
					}
					else if (index == 1) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new GamePlayScreen(this, feed, true, number, "2", false, deckId);
						next->show();
					}
					else if (index == 2) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new GamePlayScreen(this, feed, true, number, "2", true, deckId);
						next->show();
					}
					else if (index == 3) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new AlbumLoadScreen(this, feed, AlbumLoadScreen::ST_LOBBY, album,false,NULL,number);
						((AlbumLoadScreen*)next)->setDeckId(deckId);
						next->show();
					}
					break;
				case ST_CARD_OPTIONS:
					if (index == 0) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new NoteScreen(this, feed, card);
						next->show();
					}
					/*else if (index == 1) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new TradeFriendDetailScreen(this, feed, card);
						next->show();
					}*/
					else if (index == 1/*2*/) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new AuctionCreateScreen(this, feed, card);
						next->show();
					}
					else if (index == 2/*3*/) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new AlbumLoadScreen(this, feed, AlbumLoadScreen::ST_COMPARE, NULL, false, card);
						next->show();
					}
					else if (index == 3/*4*/) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new DetailScreen(this, feed,
								DetailScreen::CARD, card);
						next->show();
					}
					break;
				case ST_NEW_CARD:
					if(index == 0 && !busy) {
						busy = true;
						notice->setCaption("Accepting...");
						acceptCard();
					}
					else if (index == 1 && !busy) {
						busy = true;
						notice->setCaption("Rejecting...");
						rejectCard();
					}
					break;
				case ST_NUMBER_OPTIONS:
					if(index == 0) {
						maPlatformRequest(("tel:"+number).c_str());
					}
					else if (index == 1) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new NoteScreen(this, feed,
								card, NoteScreen::ST_SMS, number);
						next->show();
					}
					break;
				case ST_LOGIN_OPTIONS:
					if(index == 0) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new Login(this, feed, Login::S_LOGIN);
						next->show();
					}
					else if(index == 1) {
						if (next != NULL) {
							delete next;
							feed->remHttp();
							next = NULL;
						}
						next = new Login(this, feed, Login::S_REGISTER);
						next->show();
					}
					break;
			}
			break;
		case MAK_BACK:
		case MAK_SOFTRIGHT:
			switch(screenType) {
				case ST_LOGIN_OPTIONS:
#if not defined(MA_PROF_STRING_PLATFORM_IPHONEOS)
					maExit(0);
#endif
					break;
				default:
					clearListBox();
					previous->show();
					break;
			}
			break;
		case MAK_DOWN:
			if (ind+1 < listBox->getChildren().size()) {
				listBox->setSelectedIndex(ind+1);
			} else if(currentSelectedKey==NULL){
				listBox->getChildren()[ind]->setSelected(false);
				for(int i = 0; i < currentSoftKeys->getChildren().size();i++){
					if(((Button *)currentSoftKeys->getChildren()[i])->isSelectable()){
						currentKeyPosition=i;
						currentSelectedKey= currentSoftKeys->getChildren()[i];
						currentSelectedKey->setSelected(true);
						break;
					}
				}
			}
			/*if (ind == max-1) {
				listBox->setSelectedIndex(0);
			} else {
				listBox->selectNextItem();
			}*/
			break;
		case MAK_UP:
			if(currentSelectedKey!=NULL){
				currentSelectedKey->setSelected(false);
				currentSelectedKey = NULL;
				currentKeyPosition = -1;
				listBox->getChildren()[listBox->getChildren().size()-1]->setSelected(true);
			}
			else if (ind > 0) {
				listBox->setSelectedIndex(ind-1);
			}/*if (ind == 0) {
				listBox->setSelectedIndex(max-1);
			} else {
				listBox->selectPreviousItem();
			}*/
			break;
		case MAK_LEFT:
			if(currentSelectedKey!=NULL){
				if(currentKeyPosition > 0){
					currentKeyPosition = currentKeyPosition - 1;
					for(int i = currentKeyPosition; i >= 0;i--){
						if(((Button *)currentSoftKeys->getChildren()[i])->isSelectable()){
							currentSelectedKey->setSelected(false);
							currentKeyPosition=i;
							currentSelectedKey= currentSoftKeys->getChildren()[i];
							currentSelectedKey->setSelected(true);
							break;
						}
					}
				}
			}
			break;
		case MAK_RIGHT:
			if(currentSelectedKey!=NULL){
				if(currentKeyPosition+1 < currentSelectedKey->getParent()->getChildren().size()){
					currentKeyPosition = currentKeyPosition + 1;
					for(int i = currentKeyPosition; i < currentSoftKeys->getChildren().size();i++){
						if(((Button *)currentSoftKeys->getChildren()[i])->isSelectable()){
							currentSelectedKey->setSelected(false);
							currentKeyPosition=i;
							currentSelectedKey= currentSoftKeys->getChildren()[i];
							currentSelectedKey->setSelected(true);
							break;
						}
					}
				}
			}
			break;
	}
}