import game.LM;
import game.S;
import game.TL;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Player;

/**
 *
 * @author user
 */
public class RB2 implements Runnable, CommandListener {

    private boolean beContinue, jLevel, eLevel, noTime, rLevel, sAlpha, easy, nSelectDiff, wasMotion, wasBack, offAd, pGame, hasIAP, workAds, hFirst, showTurnOffAds;
    private int width, height, bKey, lQuantity, levelX, levelY, levelWidth, levelHeight, levelTime, levelPoints, mIndex, bIndex, mH, mHeight, slIndex, lbY, lHeight, lbHeight, nPoints, lMode, slY, lbIndex, sdY, sdIndex, gipY, cMode, stIndex, slColorUp = 0xd0d0d0, slColorDown = 0xd0d0d0, cIndex, lSelect, countOfMaps, lChangeMode, mlHeight, mAbout, sIndex, tMode, hMode;
    private int[] logo, curtain, rms_game, liftOut, lsY, lsH, checkXY, slColor = {0x666666, 0x666666, 0x666666, 0x666666, 0x666666, 0x666666, 0x666666, 0x666666}, stY;
    private int[][] liftCable, sXYWH;
    private float fps_time, lAlpha, gipTime, tAbout;
    private float[] rms_ball;
    private long fps, time, ptime, delta, fcount, gtime, otime, ltime, lPauseTime, pTime;
    private String cUser, price;
    private String[] menu_points, begin_text, help, lb_text, cast, sb_text, turn_off_ads;
    private String[][] menu_text, hArray;
    private Image offscreen, main_logo, dasuppa_logo, bg_menu, bg_begin, bg_game, stand, iBall, jump, go, rebound, box_frame, box_frame_selected, old_frame, line, board1, board3, board4, board5, board6, board7, board8, eng, rus, go_to_mm, button, hourglass, button_long1, button_long2, button_long3, joy, help1, help2, button_ads_off, sign_no, sign_ok;
    private Image[] sb_images, destruction, addl, flag;
    private S sBall, sLiftIn, sLiftOut;
    private LM lm;
    private Ball ball;
    private Lift lift;
    private Thorns thorns;
    private Signboard sb;
    private DrawText drawText;
    private ILoader iLoader;
    private InputBox userName;
    private ScrollBar mScroll, aScroll, hScroll, iScroll;
    private Slider[] sSlider;
    //private NewMap newMap;
    //private MapList mList;
    //private MapParameters mParam;
    private Records records;
    private RMS rms;
    private Joystick joystick;
    private Graphics gOS;
    public String gFlag, pauseFlag;
    public RB2MIDlet rb2MIDlet;
    private Command ok, cancel;
    private TextBox textBox;
    private Ad ad;
    private IAP iap;
    private Sound sound;
    
    public static boolean pUsing;
    public boolean press_rpKey;
    public boolean[] keys;
    public int pKey, rKey, dKey, rpKey, hKey, pX = -1, pY = -1, rX = -1, rY = -1, dX = -1, dY = -1;
    public Handler canvas;
    public final int KEY_FIRE = 0;
    public final int KEY_LEFT = 1;
    public final int KEY_RIGHT = 2;
    public Player background;

    public RB2(RB2MIDlet rb2MIDlet, Sound sound) {
        this.rb2MIDlet = rb2MIDlet;
        this.sound = sound;
        canvas = new Handler(this);
        pUsing = canvas.pUsing();
        canvas.setFullScreenMode(true);
        workAds = false;
        try {
            if(Class.forName("com.nokia.mid.payment.IAPClientPaymentManager") != null) hasIAP = true;
        } catch (ClassNotFoundException ex) {}
        if(hasIAP) {
            iap = new IAP(this);
            if(!iap.hasPurchased()) hasPurchased(false);
        } else {
            hasPurchased(false);
        }
        //init();
    }

    private void init() {
        keys = new boolean[3];
        iLoader = new ILoader("/resources/graphics.dat");
        main_logo = iLoader.get("logo");
        dasuppa_logo = iLoader.get("dasuppa_logo");
        sb_images = new Image[22];
        sBall = new S((iBall = iLoader.get("ball")), 18, 18);
        sLiftIn = new S(iLoader.get("lift"), 40, 60);
        sLiftOut = new S(iLoader.get("lift"), 40, 60);
        sb_images[11] = iLoader.get("passed");
        sb_images[13] = iLoader.get("points");
        sb_images[15] = iLoader.get("out");
        bg_menu = iLoader.get("bg_menu");
        bg_begin = iLoader.get("bg_begin");
        stand = iLoader.get("stand");
        jump = iLoader.get("jump");
        go = iLoader.get("go");
        rebound = iLoader.get("rebound");
        box_frame = iLoader.get("box_frame");
        box_frame_selected = iLoader.get("box_frame_selected");
        old_frame = iLoader.get("old_frame");
        line = iLoader.get("line");
        board1 = iLoader.get("board_1");
        board3 = iLoader.get("board_3");
        board4 = iLoader.get("board_4");
        board5 = iLoader.get("board_5");
        board6 = iLoader.get("board_6");
        board7 = iLoader.get("board_7");
        board8 = iLoader.get("board_8");
        eng = iLoader.get("eng");
        rus = iLoader.get("rus");
        go_to_mm = iLoader.get("go_to_mm");
        button = iLoader.get("button");
        hourglass = iLoader.get("hourglass");
        button_long1 = iLoader.get("button_long1");
        button_long2 = iLoader.get("button_long2");
        button_long3 = iLoader.get("button_long3");
        joy = iLoader.get("joy");
        help1 = iLoader.get("help1");
        help2 = iLoader.get("help2");
        flag = new Image[] {
            iLoader.get("flag_not_selected"), iLoader.get("flag_selected")
        };
        button_ads_off = iLoader.get("button_ads_off");
        sign_no = iLoader.get("sign_no");
        sign_ok = iLoader.get("sign_ok");

        Image addl_img = iLoader.get("addl");
        addl = new Image[7];
        for (int i = 0; i < 5; i++) {
            addl[i] = Image.createImage(addl_img, i * 28, 0, 28, 21, Sprite.TRANS_NONE);
        }
        addl[5] = iLoader.get("button_open");
        addl[6] = iLoader.get("button_close");

        mH = box_frame.getHeight() + 10;

        destruction = new Image[8];
        int[][] xywh = {
            {0, 10, 16, 18},
            {17, 10, 18, 18},
            {36, 8, 22, 22},
            {59, 7, 26, 24},
            {87, 1, 31, 33},
            {120, 1, 38, 35},
            {3, 42, 53, 34},
            {62, 40, 61, 36}
        };
        Image destroying = iLoader.get("destruction");
        for (int i = 0; i < 8; i++) {
            destruction[i] = Image.createImage(destroying, xywh[i][0], xywh[i][1], xywh[i][2], xywh[i][3], Sprite.TRANS_NONE);
        }
        destroying = null;
        xywh = null;

        countOfMaps = 30;

        rms = new RMS();
        rms_game = rms.getGameData();
        rms_ball = rms.getBallData();
        if (rms.getQuantityNames() >= 1) {
            beContinue = true;
        }
        width = canvas.getWidth();
        height = canvas.getHeight();
        checkXY = new int[2];
        mHeight = 4 * mH - 10;
        int fH = old_frame.getHeight() + 5;
        mlHeight = (height - 150) / fH * fH - 5;

        if (!canvas.isDoubleBuffered()) {
            offscreen = Image.createImage(width, height);
            gOS = offscreen.getGraphics();
            gOS.setClip(0, 0, width, height);
            gOS.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        }
        if (rms_game[8] != 0) {
            Localization.setLocale(rms_game[8] - 1);
            getLocalization();
        }
        
        if(!pGame && workAds) {
            ad = new Ad(rb2MIDlet, width);
            ad.loadAd();
        }
        
        gFlag = "LOGO";
        
        new Thread(this).start();
    }

    private void getLocalization() {
        drawText = new DrawText();
        drawText.loadFont(iLoader.get(Localization.getLocaleAsString() + "_font"));

        menu_points = Localization.getFullText(0);
        begin_text = Localization.getFullText(1);
        lsY = new int[3];
        lsH = new int[3];
        for (int i = 0; i < lsY.length; i++) {
            lsY[i] = (i == 0 ? 0 : lsY[i - 1] + lsH[i - 1] + 10);
            lsH[i] = Math.max(50, drawText.setText(begin_text[7 + i], 70, 0, width - 80));
        }
        menu_text = new String[4][];
        menu_text[0] = Localization.getFullText(2);
        menu_text[1] = Localization.getFullText(3);
        menu_text[2] = Localization.getFullText(4);
        menu_text[3] = Localization.getFullText(5);
        help = Localization.getFullText(6);
        lb_text = Localization.getFullText(7);
        sb_text = Localization.getFullText(8);
        turn_off_ads = Localization.getFullText(9);
        sb = new Signboard(sb_text, sb_images, (new DrawFont(iLoader.get("nums"), iLoader.get(Localization.getLocaleAsString() + "_bfont"), iLoader.get(Localization.getLocaleAsString() + "_sfont"))), width, height);
        userName = new InputBox((width - width / 4) / 3 * 2, 6 + 14 + 6, "", InputBox.STRING, drawText);
        userName.setUserNameWrite(true);

        mScroll = new ScrollBar(width / 2 - box_frame.getWidth() / 2 - 14, 140, box_frame.getWidth() + 10, mHeight, mH * (!beContinue ? menu_points.length - 1 : menu_points.length) - mHeight - 10, mH * (!beContinue ? menu_points.length - 1 : menu_points.length) - 10);
        
        drawText.setText(menu_text[2][0], width / 8 + 5, height / 2 - height / 4 + 19, width - width / 4 - 10, height / 2 - 34);
        aScroll = new ScrollBar(width / 8 + 5, height / 2 - height / 4 + 19, width - width / 4 - 15, height / 2 - 30, drawText.getHText() - (height / 2 - 34), drawText.getHText());

        //int h = mlHeight;
        //sScroll = new ScrollBar(width / 2 - old_frame.getWidth() / 2, 150, old_frame.getWidth() + 8, h, ((old_frame.getHeight() + 5) * countOfMaps - 5) - h, (old_frame.getHeight() + 5) * countOfMaps - 5);

        iScroll = new ScrollBar(0, 158 - 5, width, (height - 14) - 158, (lsY[2] + lsH[2] + 16) - ((height - 14) - 158), lsY[2] + lsH[2] + 10);
        iScroll.setVisibleScroll(false);
        
        stY = new int[] {
            0, 23, 46, 69, 93
        };
        
        sXYWH = new int[stY.length][];
        int bX = width / 2 - board6.getWidth() / 2 + 25, bY = height / 2 - board6.getHeight() / 2 + 18, bWidth = board6.getWidth() - 50, bHeight = 18;
        for(int i=0; i<sXYWH.length; i++) {
            sXYWH[i] = new int[] {bX, bY+stY[i], bWidth, bHeight};
        }
        
        //sXYWH = new int[][]{
        //    {bX, bY, bWidth, bHeight},
        //    {bX, bY + 23, bWidth, bHeight},
        //    {bX, bY + 46, bWidth, bHeight},
        //    {bX, bY + 69, bWidth, bHeight}
        //};
        
        sSlider = new Slider[] {
            new Slider(new String[] {Localization.getAloneText(54), Localization.getAloneText(55)}, bX + drawText.stringWidth(Localization.getAloneText(53)) + 16, bY+stY[0], max(drawText, Localization.getAloneText(54), Localization.getAloneText(55)) + 6, 18, drawText),
            new Slider(rms_game[5] == 0 ? (new String[]{begin_text[12], begin_text[13]}) : (new String[]{begin_text[12], begin_text[13], begin_text[14]}), bX + drawText.stringWidth(Localization.getAloneText(32)) + 16, bY+stY[1], max(drawText, begin_text[12], begin_text[13], begin_text[14]) + 6, 18, drawText),
            new Slider(new String[]{Localization.getAloneText(50), Localization.getAloneText(51)}, bX + drawText.stringWidth(Localization.getAloneText(49)) + 16, bY + stY[2], max(drawText, Localization.getAloneText(50), Localization.getAloneText(51)) + 6, 18, drawText),
            new Slider(new String[]{Localization.getAloneText(30), Localization.getAloneText(31)}, bX + drawText.stringWidth(Localization.getAloneText(29)) + 16, bY + stY[3], max(drawText, Localization.getAloneText(30), Localization.getAloneText(31)) + 6, 18, drawText),
        };
        
        sSlider[0].setIndex(rms_game[13]);
        sSlider[1].setIndex(rms_game[4] - 1);
        sSlider[2].setIndex(rms_game[8] - 1);
        sSlider[3].setIndex(rms_game[6]);
    }

    private int max(DrawText dt, String s1, String s2) {
        return Math.max(dt.stringWidth(s1), dt.stringWidth(s2));
    }

    private int max(DrawText dt, String s1, String s2, String s3) {
        return Math.max(Math.max(dt.stringWidth(s1), dt.stringWidth(s2)), dt.stringWidth(s3));
    }

    private void createLevel(MLoader mLoader, String mode) {
        lQuantity = mLoader.quantity();
        levelPoints = Integer.parseInt("" + mLoader.get("points"));
        int[] mWH = (int[]) mLoader.get("wh");
        Image block = iLoader.get("block");
        int[][] tiles = (int[][]) mLoader.get("tiles");
        int[][] temp_tiles = new int[mWH[1]][mWH[0]];
        int[][] lifts = new int[4][2];
        getLifts(tiles, lifts, block.getWidth(), block.getHeight());
        liftOut = new int[]{lifts[2][0], lifts[2][1], lifts[2][1] + sLiftOut.getHeight()};
        liftCable = new int[][]{lifts[1], lifts[2]};
        getTiles(tiles, temp_tiles, 1, 1);
        TL tl = new TL(mWH[0], mWH[1], block, block.getWidth(), block.getHeight(), temp_tiles, width, height);
        levelWidth = mWH[0] * block.getWidth();
        levelHeight = mWH[1] * block.getHeight();
        int[] mTime = (int[]) mLoader.get("time");
        mLoader = null;
        levelTime = mTime[0] * 60000 + mTime[1] * 1000;
        if (jLevel) {
            jLevel = false;
        }
        if (noTime) {
            noTime = false;
        }
        if (rLevel) {
            rLevel = false;
        }
        if (easy) {
            easy = false;
        }
        if (mode.equals("JP")) {
            jLevel = noTime = true;
        }
        if (mode.equals("JPR")) {
            rLevel = true;
        }
        if (rms_game[4] == 1) {
            noTime = easy = true;
        }
        if (rms_game[4] == 1 || rms_game[4] == 2) {
            levelTime += 15000;
        }
        mTime = null;
        lm = new LM(width, height);
        MapSides msBlocks = new MapSides(lifts, sLiftIn.getWidth(), sLiftOut.getHeight());
        msBlocks.addLayer(temp_tiles, mWH[0], mWH[1], block.getWidth(), block.getHeight());
        getTiles(tiles, temp_tiles, 5, 8);
        msBlocks.addLayer(temp_tiles, mWH[0], mWH[1], block.getWidth(), block.getHeight());
        msBlocks.build();
        
        sBall.setImage(iBall, 18, 18);
        
        ball = new Ball(sBall, rms_ball[1], rms_ball[0], 1 - rms_ball[2] / 100, msBlocks.getSides(), block.getWidth(), block.getHeight());
        msBlocks = null;
        lift = new Lift(sLiftIn, sLiftOut, lifts, sBall);
        lifts = null;
        
        Vector vGBS = new Vector();
        getObjects(tiles, vGBS, 3, block.getWidth(), block.getHeight());
        GlassBlock[] gbs = new GlassBlock[vGBS.size() / 2];
        for (int i = 0; i < vGBS.size() / 2; i++) {
            gbs[i] = new GlassBlock(height, (S) vGBS.elementAt(i * 2), (S) vGBS.elementAt(i * 2 + 1), 40);
            lm.append((S) vGBS.elementAt(i * 2));
            lm.append((S) vGBS.elementAt(i * 2 + 1));
        }
        vGBS = null;
        getTiles(tiles, temp_tiles, 2, 2);
        MapSides msGBS = new MapSides();
        msGBS.addLayer(temp_tiles, block.getWidth(), block.getHeight());
        msGBS.build();
        ball.setGlassBlocks(gbs, msGBS.getSides());
        msGBS = null;
        gbs = null;
        
        lm.append(tl, 0, 0);
        lm.append(sLiftIn);
        lm.append(sLiftOut);
        lm.append(sBall);
        
        if (pUsing) {
            joystick = new Joystick(width/2-joy.getWidth()/2, height-height/8-joy.getHeight()/2, joy, ball);
        }
        
        Vector vThorn = new Vector();
        getObjects(tiles, vThorn, 1, block.getWidth(), block.getHeight());
        for (int i = 0; i < vThorn.size(); i++) {
            lm.append((S) vThorn.elementAt(i));
        }
        vThorn = null;
        getTiles(tiles, temp_tiles, 3, 4);
        MapSides msThorns = new MapSides();
        msThorns.addLayer(temp_tiles, block.getWidth(), block.getHeight());
        msThorns.build();
        thorns = new Thorns(msThorns.getSides(), block.getHeight());
        ball.setThorns(msThorns.getSides());
        msThorns = null;
        Vector vFans = new Vector();
        getObjects(tiles, vFans, 2, block.getWidth(), block.getHeight());
        Fan[] fans = new Fan[vFans.size()];
        for (int i = 0; i < vFans.size(); i++) {
            fans[i] = (Fan) vFans.elementAt(i);
            lm.append(fans[i].getFan());
        }
        vFans = null;
        getTiles(tiles, temp_tiles, 5, 8);
        MapSides msFans = new MapSides();
        msFans.addLayer(temp_tiles, block.getWidth(), block.getHeight());
        msFans.build();
        ball.setFans(fans, msFans.getSides());
        msFans = null;
        fans = null;
        Random rnd = new Random();
        bg_game = iLoader.get("bg_" + (rnd.nextInt(6) + 1));
        rnd = null;
    }

    private void getTiles(int[][] tiles, int[][] temp_tiles, int begin, int end) {
        for (int i = 0; i < tiles.length; i++) {
            for (int j = 0; j < tiles[i].length; j++) {
                temp_tiles[i][j] = 0;
                if (tiles[i][j] >= begin && tiles[i][j] <= end) {
                    temp_tiles[i][j] = tiles[i][j];
                }
            }
        }
    }

    private void getObjects(int[][] tiles, Vector objects, int type, int bW, int bH) {
        for (int i = 0; i < tiles.length; i++) {
            for (int j = 0; j < tiles[i].length; j++) {
                if (type == 1 && (tiles[i][j] == 3 || tiles[i][j] == 4)) {
                    S sThorn = new S(iLoader.get("thorn_direction_" + (tiles[i][j] == 3 ? "up" : "down")), 40, 30);
                    sThorn.setPosition(j * bW, i * bH);
                    objects.addElement(sThorn);
                    sThorn = null;
                }
                if (type == 2 && (tiles[i][j] >= 5 && tiles[i][j] <= 8)) {
                    S sFan = new S(iLoader.get("fan_direction_" + (tiles[i][j] == 5 ? "up" : tiles[i][j] == 6 ? "down" : tiles[i][j] == 7 ? "left" : "right")), 40, 30);
                    sFan.setPosition(j * bW, i * bH);
                    Fan fan = new Fan(sFan, tiles[i][j] - 4, 1);
                    sFan = null;
                    fan.start();
                    objects.addElement(fan);
                    fan = null;
                }
                if (type == 3 && tiles[i][j] == 2) {
                    S sGB = new S(iLoader.get("glass_block"), 40, 60);
                    S sFallGB = new S(iLoader.get("glass_block"), 40, 60);
                    sFallGB.setFrame(6);
                    sGB.setPosition(j * bW, i * bH);
                    sFallGB.setPosition(-sFallGB.getWidth(), -sFallGB.getHeight());
                    objects.addElement(sGB);
                    objects.addElement(sFallGB);
                    sFallGB = null;
                    sGB = null;
                }
            }
        }
    }

    private void getLifts(int[][] tiles, int[][] lifts, int bW, int bH) {
        for (int i = 0; i < tiles.length; i++) {
            for (int j = 0; j < tiles[i].length; j++) {
                if (tiles[i][j] >= 9) {
                    int nLift = tiles[i][j] == 9 ? 1 : tiles[i][j] == 10 ? 2 : tiles[i][j] == 11 ? 0 : 3;
                    lifts[nLift][0] = j * bW;
                    lifts[nLift][1] = i * bH;
                    if (nLift == 0 || nLift == 3) {
                        lifts[nLift][1] += bH;
                    }
                }
            }
        }
    }

    //private void createLoadNewMap() {
    //    newMap = new NewMap(new MLoader(Encode.encode(rms.getLevel(mList.getCurrentIndex()))), iLoader, width, height, board3, button, button_long1, button_long2, addl, drawText);
    //    gFlag = "CREATE_MAP";
    //}

    //private void createNewMap(int wSize, int hSize) {
    //    newMap = new NewMap(wSize, hSize, iLoader.get("blocks"), 40, 30, iLoader.get("mblocks"), board3, iLoader.get("lift_enter"), iLoader.get("enter_lift"), iLoader.get("lift_exit"), iLoader.get("exit_lift"), button, button_long1, button_long2, addl, width, height, drawText);
    //    newMap.setHelp(help, flag, rms);
    //    gFlag = "CREATE_MAP";
    //}

    //private void createNewMapMenu() {
    //    mList = new MapList(width, height, rms.getLevelList(), bg_begin, board3, button, button_long1, button_long2, drawText);
    //    gFlag = "CREATE_MAP_MENU";
    //}

    //private void createNewMapBegin() {
    //    mParam = new MapParameters(width, height, (rms.getLevelList()).length + 1, bg_begin, button_long1, button_long3, drawText, this);
    //    gFlag = "CREATE_MAP_BEGIN";
    //}

    private void createTableRecords() {
        records = new Records(width, height, rms, bg_begin, board3, button, drawText);
        gFlag = "TABLE_RECORDS";
    }

    public void run() {
        while (true) {
            check();
            canvas.repaint(0, 0, width, height);
            try {
                Thread.sleep(1);
            } catch (InterruptedException ex) {}
        }
    }

    private void getFPS() {
        time = System.currentTimeMillis();
        delta = time - ptime;
        fcount++;
        if (delta >= 1000) {
            fps = fcount * 1000 / delta;
            fps_time = 1f / fps;
            ptime = time;
            fcount = 0;
        }
    }

    private void goMenu(int n) {
        if (beContinue) {
            switch (n) {
                case 0:
                    lbIndex = 0;
                    rms_ball = rms.getBallData();
                    cUser = rms.getNames()[rms.getQuantityNames() - 1];
                    offAd = true;
                    gFlag = "BEGIN";
                    break;
                case 1:
                    if (rpKey != 0) {
                        rpKey = 0;
                    }
                    String name = "Player";
                    int next = rms.getQuantityNames() + 1;
                    if (next > 1) {
                        name += " " + next;
                    }
                    userName.setValue(name);
                    gFlag = "NEW_GAME";
                    break;
                //case 2:
                //    createNewMapMenu();
                //    offAd = true;
                //    break;
                case 2:
                    createTableRecords();
                    if (records.getScrollBar() != null) {
                        records.getScrollBar().init();
                    }
                    offAd = true;
                    break;
                case 3:
                    stIndex = cIndex = 0;
                    gFlag = "SETTINGS";
                    break;
                case 4:
                    if(hScroll != null) hScroll.init();
                    gFlag = "HELP";
                    break;
                case 5:
                    tAbout = 0;
                    mAbout = 1;
                    drawText.setText(menu_text[2][0], width / 8 + 5, height / 2 - height / 4 + 19, width - width / 4 - 10, height / 2 - 34);
                    aScroll.init();
                    gFlag = "ABOUT";
                    break;
                case 6:
                    drawText.setText(menu_text[3][0], width / 8 + 5, height / 2 - height / 8 + 5, width - width / 4 - 10, height / 4 - 10);
                    gFlag = "EXIT";
                    break;
            }
        } else {
            switch (n) {
                case 0:
                    if (rpKey != 0) {
                        rpKey = 0;
                    }
                    String name = "Player";
                    int next = rms.getQuantityNames() + 1;
                    if (next > 1) {
                        name += " " + next;
                    }
                    userName.setValue(name);
                    gFlag = "NEW_GAME";
                    break;
                //case 1:
                //    createNewMapMenu();
                //    offAd = true;
                //    break;
                case 1:
                    createTableRecords();
                    if (records.getScrollBar() != null) {
                        records.getScrollBar().init();
                    }
                    offAd = true;
                    break;
                case 2:
                    stIndex = cIndex = 0;
                    gFlag = "SETTINGS";
                    break;
                case 3:
                    if(hScroll != null) hScroll.init();
                    gFlag = "HELP";
                    break;
                case 4:
                    tAbout = 0;
                    mAbout = 1;
                    drawText.setText(menu_text[2][0], width / 8 + 5, height / 2 - height / 4 + 19, width - width / 4 - 10, height / 2 - 34);
                    aScroll.init();
                    gFlag = "ABOUT";
                    break;
                case 5:
                    drawText.setText(menu_text[3][0], width / 8 + 5, height / 2 - height / 8 + 5, width - width / 4 - 10, height / 4 - 10);
                    gFlag = "EXIT";
                    break;
            }
        }
    }
    
    private void playBackground() {
        background = sound.play("background.mid", "audio/midi", -1);
    }

    private void check() {
        if (gFlag.equals("LOGO") || gFlag.equals("CURTAIN_LOGO") || gFlag.equals("CURTAIN_LANGUAGE_TO_SOUND") || gFlag.equals("CURTAIN_SOUND") || gFlag.equals("CURTAIN_SOUND_TO_MENU") || gFlag.equals("CURTAIN_MENU")) {
            if (pKey == -5 || pKey == 10 || pKey == Canvas.KEY_NUM5 || pKey == 103 || Wand.isAction(pX, pY)) {
                
                String bFlag = gFlag;
                
                label: {
                    if (gFlag.equals("LOGO") && rms_game[8] != 0) {
                        gFlag = "SOUND";
                        break label;
                    }
                    if(gFlag.equals("CURTAIN_LANGUAGE_TO_SOUND") || gFlag.equals("CURTAIN_SOUND")) {
                        gFlag = "SOUND";
                        break label;
                    }
                    if(gFlag.equals("CURTAIN_SOUND_TO_MENU") || gFlag.equals("CURTAIN_MENU")) {
                        wasMotion = true;
                        gFlag = "MENU";
                        break label;
                    }
                }
                
                if(!gFlag.equals(bFlag)) {
                    logo = null;
                    main_logo = null;
                    curtain = null;
                }
                
            }
            pKey = rKey = dKey = hKey = rpKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if (gFlag.equals("LANGUAGE")) {
            if (pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 10 || pKey == 103) {
                rms.setGameVar(lSelect + 1, 8);
                rms_game = rms.getGameData();
                Localization.setLocale(lSelect);
                getLocalization();
                gFlag = "CURTAIN_LANGUAGE_TO_SOUND";
            }
            if ((pKey == Canvas.KEY_NUM4 || pKey == -3 || pKey == 102 || Wand.region(pX, pY, width / 2 - board7.getWidth()/4 - eng.getWidth()/2, height / 2 - eng.getHeight()/2, eng.getWidth(), eng.getHeight()))) {
                if (lSelect > 0) {
                    lSelect--;
                } else {
                    if (pUsing && pKey == 0) {
                        rms.setGameVar(1, 8);
                        rms_game = rms.getGameData();
                        Localization.setLocale(Localization.EN);
                        getLocalization();
                        gFlag = "CURTAIN_LANGUAGE_TO_SOUND";
                    }
                }
            }
            if ((pKey == Canvas.KEY_NUM6 || pKey == -4 || pKey == 104 || Wand.region(pX, pY, width / 2 + board7.getWidth()/4 - rus.getWidth()/2, height / 2 - rus.getHeight()/2, rus.getWidth(), rus.getHeight()))) {
                if (lSelect < 1) {
                    lSelect++;
                } else {
                    if (pUsing && pKey == 0) {
                        rms.setGameVar(2, 8);
                        rms_game = rms.getGameData();
                        Localization.setLocale(Localization.RU);
                        getLocalization();
                        gFlag = "CURTAIN_LANGUAGE_TO_SOUND";
                    }
                }
            }
            pKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if (gFlag.equals("SOUND")) {
            if(pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 10 || pKey == 103) {
                if(cIndex == 0) {
                    sound.soundOn();
                } else {
                    sound.soundOff();
                }
                playBackground();
                gFlag = "CURTAIN_SOUND_TO_MENU";
            }
            if(pKey == Canvas.KEY_NUM2 || pKey == -1 || pKey == 116 || Wand.region(pX, pY, width/2-old_frame.getWidth()/2, height/2-old_frame.getHeight()-5, old_frame.getWidth(), old_frame.getHeight())) {
                if(sIndex == 0 && pKey == 0) {
                    sound.soundOn();
                    playBackground();
                    gFlag = "CURTAIN_SOUND_TO_MENU";
                } else {
                    sIndex = 0;
                }
            }
            if(pKey == Canvas.KEY_NUM8 || pKey == -2 || pKey == 98 || Wand.region(pX, pY, width/2-old_frame.getWidth()/2, height/2+5, old_frame.getWidth(), old_frame.getHeight())) {
                if(sIndex == 1 && pKey == 0) {
                    sound.soundOff();
                    playBackground();
                    gFlag = "CURTAIN_SOUND_TO_MENU";
                } else {
                    sIndex = 1;
                }
            }
            pKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if(!showTurnOffAds) {
            if(!pGame && (gFlag.equals("MENU") || gFlag.endsWith("NEW_GAME") || gFlag.equals("HELP") || gFlag.equals("ABOUT") || gFlag.equals("EXIT"))) checkButtonTurnOffAds();
            
            if (gFlag.equals("MENU")) {

                if(!pGame && workAds) {
                    if(offAd) {
                        ad.loadAd();
                        offAd = false;
                    }
                    ad.adCheck(pX, pY);
                }

                if (pKey == -7 || pKey == 121) getPriceToTurnOffAds();
                
                if (pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 10 || pKey == 103) {
                    goMenu(mIndex);
                }
                if (pKey == Canvas.KEY_NUM2 || pKey == -1 || pKey == 116) {
                    int mY = -mScroll.getValue();
                    if (mIndex > 0) {
                        mIndex--;
                        if (mY + mH * mIndex < 0) {
                            mY += mH;
                        }
                        mScroll.setValue(-mY);
                    }
                    if (mY + mIndex * mH < 0) {
                        mIndex = (-mY + 10) / mH;
                    }
                    if (mY + mIndex * mH > mHeight) {
                        mIndex = (-mY + mHeight) / mH;
                    }
                }
                if (pKey == Canvas.KEY_NUM8 || pKey == -2 || pKey == 98) {
                    int mY = -mScroll.getValue();
                    if (mIndex < menu_points.length - (!beContinue ? 2 : 1)) {
                        mIndex++;
                        if (mY + mH * mIndex > mHeight - mH + 10) {
                            mY -= mH;
                        }
                        mScroll.setValue(-mY);
                    }
                    if (mY + mIndex * mH < 0) {
                        mIndex = (-mY + 10) / mH;
                    }
                    if (mY + mIndex * mH > mHeight) {
                        mIndex = (-mY + mHeight) / mH;
                    }
                }
                for (int i = 0; i < menu_points.length; i++) {
                    int index = (!beContinue ? i - 1 : i);
                    if (Wand.region(rX, rY, width / 2 - box_frame.getWidth() / 2 - 14, 140 - mScroll.getValue() + index * mH - (i == menu_points.length - 1 ? 10 : 0), box_frame.getWidth(), box_frame.getHeight()) && !wasMotion && !wasBack) {
                        if (rY >= 140 && rY < 140 + mHeight) {
                            if (index == mIndex) {
                                goMenu(mIndex);
                            } else {
                                mIndex = index;
                            }
                            break;
                        }
                    }
                }
                if (Wand.isAction(rX, rY)) {
                    wasBack = false;
                }
                if (!wasMotion) {
                    if (Wand.isAction(dX, dY)) {
                        wasMotion = true;
                    }
                } else {
                    if (Wand.isAction(rX, rY)) {
                        wasMotion = false;
                    }
                }
                mScroll.pCheck(fps_time, pX, pY, rX, rY, dX, dY);

                pX = pY = rX = rY = dX = dY = -1;
                pKey = 0;
            }
            if (gFlag.equals("NEW_GAME")) {
                switch (Localization.getLocale()) {
                    case Localization.EN:
                        if (pKey == -6 || pKey == 10 || pKey == 114 || Wand.region(pX, pY, width / 8 + 17 + drawText.stringWidth(menu_text[0][1]) / 2 - button.getWidth() / 2 - 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 7, button.getWidth() + 4, button.getHeight() + 4)) {
                            rms.addName(userName.getValue());
                            cUser = rms.getNames()[rms.getQuantityNames() - 1];
                            if (!beContinue) {

                                beContinue = true;

                                mScroll = new ScrollBar(width / 2 - box_frame.getWidth() / 2 - 14, 140, box_frame.getWidth() + 10, mHeight, mH * (!beContinue ? menu_points.length - 1 : menu_points.length) - mHeight - 10, mH * (!beContinue ? menu_points.length - 1 : menu_points.length) - 10);
                            }
                            lbIndex = 0;
                            rms_game = rms.getGameData();
                            int back5 = rms_game[5], back6 = rms_game[6], back7 = rms_game[7], back8 = rms_game[8];
                            rms.flush();
                            rms_game = rms.getGameData();
                            rms.setGameVar(back5, 5);
                            rms.setGameVar(back6, 6);
                            rms.setGameVar(back7, 7);
                            rms.setGameVar(back8, 8);
                            rms_ball = rms.getBallData();
                            sdIndex = 0;
                            gFlag = "SD";
                        }
                        if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width - width / 8 - drawText.stringWidth(menu_text[0][2]) - 15 + drawText.stringWidth(menu_text[0][2]) / 2 - button_long1.getWidth() / 2 - 2, height / 2 + height / 8 + 7 - button_long1.getHeight() / 2 - 7, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
                            wasBack = true;
                            gFlag = "MENU";
                        }
                        break;
                    case Localization.RU:
                        if (pKey == -6 || pKey == 10 || pKey == 114 || Wand.region(pX, pY, width / 8 + 17 + drawText.stringWidth(menu_text[0][2]) / 2 - button.getWidth() / 2 - 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 7, button.getWidth() + 4, button.getHeight() + 4)) {
                            rms.addName(userName.getValue());
                            cUser = rms.getNames()[rms.getQuantityNames() - 1];
                            if (!beContinue) {

                                beContinue = true;

                                mScroll = new ScrollBar(width / 2 - box_frame.getWidth() / 2 - 14, 140, box_frame.getWidth() + 10, mHeight, mH * (!beContinue ? menu_points.length - 1 : menu_points.length) - mHeight - 10, mH * (!beContinue ? menu_points.length - 1 : menu_points.length) - 10);
                            }
                            lbIndex = 0;
                            rms_game = rms.getGameData();
                            int back5 = rms_game[5], back6 = rms_game[6], back7 = rms_game[7], back8 = rms_game[8];
                            rms.flush();
                            rms_game = rms.getGameData();
                            rms.setGameVar(back5, 5);
                            rms.setGameVar(back6, 6);
                            rms.setGameVar(back7, 7);
                            rms.setGameVar(back8, 8);
                            rms_ball = rms.getBallData();
                            sdIndex = 0;
                            gFlag = "SD";
                        }
                        if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width - width / 8 - drawText.stringWidth(menu_text[0][3]) - 13 + drawText.stringWidth(menu_text[0][3]) / 2 - button_long1.getWidth() / 2 - 2, height / 2 + height / 8 + 7 - button_long1.getHeight() / 2 - 7, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
                            wasBack = true;
                            gFlag = "MENU";
                        }
                        break;
                }
                if (Wand.region(pX, pY, width / 2 - userName.getWidth() / 2, height / 2 - height / 8 + 42, (width - width / 4) / 3 * 2, 6 + 14 + 6)) {
                    String title = menu_text[0][0];
                    if (Localization.getLocale() == Localization.RU) {
                        title += " " + menu_text[0][1];
                    }
                    textBox = new TextBox(title, userName.getValue(), 64, TextField.ANY);
                    ok = new Command("OK", Command.OK, 0);
                    cancel = new Command(Localization.getAloneText(52), Command.CANCEL, 0);
                    textBox.addCommand(ok);
                    textBox.addCommand(cancel);
                    textBox.setCommandListener(this);
                    rb2MIDlet.display.setCurrent(textBox);
                }
                userName.check(pKey, rpKey);
                pKey = rpKey = 0;
                pX = pY = rX = rY = dX = dY = -1;
            }
            if (gFlag.equals("HELP") || gFlag.equals("ABOUT")) {
                if (pKey == -7 || pKey == 8 || pKey == 121) {
                    gFlag = "MENU";
                }
                if (gFlag.equals("HELP")) {
                    if (Wand.region(pX, pY, width / 2 + board3.getWidth() / 2 - button.getWidth()-12 - 2, height/2+board3.getHeight()/2-button.getHeight()-8 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
                        wasBack = true;
                        gFlag = "MENU";
                    }
                    if(hScroll != null) hScroll.pCheck(fps_time, pX, pY, rX, rY, dX, dY);
                    pX = pY = rX = rY = dX = dY = -1;
                }
                if (gFlag.equals("ABOUT")) {
                    if (Wand.region(pX, pY, width - width / 8 - drawText.stringWidth(menu_text[2][1]) - 15 + drawText.stringWidth(menu_text[2][1]) / 2 - button.getWidth() / 2 - 2, height / 2 + height / 4 + 7 - button.getHeight() / 2 - 10, button.getWidth() + 4, button.getHeight() + 4)) {
                        wasBack = true;
                        gFlag = "MENU";
                    }
                    if(mAbout != 1) {
                        aScroll.pCheck(fps_time, pX, pY, rX, rY, dX, dY);
                        drawText.setY(-aScroll.getValue());
                    }
                    if (Wand.region(pX, pY, width / 8 + 5, height / 2 - height / 4 + 5, width - width / 4 - 10, height / 2 - 10)) {
                        mAbout = 0;
                    }
                    pX = pY = rX = rY = dX = dY = -1;
                }
                pKey = 0;
            }
            if (gFlag.equals("EXIT")) {
                if (pKey == -6 || pKey == 10 || pKey == 114 || Wand.region(pX, pY, width / 8 + 20 + drawText.stringWidth(menu_text[3][1]) / 2 - button.getWidth() / 2 - 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 7, button.getWidth() + 4, button.getHeight() + 4)) {
                    rb2MIDlet.exit();
                }
                if (pKey == -7 || pKey ==  8|| pKey == 121 || Wand.region(pX, pY, width - width / 8 - drawText.stringWidth(menu_text[3][2]) - 23 + drawText.stringWidth(menu_text[3][2]) / 2 - button.getWidth() / 2 - 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 7, button.getWidth() + 4, button.getHeight() + 4)) {
                    wasBack = true;
                    gFlag = "MENU";
                }
                pKey = 0;
            }
            if (gFlag.equals("HELP") || gFlag.equals("ABOUT")) {
                if (dKey == Canvas.KEY_NUM2 || dKey == -1 || dKey == 116) {
                    if (gFlag.equals("ABOUT")) {
                        aScroll.setValue(aScroll.getValue() - 1);
                        if(mAbout != 0) {
                            mAbout = 0;
                        }
                    }
                    if (gFlag.equals("HELP") && hScroll != null) {
                        hScroll.setValue(hScroll.getValue() - 1);
                    }
                }
                if (dKey == Canvas.KEY_NUM8 || dKey == -2 || dKey == 98) {
                    if (gFlag.equals("ABOUT")) {
                        aScroll.setValue(aScroll.getValue() + 1);
                        if(mAbout != 0) {
                            mAbout = 0;
                        }
                    }
                    if (gFlag.equals("HELP") && hScroll != null) {
                        hScroll.setValue(hScroll.getValue() + 1);
                    }
                }
                
                pX = pY = rX = rY = dX = dY = -1;
            }
        }
        
        if (gFlag.equals("BEGIN")) {
            if (pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 10 || pKey == 103) {
                switch (lbIndex) {
                    case 0:
                        createLevel(new MLoader("/resources/maps.dat", rms.getGameLevel(rms_game)), "");
                        lift.inStart();
                        sb.startShowlevelBegin();
                        if(rms_game[13] == 0) {
                            if(hScroll != null) hScroll.init();
                            hFirst = true;
                        }
                        gFlag = "LIFT_ENTER";
                        break;
                    case 1:
                        bIndex = 0;
                        iScroll.init();
                        gFlag = "IMPROVEMENTS";
                        break;
                    case 2:
                        slIndex = 0;
                        gFlag = "SL";
                        break;
                    case 3:
                        mIndex = 0;
                        mScroll.init();
                        wasBack = true;
                        gFlag = "MENU";
                        break;
                }
            }
            int fH = old_frame.getHeight() + 5;
            if (pKey == Canvas.KEY_NUM2 || pKey == -1 || pKey == 116) {
                if (lbIndex > 0) {
                    lbIndex--;
                    if (lbY + fH * lbIndex < 0) {
                        lbY += fH;
                    }
                }
            }
            if (pKey == Canvas.KEY_NUM8 || pKey == -2 || pKey == 116) {
                if (lbIndex < 3) {
                    lbIndex++;
                    if (lbY + fH * lbIndex > lbHeight - fH) {
                        lbY -= fH;
                    }
                }
            }
            for (int i = 0; i < 4; i++) {
                if (Wand.region(rX, rY, width / 2 - old_frame.getWidth() / 2 - 2, 158 + i * (old_frame.getHeight() + 8) - 2, old_frame.getWidth() + 4, old_frame.getHeight() + 4)) {
                    if (lbIndex != i) {
                        lbIndex = i;
                    } else {
                        switch (lbIndex) {
                            case 0:
                                createLevel(new MLoader("/resources/maps.dat", rms.getGameLevel(rms_game)), "");
                                lift.inStart();
                                sb.startShowlevelBegin();
                                if(rms_game[13] == 0) {
                                    if(hScroll != null) hScroll.init();
                                    hFirst = true;
                                }
                                gFlag = "LIFT_ENTER";
                                break;
                            case 1:
                                bIndex = 0;
                                iScroll.init();
                                gFlag = "IMPROVEMENTS";
                                break;
                            case 2:
                                slIndex = 0;
                                gFlag = "SL";
                                break;
                            case 3:
                                mIndex = 0;
                                mScroll.init();
                                wasBack = true;
                                gFlag = "MENU";
                                break;
                        }
                    }
                    break;
                }
            }
            pKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if (gFlag.equals("IMPROVEMENTS")) {
            if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width - drawText.stringWidth(Localization.getAloneText(39)) - 10 + drawText.stringWidth(Localization.getAloneText(39)) / 2 - button.getWidth() / 2 - 2, height - 20 + 7 - button.getHeight() / 2 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
                gFlag = "BEGIN";
            }
            if (pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 10 || pKey == 103) {
                int points = bIndex == 0 ? 1500 : bIndex == 1 ? 2000 : 2500;
                if (rms_game[1] >= points) {
                    rms.setBallVar(rms_ball[bIndex] + (bIndex != 2 ? 0.1f : -2), bIndex);
                    rms.setGameVar(rms_game[1] - points, 1);
                    rms_game = rms.getGameData();
                    rms_ball = rms.getBallData();
                } else {
                    pTime = System.currentTimeMillis();
                }
            }
            int lY = -iScroll.getValue();
            if ((pKey == Canvas.KEY_NUM2 || pKey == -1 || pKey == 116) && bIndex > 0) {
                bIndex--;
                if (bIndex >= 0 && lY + lsY[bIndex] < 0) {
                    iScroll.setValue(iScroll.getValue() + (lY + lsY[bIndex]));
                }
            }
            if ((pKey == Canvas.KEY_NUM8 || pKey == -2 || pKey == 98) && bIndex < 2) {
                bIndex++;
                if (bIndex >= 0 && lY + lsY[bIndex] > lHeight - lsH[bIndex] - 12) {
                    iScroll.setValue(iScroll.getValue() + (lY + lsY[bIndex]) - (lHeight - lsH[bIndex] - 12));
                }
            }
            for (int i = 0; i < 3; i++) {
                if (Wand.region(rX, rY, 5, 158 + lY + lsY[i], width - 10, lsH[i]) && !wasMotion) {
                    if (rY >= 153 && rY < 158 + ((height - 16) - 158)) {
                        if (i == bIndex) {
                            int points = bIndex == 0 ? 1500 : bIndex == 1 ? 2000 : 2500;
                            if (rms_game[1] >= points) {
                                rms.setBallVar(rms_ball[bIndex] + (bIndex != 2 ? 0.1f : -2), bIndex);
                                rms.setGameVar(rms_game[1] - points, 1);
                                rms_game = rms.getGameData();
                                rms_ball = rms.getBallData();
                            } else {
                                pTime = System.currentTimeMillis();
                            }
                        } else {
                            bIndex = i;
                        }
                    }
                    break;
                }
            }
            if (!wasMotion) {
                if (Wand.isAction(dX, dY)) {
                    wasMotion = true;
                }
            } else {
                if (Wand.isAction(rX, rY)) {
                    wasMotion = false;
                }
            }
            iScroll.pCheck(fps_time, pX, pY, rX, rY, dX, dY);
            pX = pY = rX = rY = dX = dY = -1;
            pKey = 0;
        }
        if (gFlag.equals("SL")) {
            if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width - drawText.stringWidth(Localization.getAloneText(39)) - 14 + drawText.stringWidth(Localization.getAloneText(39)) / 2 - button.getWidth() / 2 - 2, height - 19 + 7 - button.getHeight() / 2, drawText.stringWidth(Localization.getAloneText(39)) + 4, button.getHeight() + 4)) {
                gFlag = "BEGIN";
            }
            if (pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 10 || pKey == 103) {
                createLevel(new MLoader("/resources/maps.dat", slIndex + 1), "JPR");
                lift.inStart();
                sb.startShowlevelBegin();
                if(rms_game[13] == 0) {
                    if(hScroll != null) hScroll.init();
                    hFirst = true;
                }
                gFlag = "LIFT_ENTER";
            }
            int fH = old_frame.getHeight() + 5;
            if (pKey == Canvas.KEY_NUM2 || pKey == -1 || pKey == 116 || Wand.region(pX, pY, width / 2 - 10, 150 - 20, 20, 10)) {
                if (slIndex > 0) {
                    if (slY + fH * (slIndex - 1) < 0) {
                        slColorUp = 0xb97a57;
                    } else {
                        slIndex--;
                    }
                }
                if (slColorUp != 0xb97a57) {
                    if (slY + slIndex * fH < 0) {
                        slIndex = (-slY + 10) / fH;
                    }
                    if (slY + slIndex * fH > mlHeight) {
                        slIndex = (-slY + mlHeight) / fH;
                    }
                    if (slIndex >= rms.getGameLevel(rms_game)) {
                        slIndex = rms.getGameLevel(rms_game) - 1;
                    }
                }
            }
            if ((rKey == Canvas.KEY_NUM2 || rKey == -1 || rKey == 116 || Wand.region(rX, rY, width / 2 - 10, 150 - 20, 20, 10)) && slColorUp == 0xb97a57) {
                slIndex--;
                slY += fH;
                slColorUp = 0xd0d0d0;
                
                //sScroll.setValue(-slY);
            }
            if (pKey == Canvas.KEY_NUM8 || pKey == -2 || pKey == 98 || Wand.region(pX, pY, width / 2 - 10, 150 + mlHeight + 15, 20, 10)) {
                if (slIndex < rms.getGameLevel(rms_game) - 1) {
                    if (slY + fH * (slIndex + 1) > mlHeight - fH + 5) {
                        slColorDown = 0xb97a57;
                    } else {
                        slIndex++;
                    }
                }
                if (slColorUp != 0xb97a57) {
                    if (slY + slIndex * fH < 0) {
                        slIndex = (-slY + 10) / fH;
                    }
                    if (slY + slIndex * fH > mlHeight) {
                        slIndex = (-slY + mlHeight) / fH;
                    }
                    if (slIndex >= rms.getGameLevel(rms_game)) {
                        slIndex = rms.getGameLevel(rms_game) - 1;
                    }
                }
            }
            if ((rKey == Canvas.KEY_NUM8 || rKey == -2 || rKey == 98 || Wand.region(rX, rY, width / 2 - 10, 150 + mlHeight + 15, 20, 10)) && slColorDown == 0xb97a57) {
                slIndex++;
                slY -= fH;
                slColorDown = 0xd0d0d0;
                
                //sScroll.setValue(-slY);
            }
            
            for (int i = 0; i < countOfMaps; i++) {
                if (Wand.region(rX, rY, width / 2 - old_frame.getWidth() / 2, 150 + slY + i * (old_frame.getHeight() + 5), old_frame.getWidth(), old_frame.getHeight())) {
                    if (rY >= 150 && rY < 150 + mlHeight + 5) {
                        if (i == slIndex) {
                            createLevel(new MLoader("/resources/maps.dat", slIndex + 1), "JPR");
                            lift.inStart();
                            sb.startShowlevelBegin();
                            gFlag = "LIFT_ENTER";
                        } else {
                            if (i < rms.getGameLevel(rms_game)) {
                                slIndex = i;
                            }
                        }
                    }
                    break;
                }
            }
            //if (!wasMotion) {
            //    if (Wand.isAction(dX, dY)) {
            //        wasMotion = true;
            //    }
            //} else {
            //    if (Wand.isAction(rX, rY)) {
            //        wasMotion = false;
            //    }
            //}
            
            pX = pY = rX = rY = dX = dY = -1;
            pKey = rKey = 0;
        }
        if (!showTurnOffAds && gFlag.equals("SD")) {
            if (!nSelectDiff) {
                if (pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 10 || pKey == 103) {
                    if (rms_game[5] == 0 && sdIndex == 2) {
                        nSelectDiff = true;
                    } else {
                        rms.setGameVar(sdIndex + 1, 4);
                        rms_game = rms.getGameData();
                        offAd = true;
                        gFlag = "BEGIN";
                    }
                }
                int fH = old_frame.getHeight() + 15;
                if (pKey == Canvas.KEY_NUM2 || pKey == -1 || pKey == 116) {
                    if (sdIndex > 0) {
                        sdIndex--;
                        if (sdY + fH * sdIndex < 0) {
                            sdY += fH;
                        }
                    }
                }
                if (pKey == Canvas.KEY_NUM8 || pKey == -2 || pKey == 98) {
                    if (sdIndex < 2) {
                        sdIndex++;
                        if (sdY + fH * sdIndex > mHeight - fH) {
                            sdY -= fH;
                        }
                    }
                }
                for (int i = 0; i < 3; i++) {
                    if (Wand.region(rX, rY, width / 2 - drawText.stringWidth(begin_text[12 + i]) / 2 - 2, 140 + sdY + i * (old_frame.getHeight() + 15) + old_frame.getHeight() / 2 - 9, drawText.stringWidth(begin_text[12 + i]) + 4, old_frame.getHeight() + 4)) {
                        if (sdIndex != i) {
                            sdIndex = i;
                        } else {
                            if (rms_game[5] == 0 && sdIndex == 2) {
                                nSelectDiff = true;
                            } else {
                                rms.setGameVar(sdIndex + 1, 4);
                                rms_game = rms.getGameData();
                                offAd = true;
                                gFlag = "BEGIN";
                            }
                        }
                    }
                }
            } else {
                if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width / 2 + board1.getWidth() / 2 - drawText.stringWidth(Localization.getAloneText(39)) - 16 + drawText.stringWidth(Localization.getAloneText(39)) / 2 - button.getWidth() / 2 - 2, height / 2 + board1.getHeight() / 2 - 22, button.getWidth() + 4, button.getHeight() + 4)) {
                    nSelectDiff = false;
                }
            }
                
            if(!pGame) checkButtonTurnOffAds();
                
            pKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if (!showTurnOffAds && gFlag.equals("SETTINGS")) {
            if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width/2+board6.getWidth()/2-button.getWidth()-15, height / 2 - board6.getHeight() / 2 + 18 + stY[4] + 1, button.getWidth() + 12, button.getHeight() + 12)) {
                wasBack = true;
                gFlag = "MENU";
            }
            if ((pKey == Canvas.KEY_NUM2 || pKey == -1 || pKey == 116) && stIndex > 0) {
                stIndex--;
            }
            if ((pKey == Canvas.KEY_NUM8 || pKey == -2 || pKey == 98) && stIndex < stY.length-1) {
                stIndex++;
            }
            switch (stIndex) {
                case 0:
                    if ((pKey == Canvas.KEY_NUM4 || pKey == -3 || pKey == 102) && sound.isEnable()) {
                        slColor[0] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM4 || rKey == -3 || rKey == 102) && slColor[0] == 0xb97a57) {
                        rms.setGameVar(0, 13);
                        rms_game = rms.getGameData();
                        sSlider[0].setIndex(0);
                        slColor[0] = 0xd0d0d0;
                    }
                    if ((pKey == Canvas.KEY_NUM6 || pKey == -4 || pKey == 104) && !sound.isEnable()) {
                        slColor[1] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM6 || rKey == -4 || rKey == 104) && slColor[1] == 0xb97a57) {
                        rms.setGameVar(1, 13);
                        rms_game = rms.getGameData();
                        sSlider[0].setIndex(1);
                        slColor[1] = 0xd0d0d0;
                    }
                    break;
                case 1:
                    if ((pKey == Canvas.KEY_NUM4 || pKey == -3 || pKey == 102) && rms_game[4] > 1) {
                        slColor[2] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM4 || rKey == -3 || rKey == 102) && slColor[2] == 0xb97a57) {
                        rms.setGameVar(rms_game[4] - 1, 4);
                        rms_game = rms.getGameData();
                        sSlider[1].setIndex(rms_game[4] - 1);
                        slColor[2] = 0xd0d0d0;
                    }
                    if ((pKey == Canvas.KEY_NUM6 || pKey == -4 || pKey == 104) && rms_game[4] < (rms_game[5] == 0 ? 2 : 3)) {
                        slColor[3] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM6 || rKey == -4 || rKey == 104) && slColor[3] == 0xb97a57) {
                        rms.setGameVar(rms_game[4] + 1, 4);
                        rms_game = rms.getGameData();
                        sSlider[1].setIndex(rms_game[4] - 1);
                        slColor[3] = 0xd0d0d0;
                    }
                    break;
                case 2:
                    if ((pKey == Canvas.KEY_NUM4 || pKey == -3 || pKey == 102) && rms_game[8] == 2) {
                        slColor[4] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM4 || rKey == -3 || rKey == 102) && slColor[4] == 0xb97a57) {
                        rms.setGameVar(1, 8);
                        rms_game = rms.getGameData();
                        sSlider[2].setIndex(rms_game[8] - 1);
                        Localization.setLocale(0);
                        getLocalization();
                        slColor[4] = 0xd0d0d0;
                    }
                    if ((pKey == Canvas.KEY_NUM6 || pKey == -4 || pKey == 104) && rms_game[8] == 1) {
                        slColor[5] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM6 || rKey == -4 || rKey == 104) && slColor[5] == 0xb97a57) {
                        rms.setGameVar(2, 8);
                        rms_game = rms.getGameData();
                        sSlider[2].setIndex(rms_game[8] - 1);
                        Localization.setLocale(1);
                        getLocalization();
                        slColor[5] = 0xd0d0d0;
                    }
                    break;
                case 3:
                    if ((pKey == Canvas.KEY_NUM4 || pKey == -3 || pKey == 102) && rms_game[6] > 0) {
                        slColor[6] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM4 || rKey == -3 || rKey == 102) && slColor[6] == 0xb97a57) {
                        rms.setGameVar(rms_game[6] - 1, 6);
                        rms_game = rms.getGameData();
                        sSlider[3].setIndex(rms_game[6]);
                        slColor[6] = 0xd0d0d0;
                    }
                    if ((pKey == Canvas.KEY_NUM6 || pKey == -4 || pKey == 104) && rms_game[6] < 1) {
                        slColor[7] = 0xb97a57;
                    }
                    if ((rKey == Canvas.KEY_NUM6 || rKey == -4 || rKey == 104) && slColor[7] == 0xb97a57) {
                        rms.setGameVar(rms_game[6] + 1, 6);
                        rms_game = rms.getGameData();
                        sSlider[3].setIndex(rms_game[6]);
                        slColor[7] = 0xd0d0d0;
                    }
                    break;
                case 4:
                    if ((pKey == Canvas.KEY_NUM4 || pKey == -3 || pKey == 102) && cIndex > 0) {
                        cIndex--;
                    }
                    if ((pKey == Canvas.KEY_NUM6 || pKey == -4 || pKey == 104) && cIndex < 1) {
                        cIndex++;
                    }
                    if ((pKey == Canvas.KEY_NUM5 || pKey == -5 || pKey == 103) && cIndex == 1) {
                        rms.deleteAll();
                        rb2MIDlet.exit();
                    }
                    int bX = width / 2 - board6.getWidth() / 2 + 25,
                    bY = height / 2 - board6.getHeight() / 2 + 18;
                    if (Wand.region(pX, pY, bX + drawText.stringWidth(Localization.getAloneText(34)) + 10, bY + stY[4], drawText.stringWidth(Localization.getAloneText(35)) + 4, 16)) {
                        cIndex = 0;
                    }
                    if (Wand.region(pX, pY, bX + drawText.stringWidth(Localization.getAloneText(36)) + 10, bY + stY[4], drawText.stringWidth(Localization.getAloneText(37)) + 4, 16)) {
                        if (cIndex != 1) {
                            cIndex = 1;
                        } else {
                            rms.deleteAll();
                            rb2MIDlet.exit();
                        }
                    }
                    break;
            }
            for (int i = 0; i < sXYWH.length; i++) {
                if (Wand.region(pX, pY, sXYWH[i][0], sXYWH[i][1], sXYWH[i][2], sXYWH[i][3])) {
                    stIndex = i;
                }
            }

            for (int i = 0; i < sSlider.length; i++) {
                sSlider[i].pCheck(pX, pY, rX, rY, dX, dY);
                if (sSlider[i].hasValue()) {
                    if(i == 0) {
                        if(sSlider[0].getIndex() == 0) {
                            sound.soundOff();
                        } else {
                            sound.soundOn();
                        }
                    }
                    if (i == 1) {
                        rms.setGameVar(sSlider[1].getIndex() + 1, 4);
                        rms_game = rms.getGameData();
                    }
                    if (i == 2) {
                        rms.setGameVar(sSlider[2].getIndex() + 1, 8);
                        rms_game = rms.getGameData();
                        Localization.setLocale(rms_game[8] - 1);
                        getLocalization();
                    }
                    if (i == 3) {
                        rms.setGameVar(sSlider[3].getIndex(), 6);
                        rms_game = rms.getGameData();
                    }
                }
            }
            
            if(!pGame) checkButtonTurnOffAds();
            
            pKey = rKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        //if (gFlag.equals("PURCHASE")) {
        //    if(pMode == 0) {
        //        int X = bsX+(width/2-board6.getWidth()/2)+(board6.getWidth()-9), Y = height/2+board6.getHeight()/2-22;
        //        if(Localization.getLocale() == Localization.EN) {
        //            if(hasIAP && !fDetails && (pKey == -7 || pKey == 10 || pKey == 121 || Wand.region(pX, pY, X+board6.getWidth()-button_long1.getWidth()-17, Y-2, button_long1.getWidth()+4, button_long1.getHeight()+4))) {
        //                if(fAds) {
        //                    fAds = false;
        //                } else {
        //                    boolean status = iap.purchaseProduct();
        //                    if(!status) {
        //                        fAds = true;
        //                    }
        //                }
        //            }
        //            if(pKey == -6 || pKey == 8 || pKey == 114 || Wand.region(pX, pY, X+13, Y-2, button.getWidth()+4, button.getHeight()+4)) {
        //                pMode = 2;
        //            }
        //        } else {
        //            if(hasIAP && !fDetails && (pKey == -7 || pKey == 10 || pKey == 121 || Wand.region(pX, pY, X+board6.getWidth()-button_long2.getWidth()-17, Y-2, button_long2.getWidth()+4, button_long2.getHeight()+4))) {
        //                if(fAds) {
        //                    fAds = false;
        //                } else {
        //                    boolean status = iap.purchaseProduct();
        //                    if(!status) {
        //                        fAds = true;
        //                    }
        //                }
        //            }
        //            if(pKey == -6 || pKey == 8 || pKey == 114 || Wand.region(pX, pY, X+13, Y-2, button_long1.getWidth()+4, button_long1.getHeight()+4)) {
        //                pMode = 2;
        //            }
        //        }
        //    } else {
        //        switch(pMode) {
        //            case 1:
        //                bsX -= 10;
        //                break;
        //            case 2:
        //                bsX += 10;
        //                break;
        //        }
        //        boolean change = false;
        //        if(pMode == 1 && bsX <= -(board6.getWidth()-9)) {
        //            bsX = -(board6.getWidth()-9);
        //            pMode = 0; change = true;
        //        }
        //        if(pMode == 2 && bsX >=  0) {
        //            bsX = 0;
        //            fAds = fDetails = false;
        //            gFlag = "SETTINGS";
        //            pMode = 0; change = true;
        //        }
        //        if(!change) {
        //            for(int i=0; i<sSlider.length; i++) {
        //                sSlider[i].setX(sSlider[i].getX()-(pMode == 1 ? 10 : -10));
        //            }
        //        }
        //    }
        //    pKey = 0;
        //    pX = pY = rX = rY = dX = dY = -1;
        //}
        if (gFlag.equals("CAST")) {
            if ((pKey == -5 || pKey == 10 || pKey == Canvas.KEY_NUM5 || pKey == 103 || Wand.isAction(rX, rY)) && cMode == 2 && gipY + 14 < height - 12) {
                drawText.setColorWithAlpha(false);
                cast = null;
                gFlag = "BEGIN";
            }
            pKey = 0; pX = pY = rX = rY = dX = dY = -1;
        }
        
        //if (!hFirst && (gFlag.equals("GAME") || gFlag.equals("LIFT_ENTER") || gFlag.equals("LIFT_EXIT"))) {
        //    if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(rX, rY, !pUsing ? width - 28 : 0, !pUsing ? height - 30 : 0, 28, 30)) {
        //        sb.pause();
        ///       pauseFlag = gFlag;
        //        gFlag = "PAUSE";
        //    }
        //}
        
        if (gFlag.equals("GAME")) {
            if (pKey == -7 || pKey == 8 || pKey == 121 || pKey == Canvas.KEY_STAR || Wand.region(rX, rY, !pUsing ? width - 28 : 0, !pUsing ? height - 30 : 0, 28, 30)) {
                sb.pause();
                pauseFlag = gFlag;
                gFlag = "PAUSE";
            }
            
            if (keys[KEY_FIRE] || keys[KEY_LEFT] || keys[KEY_RIGHT]) {
                if (keys[KEY_FIRE]) {
                    ball.jump();
                }
                if (keys[KEY_LEFT]) {
                    ball.startMove(1);
                    if (bKey == 0) {
                        bKey = 1;
                    }
                }
                if (keys[KEY_RIGHT]) {
                    ball.startMove(2);
                    if (bKey == 0) {
                        bKey = 2;
                    }
                }
            } else {
                if (bKey != 0) {
                    ball.stopMove(bKey);
                    bKey = 0;
                }
            }
            if (thorns.check(sBall)) {
                otime = System.currentTimeMillis() - gtime;
                ball.destroy(destruction);
                gFlag = "GAME_OVER";
            } else {
                ball.updateMove(fps_time);
                ball.updateJump(fps_time);
                ball.updateFall(fps_time);
                ball.updateFans(fps_time);
            }
            if (!noTime) {
                if (System.currentTimeMillis() - gtime > levelTime) {
                    otime = System.currentTimeMillis() - gtime;
                    gFlag = "TIME_OUT";
                }
            }
            if (sBall.getX() > liftOut[0] && sBall.getX() < liftOut[0] + sLiftOut.getWidth() && sBall.getY() > liftOut[1] - sBall.getHeight() && sBall.getY() < liftOut[2]) {
                sBall.setPosition(sLiftOut.getX() + sLiftOut.getWidth() / 2 - sBall.getWidth() / 2, sLiftOut.getY() + sLiftOut.getHeight() - sBall.getHeight());
                otime = System.currentTimeMillis() - gtime;
                lift.outStart();
                sb.startShowLevelEnd();
                if(otime < levelTime) {
                    nPoints = levelPoints + (int)((levelTime - otime) / 1000);
                } else {
                    nPoints = levelPoints+15;
                }
                gFlag = "LIFT_EXIT";
            }
            
            if (joystick != null && gFlag.equals("GAME")) {
                joystick.pCheck(pX, pY, rX, rY, dX, dY);
            }

            pKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if (gFlag.equals("LIFT_ENTER")) {
            if(!hFirst) {
                if (pKey == -7 || pKey == 8 || pKey == 121 || pKey == Canvas.KEY_STAR || Wand.region(rX, rY, !pUsing ? width - 28 : 0, !pUsing ? height - 30 : 0, 28, 30)) {
                    sb.pause();
                    pauseFlag = gFlag;
                    gFlag = "PAUSE";
                }
                if (!lift.isIn()) {
                    lift.move(fps_time);
                } else {
                    gtime = System.currentTimeMillis();
                    gFlag = "GAME";
                }
            } else {
                if(pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width / 2 + board3.getWidth() / 2 - button_long1.getWidth()-12 - 2, height/2+board3.getHeight()/2-button_long1.getHeight()-8 - 2, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
                    rms.setGameVar(1, 13);
                    rms_game = rms.getGameData();
                    hFirst = false;
                }
                if(hScroll != null && dKey == -1 && dKey == Canvas.KEY_NUM2 && dKey == 116) {
                    hScroll.setValue(hScroll.getValue() - 1);
                }
                if(hScroll != null && dKey == -2 && dKey == Canvas.KEY_NUM8 && dKey == 98) {
                    hScroll.setValue(hScroll.getValue() + 1);
                }
                if(hScroll != null) hScroll.pCheck(fps_time, pX, pY, rX, rY, dX, dY);
            }
            
            pKey = dKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if (gFlag.equals("LIFT_EXIT")) {
            if (!lift.isOut()) {
                if (pKey == -7 || pKey == 8 || pKey == 121 || pKey == Canvas.KEY_STAR || Wand.region(rX, rY, !pUsing ? width - 28 : 0, !pUsing ? height - 30 : 0, 28, 30)) {
                    sb.pause();
                    pauseFlag = gFlag;
                    gFlag = "PAUSE";
                }
                lift.move(fps_time);
            } else {
                if (!sb.isShowLevelEnd()) {
                    if (!jLevel) {
                        boolean sCast = false;
                        if (!rLevel) {
                            if (rms.getGameLevel(rms_game) == lQuantity) {
                                if (rms_game[5] == 0) {
                                    rms.setGameVar(rms_game[1] + 2500, 1);
                                    rms_game = rms.getGameData();
                                    
                                    int bX = width / 2 - board6.getWidth() / 2 + 25, bY = height / 2 - board6.getHeight() / 2 + 18;
                                    int bSliderIndex = sSlider[0].getIndex();
                                    sSlider[0] = new Slider(new String[]{begin_text[12], begin_text[13], begin_text[14]}, bX + drawText.stringWidth(Localization.getAloneText(32)) + 16, bY, max(drawText, begin_text[12], begin_text[13], begin_text[14]) + 6, 18, drawText);
                                    sSlider[0].setIndex(bSliderIndex);

                                    rms.setGameVar(1, 5);
                                    sCast = true;
                                } else {
                                    if (!rms.isPassed(rms_game)) {
                                        int[] points = new int[] {2250, 2520, 2830};
                                        rms.setGameVar(rms_game[1]+points[rms_game[4]-1], 1);
                                        rms_game = rms.getGameData();
                                    }
                                }
                                rms.setPassed(rms_game);
                            } else {
                                rms.setGameLevel(rms_game, rms.getGameLevel(rms_game)+1);
                            }
                            rms.setGameVar(rms_game[1] + nPoints, 1);
                        }
                        if (!noTime || easy) {
                            rms.upRecord(rms.getQuantityNames() - 1, rms.getGameLevel(rms_game), otime, nPoints);
                        }
                        rms_game = rms.getGameData();
                        if (!sCast) {
                            gFlag = "BEGIN";
                        } else {
                            gFlag = "CAST";
                        }
                    } else {
                        drawText.setColor(0xffffff);
                        gFlag = "CREATE_MAP_MENU";
                    }
                }
            }
            pKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if (gFlag.equals("PAUSE")) {
            
            if(!pGame && workAds) {
                if(offAd) {
                    ad.loadAd();
                    offAd = false;
                }
                ad.adCheck(pX, pY);
            }
            
            if (pKey == -6 || pKey == 10 || pKey == 114) {
                if (!jLevel) {
                    mIndex = 0;
                    mScroll.init();
                    wasBack = true;
                    gFlag = "MENU";
                } else {
                    drawText.setColor(0xffffff);
                    gFlag = "CREATE_MAP_MENU";
                }
                offAd = true;
            }
            if (pKey == -7 || pKey == 8 || pKey == 121 || pKey == Canvas.KEY_STAR) {
                if (pauseFlag.equals("GAME")) {
                    gtime += System.currentTimeMillis() - (gtime + otime);
                }
                sb.play();
                gFlag = pauseFlag;
                offAd = true;
            }
            
            pKey = 0;
        }
        if (gFlag.equals("TIME_OUT") || gFlag.equals("GAME_OVER")) {
            if (pKey == -6 || pKey == 10 || pKey == 114) {
                if (jLevel) {
                    //createLevel(new MLoader(Encode.encode(rms.getLevel(mList.getCurrentIndex()))), "JP");
                } else {
                    if (rLevel) {
                        createLevel(new MLoader("/resources/maps.dat", slIndex + 1), "JPR");
                    } else {
                        createLevel(new MLoader("/resources/maps.dat", rms.getGameLevel(rms_game)), "");
                    }
                }
                lift.inStart();
                sb.startShowlevelBegin();
                gFlag = "LIFT_ENTER";
            }
            if (pKey == -7 || pKey == 8 || pKey == 121) {
                gFlag = "BEGIN";
            }
            pKey = 0;
        }
        if (gFlag.equals("GAME") || gFlag.equals("LIFT_ENTER") || gFlag.equals("LIFT_EXIT")) {
            levelX = sBall.getX() > width / 2 ? -(sBall.getX() - width / 2) : 0;
            levelY = sBall.getY() > height / 2 ? -(sBall.getY() - height / 2) : 0;
            if (levelWidth <= width) {
                levelX = 0;
            } else {
                if (levelX <= -(levelWidth - width) && levelWidth > width) {
                    levelX = -(levelWidth - width);
                }
            }
            if (levelHeight <= height) {
                levelY = 0;
            } else {
                if (levelY <= -(levelHeight - height) && levelHeight > height) {
                    levelY = -(levelHeight - height);
                }
            }
            checkXY[0] = sBall.getX() + sBall.getWidth() / 2;
            checkXY[1] = sBall.getY() + sBall.getHeight() / 2;
            lm.check(checkXY[0] < 0 ? 0 : checkXY[0] >= levelWidth ? levelWidth - 1 : checkXY[0], checkXY[1] < 0 ? 0 : checkXY[1] >= levelHeight ? levelHeight - 1 : checkXY[1]);
        }
        //if (gFlag.equals("CREATE_MAP")) {
        //    if ((pKey == -6 || pKey == 10 || pKey == 114 || NMWand(0)) && !newMap.isShowHelp()) {
        //        label:
        //        {
        //            if (!newMap.isSetLift(1)) {
        //                newMap.ptime = System.currentTimeMillis();
        //                newMap.error = NewMap.DONT_SET_LIFT_UP;
        //                break label;
        //            }
        //            if (!newMap.isSetLift(2)) {
        //                newMap.ptime = System.currentTimeMillis();
        //                newMap.error = NewMap.DONT_SET_LIFT_DOWN;
        //                break label;
        //            }
        //            if (!newMap.isSetLift(0)) {
        //                newMap.ptime = System.currentTimeMillis();
        //                newMap.error = NewMap.DONT_SET_POINT_UP;
        //                break label;
        //            }
        //            if (!newMap.isSetLift(3)) {
        //                newMap.ptime = System.currentTimeMillis();
        //                newMap.error = NewMap.DONT_SET_POINT_DOWN;
        //                break label;
        //            }
        //
        //            if (!eLevel) {
        //                rms.putLevel(mParam.getName(), Encode.encode(newMap.getNewMap()));
        //            } else {
        //                rms.updateLevel(mList.getCurrentIndex(), Encode.encode(newMap.getNewMap()));
        //            }
        //            createNewMapMenu();
        //        }
        //    }
        //    if ((pKey == -7 || pKey == 8 || pKey == 121 || NMWand(1)) && !newMap.isShowHelp()) {
        //        drawText.setColor(0xffffff);
        //        gFlag = "CREATE_MAP_MENU";
        //    }
        //    if (newMap.check(fps_time, pKey, rKey, hKey, dKey, pX, pY, rX, rY, dX, dY) && !newMap.isShowHelp()) {
        //        newMap.setHelp(help);
        //    }
        //    pKey = rKey = rpKey = 0;
        //    pX = pY = rX = rY = dX = dY = -1;
        //}
        //if (gFlag.equals("CREATE_MAP_MENU")) {
        //    if ((pKey == -5 || pKey == 103 || pKey == Canvas.KEY_NUM5 || MLWand(2) || mList.check(fps_time, pKey, pX, pY, rX, rY, dX, dY)) && rms.getLevelList().length > 0) {
        //        createLevel(new MLoader(Encode.encode(rms.getLevel(mList.getCurrentIndex()))), "JP");
        //        lift.inStart();
        //        sb.startShowlevelBegin();
        //        gFlag = "LIFT_ENTER";
        //    }
        //    if ((pKey == Canvas.KEY_POUND || MLWand(3)) && rms.getLevelList().length > 0) {
        //        rms.deleteLevel(mList.getCurrentIndex());
        //        mList.rebuild(rms.getLevelList());
        //    }
        //    if ((pKey == Canvas.KEY_NUM0 || MLWand(1)) && rms.getLevelList().length > 0) {
        //        createLoadNewMap();
        //        eLevel = true;
        //    }
        //    if (pKey == -6 || pKey == 10 || pKey == 114 || MLWand(0)) {
        //        createNewMapBegin();
        //    }
        //    if (pKey == -7 || pKey == 8 || pKey == 121 || MLWand(4)) {
        //        wasBack = true;
        //        gFlag = "MENU";
        //    }
        //    pX = pY = rX = rY = dX = dY = -1;
        //    pKey = 0;
        //}
        //if (gFlag.equals("CREATE_MAP_BEGIN")) {
        //    if (!mParam.isLockKeyboard()) {
        //        String string = Localization.getAloneText(10);
        //        if (pKey == -6 || pKey == 10 || pKey == 114 || Wand.region(pX, pY, 20 + drawText.stringWidth(string) / 2 - button_long3.getWidth() / 2 - 2, height - 24 + 7 - button_long3.getHeight() / 2 - 2, button_long3.getWidth() + 4, button_long3.getHeight() + 4)) {
        //            createNewMap(mParam.getWidth(), mParam.getHeight());
        //            eLevel = false;
        //        }
        //        string = Localization.getAloneText(11);
        //        if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width - drawText.stringWidth(string) / 2 - button_long1.getWidth() / 2 - 20 - 2, height - 24 + 7 - button_long1.getHeight() / 2 - 2, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
        //            drawText.setColor(0xffffff);
        //            gFlag = "CREATE_MAP_MENU";
        //        }
        //    }
        //    mParam.check(pKey, rpKey, pX, pY);
        //    pKey = rpKey = 0;
        //    pX = pY = rX = rY = dX = dY = -1;
        //}
        if (gFlag.equals("TABLE_RECORDS")) {
            if (pKey == -7 || pKey == 8 || pKey == 121 || Wand.region(pX, pY, width - drawText.stringWidth(Localization.getAloneText(39)) - 14 + drawText.stringWidth(Localization.getAloneText(39)) / 2 - button.getWidth() / 2 - 2, height - 27 + 7 - button.getHeight() / 2 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
                wasBack = true;
                gFlag = "MENU";
            }
            records.check(pKey, rKey, dKey);
            if (records.getScrollBar() != null) {
                records.getScrollBar().pCheck(fps_time, pX, pY, rX, rY, dX, dY);
            }
            if (records.getSlider() != null) {
                records.getSlider().pCheck(pX, pY, rX, rY, dX, dY);
                if (records.getSlider().hasValue()) {
                    records.setName(records.getSlider().getIndex());
                }
            }
            pKey = rKey = 0;
            pX = pY = rX = rY = dX = dY = -1;
        }
        if(showTurnOffAds) {
            checkTurnOffAds();
            pKey = 0; pX = pY = rX = rY = dX = dY = -1;
        }
    }

    //private boolean NMWand(int m) {
    //    switch (m) {
    //        case 0:
    //            String string = Localization.getAloneText(13);
    //            if (Localization.getLocale() == Localization.EN && Wand.region(pX, pY, 10 + drawText.stringWidth(string) / 2 - button.getWidth() / 2 - 2, height - 17 + 7 - button.getHeight() / 2 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
    //                return true;
    //            }
    //            if (Localization.getLocale() == Localization.RU && Wand.region(pX, pY, 10 + drawText.stringWidth(string) / 2 - button_long2.getWidth() / 2 - 2, height - 17 + 7 - button_long2.getHeight() / 2 - 2, button_long2.getWidth() + 4, button_long2.getHeight() + 4)) {
    //                return true;
    //            }
    //            break;
    //        case 1:
    //            string = Localization.getAloneText(14);
    //            if (Localization.getLocale() == Localization.EN && Wand.region(pX, pY, width - drawText.stringWidth(string) / 2 - button.getWidth() / 2 - 12 - 2, height - 17 + 7 - button.getHeight() / 2 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
    //                return true;
    //            }
    //            if (Localization.getLocale() == Localization.RU && Wand.region(pX, pY, width - drawText.stringWidth(string) / 2 - button_long1.getWidth() / 2 - 12 - 2, height - 17 + 7 - button_long1.getHeight() / 2 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
    //                return true;
    //            }
    //            break;
    //    }
    //    return false;
    //}

    //private boolean MLWand(int m) {
    //    switch (m) {
    //        case 0:
    //            String string = Localization.getAloneText(1);
    //            if (Localization.getLocale() == Localization.EN && Wand.region(pX, pY, 10 + drawText.stringWidth(string) / 2 - button_long1.getWidth() / 2 - 2, height - 24 + 7 - button_long1.getHeight() / 2 - 2, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
    //                return true;
    //            }
    //            if (Localization.getLocale() == Localization.RU && Wand.region(pX, pY, 10 + drawText.stringWidth(string) / 2 - button_long1.getWidth() / 2 - 2, height - 24 + 7 - button_long1.getHeight() / 2 - 2, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
    //                return true;
    //            }
    //            break;
    //        case 1:
    //            if (Localization.getLocale() == Localization.EN && Wand.region(pX, pY, width / 2 - button_long1.getWidth() / 2 - 2, 29 + mList.getlMax() + (height - mList.getlMax() - 44) / 2 - 3 + 7 - button_long1.getHeight() / 2 - 2, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
    //                return true;
    //            }
    //            if (Localization.getLocale() == Localization.RU && Wand.region(pX, pY, width / 2 - button_long2.getWidth() / 2 - 2, 29 + mList.getlMax() + (height - mList.getlMax() - 44) / 2 - 3 + 7 - button_long2.getHeight() / 2 - 2, button_long2.getWidth() + 4, button_long2.getHeight() + 4)) {
    //                return true;
    //            }
    //            break;
    //        case 2:
    //            if (Localization.getLocale() == Localization.EN && Wand.region(pX, pY, width / 2 - button_long1.getWidth() / 2 - 2, height - 24 + 7 - button_long1.getHeight() / 2 - 2, button_long1.getWidth() + 4, button_long1.getHeight() + 4)) {
    //                return true;
    //            }
    //            if (Localization.getLocale() == Localization.RU && Wand.region(pX, pY, width / 2 - button_long2.getWidth() / 2 - 2, height - 24 + 7 - button_long2.getHeight() / 2 - 2, button_long2.getWidth() + 4, button_long2.getHeight() + 4)) {
    //                return true;
    //            }
    //            break;
    //        case 3:
    //            string = Localization.getAloneText(4);
    //            if (Localization.getLocale() == Localization.EN && Wand.region(pX, pY, width - drawText.stringWidth(string) - 2 + drawText.stringWidth(string) / 2 - button_long2.getWidth() / 2 - 7, 29 + mList.getlMax() + (height - mList.getlMax() - 44) / 2 - 3 + 7 - button_long2.getHeight() / 2 - 2, button_long2.getWidth() + 4, button_long2.getHeight() + 4)) {
    //                return true;
    //            }
    //            if (Localization.getLocale() == Localization.RU && Wand.region(pX, pY, width - drawText.stringWidth(string) - 2 + drawText.stringWidth(string) / 2 - button_long2.getWidth() / 2 - 2, 29 + mList.getlMax() + (height - mList.getlMax() - 44) / 2 - 3 + 7 - button_long2.getHeight() / 2 - 2, button_long2.getWidth() + 4, button_long2.getHeight() + 4)) {
    //                return true;
    //            }
    //            break;
    //        case 4:
    //            string = Localization.getAloneText(5);
    //            if (Localization.getLocale() == Localization.EN && Wand.region(pX, pY, width - drawText.stringWidth(string) - 10 + drawText.stringWidth(string) / 2 - button.getWidth() / 2 - 2, height - 24 + 7 - button.getHeight() / 2 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
    //                return true;
    //            }
    //            if (Localization.getLocale() == Localization.RU && Wand.region(pX, pY, width - drawText.stringWidth(string) - 10 + drawText.stringWidth(string) / 2 - button.getWidth() / 2 - 2, height - 24 + 7 - button.getHeight() / 2 - 2, button.getWidth() + 4, button.getHeight() + 4)) {
    //                return true;
    //            }
    //            break;
    //    }
    //    return false;
    //}

    public void paint(Graphics g) {
        if (!canvas.isDoubleBuffered()) {
            rb2_paint(gOS);
            g.drawImage(offscreen, 0, 0, 20);
        } else {
            g.setClip(0, 0, width, height);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
            rb2_paint(g);
        }
    }

    private void rb2_paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, width, height);
        if (gFlag.equals("LOGO")) {
            g.setColor(0x000000);
            g.fillRect(0, 0, width, height);
            if (fps != 0) {
                if (logo == null) {
                    logo = new int[main_logo.getWidth() * main_logo.getHeight()];
                    lMode = 1;
                    lChangeMode = 1;
                }
                Image l = (lChangeMode == 1 ? main_logo : dasuppa_logo);
                if (System.currentTimeMillis() - lPauseTime >= 3000) {
                    l.getRGB(logo, 0, l.getWidth(), 0, 0, l.getWidth(), l.getHeight());
                    for (int i = 0; i < logo.length; i++) {
                        int alpha = (logo[i] >> 24) & 0xff;
                        alpha = (int) (alpha / 100f * lAlpha + 0.5f);
                        logo[i] = (alpha << 24) | (logo[i] & 0xffffff);
                    }
                    lAlpha += (lMode == 1 ? 100 * fps_time : -(50 * fps_time));
                    if (lAlpha > 100) {
                        lAlpha = 100;
                        lMode = 2;
                        lPauseTime = System.currentTimeMillis();
                    }
                    g.drawRGB(logo, 0, l.getWidth(), width / 2 - l.getWidth() / 2, height / 2 - l.getHeight() / 2, l.getWidth(), l.getHeight(), true);
                    if (lAlpha < 0) {
                        main_logo = null;
                        if (lChangeMode == 2) {
                            logo = null;
                            dasuppa_logo = null;
                            if (rms_game[8] == 0) {
                                gFlag = "CURTAIN_LANGUAGE";
                            } else {
                                gFlag = "CURTAIN_SOUND";
                            }
                        } else {
                            logo = new int[dasuppa_logo.getWidth() * dasuppa_logo.getHeight()];
                            lAlpha = 0;
                            lMode = 1;
                            lChangeMode = 2;
                        }
                    }
                } else {
                    g.drawImage(l, width / 2 - l.getWidth() / 2, height / 2 - l.getHeight() / 2, 20);
                }
            }
        }
        if (gFlag.equals("LANGUAGE") || gFlag.equals("CURTAIN_LANGUAGE") || gFlag.equals("CURTAIN_LANGUAGE_TO_SOUND")) {
            g.drawImage(bg_begin, 0, 0, 20);
            g.drawImage(board7, width / 2 - board7.getWidth() / 2, height / 2 - board7.getHeight() / 2, 20);
            g.setColor(0xbfbfbf);
            if (lSelect == 0) {
                g.drawRect(width / 2 - board7.getWidth()/4 - eng.getWidth()/2 - 2, height / 2 - eng.getHeight()/2 - 2, eng.getWidth() + 3, eng.getHeight() + 3);
                g.drawRect(width / 2 - board7.getWidth()/4 - eng.getWidth()/2 - 1, height / 2 - eng.getHeight()/2 - 1, eng.getWidth() + 1, eng.getHeight() + 1);
            }
            if (lSelect == 1) {
                g.drawRect(width / 2 + board7.getWidth()/4 - rus.getWidth() / 2 - 2, height / 2 - rus.getHeight()/2 - 2, rus.getWidth() + 3, rus.getHeight() + 3);
                g.drawRect(width / 2 + board7.getWidth()/4 - rus.getWidth() / 2 - 1, height / 2 - rus.getHeight()/2 - 1, rus.getWidth() + 1, rus.getHeight() + 1);
            }
            g.drawImage(eng, width / 2 - board7.getWidth()/4 - eng.getWidth()/2, height / 2 - eng.getHeight()/2, 20);
            g.drawImage(rus, width / 2 + board7.getWidth()/4 - rus.getWidth() / 2, height / 2 - rus.getHeight()/2, 20);

            if (gFlag.equals("CURTAIN_LANGUAGE")) {
                if (curtain == null) {
                    curtain = new int[width * height];
                    lAlpha = 100;
                }
                for (int i = 0; i < curtain.length; i++) {
                    int alpha = (int) (2.55f * lAlpha + 0.5f);
                    curtain[i] = (alpha << 24);
                }
                lAlpha -= 50 * fps_time;
                g.drawRGB(curtain, 0, width, 0, 0, width, height, true);
                if (lAlpha < 0) {
                    curtain = null;
                    gFlag = "LANGUAGE";
                    pKey = rKey = dKey = hKey = rpKey = 0;
                    pX = pY = rX = rY = dX = dY = -1;
                }
            }
            if (gFlag.equals("CURTAIN_LANGUAGE_TO_SOUND")) {
                if (curtain == null) {
                    curtain = new int[width * height];
                    lAlpha = 0;
                }
                for (int i = 0; i < curtain.length; i++) {
                    int alpha = (int) (2.55f * lAlpha + 0.5f);
                    curtain[i] = (alpha << 24);
                }
                lAlpha += 50 * fps_time;
                g.drawRGB(curtain, 0, width, 0, 0, width, height, true);
                if (lAlpha > 100) {
                    curtain = null;
                    gFlag = "CURTAIN_SOUND";
                    pKey = rKey = dKey = hKey = rpKey = 0;
                    pX = pY = rX = rY = dX = dY = -1;
                }
            }
        }
        if (gFlag.equals("SOUND") || gFlag.equals("CURTAIN_SOUND") || gFlag.equals("CURTAIN_SOUND_TO_MENU")) {
            g.drawImage(bg_begin, 0, 0, 20);
            drawText.setColor(0xffffff);
            drawText.drawString(g, width/2-drawText.stringWidth(Localization.getAloneText(56))/2, height/2-old_frame.getHeight()-35, Localization.getAloneText(56));
            g.drawImage(old_frame, width/2-old_frame.getWidth()/2, height/2-old_frame.getHeight()-5, 20);
            drawText.setColor(sIndex == 0 ? 0xcccccc : 0x222222);
            drawText.drawString(g, width/2-drawText.stringWidth(Localization.getAloneText(57))/2, height/2-old_frame.getHeight()/2-12, Localization.getAloneText(57));
            g.drawImage(old_frame, width/2-old_frame.getWidth()/2, height/2+5, 20);
            drawText.setColor(sIndex == 1 ? 0xcccccc : 0x222222);
            drawText.drawString(g, width/2-drawText.stringWidth(Localization.getAloneText(58))/2, height/2+5+old_frame.getHeight()/2-7, Localization.getAloneText(58));
            
            if (gFlag.equals("CURTAIN_SOUND")) {
                if (curtain == null) {
                    curtain = new int[width * height];
                    lAlpha = 100;
                }
                for (int i = 0; i < curtain.length; i++) {
                    int alpha = (int) (2.55f * lAlpha + 0.5f);
                    curtain[i] = (alpha << 24);
                }
                lAlpha -= 50 * fps_time;
                g.drawRGB(curtain, 0, width, 0, 0, width, height, true);
                if (lAlpha < 0) {
                    curtain = null;
                    gFlag = "SOUND";
                    pKey = rKey = dKey = hKey = rpKey = 0;
                    pX = pY = rX = rY = dX = dY = -1;
                }
            }
            if (gFlag.equals("CURTAIN_SOUND_TO_MENU")) {
                if (curtain == null) {
                    curtain = new int[width * height];
                    lAlpha = 0;
                }
                for (int i = 0; i < curtain.length; i++) {
                    int alpha = (int) (2.55f * lAlpha + 0.5f);
                    curtain[i] = (alpha << 24);
                }
                lAlpha += 50 * fps_time;
                g.drawRGB(curtain, 0, width, 0, 0, width, height, true);
                if (lAlpha > 100) {
                    curtain = null;
                    gFlag = "CURTAIN_MENU";
                    pKey = rKey = dKey = hKey = rpKey = 0;
                    pX = pY = rX = rY = dX = dY = -1;
                }
            }
        }
        if (gFlag.equals("CURTAIN_MENU") || gFlag.equals("MENU") || gFlag.equals("NEW_GAME") || gFlag.equals("SETTINGS") || gFlag.equals("HELP") || gFlag.equals("ABOUT") || gFlag.equals("EXIT")) {
            g.drawImage(bg_menu, 0, 0, 20);
            g.setClip(width / 2 - box_frame.getWidth() / 2 - 14, 140, box_frame.getWidth(), mHeight);
            drawText.setColor(0xdddddd);
            for (int i = 0; i < menu_points.length; i++) {
                if (!beContinue && i == 0) {
                    continue;
                }
                int Y = -mScroll.getValue();
                if (mIndex == (!beContinue ? i - 1 : i)) {
                    g.drawImage(box_frame_selected, width / 2 - box_frame_selected.getWidth() / 2 - 14, 140 + Y + mH * (!beContinue ? i - 1 : i), 20);
                    g.drawImage(iBall, width / 2 - box_frame_selected.getWidth() / 2 - 8, 146 + Y + mH * (!beContinue ? i - 1 : i), 20);
                } else {
                    g.drawImage(box_frame, width / 2 - box_frame.getWidth() / 2 - 14, 140 + Y + mH * (!beContinue ? i - 1 : i), 20);
                }
                drawText.drawString(g, width / 2 - box_frame.getWidth() / 2 + 25, 139 + Y + mH * (!beContinue ? i - 1 : i) + box_frame.getHeight() / 2 - 14 / 2, menu_points[i]);
            }
            g.setClip(0, 0, width, height);
            mScroll.paint(g);
            if (gFlag.equals("NEW_GAME")) {
                g.drawImage(board1, width / 8, height / 2 - height / 8, 20);
                drawText.setColor(0xe0e0e0);
                userName.paint(g, width / 2 - userName.getWidth() / 2, height / 2 - height / 8 + 42);
                switch (Localization.getLocale()) {
                    case Localization.EN:
                        drawText.drawString(g, width / 2 - drawText.stringWidth(menu_text[0][0]) / 2, height / 2 - height / 8 + 22, menu_text[0][0]);
                        int x0 = width / 8 + 17;
                        g.drawImage(button, x0 + drawText.stringWidth(menu_text[0][1]) / 2 - button.getWidth() / 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 5, 20);
                        drawText.drawString(g, x0, height / 2 + height / 8 - 5, menu_text[0][1]);
                        int x1 = width - width / 8 - drawText.stringWidth(menu_text[0][2]) - 15;
                        g.drawImage(button_long1, x1 + drawText.stringWidth(menu_text[0][2]) / 2 - button_long1.getWidth() / 2, height / 2 + height / 8 + 7 - button_long1.getHeight() / 2 - 5, 20);
                        drawText.drawString(g, x1, height / 2 + height / 8 - 5, menu_text[0][2]);
                        break;
                    case Localization.RU:
                        drawText.drawString(g, width / 2 - drawText.stringWidth(menu_text[0][0]) / 2, height / 2 - height / 8 + 10, menu_text[0][0]);
                        drawText.drawString(g, width / 2 - drawText.stringWidth(menu_text[0][1]) / 2, height / 2 - height / 8 + 26, menu_text[0][1]);
                        int x2 = width / 8 + 17;
                        g.drawImage(button, x2 + drawText.stringWidth(menu_text[0][2]) / 2 - button.getWidth() / 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 5, 20);
                        drawText.drawString(g, x2, height / 2 + height / 8 - 5, menu_text[0][2]);
                        int x3 = width - width / 8 - drawText.stringWidth(menu_text[0][3]) - 13;
                        g.drawImage(button_long1, x3 + drawText.stringWidth(menu_text[0][3]) / 2 - button_long1.getWidth() / 2, height / 2 + height / 8 + 7 - button_long1.getHeight() / 2 - 5, 20);
                        drawText.drawString(g, x3, height / 2 + height / 8 - 5, menu_text[0][3]);
                        break;
                }
            }
            if (gFlag.equals("SETTINGS")) {
                int bX = width / 2 - board6.getWidth() / 2, bY = height / 2 - board6.getHeight() / 2;
                g.drawImage(board6, bX, bY + 3, 20);
                bX += 25;
                bY += 18;
                
                g.setColor(0xbbbbbb);
                if (stIndex == 0) {
                    g.fillRect(bX + drawText.stringWidth(Localization.getAloneText(53)) + 16, bY+stY[0], sSlider[0].getWidth(), 18);
                }
                if (stIndex == 1) {
                    g.fillRect(bX + drawText.stringWidth(Localization.getAloneText(32)) + 16, bY+stY[1], sSlider[1].getWidth(), 18);
                }
                if (stIndex == 2) {
                    g.fillRect(bX + drawText.stringWidth(Localization.getAloneText(49)) + 16, bY + stY[2], sSlider[2].getWidth(), 18);
                }
                if (stIndex == 3) {
                    g.fillRect(bX + drawText.stringWidth(Localization.getAloneText(29)) + 16, bY + stY[3], sSlider[3].getWidth(), 18);
                }
                if (stIndex == 4 && cIndex == 0) {
                    g.fillRect(bX + drawText.stringWidth(Localization.getAloneText(34)) + 10, bY + stY[4], drawText.stringWidth(Localization.getAloneText(35)) + 4, 16);
                }
                if (stIndex == 4 && cIndex == 1) {
                    g.fillRect(bX + drawText.stringWidth(Localization.getAloneText(36)) + 10, bY + stY[4], drawText.stringWidth(Localization.getAloneText(37)) + 4, 16);
                }
                
                drawText.setColor(0xeeeeee);
                drawText.drawString(g, bX, bY + stY[0] + 2, Localization.getAloneText(53));
                g.setColor(0x666666);
                if (stIndex == 0) {
                    int xPos0 = bX + drawText.stringWidth(Localization.getAloneText(53)) + 2;
                    int yPos0 = bY + stY[0] + 9;
                    g.setColor(slColor[0]);
                    if (rms_game[13] == 1) {
                        g.fillTriangle(xPos0, yPos0, xPos0 + 10, yPos0-5, xPos0 + 10, yPos0+5);
                    }
                    xPos0 += 14 + sSlider[0].getWidth() + 14;
                    g.setColor(slColor[1]);
                    if (rms_game[13] == 0) {
                        g.fillTriangle(xPos0, yPos0, xPos0 - 10, yPos0-5, xPos0 - 10, yPos0+5);
                    }
                }
                drawText.setColor(0x000000);
                sSlider[0].paint(g, width, height);

                drawText.setColor(0xeeeeee);
                drawText.drawString(g, bX, bY + stY[1] + 2, Localization.getAloneText(32));
                g.setColor(0x666666);
                if (stIndex == 1) {
                    int xPos0 = bX + drawText.stringWidth(Localization.getAloneText(32)) + 2;
                    int yPos0 = bY + stY[1] + 9;
                    g.setColor(slColor[2]);
                    if (rms_game[4] > 1) {
                        g.fillTriangle(xPos0, yPos0, xPos0 + 10, yPos0-5, xPos0 + 10, yPos0+5);
                    }
                    xPos0 += 14 + sSlider[1].getWidth() + 14;
                    g.setColor(slColor[3]);
                    if (rms_game[4] < (rms_game[5] == 0 ? 2 : 3)) {
                        g.fillTriangle(xPos0, yPos0, xPos0 - 10, yPos0-5, xPos0 - 10, yPos0+5);
                    }
                }
                drawText.setColor(0x000000);
                sSlider[1].paint(g, width, height);

                drawText.setColor(0xeeeeee);
                drawText.drawString(g, bX, bY + stY[2] + 2, Localization.getAloneText(49));
                g.setColor(0x666666);
                if (stIndex == 2) {
                    int xPos0 = bX + drawText.stringWidth(Localization.getAloneText(49)) + 2;
                    int yPos0 = bY + stY[2] + 9;
                    g.setColor(slColor[4]);
                    if (rms_game[8] == 2) {
                        g.fillTriangle(xPos0, yPos0, xPos0 + 10, yPos0-5, xPos0 + 10, yPos0+5);
                    }
                    xPos0 += 14 + sSlider[2].getWidth() + 14;
                    g.setColor(slColor[5]);
                    if (rms_game[8] == 1) {
                        g.fillTriangle(xPos0, yPos0, xPos0 - 10, yPos0-5, xPos0 - 10, yPos0+5);
                    }
                }
                drawText.setColor(0x000000);
                sSlider[2].paint(g, width, height);
                
                drawText.setColor(0xeeeeee);
                drawText.drawString(g, bX, bY + stY[3] + 2, Localization.getAloneText(29));
                if (stIndex == 3) {
                    int xPos0 = bX + drawText.stringWidth(Localization.getAloneText(29)) + 2;
                    int yPos0 = bY + stY[3] + 9;
                    g.setColor(slColor[6]);
                    if (rms_game[6] > 0) {
                        g.fillTriangle(xPos0, yPos0, xPos0 + 10, yPos0-5, xPos0 + 10, yPos0+5);
                    }
                    xPos0 += 14 + sSlider[3].getWidth() + 14;
                    g.setColor(slColor[7]);
                    if (rms_game[6] < 1) {
                        g.fillTriangle(xPos0, yPos0, xPos0 - 10, yPos0-5, xPos0 - 10, yPos0+5);
                    }
                }
                drawText.setColor(0x000000);
                sSlider[3].paint(g, width, height);

                drawText.setColor(0xeeeeee);
                drawText.drawString(g, bX, bY + stY[4] + 1, Localization.getAloneText(34));
                drawText.setColor(0x000000);
                drawText.drawString(g, bX + drawText.stringWidth(Localization.getAloneText(34)) + 12, bY + stY[4] + 1, Localization.getAloneText(38));

                drawText.setColor(0xeeeeee);
                int x = width/2+board6.getWidth()/2-button.getWidth()-9;
                g.drawImage(button, x, bY + stY[4] + 7, 20);
                drawText.drawString(g, x+button.getWidth()/2-drawText.stringWidth(Localization.getAloneText(39))/2+1, bY + stY[ 4] + 8, Localization.getAloneText(39));
            }
            //if(gFlag.equals("PURCHASE")) {
            //    drawText.setColor(0xffffff);
            //    int X = bsX+(width/2-board6.getWidth()/2)+(board6.getWidth()-9), Y = height/2+board6.getHeight()/2-22;
            //    if(hasIAP) {
            //        if(!fAds) {
            //            drawText.drawString(g, X+board6.getWidth()/2-drawText.stringWidth(Localization.getAloneText(57))/2, height/2-28, Localization.getAloneText(57));
            //            drawText.drawString(g, X+board6.getWidth()/2-drawText.stringWidth(Localization.getAloneText(58))/2, height/2-14, Localization.getAloneText(58));
            //            drawText.drawString(g, X+board6.getWidth()/2-drawText.stringWidth(price)/2, height/2+7, price);
            //        } else {
            //            drawText.drawString(g, X+board6.getWidth()/2-drawText.stringWidth(Localization.getAloneText(67))/2, height/2-14, Localization.getAloneText(67));
            //            drawText.drawString(g, X+board6.getWidth()/2-drawText.stringWidth(Localization.getAloneText(68))/2, height/2, Localization.getAloneText(68));
            //        }
            //    } else {
            //        drawText.drawString(g, X+board6.getWidth()/2-drawText.stringWidth(Localization.getAloneText(64))/2, height/2-14, Localization.getAloneText(64));
            //        drawText.drawString(g, X+board6.getWidth()/2-drawText.stringWidth(Localization.getAloneText(65))/2, height/2, Localization.getAloneText(65));
            //    }
            //    if(pMode == 0) {
            //        if(hasIAP) {
            //            if(Localization.getLocale() == Localization.EN) {
            //                g.drawImage(button, X+15, Y, 20);
            //                drawText.drawString(g, X+15+button.getWidth()/2-drawText.stringWidth(Localization.getAloneText(60))/2, Y+button.getHeight()/2-7, Localization.getAloneText(60));
            //                if(!fDetails) {
            //                    g.drawImage(button_long1, X+board6.getWidth()-button_long1.getWidth()-15, Y, 20);
            //                    drawText.drawString(g, X+board6.getWidth()-button_long1.getWidth()/2-15-drawText.stringWidth(Localization.getAloneText(!fAds ? 59 : 69))/2, Y+button_long1.getHeight()/2-7, Localization.getAloneText(!fAds ? 59 : 69));
            //                }
            //            }
            //            if(Localization.getLocale() == Localization.RU) {
            //                g.drawImage(button_long1, X+15, Y, 20);
            //               drawText.drawString(g, X+15+button_long1.getWidth()/2-drawText.stringWidth(Localization.getAloneText(60))/2, Y+button_long1.getHeight()/2-7, Localization.getAloneText(60));
            //                if(!fDetails) {
            //                    g.drawImage(button_long2, X+board6.getWidth()-button_long2.getWidth()-15, Y, 20);
            //                    drawText.drawString(g, X+board6.getWidth()-button_long2.getWidth()/2-15-drawText.stringWidth(Localization.getAloneText(!fAds ? 59 : 69))/2, Y+button_long2.getHeight()/2-7, Localization.getAloneText(!fAds ? 59 : 69));
            //                }
            //            }
            //        } else {
            //            g.drawImage(button, X+15, Y, 20);
            //            drawText.drawString(g, X+15+button.getWidth()/2-drawText.stringWidth(Localization.getAloneText(66))/2+1, Y+button.getHeight()/2-7, Localization.getAloneText(66));
            //        }
            //    }
            //}
            if (gFlag.equals("HELP")) {
                showHelp(g, 1);
            }
            if (gFlag.equals("ABOUT")) {
                if(mAbout != 0) {
                    if (drawText.getY() > (height / 2 - 34) - drawText.getHText()) {
                        int Y = (height / 2 - 34) - (int) (10 * tAbout);
                        if(Y > 0) {
                            drawText.setY(Y);
                        } else {
                            aScroll.setValue(-Y);
                            if(mAbout == 1) mAbout = 2;
                        }
                        tAbout += fps_time;
                    } else {
                        mAbout = 0;
                    }
                }
                g.drawImage(board4, width / 8, height / 2 - height / 4, 20);
                drawText.setColor(0xe0e0e0);
                drawText.drawText(g, true);
                int x = width - width / 8 - drawText.stringWidth(menu_text[2][1]) - 15;
                g.drawImage(button, x + drawText.stringWidth(menu_text[2][1]) / 2 - button.getWidth() / 2, height / 2 + height / 4 + 7 - button.getHeight() / 2 - 8, 20);
                drawText.drawString(g, x, height / 2 + height / 4 - 8, menu_text[2][1]);
                aScroll.paint(g);
            }
            if (gFlag.equals("EXIT")) {
                g.drawImage(board1, width / 8, height / 2 - height / 8, 20);
                drawText.setColor(0xe0e0e0);
                drawText.drawText(g, true);
                int x0 = width / 8 + 20;
                g.drawImage(button, x0 + drawText.stringWidth(menu_text[3][1]) / 2 - button.getWidth() / 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 5, 20);
                drawText.drawString(g, x0, height / 2 + height / 8 - 5, menu_text[3][1]);
                int x1 = width - width / 8 - drawText.stringWidth(menu_text[3][2]) - 23;
                g.drawImage(button, x1 + drawText.stringWidth(menu_text[3][2]) / 2 - button.getWidth() / 2, height / 2 + height / 8 + 7 - button.getHeight() / 2 - 5, 20);
                drawText.drawString(g, x1, height / 2 + height / 8 - 5, menu_text[3][2]);
            }
            
            if(!pGame && !gFlag.equals("HELP")) {
                showButtonTurnOffAds(g);
                if(workAds) ad.adView(g);
            }
            
            if(showTurnOffAds) showTurnOffAds(g);
            
            if (gFlag.equals("CURTAIN_MENU")) {
                if (curtain == null) {
                    curtain = new int[width * height];
                    lAlpha = 100;
                }
                for (int i = 0; i < curtain.length; i++) {
                    int alpha = (int) (2.55f * lAlpha + 0.5f);
                    curtain[i] = (alpha << 24);
                }
                lAlpha -= 50 * fps_time;
                g.drawRGB(curtain, 0, width, 0, 0, width, height, true);
                if (lAlpha < 0) {
                    curtain = null;
                    wasMotion = true;
                    gFlag = "MENU";
                    pKey = rKey = dKey = hKey = rpKey = 0;
                    pX = pY = rX = rY = dX = dY = -1;
                }
            }
        }
        if (gFlag.equals("BEGIN") || gFlag.equals("IMPROVEMENTS")) {
            switch (Localization.getLocale()) {
                case Localization.EN:
                    g.drawImage(bg_begin, 0, 0, 20);
                    g.drawImage(board5, 4, 9, 20);
                    drawText.setColor(0xe4e4e4);
                    drawText.drawString(g, 71 - drawText.stringWidth(begin_text[0] + rms.getGameLevel(rms_game) + " (" + cUser + ")") / 2, 19, begin_text[0] + rms.getGameLevel(rms_game) + " (" + cUser + ")");
                    g.drawImage(stand, width / 4 - 45, 94 - stand.getHeight() / 2, 20);
                    g.drawImage(iBall, width / 4 - 14, 78 - iBall.getHeight() / 2, 20);
                    drawText.setColor((System.currentTimeMillis() - pTime) > 1000 ? 0xb0b0b0 : 0xff0000);
                    drawText.drawString(g, 182 - drawText.stringWidth(begin_text[1] + rms_game[1]) / 2, 20, begin_text[1] + rms_game[1]);

                    drawText.setColor(0xe0e0e0);

                    drawText.drawString(g, 113 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(begin_text[2]) / 2, 45, begin_text[2]);
                    drawText.drawString(g, 114 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(rms_ball[1] + begin_text[4]) / 2, 59, rms_ball[1] + begin_text[4]);

                    drawText.drawString(g, 114, 73, begin_text[3]);
                    drawText.drawString(g, 113 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(rms_ball[0] + begin_text[4]) / 2, 87, rms_ball[0] + begin_text[4]);

                    drawText.drawString(g, 111 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(begin_text[5].substring(0, 21).trim()) / 2, 101, begin_text[5].substring(0, 21).trim());
                    drawText.drawString(g, 111 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(begin_text[5].substring(22).trim()) / 2, 115, begin_text[5].substring(22).trim());
                    drawText.drawString(g, 113 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(rms_ball[2] + begin_text[6]) / 2, 129, rms_ball[2] + begin_text[6]);
                    break;
                case Localization.RU:
                    g.drawImage(bg_begin, 0, 0, 20);
                    g.drawImage(board5, 4, 9, 20);
                    drawText.setColor(0xe4e4e4);
                    drawText.drawString(g, 71 - drawText.stringWidth(begin_text[0] + rms.getGameLevel(rms_game) + " (" + cUser + ")") / 2, 19, begin_text[0] + rms.getGameLevel(rms_game) + " (" + cUser + ")");
                    g.drawImage(stand, width / 4 - 40, 94 - stand.getHeight() / 2, 20);
                    g.drawImage(iBall, width / 4 - 9, 78 - iBall.getHeight() / 2, 20);
                    drawText.setColor((System.currentTimeMillis() - pTime) > 1000 ? 0xb0b0b0 : 0xff0000);
                    drawText.drawString(g, 180 - drawText.stringWidth(begin_text[1] + rms_game[1]) / 2, 20, begin_text[1] + rms_game[1]);

                    drawText.setColor(0xe0e0e0);

                    drawText.drawString(g, 118 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(begin_text[2]) / 2, 45, begin_text[2]);
                    drawText.drawString(g, 119 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(rms_ball[1] + begin_text[4]) / 2, 59, rms_ball[1] + begin_text[4]);

                    drawText.drawString(g, 119, 73, begin_text[3]);
                    drawText.drawString(g, 118 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(rms_ball[0] + begin_text[4]) / 2, 87, rms_ball[0] + begin_text[4]);

                    drawText.drawString(g, 116 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(begin_text[5].substring(0, 16).trim()) / 2, 101, begin_text[5].substring(0, 16).trim());
                    drawText.drawString(g, 117 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(begin_text[5].substring(17).trim()) / 2, 115, begin_text[5].substring(17).trim());
                    drawText.drawString(g, 118 + drawText.stringWidth(begin_text[3]) / 2 - drawText.stringWidth(rms_ball[2] + begin_text[6]) / 2, 129, rms_ball[2] + begin_text[6]);
                    break;
            }
        }
        if (gFlag.equals("BEGIN")) {
            int bY = 158;
            g.setClip(0, bY - 5, width, (lbHeight = height - bY));
            bY += lbY;
            for (int i = 0; i < 4; i++) {
                g.drawImage(old_frame, width / 2 - old_frame.getWidth() / 2, bY + i * (old_frame.getHeight() + 8), 20);
                if (lbIndex == i) {
                    drawText.setColor(0x000000);
                } else {
                    drawText.setColor(0xe0e0e0);
                }
                drawText.drawString(g, width / 2 - drawText.stringWidth(lb_text[i]) / 2, bY + i * (old_frame.getHeight() + 8) + old_frame.getHeight() / 2 - 7, lb_text[i]);
            }
        }
        if (gFlag.equals("IMPROVEMENTS")) {
            int bY = 158;
            g.setClip(0, bY - 5, width, (lHeight = (height - 16) - bY));
            bY += -iScroll.getValue();
            for (int i = 0; i < 3; i++) {
                switch (i) {
                    case 0:
                        g.drawImage(go, 10, bY + lsY[i] + lsH[i] / 2 - go.getHeight() / 2, 20);
                        break;
                    case 1:
                        g.drawImage(jump, 10, bY + lsY[i] + lsH[i] / 2 - jump.getHeight() / 2, 20);
                        break;
                    case 2:
                        g.drawImage(rebound, 10, bY + lsY[i] + lsH[i] / 2 - rebound.getHeight() / 2, 20);
                        break;
                }
                drawText.setColor(0xe4e4e4);
                drawText.setText(begin_text[8 + i], 70, bY + lsY[i], width - 80, 50);
                drawText.drawText(g, false);
                if (i == bIndex) {
                    g.setColor(0xb0b0b0);
                    g.drawRect(5, bY + lsY[i] - 5, width - 10, lsH[i] + 10);
                }
            }
            g.setClip(0, 0, width, height);
            drawText.setColor(0xf0f0f0);
            int x = width - drawText.stringWidth(Localization.getAloneText(39)) - 10;
            g.drawImage(button, x + drawText.stringWidth(Localization.getAloneText(39)) / 2 - button.getWidth() / 2, height - 20 + 7 - button.getHeight() / 2, 20);
            drawText.drawString(g, x, height - 20, Localization.getAloneText(39));
        }
        if (gFlag.equals("SL")) {
            g.drawImage(bg_menu, 0, 0, 20);
            int h = mlHeight;
            g.setClip(width / 2 - old_frame.getWidth() / 2, 150, old_frame.getWidth(), h + 5);
            drawText.setColor(0xdddddd);
            for (int i = 0; i < countOfMaps; i++) {
                int lY = 150 + slY + i * (old_frame.getHeight() + 5);
                if (lY > 150 - old_frame.getHeight() && lY <= 150 + h) {
                    g.drawImage(old_frame, width / 2 - old_frame.getWidth() / 2, lY, 20);
                    if (slIndex == i) {
                        drawText.setColor(0x000000);
                    } else {
                        drawText.setColor(0xe0e0e0);
                    }
                    drawText.drawString(g, width / 2 - drawText.stringWidth(Localization.getAloneText(40) + (i + 1)) / 2, lY + old_frame.getHeight() / 2 - 7, Localization.getAloneText(40) + (i + 1));
                    if (i >= rms.getGameLevel(rms_game)) {
                        g.drawImage(line, width / 2 - line.getWidth() / 2, lY + old_frame.getHeight() / 2 - 1, 20);
                    }
                }
            }
            g.setClip(0, 0, width, height);
            drawText.setColor(0xf0f0f0);
            int x = width - drawText.stringWidth(Localization.getAloneText(39)) - 14;
            g.drawImage(button, x + drawText.stringWidth(Localization.getAloneText(39)) / 2 - button.getWidth() / 2, height - 19 + 7 - button.getHeight() / 2, 20);
            drawText.drawString(g, x, height - 19, Localization.getAloneText(39));
            g.setColor(slColorUp);
            if (slY < 0) {
                g.fillTriangle(width / 2 - 10, 150 - 10, width / 2, 150 - 20, width / 2 + 10, 150 - 10);
            }
            g.setColor(slColorDown);
            if (slY > h - ((!pUsing ? rms.getGameLevel(rms_game) : countOfMaps) * (old_frame.getHeight() + 5) - (!pUsing ? 0 : 10))) {
                g.fillTriangle(width / 2 - 10, 150 + h + 15, width / 2, 150 + h + 25, width / 2 + 10, 150 + h + 15);
            }
            //sScroll.paint(g);
        }
        if (gFlag.equals("SD")) {
            g.drawImage(bg_menu, 0, 0, 20);
            //g.setClip(width / 2 - old_frame.getWidth() / 2, 150, old_frame.getWidth(), mHeight);
            drawText.setColor(0xdddddd);
            for (int i = 0; i < 3; i++) {
                g.drawImage(old_frame, width / 2 - old_frame.getWidth() / 2, 150 + sdY + i * (old_frame.getHeight() + 15), 20);
                if (sdIndex == i) {
                    drawText.setColor(0x000000);
                } else {
                    drawText.setColor(0xe0e0e0);
                }
                drawText.drawString(g, width / 2 - drawText.stringWidth(begin_text[12 + i]) / 2, 150 + sdY + i * (old_frame.getHeight() + 15) + old_frame.getHeight() / 2 - 7, begin_text[12 + i]);
            }
            g.setClip(0, 0, width, height);
            if (nSelectDiff) {
                g.drawImage(board1, width / 2 - board1.getWidth() / 2, height / 2 - board1.getHeight() / 2, 20);
                drawText.setColor(0xe0e0e0);
                drawText.drawString(g, width / 2 - drawText.stringWidth(Localization.getAloneText(41)) / 2, height / 2 - 22, Localization.getAloneText(41));
                drawText.drawString(g, width / 2 - drawText.stringWidth(Localization.getAloneText(42)) / 2 + 1, height / 2 - 7, Localization.getAloneText(42));
                drawText.drawString(g, width / 2 - drawText.stringWidth(Localization.getAloneText(43)) / 2, height / 2 + 8, Localization.getAloneText(43));
                int x = width / 2 + board1.getWidth() / 2 - drawText.stringWidth(Localization.getAloneText(39)) - 16;
                g.drawImage(button, x + drawText.stringWidth(Localization.getAloneText(39)) / 2 - button.getWidth() / 2, height / 2 + board1.getHeight() / 2 - 20 + 7 - button.getHeight() / 2, 20);
                drawText.drawString(g, x, height / 2 + board1.getHeight() / 2 - 20, Localization.getAloneText(39));
            }
            
            if(!pGame) {
                showButtonTurnOffAds(g);
                if(workAds) ad.adView(g);
            }
            
            if(showTurnOffAds) showTurnOffAds(g);
        }

        if (gFlag.equals("CAST")) {
            if (!sAlpha) {
                drawText.setColorWithAlpha(true);
                sAlpha = true;
            }
            g.drawImage(bg_begin, 0, 0, 20);
            if (cMode == 0) {
                drawText.setColor(((int) (gipTime < 2.5f ? 255f / 2.5f * gipTime : gipTime >= 5f ? 255f / 2.5f * (7.5f - gipTime) : 255f) << 24) | 0xe4e4e4);
                drawText.drawString(g, width / 2 - drawText.stringWidth(Localization.getAloneText(44)) / 2, height / 2 - 7, Localization.getAloneText(44));
                gipTime += fps_time;
                if (gipTime >= 7.5f) {
                    gipTime = 0;
                    cMode = 1;
                }
            }
            if (cMode == 1) {
                drawText.setColor(((int) (gipTime < 2.5f ? 255f / 2.5f * gipTime : gipTime >= 7.5f ? 255f / 2.5f * (10f - gipTime) : 255f) << 24) | 0xe4e4e4);
                drawText.drawString(g, width / 2 - drawText.stringWidth(Localization.getAloneText(45)) / 2, height / 2 - 7, Localization.getAloneText(45));
                gipTime += fps_time;
                if (gipTime >= 10f) {
                    gipTime = 0;
                    cMode = 2;
                }
            }
            if (cMode == 2) {
                if (cast == null) {
                    drawText.setText(menu_text[2][0], 30, gipY, width - 60);
                    cast = drawText.getArray();
                    drawText.setColorWithAlpha(true);
                }
                
                gipY = height - (int) (15 * gipTime);
                gipTime += fps_time;
                
                for (int i = 0; i < cast.length; i++) {
                    int Y = gipY + i * 14;
                    if (Y >= 0 && Y < height) {
                        drawText.setColor(((int) (Y > height / 2 ? 255f - (255f / (height / 2f)) * (Y - height / 2f) : (255f / (height / 2f)) * Y) << 24) | 0xe4e4e4);
                        drawText.drawString(g, width / 2 - drawText.stringWidth(cast[i]) / 2, Y, cast[i]);
                    }
                }

                if (gipY + cast.length * 14 < 0) {
                    drawText.setColorWithAlpha(false);
                    cast = null;
                    gFlag = "BEGIN";
                }
            }
        }
        if (gFlag.equals("GAME") || gFlag.equals("LIFT_ENTER") || gFlag.equals("LIFT_EXIT") || gFlag.equals("TIME_OUT") || gFlag.equals("PAUSE") || gFlag.equals("GAME_OVER")) {
            if (bg_game != null && rms_game[6] == 1) {
                g.drawImage(bg_game, width / 2 - bg_game.getWidth() / 2, height / 2 - bg_game.getHeight() / 2, 20);
            }
            lm.paint(g, levelX, levelY);
            if (gFlag.equals("LIFT_ENTER") || gFlag.equals("PAUSE")) {
                if (sLiftIn.getY() > liftCable[0][1]) {
                    g.setColor(0x000000);
                    g.drawLine(levelX + liftCable[0][0] + 17, levelY + liftCable[0][1], levelX + liftCable[0][0] + 17, levelY + sLiftIn.getY() - 1);
                    g.drawLine(levelX + liftCable[0][0] + 22, levelY + liftCable[0][1], levelX + liftCable[0][0] + 22, levelY + sLiftIn.getY() - 1);
                }
            }
            if (gFlag.equals("LIFT_EXIT") || gFlag.equals("PAUSE")) {
                if (sLiftOut.getY() > liftCable[1][1]) {
                    g.setColor(0x000000);
                    g.drawLine(levelX + liftCable[1][0] + 17, levelY + liftCable[1][1], levelX + liftCable[1][0] + 17, levelY + sLiftOut.getY() - 1);
                    g.drawLine(levelX + liftCable[1][0] + 22, levelY + liftCable[1][1], levelX + liftCable[1][0] + 22, levelY + sLiftOut.getY() - 1);
                }
            }
            if(joystick != null) joystick.paint(g);
            if(gFlag.equals("GAME") || gFlag.equals("LIFT_ENTER") || gFlag.equals("LIFT_EXIT")) g.drawImage(go_to_mm, !pUsing ? width - go_to_mm.getWidth()-1 : 1, !pUsing ? height - go_to_mm.getHeight()-2 : 2, 20);
            if (gFlag.equals("GAME") || gFlag.equals("LIFT_ENTER")) {
                if (sb.isShowLevelBegin()) {
                    sb.showLevelBegin(g, (jLevel ? 1 : rLevel ? slIndex + 1 : rms.getGameLevel(rms_game)));
                }
            }
            if (gFlag.equals("LIFT_EXIT")) {
                if (sb.isShowLevelEnd()) {
                    sb.showLevelEnd(g, (jLevel ? 1 : rLevel ? slIndex + 1 : rms.getGameLevel(rms_game)), nPoints, (jLevel || rLevel ? true : false));
                }
            }
            g.setColor(0x000000);
            if (rms_game[7] == 1) {
                if (gFlag.equals("GAME") || gFlag.equals("LIFT_ENTER") || gFlag.equals("LIFT_EXIT") || gFlag.equals("TIME_OUT") || gFlag.equals("PAUSE") || gFlag.equals("GAME_OVER")) {
                    if (!gFlag.equals("LIFT_ENTER")) {
                        if (gFlag.equals("GAME")) {
                            ltime = System.currentTimeMillis() - gtime;
                        } else {
                            ltime = otime;
                        }
                        if (!noTime) {
                            ltime = levelTime - ltime;
                        }
                    } else {
                        if (!noTime) {
                            ltime = levelTime;
                        } else {
                            ltime = 0;
                        }
                    }
                    if (!noTime && ltime <= levelTime * 0.2f) {
                        g.setColor(0xff0000);
                    } else {
                        g.setColor(0x9f9f9f);
                    }
                    int second = (int) ((ltime - (ltime / 60000 * 60000)) / 1000);
                    String minutes = ""+(ltime / 60000);
                    String split = ":";
                    String seconds = (second < 10 ? "0" : "") + second;
                    if(gFlag.equals("GAME") || gFlag.equals("LIFT_ENTER") || gFlag.equals("LIFT_EXIT") || gFlag.equals("TIME_OUT")) {
                        g.drawImage(hourglass, width - hourglass.getWidth()-6, 3, 20);
                        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
                        g.drawString(minutes, width-hourglass.getWidth()-(g.getFont().stringWidth(seconds))-(g.getFont().stringWidth(split))-(g.getFont().stringWidth(minutes))-15, 3+hourglass.getHeight()/2-g.getFont().getHeight()/2, 20);
                        g.drawString(split, width-hourglass.getWidth()-(g.getFont().stringWidth(seconds))-(g.getFont().stringWidth(split))-13, 3+hourglass.getHeight()/2-g.getFont().getHeight()/2, 20);
                        g.drawString(seconds, width-hourglass.getWidth()-(g.getFont().stringWidth(seconds))-11, 3+hourglass.getHeight()/2-g.getFont().getHeight()/2, 20);
                    }
                }
                if(hFirst) showHelp(g, 2);
            }
            if (gFlag.equals("PAUSE")) {
                int s = sb.showPause(g, pX, pY);
                switch (s) {
                    case 1:
                        if (!jLevel) {
                            mIndex = 0;
                            mScroll.init();
                            wasBack = true;
                            gFlag = "MENU";
                        } else {
                            drawText.setColor(0xffffff);
                            gFlag = "CREATE_MAP_MENU";
                        }
                        break;
                    case 2:
                        if (pauseFlag.equals("GAME")) {
                            gtime += System.currentTimeMillis() - (gtime + otime);
                        }
                        sb.play();
                        gFlag = pauseFlag;
                        break;
                }
                
                if(s != 0) offAd = true;
                
                if(!pGame && workAds) ad.adView(g);
                
                pX = pY = rX = rY = dX = dY = -1;
            }
            if (gFlag.equals("TIME_OUT")) {
                int s = sb.showTimeOut(g, pX, pY);
                switch (s) {
                    case 1:
                        if (jLevel) {
                            //createLevel(new MLoader(Encode.encode(rms.getLevel(mList.getCurrentIndex()))), "JP");
                        } else {
                            if (rLevel) {
                                createLevel(new MLoader("/resources/maps.dat", slIndex + 1), "JPR");
                            } else {
                                createLevel(new MLoader("/resources/maps.dat", rms.getGameLevel(rms_game)), "");
                            }
                        }
                        lift.inStart();
                        sb.startShowlevelBegin();
                        gFlag = "LIFT_ENTER";
                        break;
                    case 2:
                        gFlag = "BEGIN";
                        break;
                }
                pX = pY = rX = rY = dX = dY = -1;
            }
            if (gFlag.equals("GAME_OVER")) {
                if (!ball.destroying()) {
                    sBall.setImage(iBall, 18, 18);
                    sBall.setPosition(-sBall.getWidth(), -sBall.getHeight());
                }
                int s = sb.showGameOver(g, pX, pY);
                switch (s) {
                    case 1:
                        if (jLevel) {
                            //createLevel(new MLoader(Encode.encode(rms.getLevel(mList.getCurrentIndex()))), "JP");
                        } else {
                            if (rLevel) {
                                createLevel(new MLoader("/resources/maps.dat", slIndex + 1), "JPR");
                            } else {
                                createLevel(new MLoader("/resources/maps.dat", rms.getGameLevel(rms_game)), "");
                            }
                        }
                        lift.inStart();
                        sb.startShowlevelBegin();
                        gFlag = "LIFT_ENTER";
                        break;
                    case 2:
                        gFlag = "BEGIN";
                        break;
                }
                pX = pY = rX = rY = dX = dY = -1;
            }

        }
        //if (gFlag.equals("CREATE_MAP")) {
        //    drawText.setColor(0x000000);
        //    newMap.paint(g);
        //}
        //if (gFlag.equals("CREATE_MAP_MENU")) {
        //    drawText.setColor(0xffffff);
        //    mList.paint(g);
        //}
        //if (gFlag.equals("CREATE_MAP_BEGIN")) {
        //    drawText.setColor(0xc0c0c0);
        //    mParam.paint(g);
        //}
        //if (gFlag.equals("TABLE_RECORDS")) {
        //    drawText.setColor(0x000000);
        //    records.paint(g);
        //    if (records.getScrollBar() != null) {
        //        records.getScrollBar().paint(g);
        //    }
        //}
        getFPS();
    }
    
    private void showHelp(Graphics g, int mode) {
        if(hMode != mode) {
            hArray = new String[3][];
            for(int i=0; i<hArray.length; i++) {
                String text = (i == 2 && mode == 2 ? "\n" : (i == 0 && mode == 1 ? menu_text[1][0] : "")+menu_text[1][i+1]);
                drawText.setText(text, 0, 0, board3.getWidth()-22);
                hArray[i] = drawText.getArray();
            }
            
            int hHeight = (hArray[0].length*14)+help1.getHeight()+(hArray[1].length*14)+help2.getHeight()+(hArray[2].length*14);
            
            hScroll = new ScrollBar(width / 2 - board3.getWidth() / 2 + 10, height / 2 - board3.getHeight() / 2 + 10, board3.getWidth() - 16, board3.getHeight() - 40, hHeight - (board3.getHeight() - 40), hHeight);
            
            hMode = mode;
        }
        g.drawImage(board3, width / 2 - board3.getWidth() / 2, height / 2 - board3.getHeight() / 2, 20);
        drawText.setColor(0xe0e0e0);
        g.setClip(width/2-board3.getWidth()/2+10, height/2-board3.getHeight()/2+10, board3.getWidth()-20, board3.getHeight()-40);

        int Y = (height/2-board3.getHeight()/2+10)-hScroll.getValue();
        for(int i=0; i<hArray.length; i++) {

            for(int j=0; j<hArray[i].length; j++) {
                drawText.drawString(g, width/2-drawText.stringWidth(hArray[i][j])/2, Y, hArray[i][j]);
                Y += 14;
            }

            if(i == 0 || i == 1) {
                Image iHelp = (i == 0 ? help1 : help2);
                g.drawImage(iHelp, width/2-iHelp.getWidth()/2, Y, 20);
                Y += iHelp.getHeight();
            }

        }

        g.setClip(0, 0, width, height);
        if(!hFirst) {
            int x = width / 2 + board3.getWidth() / 2 - button.getWidth()-12, y = height/2+board3.getHeight()/2-button.getHeight()-8;
            g.drawImage(button, x, y, 20);
            drawText.drawString(g, x+button.getWidth()/2-drawText.stringWidth(menu_text[1][4])/2+1, y+button.getHeight()/2-7, menu_text[1][4]);
        } else {
            int x = width / 2 + board3.getWidth() / 2 - button_long1.getWidth()-12, y = height/2+board3.getHeight()/2-button_long1.getHeight()-8;
            g.drawImage(button_long1, x, y, 20);
            drawText.drawString(g, x+button_long1.getWidth()/2-drawText.stringWidth(Localization.getAloneText(59))/2, y+button_long1.getHeight()/2-7, Localization.getAloneText(59));
        }
        hScroll.paint(g);
    }
    
    private void getPriceToTurnOffAds() {
        if(hasIAP) {
            boolean status = iap.getProductData();
            if(!status) {
                tMode = 3;
                showTurnOffAds = true;
            }
        } else {
            tMode = 3;
            showTurnOffAds = true;
        }
    }
    
    private void checkButtonTurnOffAds() {
        int X = width - button_ads_off.getWidth() - 2, Y = height - button_ads_off.getHeight() - 2;
        if(Wand.region(pX, pY, X-2, Y-2, button_ads_off.getWidth()+4, button_ads_off.getHeight()+4)) getPriceToTurnOffAds();
    }
    
    private void showButtonTurnOffAds(Graphics g) {
        int X = width - button_ads_off.getWidth() - 2, Y = height - button_ads_off.getHeight() - 2;
        g.drawImage(button_ads_off, X, Y, 20);
        drawText.setColor(0xffffff);
        drawText.drawString(g, X+button_ads_off.getWidth()/2-drawText.stringWidth(Localization.getAloneText(60))/2, Y+button_ads_off.getHeight()/2-7, Localization.getAloneText(60));
    }
    
    private void checkTurnOffAds() {
        int X = width/2-board8.getWidth()/2, Y = height/2-board8.getHeight()/2;
        if(pKey == -7 || pKey == 10 || pKey == 121 | Wand.region(rX, rY, X+board8.getWidth()-sign_ok.getWidth()-4, Y+board8.getHeight()-sign_ok.getHeight()-2, sign_ok.getWidth(), sign_ok.getHeight())) {
            if(tMode == 0) {
                if(hasIAP) {
                    boolean status = iap.purchaseProduct();
                    if(status) {
                        tMode = 1;
                    } else {
                        tMode = 3;
                    }
                } else {
                    tMode = 3;
                }
            }
            if(tMode == 2 || tMode == 3) showTurnOffAds = false;
        }
        if(tMode == 0 && (pKey == -6 || pKey == 8 || pKey == 114 || Wand.region(rX, rY, X+4, Y+board8.getHeight()-sign_no.getHeight()-2, sign_no.getWidth(), sign_no.getHeight()))) showTurnOffAds = false;
    }
    
    private void showTurnOffAds(Graphics g) {
        g.setColor(0x858585);
        for(int i=0; i<width; i+=3) g.drawLine(i, 0, i, height);
        for(int i=0; i<height; i+=3) g.drawLine(0, i, width, i);
        
        int X = width/2-board8.getWidth()/2, Y = height/2-board8.getHeight()/2;
        g.drawImage(board8, X, Y, 20);
        drawText.setColor(0xffffff);
        drawText.drawString(g, width/2-drawText.stringWidth(Localization.getAloneText(60))/2, Y+16, Localization.getAloneText(60));
        int l = Localization.getLocale();
        switch(tMode) {
            case 0:
                if(l == Localization.EN) {
                    String sString0 = turn_off_ads[0]+" "+turn_off_ads[1];
                    drawText.drawString(g, width/2-drawText.stringWidth(sString0)/2, height/2-16, sString0);
                    String sString1 = turn_off_ads[2]+price+"?";
                    drawText.drawString(g, width/2-drawText.stringWidth(sString1)/2, height/2+2, sString1);
                } else {
                    drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[0])/2, height/2-23, turn_off_ads[0]);
                    String sString0 = price+turn_off_ads[1];
                    drawText.drawString(g, width/2-drawText.stringWidth(sString0)/2, height/2-7, sString0);
                    drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[2])/2, height/2+9, turn_off_ads[2]);
                }
                break;
            case 1:
                drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[3])/2, height/2-16, turn_off_ads[3]);
                drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[4])/2, height/2+2, turn_off_ads[4]);
                break;
            case 2:
                drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[5])/2, height/2-16, turn_off_ads[5]);
                drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[6])/2, height/2+2, turn_off_ads[6]);
                break;
            case 3:
                if(l == Localization.RU) {
                    drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[7])/2, height/2-16, turn_off_ads[7]);
                    drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[8])/2, height/2+2, turn_off_ads[8]);
                } else {
                    drawText.drawString(g, width/2-drawText.stringWidth(turn_off_ads[7])/2, height/2-7, turn_off_ads[7]);
                }
                break;
        }
        if(tMode == 0) g.drawImage(sign_no, X+4, Y+board8.getHeight()-sign_no.getHeight()-2, 20);
        if(tMode != 1) g.drawImage(sign_ok, X+board8.getWidth()-sign_ok.getWidth()-4, Y+board8.getHeight()-sign_ok.getHeight()-2, 20);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == ok || c == cancel) {
            if (c == ok) {
                userName.setValue(textBox.getString());
            }
            rb2MIDlet.display.setCurrent(canvas);
        }
    }
    
    public void hasPurchased(boolean flag) {
        pGame = flag;
        init();
    }
    
    public void productPrice(String price, boolean flag) {
        if(flag) {
            this.price = price;
            tMode = 0;
        } else {
            tMode = 3;
        }
        showTurnOffAds = true;
    }
    
    public void purchaseCompleted(boolean flag) {
        if(flag) {
            pGame = true;
            tMode = 2;
        } else {
            tMode = 3;
        }
    }
    
}
