UIControl* AutotestingSystemLua::FindControl(UIList* srcList, int32 index)
{
	if(UIControlSystem::Instance()->GetLockInputCounter() > 0) return NULL;

	if(srcList)
	{
		const List<UIControl*> &cells = srcList->GetVisibleCells();
		for(List<UIControl*>::const_iterator it = cells.begin(); it != cells.end(); ++it)
		{
			UIListCell* cell = dynamic_cast<UIListCell*>(*it);
			if(cell)
			{
				if(cell->GetIndex() == index && IsCenterInside(srcList, cell))
				{
					return cell;
				}
			}
		}
	}
	return NULL;
}
void UIList::Update(float32 timeElapsed)
{
	if(!delegate)
	{
		return;
	}
	
	if(needRefresh)
	{
		FullRefresh();
	}
	
	float d = newPos - oldPos;
	oldPos = newPos;
	Rect r = scrollContainer->GetRect();
	if(orientation == ORIENTATION_HORIZONTAL)
	{
		r.x = scroll->GetPosition(d, SystemTimer::FrameDelta(), lockTouch);
	}
	else 
	{
		r.y = scroll->GetPosition(d, SystemTimer::FrameDelta(), lockTouch);
	}
	scrollContainer->SetRect(r);
	
	List<UIControl*>::const_iterator it;
	Rect viewRect = GetGeometricData().GetUnrotatedRect();//GetRect(TRUE);
	const List<UIControl*> &scrollList = scrollContainer->GetChildren();
	List<UIControl*> removeList;
	
	//removing invisible elements
	for(it = scrollList.begin(); it != scrollList.end(); it++)
	{
		Rect crect = (*it)->GetGeometricData().GetUnrotatedRect();//GetRect(TRUE);
		if(orientation == ORIENTATION_HORIZONTAL)
		{
			if(crect.x + crect.dx < viewRect.x - viewRect.dx || crect.x > viewRect.x + viewRect.dx*2)
			{
				removeList.push_back(*it);
			}
		}
		else 
		{
			if(crect.y + crect.dy < viewRect.y - viewRect.dy || crect.y > viewRect.y + viewRect.dy*2)
			{
				removeList.push_back(*it);
			}
		}
	}
	for(it = removeList.begin(); it != removeList.end(); it++)
	{
		scrollContainer->RemoveControl((*it));
	}
	
    if (!scrollList.empty()) 
    {
            //adding elements at the list end
        int32 ind = -1;
        UIListCell *fc = NULL;
        for(it = scrollList.begin(); it != scrollList.end(); it++)
        {
            UIListCell *lc = (UIListCell *)(*it);
            int32 i = lc->GetIndex();
            if(i > ind)
            {
                ind = i;
                fc = lc;
            }
        }
        if(fc)
        {
            int32 borderPos;
            int32 rPos;
            int size = 0;
            int32 off;
            if(orientation == ORIENTATION_HORIZONTAL)
            {
                borderPos = (int32)(viewRect.dx + viewRect.dx / 2.0f);
                off = (int32)scrollContainer->GetRect().x;
                rPos = (int32)(fc->GetRect().x + fc->GetRect().dx + off);
            }
            else 
            {
                borderPos = (int32)(viewRect.dy + viewRect.dy / 22.0f);
                off = (int32)scrollContainer->GetRect().y;
                rPos = (int32)(fc->GetRect().y + fc->GetRect().dy + off);
            }
            while(rPos < borderPos && fc->GetIndex() < delegate->ElementsCount(this) - 1)
            {
                int32 i = fc->GetIndex() + 1;
                fc = delegate->CellAtIndex(this, i);
                if(orientation == ORIENTATION_HORIZONTAL)
                {
                    size = delegate->CellWidth(this, i);
                }
                else 
                {
                    size = delegate->CellHeight(this, i);
                }
                AddCellAtPos(fc, rPos - off, size, i);
                rPos += size;
                    //			scroll->SetElementSize((float32)(rPos - off));
            }
        }
        
            //adding elements at the list begin
        ind = maximumElementsCount;
        fc = NULL;
        for(it = scrollList.begin(); it != scrollList.end(); it++)
        {
            UIListCell *lc = (UIListCell *)(*it);
            int32 i = lc->GetIndex();
            if(i < ind)
            {
                ind = i;
                fc = lc;
            }
        }
        if(fc)
        {
            int32 borderPos;
            int32 rPos;
            int size = 0;
            int32 off;
            if(orientation == ORIENTATION_HORIZONTAL)
            {
                borderPos = (int32)(-viewRect.dx/2.0f);
                off = (int32)scrollContainer->GetRect().x;
                rPos = (int32)(fc->GetRect().x + off);
            }
            else 
            {
                borderPos = (int32)(-viewRect.dy/2.0f);
                off = (int32)scrollContainer->GetRect().y;
                rPos = (int32)(fc->GetRect().y + off);
            }
            while(rPos > borderPos && fc->GetIndex() > 0)
            {
                int32 i = fc->GetIndex() - 1;
                fc = delegate->CellAtIndex(this, i);
                if(orientation == ORIENTATION_HORIZONTAL)
                {
                    size = delegate->CellWidth(this, i);
                }
                else 
                {
                    size = delegate->CellHeight(this, i);
                }
                rPos -= size;
                AddCellAtPos(fc, rPos - off, size, i);
            }
        }
    }
    else 
    {
        FullRefresh();
    }


	
}