void TiUIScrollViewProxy::setScrollType(Ti::TiValue val)
{
	if(val.toString() == "vertical")
	{
		_nativeScrollView->scrollViewProperties()->setScrollMode(bb::cascades::ScrollMode::Vertical);
	}
	else if(val.toString() == "horizontal")
	{
		_nativeScrollView->scrollViewProperties()->setScrollMode(bb::cascades::ScrollMode::Horizontal);
	}
}
Ti::TiValue TiUIWebViewProxy::evalJS(Ti::TiValue val)
{
	if(val.isList())
	{
		QList<Ti::TiValue> array = val.toList();
		Ti::TiValue str = array.at(0);
		Ti::TiValue callback = array.at(1);
		_tiWebView->evalJS(str.toString(), callback);
	}
	else
	{
		_tiWebView->evalJS(val.toString());
	}
	return Ti::TiValue();
}
void ToastNotificationProxy::setIcon(Ti::TiValue value)
{
	QString res = Ti::TiConstants::ResourcesDir;
	res.append("/").append(value.toString());
	res.replace("//", "/");
	_toast->setIcon(QUrl(res));
}
// 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());
}
Ti::TiValue Ti::TiProxy::removeEventListener(Ti::TiValue value)
{
	Ti::TiValue nameValue = value.toList().at(0);
	QString eventName = nameValue.toString();

	Ti::TiValue functionValue = value.toList().at(1);
	Handle<Function> func = Handle<Function>::Cast(functionValue.toJSValue());

	Ti::TiEvent::RemoveEventFromObject(_jsObject, eventName, func);
	Ti::TiValue res;
	res.setUndefined();
	return res;
}
QByteArray TiUtilsModule::bytesFromValue(Ti::TiValue value)
{
	QByteArray rawData;
	if(value.isProxy())
	{
		Ti::TiBlob* blob = static_cast<Ti::TiBlob*>(value.toProxy());
		rawData = blob->getData();
	}
	else
	{
		rawData = value.toString().toLocal8Bit();
	}
	return rawData;
}
Ti::TiValue Ti::TiProxy::addEventListener(Ti::TiValue value)
{
	QString eventName;

	Ti::TiValue nameValue = value.toList().at(0);
	Ti::TiValue functionValue = value.toList().at(1);

	eventName = nameValue.toString();

	onEventAdded(eventName);

	Ti::TiEvent::AddEventToObject(_jsObject, eventName, Handle<Function>::Cast(functionValue.toJSValue()));

	Ti::TiValue res;
	res.setUndefined();
	return res;
}
void TiUIWebViewProxy::setUrl(Ti::TiValue val)
{
    QString url = val.toString();
    if(url.startsWith("http://") || url.startsWith("https://"))
    {
        _tiWebView->setIsLocal(false);
    	_tiWebView->getNativeWebView()->setUrl(QUrl(url));
    	return;
   	}
    const char* a = url.toLocal8Bit().constData();
    QString localUrl = QString("local:///").append(Ti::TiHelper::getAssetPath(url));
    const char* aa = localUrl.toLocal8Bit().constData();
//    localUrl.replace("local:///assets//", "local:///assets/");
//    const char* aaa = localUrl.toLocal8Bit().constData();
    _tiWebView->setIsLocal(true);
	_tiWebView->getNativeWebView()->setUrl(QUrl(localUrl));
	_tiWebView->hideLoadingIndicator();
}
Ti::TiValue TiUtilsModule::sha256(Ti::TiValue value)
{
	Ti::TiValue val;
	QString str = value.toString();

	unsigned char *key = NULL;
	key = (unsigned char*)qstrdup(str.toLocal8Bit().constData());

	unsigned char hash[32];
	sha256_context ctx;
	sha256_starts(&ctx);
	sha256_update(&ctx, key, str.length());
	sha256_finish(&ctx, hash);
	QByteArray result = QByteArray(reinterpret_cast<const char *>(hash)).toHex();

	// for some reason we're getting 4 extra characters
	QString returnString = QString(result);
	returnString.remove(returnString.length() - 4, 4);

	val.setString(returnString);

	return val;
}
void TiUIScrollViewProxy::setContentWidth(Ti::TiValue val)
{
	_contentWidthSet = true;
	_scrollView->getInnerViewProxy()->setWidth(val);
	_scrollView->_contentWidth = val.toString();
}
void TiUITableViewSectionProxy::setHeaderSubTitle(Ti::TiValue val)
{
	_headerSubtitle = val.toString();
}
void TiUITabProxy::setTitle(Ti::TiValue value)
{
	_tab->setTitle(value.toString());
}
void TiUITabProxy::setIcon(Ti::TiValue value)
{
	_tab->setImageSource(QUrl(Ti::TiHelper::getAssetPath(value.toString())));
}
void TiUIWebViewProxy::setHtml(Ti::TiValue val)
{
	_tiWebView->setIsLocal(true);
	_tiWebView->getNativeWebView()->setHtml(val.toString());
}
void TiUIButtonProxy::setTitle(Ti::TiValue value)
{
	_native->setText(value.toString());
}
void TiUIWebViewProxy::setUserAgent(Ti::TiValue val)
{
	_tiWebView->setUserAgent(val.toString());
}
void ToastNotificationProxy::setMessage(Ti::TiValue value)
{
	_toast->setBody(value.toString());
}
void Ti::TiViewProxy::setTop(Ti::TiValue value)
{
	getView()->_setTop(value.toString());
}
void Ti::TiViewProxy::setBottom(Ti::TiValue value)
{
	getView()->_setBottom(value.toString());
}
void TiUIScrollViewProxy::setLayout(Ti::TiValue val)
{
	_layout = val.toString();
}
void Ti::TiViewProxy::setLayout(Ti::TiValue value) {
	getView()->_setLayout(value.toString());
}
void Ti::TiViewProxy::setBackgroundImage(Ti::TiValue value)
{
	_backgroundImageUrl = Ti::TiHelper::getAssetPath(value.toString());
	setBackground();
}
void TiUIScrollViewProxy::setContentHeight(Ti::TiValue val)
{
	_contentHeightSet = true;
	_scrollView->getInnerViewProxy()->setHeight(val);
	_scrollView->_contentHeight = val.toString();
}
void ToastNotificationProxy::setButtonTitle(Ti::TiValue value)
{
	_toast->button()->setLabel(value.toString());
}