Handle<Value> TiUIBase::_blur(void* userContext, TiObject* caller, const Arguments& args)
{
    TiUIBase* obj = static_cast<TiUIBase*>(userContext);
    NativeControlObject* control = static_cast<NativeControlObject*>(obj->getNativeObject());
    control->blur();
    return Undefined();
}
Handle<Value> TiUIBase::_finishLayout(void* userContext, TiObject*, const Arguments&)
{
    TiUIBase* obj = (TiUIBase*) userContext;
    NativeObject* no = obj->getNativeObject();
    no->finishLayout();
    return Undefined();
}
Handle<Value> TiUIBase::_add(void* userContext, TiObject*, const Arguments& args)
{
    HandleScope handleScope;
    TiUIBase* obj = (TiUIBase*) userContext;
    if ((args.Length() > 0) && (args[0]->IsObject()))
    {
        TiObject* addObj = getTiObjectFromJsObject(args[0]);
        if ((addObj == NULL) || (!addObj->isUIObject()))
        {
            return ThrowException(String::New(Ti::Msg::Invalid_add_argument));
        }
        TiUIBase* uiObj = (TiUIBase*) addObj;
        NativeObject* childNO = uiObj->getNativeObject();
        if (childNO == NULL)
        {
            return ThrowException(String::New(Ti::Msg::Invalid_add_argument));
        }
        NativeObject* parentNO = obj->getNativeObject();
        if (N_SUCCEEDED(parentNO->addChildNativeObject(childNO)))
        {
            ObjectEntry entry = addObj;
            obj->childControls_.push_back(entry);
            obj->jsChildren_->Set(obj->jsChildren_->Length(), addObj->getValue());
        }
        childNO->release();
        parentNO->release();
    }
    else
    {
        // TODO: expand this exception
    }
    return Undefined();
}
Handle<Value> TiUIBase::_show(void* userContext, TiObject*, const Arguments&)
{
    TiUIBase* obj = (TiUIBase*) userContext;
    NativeObject* no = obj->getNativeObject();
    no->setVisibility(true);
    return Undefined();
}
TiUIBase* TiUIBase::createView(NativeObjectFactory* nativeObjectFactory)
{
    TiUIBase* obj = new TiUIBase("View");
    obj->setNativeObjectFactory(nativeObjectFactory);
    obj->initializeTiObject(NULL);

    return obj;
}
Handle<Value> TiUIBase::_updateLayout(void* userContext, TiObject*, const Arguments& args)
{
    if ((args.Length() > 0) && (args[0]->IsObject()))
    {
        Local<Object> settingsObj = Local<Object>::Cast(args[0]);
        TiUIBase* obj = (TiUIBase*) userContext;
        obj->setParametersFromObject(obj, settingsObj);
    }
    return Undefined();
}
Handle<Value> TiUIBase::_getValue(int propertyNumber, void* context)
{

    TiUIBase* self = (TiUIBase*) context;
    NativeObject* object = self->getNativeObject();
    TiObject value;
    if (object != NULL)
    {
        object->getPropertyValue(propertyNumber, &value);
    }
    return value.getValue();
}
Handle<Value> TiUIBase::_remove(void* userContext, TiObject*, const Arguments& args)
{
    HandleScope handleScope;
    // JavaScript usage:
    //
    // arg[0] = Titanium.UI.View
    //
    if (args.Length() < 1)
    {
        return ThrowException(String::New(Ti::Msg::Missing_argument));
    }
    if (!args[0]->IsObject())
    {
        return ThrowException(String::New(Ti::Msg::Invalid_remove_argument));
    }

    TiUIBase* obj = (TiUIBase*) userContext;
    TiObject* removeObject = TiObject::getTiObjectFromJsObject(args[0]);
    if (removeObject == NULL)
    {
        return ThrowException(String::New(Ti::Msg::Invalid_remove_argument));
    }
    NativeObject* parentControl = obj->getNativeObject();
    if (parentControl == NULL)
    {
        return ThrowException(String::New(Ti::Msg::INTERNAL__Missing_native_object));
    }
    vector<ObjectEntry>::const_iterator it;
    bool foundChild = false;
    for (it = obj->childControls_.begin(); it != obj->childControls_.end(); it++)
    {
        if ((*it).isSameInstance(removeObject))
        {
            NativeObject* childControl = (*it)->getNativeObject();
            if (childControl == NULL)
            {
                parentControl->release();
                return ThrowException(String::New(Ti::Msg::INTERNAL__Missing_native_object));
            }
            parentControl->removeChildNativeObject(childControl);
            childControl->release();
            foundChild = true;
            break;
        }
    }
    parentControl->release();
    if (!foundChild)
    {
        TI_WARNING(Ti::Msg::Remove_child_warning);
    }
    return Undefined();
}
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;
}
Handle<Value> TiUIBase::_animate(void* userContext, TiObject*, const Arguments& args)
{
    HandleScope handleScope;

    TiUIBase* self = static_cast<TiUIBase*>(userContext);
    NativeControlObject *native = static_cast<NativeControlObject*>(self->getNativeObject());
    TiObject* tiObject = TiObject::getTiObjectFromJsObject(args[0]);

    if(!tiObject) {
        Local<Object> jsObject = Local<Object>::Cast(args[0]);
        if(args.Length() > 1 && args[1]->IsFunction()) {
        	Handle<Function> callback = Handle<Function>::Cast(args[1]);
        	Handle<Object> source = Handle<Object>::Cast(self->getValue());
            TiV8Event* event = TiV8Event::createEvent("complete", callback, source);
        	native->animate(jsObject, event);
        } else {
            native->animate(jsObject);
        }
    } else {
        native->animate(tiObject->getNativeObject());
    }

    return Undefined();
}
VALUE_MODIFY TiUIBase::_valueModify(int propertyNumber, TiObject* value, void* context)
{
    TiUIBase* self = (TiUIBase*) context;
    NativeObject* object = self->getNativeObject();
    if (object == NULL)
    {
        return VALUE_MODIFY_NOT_SUPPORTED;
    }
    VALUE_MODIFY modify = VALUE_MODIFY_ALLOW;
    switch (object->setPropertyValue(propertyNumber, value))
    {
    case NATIVE_ERROR_OK:
        modify = VALUE_MODIFY_ALLOW;
        break;
    case NATIVE_ERROR_NOTSUPPORTED:
        modify = VALUE_MODIFY_NOT_SUPPORTED;
        break;
    default:
        modify = VALUE_MODIFY_INVALID;
        break;
    }
    object->release();
    return modify;
}
Handle<Value> TiUITab::_open(void* userContext, TiObject*, const Arguments& args)
{
    HandleScope handleScope;
    if ((args.Length() > 0) && (args[0]->IsObject()))
    {
        TiObject* addObj = getTiObjectFromJsObject(args[0]);
        if ((addObj == NULL) || (!addObj->isUIObject()))
        {
            return Undefined();
        }
        TiUIBase* uiObj = (TiUIBase*) addObj;
        NativeObject* win = uiObj->getNativeObject();
        TiUITab* obj = (TiUITab*) userContext;
        NativeObject* tab = obj->getNativeObject();
        tab->openWindowOnTab(win);
        tab->release();
        win->release();
    }
    else
    {
        return ThrowException(String::New(Native::Msg::Expected_argument_of_type_object_or_external));
    }
    return Undefined();
}