Ti::TiValue TiUITabProxy::open(Ti::TiValue value)
{
	Ti::TiValue val;
	val.setUndefined();

	Ti::TiWindowProxy* windowProxy;
	if(value.isList())
	{
		QList<Ti::TiValue> vs = value.toList();
		Ti::TiValue v = vs.at(0);
		windowProxy = static_cast<TiUIWindowProxy*>(v.toProxy());
	}
	else if(value.isProxy())
	{
		windowProxy = static_cast<TiUIWindowProxy*>(value.toProxy());
	}
	else
	{
		return val;
	}

	windowProxy->clearWeak();
	windowProxy->getPane()->setParent(NULL);
	_windows.append(windowProxy);

	Ti::TiEventParameters _params;
	windowProxy->fireEvent(Ti::TiConstants::EventOpen, _params);

	_navigationPane->push(static_cast<bb::cascades::Page*>(windowProxy->getPane()));
	return val;
}
Ti::TiValue Ti::TiViewProxy::remove(Ti::TiValue value)
{
	Ti::TiValue val;
	val.setUndefined();

	Ti::TiViewProxy *childProxy = static_cast<Ti::TiViewProxy*>(value.toProxy());
	if(!_childViewsProxies.contains(childProxy)) return val;

	Ti::TiView* childView = childProxy->getView();
	Ti::TiView* thisView = getView();
	childProxy->makeWeak();
	thisView->remove(childView);
	_childViewsProxies.removeOne(childProxy);

	childProxy->_parentProxy = NULL;
    Local<Value> children = _jsObject->Get(String::New("children"));
    if(!children.IsEmpty() && !children->IsUndefined())
    {
    	Local<Array> array = Local<Array>::Cast(children);
    	for(int i = 0, len = array->Length(); i < len; i++) {
    		if(array->Get(i) == value.toJSValue())
    		{
    	    	array->Delete(i);
    	    	break;
    		}
    	}
    }

	return val;
}
Ti::TiValue Ti::TiViewProxy::animate(Ti::TiValue val)
{
	Ti::TiCallback* callback = NULL;

	if(val.isList()) {
		QList<Ti::TiValue> array = val.toList();
		callback = new Ti::TiCallback(this, array.at(1));
		val = Ti::TiValue(array.at(0));
	}

	if(val.isProxy())
	{
		Ti::TiAnimationProxy *proxy = static_cast<Ti::TiAnimationProxy*>(val.toProxy());
		proxy->animate(this, callback);
	}
	else if(val.isMap())
	{
		TiUI::TiUIAnimationProxy *proxy = TiUI::TiUIAnimationProxy::CreateProxy();
		proxy->applyProperties(val);
		proxy->animate(this, callback);
	}
	Ti::TiValue result;
	result.setUndefined();
	return result;
}
Ti::TiValue TiUITabProxy::close(Ti::TiValue value)
{
	Ti::TiValue val;
	val.setUndefined();
	Ti::TiWindowProxy* windowProxy;
	bool animated = true;
	if(value.isList())
	{
		QList<Ti::TiValue> vs = value.toList();
		Ti::TiValue v = vs.at(0);
		windowProxy = static_cast<TiUIWindowProxy*>(v.toProxy());
		Ti::TiValue optionsValue = vs.at(1);
		if(optionsValue.isMap())
		{
			QMap<QString, Ti::TiValue> options = optionsValue.toMap();
			if(options.contains("animated"))
			{
				animated = options["animated"].toBool();
			}
		}
	}
	else if(value.isProxy())
	{
		windowProxy = static_cast<TiUIWindowProxy*>(value.toProxy());
	}
	else
	{
		return val;
	}
	if(_windows.last() == windowProxy && animated == true)
	{
		_navigationPane->pop();
	}
	else
	{
		_navigationPane->remove(static_cast<bb::cascades::Page*>(windowProxy->getPane()));
		Ti::TiEventParameters _params;
		windowProxy->fireEvent(Ti::TiConstants::EventClose, _params);
		windowProxy->makeWeak();
	}
	_windows.removeOne(windowProxy);

	return val;
}
Ti::TiValue TiUITabGroupProxy::setActiveTab(Ti::TiValue value)
{
	if(value.isNumber())
	{
		setActiveTab(_allTabs.at((int)value.toNumber()));
	} else
	if(value.isProxy())
	{
		setActiveTab(static_cast<TiUITabProxy*>(value.toProxy()));
	}
	return Ti::TiValue();
}
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 TiUITabGroupProxy::addTab(Ti::TiValue value)
{
	Ti::TiValue val;
	val.setUndefined();
	if(!value.isProxy())
	{
		return val;
	}

	TiUITabProxy *tabProxy = static_cast<TiUITabProxy*>(value.toProxy());
	tabProxy->clearWeak();
	getTabbedPane()->add(tabProxy->getTab());
	_allTabs.append(tabProxy);
	_activeTab = _allTabs.at(0);
	return val;
}
// 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 TiUITableViewSectionProxy::add(Ti::TiValue value)
{
	TiUITableViewRowProxy* row = NULL;
	if(value.isMap())
	{
		row = TiUITableViewRowProxy::CreateProxy();
		row->initWithObject(value.toJSValue()->ToObject());
	}
	else
	if(value.isProxy())
	{
		row = static_cast<TiUITableViewRowProxy*>(value.toProxy());
	}
	row->setRowHeader("");
	row->clearWeak();

	_tableViewRows.append(row);

	Ti::TiValue val;
	val.setUndefined();
	return val;
}
Ti::TiValue Ti::TiViewProxy::add(Ti::TiValue value)
{
	if(value.isProxy())
	{
		Ti::TiViewProxy *childProxy = static_cast<Ti::TiViewProxy*>(value.toProxy());
		childProxy->clearWeak();
		_childViewsProxies.append(childProxy);
		Ti::TiView* childView = childProxy->getView();
		Ti::TiView* thisView = getView();
		thisView->add(childView);
		childProxy->_parentProxy = this;
	}
	else
	{
        TiObject* addObj = TiObject::getTiObjectFromJsObject(value.toJSValue());
        TiUIBase* uiObj = (TiUIBase*) addObj;
        NativeObject* childNO = uiObj->getNativeObject();
        getView()->addOldObject(childNO);

       Local<Value> children = _jsObject->Get(String::New("children"));
       Local<Array> array;
       if(children.IsEmpty() || children->IsUndefined())
       {
    	   array = Array::New();
    	   _jsObject->Set(String::New("children"), array);
       }
       else
       {
    	   array = Local<Array>::Cast(children);
       }
       array->Set(array->Length(), value.toJSValue());
       childNO->release();

	}

	Ti::TiValue val;
	val.setUndefined();
	return val;
}
void TiUITabProxy::setWindow(Ti::TiValue value)
{
	open(value);
	TiUIWindowProxy *windowProxy = static_cast<TiUIWindowProxy*>(value.toProxy());
	setRootWindow(windowProxy);
}