var home_cout, fb_cout;

function WindowMap()
{
    home_cout = 0;
    fb_cout = 0;

    this.name = "map";
    this.layer = 0;

    this.pointsX = 0;

    var s = new createjs.Shape();
    s.graphics.beginFill("#fff").drawRect(0, 80*devise_size, 320*devise_size, 267*devise_size);
    s.on("mousedown",function(evt){
        evt.target.m_x = evt.stageX;
    });
    s.on("pressup",function(evt){
        if(Math.abs(evt.target.m_x - evt.stageX)>30*devise_size)
            window_controller.windows[window_controller.getIDbyName("map")].Swipe(Math.max(-1, Math.min(1, evt.stageX - evt.target.m_x)));
    });
    this.getImg.addChild(s);

    var bg = new createjs.Bitmap(load.queue.getResult("bg_menu"));
    this.getImg.addChild(bg);

    this.points = new createjs.Container();
    this.points.y = 54*devise_size;
    this.point = [];
    this.stars = [];
    this.point_text = [];
    this.bonus = [];

    for(var i=0; i<config.lvls.length; i++)
    {
        this.point.push(ss_data.clone());
        this.point[i].name = i;
        this.point[i].on("mousedown",function(evt){
            evt.target.m_x = evt.stageX;
        });
        this.point[i].on("pressup",function(evt){
            if(Math.abs(evt.target.m_x - evt.stageX)>30*devise_size)
            {
                window_controller.windows[window_controller.getIDbyName("map")].Swipe(Math.max(-1, Math.min(1, evt.stageX - evt.target.m_x)));
            }
            else
            {
                adManager.showAd(function () {
                    if(game.save.tut)
                    {
                        tut = evt.target.name;
                        window_controller.Show("tut");
                    }
                    else
                    {
                        game.CreateLvl(evt.target.name);
                        window_controller.Show("location");
                        if(config.lvls[evt.target.name].bonus_lvl)
                            window_controller.Show("bonuslvl");
                        else
                            window_controller.Show("job");
                    }
                });
            }
        });
        this.point[i].regX = 28*devise_size;
        this.point[i].regY = 29*devise_size;
        this.point[i].x = (54 + (70*(i%4)) + (((i/4)<<0)/4<<0) * 320)*devise_size;
        this.point[i].y = (54 + (70*(i/4<<0)) - (((i/4)<<0)/4<<0) * 280)*devise_size;
        this.points.addChild(this.point[i]);

        this.point_text.push(new ClassText((i+1).toString(), 0));
        this.point_text[i].getImg.regX = this.point_text[i].getImg.width/2;
        this.point_text[i].getImg.regY = this.point_text[i].getImg.height/2;
        this.point_text[i].getImg.x = this.point[i].x;
        this.point_text[i].getImg.y = this.point[i].y - 25*devise_size;
        this.points.addChild(this.point_text[i].getImg);

        this.stars.push(ss_data.clone());
        this.stars[i].x = this.point[i].x;
        this.stars[i].y = this.point[i].y;
        this.points.addChild(this.stars[i]);

        this.bonus.push(ss_data.clone());
        this.bonus[i].gotoAndStop("text_white_bonus");
        this.bonus[i].x = this.point[i].x-25*devise_size;
        this.bonus[i].y = this.point[i].y+16*devise_size;
        this.points.addChild(this.bonus[i]);
    }
    this.getImg.addChild(this.points);

    this.markers = [];
    for(i=0; i<3; i++)
    {
        this.markers.push(ss_data.clone());
        this.markers[i].gotoAndStop("marker_map");
        this.markers[i].x = (106+40*i)*devise_size;
        this.markers[i].y = 56*devise_size;
        this.markers[i].alpha = 0.5;
        this.getImg.addChild(this.markers[i]);
    }

    this.btn_b = ss_data.clone();
    this.btn_b.gotoAndStop("btn");
    this.btn_b.on("mousedown", function(){ window_controller.Show("main_menu"); });
    this.btn_b.x = 5*devise_size;
    this.btn_b.y = (369)*devise_size;
    this.getImg.addChild(this.btn_b);
    this.btn_b_up = ss_data.clone();
    this.btn_b_up.gotoAndStop("icon_back");
    this.btn_b_up.x = 5*devise_size;
    this.btn_b_up.y = (369)*devise_size;
    this.getImg.addChild(this.btn_b_up);

    this.btn_left = new createjs.Container();
    this.btn_left.on("mousedown", function(){ window_controller.windows[window_controller.getIDbyName("map")].Swipe(1); });
    this.btn_left.x = 5*devise_size;
    this.btn_left.y = 421*devise_size;
    this.btn_left.regY = 52*devise_size;
    var btn_left_bg = ss_data.clone();
    btn_left_bg.gotoAndStop("btn");
    this.btn_left.addChild(btn_left_bg);
    var btn_left_up = ss_data.clone();
    btn_left_up.gotoAndStop("icon_left");
    this.btn_left.addChild(btn_left_up);
    this.getImg.addChild(this.btn_left);

    this.btn_right = new createjs.Container();
    this.btn_right.on("mousedown", function(){ window_controller.windows[window_controller.getIDbyName("map")].Swipe(-1); });
    this.btn_right.x = 263*devise_size;
    this.btn_right.y = 421*devise_size;
    this.btn_right.regY = 52*devise_size;
    var btn_right_bg = ss_data.clone();
    btn_right_bg.gotoAndStop("btn");
    this.btn_right.addChild(btn_right_bg);
    var btn_right_up = ss_data.clone();
    btn_right_up.gotoAndStop("icon_right");
    this.btn_right.addChild(btn_right_up);
    this.getImg.addChild(this.btn_right);
}
WindowMap.prototype = new ClassWindow();

WindowMap.prototype.Show = function()
{
    if(home_cout === config.add_home_post)
    {
        addToHome.show(true);
        home_cout = 0;
    }
    else
        home_cout++;

    if(fb_cout == config.fb_post)
    {
        FB.ui(
            {
                method: 'feed',
                name: config.fb_name,
                link: window.location.href,
                picture: window.location.href.substring(0, window.location.href.lastIndexOf('/')+1) + "assets/facebook.jpg",
                caption: config.fb_caption,
                description: config.fb_description
            },
            function(response) {
            }
        );
        fb_cout=0;
    }
    else
        fb_cout++;

    for(var i=0; i<config.lvls.length; i++)
    {
        if(game.save.map[i] == -1)
        {
            this.point[i].mouseEnabled = false;
            this.stars[i].gotoAndStop("lock");
            this.stars[i].regX = -13*devise_size;
            this.stars[i].regY = -7*devise_size;
            this.stars[i].visible = true;
            this.bonus[i].visible = config.lvls[i].bonus_lvl;
        }
        else
        {
            this.point[i].mouseEnabled = true;
            if(game.save.map[i] == 0)
            {
                this.bonus[i].visible = config.lvls[i].bonus_lvl;
                this.stars[i].visible = false;
            }
            else
            {
                this.bonus[i].visible = false;
                this.stars[i].gotoAndStop("corn_map"+game.save.map[i]);
                this.stars[i].regX = 26*devise_size;
                this.stars[i].regY = -10*devise_size;
                this.stars[i].visible = true;
            }
        }
        if(config.lvls[i].bonus_lvl)
            this.point[i].gotoAndStop("map_level_bonus");
        else
        {
            if(game.save.map[i] == -1)
                this.point[i].gotoAndStop("map_level_off");
            else
                this.point[i].gotoAndStop("map_level_on");
        }
    }

    for(i=0; i<3; i++)
        this.markers[i].alpha = (this.pointsX != -i)? 0.5 : 1.0;
    this.points.x = this.pointsX*(320*devise_size);

    if(this.pointsX == 0)
    {
        this.btn_b.visible = true;
        this.btn_b_up.visible = true;
        this.btn_left.visible = false;
        this.btn_right.visible = true;
    }
    if(this.pointsX == -1)
    {
        this.btn_b.visible = false;
        this.btn_b_up.visible = false;
        this.btn_left.visible = true;
        this.btn_right.visible = true;
    }
    if(this.pointsX == -2)
    {
        this.btn_b.visible = false;
        this.btn_b_up.visible = false;
        this.btn_left.visible = true;
        this.btn_right.visible = false;
    }
};

WindowMap.prototype.Swipe = function(a)
{
    this.pointsX = Math.max(-2, Math.min(0, this.pointsX + a));
    for(var i=0; i<3; i++)
        this.markers[i].alpha = (this.pointsX != -i)? 0.5 : 1.0;

    if(this.pointsX == 0)
    {
        this.btn_b.visible = true;
        this.btn_b_up.visible = true;
        this.btn_left.visible = false;
        this.btn_right.visible = true;
    }
    if(this.pointsX == -1)
    {
        this.btn_b.visible = false;
        this.btn_b_up.visible = false;
        this.btn_left.visible = true;
        this.btn_right.visible = true;
    }
    if(this.pointsX == -2)
    {
        this.btn_b.visible = false;
        this.btn_b_up.visible = false;
        this.btn_left.visible = true;
        this.btn_right.visible = false;
    }
};

WindowMap.prototype.Drw = function()
{
    if(this.pointsX*(320*devise_size) > this.points.x)
        this.points.x +=(20*devise_size);
    if(this.pointsX*(320*devise_size) < this.points.x)
        this.points.x -=(20*devise_size);
};