void TiUITabGroupProxy::setActiveTab(TiUITabProxy* tab)
{
	Ti::TiEventParameters eventParams;
	eventParams.addParam("previousTab", _activeTab);
	eventParams.addParam("previousIndex", getTabbedPane()->indexOf(_activeTab->getTab()));
	getTabbedPane()->setActiveTab(tab->getTab());
	_activeTab = tab;
	eventParams.addParam("index", getTabbedPane()-> indexOf(_activeTab->getTab()));
	eventParams.addParam("tab", tab);
	changeIsFocus(false);
	fireEvent(Ti::TiConstants::EventFocus, eventParams);
}
void Ti::TiView::onPinchCancelledEvent(bb::cascades::PinchEvent* event)
{
	Ti::TiEventParameters clickEvent;
	if(clickSource != NULL) {
		clickEvent.addParam("source", clickSource->getProxy());
	}
	clickEvent.addParam("x", Ti::TiHelper::PixelsToDP(event->x()));
	clickEvent.addParam("y", Ti::TiHelper::PixelsToDP(event->y()));

	clickEvent.addParam("type", Ti::TiConstants::EventPinch);
	getProxy()->fireEvent(Ti::TiConstants::EventPinch, clickEvent);

	clickSource = NULL;
}
void Ti::TiView::onTapEvent(bb::cascades::TapEvent*)
{
	Ti::TiEventParameters clickEvent;
	clickEvent.addParam("type", "click");
	getProxy()->fireEvent(Ti::TiConstants::EventClick, clickEvent);
	getProxy()->fireEvent(Ti::TiConstants::EventSingleTap, clickEvent);
}
void Ti::TiView::onLongPressEvent(bb::cascades::LongPressEvent* event)
{
	Ti::TiEventParameters clickEvent;
	if(clickSource != NULL) {
		clickEvent.addParam("source", clickSource->getProxy());
	}
	clickEvent.addParam("x", Ti::TiHelper::PixelsToDP(event->x()));
	clickEvent.addParam("y", Ti::TiHelper::PixelsToDP(event->y()));

	clickEvent.addParam("type", Ti::TiConstants::EventLongClick);
	getProxy()->fireEvent(Ti::TiConstants::EventLongClick, clickEvent);

	clickEvent.addParam("type", Ti::TiConstants::EventLongPress);
	getProxy()->fireEvent(Ti::TiConstants::EventLongPress, clickEvent);
	clickSource = NULL;
}
// TODO: Finish this
Ti::TiValue Ti::TiProxy::fireEvent(Ti::TiValue value)
{
	QString eventName;
	Ti::TiEventParameters eventParams;
	if(value.isList()) {

		Ti::TiValue eName = value.toList().at(0);
		Ti::TiValue eParams = value.toList().at(1);
		eventName = eName.toString();

		if(eParams.isMap())
		{
			QMap<QString, Ti::TiValue> map = eParams.toMap();
			QList<QString> keys = map.keys();
			int len = keys.size();
			while(len--)
			{
				QString 	name 	= keys.at(len);
				Ti::TiValue val 	= map[name];

				if(val.isNumber())
				{
					eventParams.addParam(name, val.toNumber());
				}
				else if (val.isProxy())
				{
					eventParams.addParam(name, val.toProxy());
				}
				else // if(val.isString())
				{
					eventParams.addParam(name, val.toString());
				}
			}
		}
	}
	else
	{
		eventName = value.toString();
	}

	fireEvent(eventName, eventParams);

	return Ti::TiValue(Undefined());
}
void Ti::TiProxy::fireEvent(QString eventName, Ti::TiEventParameters params)
{
	HandleScope scope;
	if(events.contains(eventName)) {
		if(!params.contains("source"))
		{
			params.addParam("source", this);
		}
		Ti::TiEvent::FireEventOnObject(_jsObject, eventName, &params);
	}
	if(eventName == Ti::TiConstants::EventClose)
	{
		Ti::TiHelper::LogInternal("Forcing GC");
		V8::LowMemoryNotification();
	}
}
void ToastNotificationEventHandler::onFinish(bb::system::SystemUiResult::Type type)
{
	Ti::TiEventParameters eventParams;

	QString result = "";
	switch(type) {
	case bb::system::SystemUiResult::None: result = "none"; break;
	case bb::system::SystemUiResult::ButtonSelection: result = "buttonSelection";break;
	case bb::system::SystemUiResult::ConfirmButtonSelection: result = "confirmButtonSelection";break;
	case bb::system::SystemUiResult::CancelButtonSelection: result = "cancelButtonSelection";break;
	case bb::system::SystemUiResult::TimeOut: result = "timeOut";break;
	case bb::system::SystemUiResult::CustomButtonSelection: result = "customButtonSelection";break;
	}
	eventParams.addParam("result", result);
	eventParams.addParam("type", "finish");
	_proxy->fireEvent("finish", eventParams);
}
void Ti::TiView::onTouch(bb::cascades::TouchEvent* event)
{
	Ti::TiEventParameters clickEvent;
	if(clickSource != NULL) {
		clickEvent.addParam("source", clickSource->getProxy());
	}
	clickEvent.addParam("x", Ti::TiHelper::PixelsToDP(event->localX()));
	clickEvent.addParam("y", Ti::TiHelper::PixelsToDP(event->localY()));
	switch(event->touchType())
	{
	case bb::cascades::TouchType::Down:
	{
		clickEvent.addParam("type", Ti::TiConstants::EventTouchStart);
		getProxy()->fireEvent(Ti::TiConstants::EventTouchStart, clickEvent);
		break;
	}
	case bb::cascades::TouchType::Move:
	{
		clickEvent.addParam("type", Ti::TiConstants::EventTouchMove);
		getProxy()->fireEvent(Ti::TiConstants::EventTouchMove, clickEvent);
		break;
	}
	case bb::cascades::TouchType::Up:
	{
		clickEvent.addParam("type", Ti::TiConstants::EventTouchEnd);
		getProxy()->fireEvent(Ti::TiConstants::EventTouchEnd, clickEvent);
		break;
	}
	case bb::cascades::TouchType::Cancel:
	{
		clickEvent.addParam("type", Ti::TiConstants::EventTouchCancel);
		getProxy()->fireEvent(Ti::TiConstants::EventTouchCancel, clickEvent);
		break;
	}
	default:
		break;
	}
}
void TiUIImageButton::onClick()
{
	Ti::TiEventParameters params;
	params.addParam("type", Ti::TiConstants::EventClick);
	getProxy()->fireEvent(Ti::TiConstants::EventClick, params);
}