private void addScriptToProject(int position, ScriptBrick scriptBrick) {
    Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite();

    int[] temp = getScriptAndBrickIndexFromProject(position);

    int scriptPosition = temp[0];
    int brickPosition = temp[1];

    Script newScript = scriptBrick.getScriptSafe();
    if (currentSprite.getNumberOfBricks() > 0) {
      int addScriptTo = position == 0 ? 0 : scriptPosition + 1;
      currentSprite.addScript(addScriptTo, newScript);
    } else {
      currentSprite.addScript(newScript);
    }

    Script previousScript = currentSprite.getScript(scriptPosition);
    if (previousScript != null) {
      Brick brick;
      int size = previousScript.getBrickList().size();
      for (int i = brickPosition; i < size; i++) {
        brick = previousScript.getBrick(brickPosition);
        previousScript.removeBrick(brick);
        newScript.addBrick(brick);
      }
    }
    ProjectManager.getInstance().setCurrentScript(newScript);
  }
  private int[] getScriptAndBrickIndexFromProject(int position) {
    int[] returnValue = new int[2];

    if (position >= brickList.size()) {

      returnValue[0] = sprite.getNumberOfScripts() - 1;
      if (returnValue[0] < 0) {
        returnValue[0] = 0;
        returnValue[1] = 0;
      } else {
        Script script = sprite.getScript(returnValue[0]);
        if (script != null) {
          returnValue[1] = script.getBrickList().size();
        } else {
          returnValue[1] = 0;
        }
      }

      return returnValue;
    }

    int scriptPosition = 0;
    int scriptOffset;
    for (scriptOffset = 0; scriptOffset < position; ) {
      scriptOffset += sprite.getScript(scriptPosition).getBrickList().size() + 1;
      if (scriptOffset < position) {
        scriptPosition++;
      }
    }
    scriptOffset -= sprite.getScript(scriptPosition).getBrickList().size();

    returnValue[0] = scriptPosition;
    List<Brick> brickListFromProject = sprite.getScript(scriptPosition).getBrickList();
    int brickPosition = position;
    if (scriptOffset > 0) {
      brickPosition -= scriptOffset;
    }

    Brick brickFromProject;
    if (brickListFromProject.size() != 0 && brickPosition < brickListFromProject.size()) {
      brickFromProject = brickListFromProject.get(brickPosition);
    } else {
      brickFromProject = null;
    }

    returnValue[1] = sprite.getScript(scriptPosition).getBrickList().indexOf(brickFromProject);
    if (returnValue[1] < 0) {
      returnValue[1] = sprite.getScript(scriptPosition).getBrickList().size();
    }

    return returnValue;
  }
  private int getPositionInUserScript(int position) {
    position--;
    if (position < 0) {
      position = 0;
    }
    if (position >= brickList.size()) {
      return brickList.size() - 1;
    }

    List<Brick> brickListFromScript = script.getBrickList();

    Brick scriptBrick;
    if (brickListFromScript.size() != 0 && position < brickListFromScript.size()) {
      scriptBrick = brickListFromScript.get(position);
    } else {
      scriptBrick = null;
    }

    int returnValue = script.getBrickList().indexOf(scriptBrick);
    if (returnValue < 0) {
      returnValue = script.getBrickList().size();
    }
    return returnValue;
  }
  private void initBrickListUserScript() {
    script = getUserScript();
    brickList = new ArrayList<Brick>();
    brickList.add(script.getScriptBrick());

    script.getScriptBrick().setBrickAdapter(this);
    for (Brick brick : script.getBrickList()) {
      if (brick.getClass().equals(ChangeVariableBrick.class)) {
        ChangeVariableBrick changeVariableBrick = (ChangeVariableBrick) brick;
        changeVariableBrick.setInUserBrick(true);
      } else if (brick.getClass().equals(SetVariableBrick.class)) {
        SetVariableBrick setVariableBrick = (SetVariableBrick) brick;
        setVariableBrick.setInUserBrick(true);
      }
      brickList.add(brick);
      brick.setBrickAdapter(this);
    }
  }
  public void initBrickList() {
    brickList = new ArrayList<Brick>();

    if (userBrick != null) {
      initBrickListUserScript();
      return;
    }
    Sprite sprite = ProjectManager.getInstance().getCurrentSprite();

    int numberOfScripts = sprite.getNumberOfScripts();
    for (int scriptPosition = 0; scriptPosition < numberOfScripts; scriptPosition++) {
      Script script = sprite.getScript(scriptPosition);
      brickList.add(script.getScriptBrick());
      script.getScriptBrick().setBrickAdapter(this);
      for (Brick brick : script.getBrickList()) {
        brickList.add(brick);
        brick.setBrickAdapter(this);
      }
    }
  }
  private int getScriptIndexFromProject(int index) {
    int scriptIndex = 0;
    Script temporaryScript = null;
    for (int i = 0; i < index; ) {
      temporaryScript = sprite.getScript(scriptIndex);
      if (temporaryScript == null) {
        Log.e(
            TAG,
            "getScriptIndexFromProject() tmpScript was null. Index was "
                + index
                + " scriptIndex was "
                + scriptIndex);
        return -1;
      }
      i += temporaryScript.getBrickList().size() + 1;
      if (i <= index) {
        scriptIndex++;
      }
    }

    return scriptIndex;
  }