Handle<Value> TiProxy::_getApiName(void*userContext)
{
	HandleScope scope;
	TiProxy *proxy = (TiProxy*)userContext;


	return scope.Close(String::New(proxy->getApiName()));
}
Handle<Value> TiUIObject::_createControlHelper(void* userContext, CREATEOBJECTCALLBACK createCallback, const Arguments& args)
{
    HandleScope handleScope;
    TiUIObject* obj = (TiUIObject*) userContext;
    Handle<ObjectTemplate> global = getObjectTemplateFromJsObject(args.Holder());
    Handle<Object> result;
    result = global->NewInstance();
    TiProxy* newControl = (createCallback)(obj->objectFactory_);
    newControl->setValue(result);
    if ((args.Length() > 0) && (args[0]->IsObject()))
    {
        Local<Object> settingsObj = Local<Object>::Cast(args[0]);
        newControl->setParametersFromObject(newControl, settingsObj);
    }
    setTiObjectToJsObject(result, newControl);
    return handleScope.Close(result);
}
Handle<Value> TiMedia::_createControlHelper(void* userContext, CREATEOBJECTCALLBACK createCallback, const Arguments& args)
{
    HandleScope handleScope;
    TiMedia* obj = (TiMedia*) userContext;
    Handle<ObjectTemplate> global = getObjectTemplateFromJsObject(args.Holder());
    Handle<Object> result;
    result = global->NewInstance();
    TiProxy* newControl = (createCallback)(obj->objectFactory_);
    newControl->setAttachedObject(obj);
    newControl->setValue(result);
    newControl->makeWeak();
    if (args.Length() > 0)
    {
        newControl->applyProperties(args[0]);
    }
    setTiObjectToJsObject(result, newControl);
    return handleScope.Close(result);
}
void TiProxy::setParametersFromObject(void* userContext, Local<Object> obj)
{
    HandleScope handleScope;
    Handle<Value> value;
    Handle<Value> controlValue = getValue();
    if (!controlValue->IsObject())
    {
        return;
    }
    Handle<Array> propNames = obj->GetPropertyNames();
    uint32_t props = propNames->Length();
    Local<Value> propValue;
    for (uint32_t i = 0; i < props; i++)
    {
        Handle<String> propString = Handle<String>::Cast(propNames->Get(Integer::New(i)));
        String::Utf8Value propNameUTF(propString);
        Local<Value> propValue = obj->Get(propString);
        TiObject* foundProp = onLookupMember(*propNameUTF);
        if (foundProp != NULL)
        {
            TiObject* addObj = getTiObjectFromJsObject(propValue);
            if (addObj)
            {
                TiProxy* obj = (TiProxy*) userContext;
                TiProxy* uiObj = (TiProxy*) addObj;
                NativeObject* childNO = uiObj->getNativeObject();
                NativeObject* parentNO = obj->getNativeObject();
                parentNO->addChildNativeObject(childNO);
                parentNO->release();
            }
            else
            {
                foundProp->setValue(propValue);
            }
        }
        else
        {
            // Set any custom properties onto the JS object.
            getValue()->ToObject()->ForceSet(propString, propValue);
        }
    }
}
Handle<Value> TiProxy::_addEventListener(void* userContext, TiObject*, const Arguments& args)
{
    // JavaScript usage:
    //
    // arg[0] = event name (string)
    // arg[1] = event function (function)
    //
    // myobject.addEventListener('myevent',function(e) {...});
    //
    if ((args.Length() != 2) || (!args[0]->IsString()) || (!args[1]->IsFunction()))
    {
        return Undefined();
    }
    TiProxy* obj = (TiProxy*) userContext;
    Handle<String> eventName = Handle<String>::Cast(args[0]);
    Handle<Function> func = Handle<Function>::Cast(args[1]);
    String::Utf8Value eventNameUTF(eventName);
    obj->onAddEventListener(*eventNameUTF, func);
    return Undefined();
}
Handle<Value> TiProxy::createProxy(TiProxy *proxy, void* userContext, const Arguments& args)
{
    
    TiProxy *module = static_cast<TiProxy*>(userContext);
    HandleScope handleScope;
    Handle<ObjectTemplate> global = getObjectTemplateFromJsObject(args.Holder());
    Handle<Object> result = global->NewInstance();

    proxy->setNativeObjectFactory(module->getNativeObjectFactory());
    proxy->initializeTiObject(NULL);
    proxy->setValue(result);
    proxy->setAttachedObject(module);
    if ((args.Length() > 0) && (args[0]->IsObject()))
    {
        Local<Object> settingsObj = Local<Object>::Cast(args[0]);
        proxy->setParametersFromObject(proxy, settingsObj);
    }
    setTiObjectToJsObject(result, proxy);
    return handleScope.Close(result);
}