var mouse_x, mouse_y;

function ClassGr()
{
    this.getImg = new createjs.Container();

    this.img = [[],[],[],[],[],[],[],[]];
    this.img_des = [[],[],[],[],[],[],[],[]];
    this.img_glow = [[],[],[],[],[],[],[],[]];
    this.img_bonus = [[],[],[],[],[],[],[],[]];
    this.text_multi = [[],[],[],[],[],[],[],[]];

    this.time_bonus = [0,0,0,0,0,0];

    for(i=0; i<8; i++)
        for(j=0; j<8; j++)
        {
            this.img_glow[i].push(ss_data.clone());
            this.img_glow[i][j].visible = false;
            this.img_glow[i][j].gotoAndPlay("glow");
            this.img_glow[i][j].regX = 37*devise_size;
            this.img_glow[i][j].regY = 37*devise_size;
            this.img_glow[i][j].x = j*40*devise_size;
            this.img_glow[i][j].y = i*40*devise_size;
            this.getImg.addChild(this.img_glow[i][j]);
        }

    for(var i=0; i<8; i++)
        for(var j=0; j<8; j++)
        {
            this.img[i].push(ss_data.clone());
            this.img[i][j].name = i*10 + j;

            var s = new createjs.Shape();
            s.graphics.f("#000").dc(21*devise_size,18*devise_size,18*devise_size);
            this.img[i][j].hitArea = s;

            this.img[i][j].on("mousedown", function(evt){
                mouse_x = evt.stageX;
                mouse_y = evt.stageY;
                var a = (evt.target.name/10)<<0,
                    b = evt.target.name%10;
                stage_glow.addChild(ClassBorder(game.grid.getImg.x+b*(40*devise_size)-20*devise_size, game.grid.getImg.y+a*(40*devise_size)-20*devise_size, 40*devise_size, 40*devise_size));
            });
            this.img[i][j].on("pressup", function(evt){
                stage_glow.removeAllChildren();
                if(game.move)
                {
                    var a = (evt.target.name/10)<<0,
                        b = evt.target.name%10;

                    switch (game.bonus.ID)
                    {
                        case 0 :
                            if(game.grid.mass[a][b].ID == 2)
                            {
                                game.fly_w.set(game.grid.getImg.x + b*40*devise_size, game.grid.getImg.y + a*40*devise_size);
                                game.grid.destroy[a][b] = 1;
                                if(game.grid.isDestroy())
                                    game.grid.Restroy();
                            }
                            else
                            {
                                if(Math.max(Math.abs(mouse_x - evt.stageX), Math.abs(mouse_y - evt.stageY))>5*devise_size)
                                {
                                    if(Math.abs(mouse_x - evt.stageX) > Math.abs(mouse_y - evt.stageY))
                                        game.grid.MouseSwap(a, b, a, b+(((mouse_x - evt.stageX) < 0)?1:-1));
                                    else
                                        game.grid.MouseSwap(a, b, a+(((mouse_y - evt.stageY) < 0)?1:-1), b);
                                }
                            }
                            break;
                        case 1:
                            if(game.grid.Bonus1(a, b))
                            {
                                game.grid.gr.bonus_img.visible = true;
                                game.grid.gr.bonus_img.gotoAndPlay("water");
                                game.grid.gr.bonus_img.regX = 38*devise_size;
                                game.grid.gr.bonus_img.regY = 38*devise_size;
                                game.grid.gr.bonus_img.x = b*40*devise_size;
                                game.grid.gr.bonus_img.y = a*40*devise_size;
                                game.bonus.Add(0, -1);
                                game.bonus.reBonus();
                                game.grid.gr.setAnimBonus1();
                            }
                            break;
                        case 2:
                            if(game.grid.Bonus2(a, b))
                            {
                                game.grid.gr.bonus_img.visible = true;
                                game.grid.gr.bonus_img.gotoAndPlay("bsmal");
                                game.grid.gr.bonus_img.regX = 32*devise_size;
                                game.grid.gr.bonus_img.regY = 32*devise_size;
                                game.grid.gr.bonus_img.x = b*40*devise_size;
                                game.grid.gr.bonus_img.y = a*40*devise_size;
                                game.bonus.Add(1, -1);
                                game.bonus.reBonus();
                                game.grid.gr.setAnimBonus2();
                            }
                            break;
                        case 3:
                            if(game.grid.Bonus3(a, b))
                            {
                                game.grid.gr.bonus_img.visible = true;
                                game.grid.gr.bonus_img.gotoAndPlay("bomb");
                                game.grid.gr.bonus_img.regX = 88*devise_size;
                                game.grid.gr.bonus_img.regY = 88*devise_size;
                                game.grid.gr.bonus_img.x = b*40*devise_size;
                                game.grid.gr.bonus_img.y = a*40*devise_size;
                                game.bonus.Add(2, -1);
                                game.bonus.reBonus();
                                game.grid.gr.setAnimBonus3();
                            }
                            break;
                        case 4:
                            if(game.grid.Bonus4(a, b))
                            {
                                game.bonus.Add(3, -1);
                                game.bonus.reBonus();
                                game.grid.gr.setAnimBonus4();
                            }
                            break;
                        case 5:
                            if(game.grid.Bonus5(a, b))
                            {
                                game.bonus.Add(4, -1);
                                game.bonus.reBonus();
                                game.grid.gr.setAnimBonus5();
                            }
                            break;
                        case 6:
                            if(game.grid.Bonus6(a, b))
                            {
                                game.grid.gr.bonus_img.visible = true;
                                game.grid.gr.bonus_img.gotoAndPlay("a_lock");
                                game.grid.gr.bonus_img.regX = 38*devise_size;
                                game.grid.gr.bonus_img.regY = 38*devise_size;
                                game.grid.gr.bonus_img.x = b*40*devise_size;
                                game.grid.gr.bonus_img.y = a*40*devise_size;
                                game.bonus.Add(5, -1);
                                game.bonus.reBonus();
                                game.grid.gr.setAnimBonus6();
                            }
                            break;
                    }
                }
            });

            this.img[i][j].regX = 21*devise_size;
            this.img[i][j].regY = 40*devise_size;//18*devise_size;
            this.img[i][j].x = j*40*devise_size;
            this.img[i][j].y = (i*40+20)*devise_size;
            this.getImg.addChild(this.img[i][j]);
        }
    for(i=0; i<8; i++)
        for(j=0; j<8; j++)
        {
            this.img_des[i].push(ss_data.clone());
            this.img_des[i][j].visible = false;
            this.img_des[i][j].on("animationend", function(evt){ evt.target.visible = false; });
            this.img_des[i][j].regX = 77*devise_size;
            this.img_des[i][j].regY = 77*devise_size;
            this.img_des[i][j].x = j*40*devise_size;
            this.img_des[i][j].y = i*40*devise_size;
            this.getImg.addChild(this.img_des[i][j]);

            this.img_bonus[i].push(ss_data.clone());
            this.img_bonus[i][j].visible = false;
            this.img_bonus[i][j].regX = -4*devise_size;
            this.img_bonus[i][j].regY = -4*devise_size;
            this.img_bonus[i][j].x = j*40*devise_size;
            this.img_bonus[i][j].y = i*40*devise_size;
            this.getImg.addChild(this.img_bonus[i][j]);

            this.text_multi[i].push(new ClassText("0", 2));
            this.text_multi[i][j].getImg.regX = -2*devise_size;
            this.text_multi[i][j].getImg.x = (40*devise_size)*j;
            this.text_multi[i][j].getImg.y = (40*devise_size)*i;
            this.getImg.addChild(this.text_multi[i][j].getImg);
        }

    this.bonus_img = ss_data.clone();
    this.bonus_img.on("animationend", function(evt){ evt.target.visible = false; });
    this.bonus_img.visible = false;
    this.getImg.addChild(this.bonus_img);
}

ClassGr.prototype.setV = function(i, j)
{
    switch(game.grid.mass[i][j].ID)
    {
        case -1:
            this.img[i][j].visible = false;
            this.img_bonus[i][j].visible = false;
            this.img_glow[i][j].visible = false;
            this.text_multi[i][j].getImg.visible = false;
            break;
        case 0:
            this.img[i][j].visible = true;
            this.img[i][j].gotoAndStop(game.color[game.grid.mass[i][j].val]);
            if(game.grid.mass[i][j].multi == 1)
                this.text_multi[i][j].getImg.visible = false;
            else
            {
                if(game.all_job.job.isTrue(game.grid.mass[i][j].val)) {
                    this.text_multi[i][j].getImg.visible = true;
                    this.text_multi[i][j].setText("x" + game.grid.mass[i][j].multi);
                }
                else
                    this.text_multi[i][j].getImg.visible = false;
            }
            switch(game.grid.mass[i][j].bonusID)
            {
                case 0:
                    this.img_bonus[i][j].visible = false;
                    this.img_glow[i][j].visible = false;
                    break;
                case 1:
                    this.img_bonus[i][j].visible = false;
                    this.img_glow[i][j].visible = true;
                    break;
                case 2:
                    this.img_glow[i][j].visible = false;
                    this.img_bonus[i][j].visible = true;
                    this.img_bonus[i][j].gotoAndStop("lock");
                    break;
                case 3:
                    this.img_glow[i][j].visible = false;
                    this.img_bonus[i][j].visible = true;
                    this.img_bonus[i][j].gotoAndStop("gift");
                    break;
            }
            break;
        case 1:
            this.img[i][j].visible = true;
            this.img[i][j].gotoAndStop("heart");
            this.img_bonus[i][j].visible = false;
            this.img_glow[i][j].visible = false;
            this.text_multi[i][j].getImg.visible = false;
            break;
        case 2:
            this.img[i][j].visible = true;
            this.img[i][j].gotoAndStop("bonus00_i");
            this.img_bonus[i][j].visible = false;
            this.img_glow[i][j].visible = false;
            this.text_multi[i][j].getImg.visible = false;
            break;
        case 3:
            this.img[i][j].visible = true;
            this.img[i][j].gotoAndStop("pumpkin"+game.grid.mass[i][j].val);
            this.img_bonus[i][j].visible = false;
            this.img_glow[i][j].visible = false;
            this.text_multi[i][j].getImg.visible = false;
            break;
        case 4:
            this.img[i][j].visible = true;
            this.img[i][j].gotoAndStop("watermelon"+game.grid.mass[i][j].val);
            this.img_bonus[i][j].visible = false;
            this.img_glow[i][j].visible = false;
            this.text_multi[i][j].getImg.visible = false;
            break;
    }
};

ClassGr.prototype.Re = function()
{
    this.getImg.mouseEnabled = true;
    for(var i=0; i<8; i++)
        for(var j=0; j<8; j++)
        {
            this.img[i][j].scaleX = this.img[i][j].scaleY = 1;
            this.img[i][j].x = j*40*devise_size;
            this.img[i][j].y = (i*40+20)*devise_size;
        }
    this.Upd();
};

ClassGr.prototype.Upd = function()
{
    for(var i=0; i<8; i++)
        for(var j=0; j<8; j++)
            this.setV(i, j);
};

ClassGr.prototype.setAnimDes = function()
{
    for(var i=0; i<game.grid.height; i++)
        for(var j=0; j<game.grid.width; j++)
            if(game.grid.destroy[i][j] != 0)
            {
                this.img[i][j].gotoAndStop(this.img[i][j].currentAnimation+"m");
                this.img_des[i][j].visible = true;
                if(this.img_des[i][j].visible)
                    this.img_des[i][j].gotoAndPlay("pets");
            }
    if(audio_supported)
        s_pop_pets.play();
    this.time_bonus[0] = 26;
};
ClassGr.prototype.setAnimBonus1 = function()
{
    this.Upd();
    this.time_bonus[1] = 13;
};
ClassGr.prototype.setAnimBonus2 = function()
{
    this.time_bonus[2] = 13;
};
ClassGr.prototype.setAnimBonus3 = function()
{
    this.time_bonus[3] = 13;
};
ClassGr.prototype.setAnimBonus4 = function()
{
    for(var i=0; i<game.grid.height; i++)
        for(var j=0; j<game.grid.width; j++)
            if(game.grid.destroy[i][j] != 0)
            {
                this.img[i][j].gotoAndStop(this.img[i][j].currentAnimation+"m");
                this.img_des[i][j].visible = true;
                if(this.img_des[i][j].visible)
                    this.img_des[i][j].gotoAndPlay("pets");
            }
    this.time_bonus[4] = 26;
};
ClassGr.prototype.setAnimBonus5 = function()
{
    for(var i=0; i<game.grid.height; i++)
        for(var j=0; j<game.grid.width; j++)
            if(game.grid.destroy[i][j] != 0)
            {
                this.img[i][j].gotoAndStop(this.img[i][j].currentAnimation+"m");
                this.img_des[i][j].visible = true;
                if(this.img_des[i][j].visible)
                    this.img_des[i][j].gotoAndPlay("pets");
            }
    this.time_bonus[5] = 26;
};
ClassGr.prototype.setAnimBonus6 = function()
{
    this.Upd();
    this.time_bonus[6] = 14;
};

ClassGr.prototype.Drw = function()
{
    var a = true, anim_s = false;
    var i, j;

    if(this.time_bonus[0] != 0)  //none
    {
        a = false;
        this.time_bonus[0]--;
        if(this.time_bonus[0] == 0)
        {
            game.fly.StartFly();
            game.grid.Restroy();
        }
    }
    else
    {
        if(this.time_bonus[1] != 0)  //bonus1
        {
            this.time_bonus[1]--;
            if(this.time_bonus[1] == 0)
            {
                game.fly.StartFly();
                game.grid.Restroy();
            }
        }
        else
        {
            if(this.time_bonus[2] != 0)  //bonus2
            {
                this.time_bonus[2]--;
                if(this.time_bonus[2] == 0)
                {
                    game.fly.StartFly();
                    game.grid.Restroy();
                }
            }
            else
            {
                if(this.time_bonus[3] != 0)
                {
                    this.time_bonus[3]--;
                    if(this.time_bonus[3] == 0)
                    {
                        game.fly.StartFly();
                        game.grid.Restroy();
                    }
                }
                else
                {
                    if(this.time_bonus[4] != 0)
                    {
                        this.time_bonus[4]--;
                        if(this.time_bonus[4] == 0)
                        {
                            game.fly.StartFly();
                            game.grid.Restroy();
                        }
                    }
                    else
                    {
                        if(this.time_bonus[5] != 0)
                        {
                            this.time_bonus[5]--;
                            if(this.time_bonus[5] == 0)
                            {
                                game.grid.FindLine();
                                if(game.grid.isDestroy())
                                {
                                    this.setAnimDes();
                                }
                            }
                        }
                        else {
                            //------------------------------------------------------------not-bonus
                            for (i = 0; i < game.grid.height; i++)
                                for (j = 0; j < game.grid.width; j++) {
                                    var b = false;
                                    if (this.img[i][j].x > 40 * j * devise_size) {
                                        a = false;
                                        b = true;
                                        this.img[i][j].x -= config.speed * devise_size;
                                        if (this.img[i][j].x <= 40 * j * devise_size)
                                            this.img[i][j].x = 40 * j * devise_size;
                                        this.text_multi[i][j].getImg.x = this.img[i][j].x;
                                        this.img_bonus[i][j].x = this.img[i][j].x;
                                        this.img_glow[i][j].x = this.img[i][j].x;
                                    }
                                    else if (this.img[i][j].x < 40 * j * devise_size) {
                                        a = false;
                                        b = true;
                                        this.img[i][j].x += config.speed * devise_size;
                                        if (this.img[i][j].x >= 40 * j * devise_size)
                                            this.img[i][j].x = 40 * j * devise_size;
                                        this.text_multi[i][j].getImg.x = this.img[i][j].x;
                                        this.img_bonus[i][j].x = this.img[i][j].x;
                                        this.img_glow[i][j].x = this.img[i][j].x;
                                    }
                                    if (this.img[i][j].y > (i*40+20) * devise_size) {
                                        a = false;
                                        b = true;
                                        this.img[i][j].y -= config.speed * devise_size;
                                        if (this.img[i][j].y <= (i*40+20) * devise_size)
                                            this.img[i][j].y = (i*40+20) * devise_size;
                                        this.text_multi[i][j].getImg.y = this.img[i][j].y-20*devise_size;
                                        this.img_bonus[i][j].y = this.img[i][j].y-20*devise_size;
                                        this.img_glow[i][j].y = this.img[i][j].y-20*devise_size;
                                    }
                                    else if (this.img[i][j].y < (i*40+20) * devise_size) {
                                        a = false;
                                        b = true;
                                        this.img[i][j].y += config.speed * devise_size;
                                        if (this.img[i][j].y >= (i*40+20) * devise_size)
                                            this.img[i][j].y = (i*40+20) * devise_size;
                                        this.text_multi[i][j].getImg.y = this.img[i][j].y-20*devise_size;
                                        this.img_bonus[i][j].y = this.img[i][j].y-20*devise_size;
                                        this.img_glow[i][j].y = this.img[i][j].y-20*devise_size;
                                    }
                                if (b)
                                    this.img[i][j].scaleY =  Math.max(0.6, this.img[i][j].scaleY - 0.1);
                            }
                        }
                    }
                }
            }
        }
    }
    for (i = 0; i < game.grid.height; i++)
        for (j = 0; j < game.grid.width; j++)
            if (this.img[i][j].scaleY != 1)
            {
                this.img[i][j].scaleY = Math.min(1, this.img[i][j].scaleY + 0.05);
                anim_s = true;
            }

    if(!game.move && a) {
        stage_glow.removeAllChildren();
        game.grid.FindLine();
        if(game.grid.isDestroy())
        {
            this.setAnimDes();
            if(save_swap[0] != -1) game.all_job.moves.Add(-1);
        }
        else
        {
            game.grid.FindAll();

            game.all_job.isEnd();

            if(game.bot)
            {
                game.grid.MouseSwap(bot[0], bot[1], bot[2], bot[3]);
            }
            else
            {
                game.grid.backSwap();
            }
        }
    }

    if(!anim_s && !this.getImg.mouseEnabled)
        game.all_job.goEnd();

    game.move = a;
};

ClassGr.prototype.ToCor = function(i1, j1, i2, j2)
{
    this.img[i1][j1].x = 40*j2*devise_size;
    this.img[i1][j1].y = 40*i2*devise_size;
};

ClassGr.prototype.ToImg = function(i1, j1, i2, j2)
{
    this.img[i1][j1].x = this.img[i2][j2].x;
    this.img[i1][j1].y = this.img[i2][j2].y;
};