var game_area, canvas, stage, rotate, devise_size, width, height, load;
var test, bg;

var addToHomeConfig = {
    autostart: false
};
(function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function init()
{
    adManager.init();
    document.bgColor = "#000";

    if(window.innerWidth < 640 || window.innerHeight < 744)
        devise_size = 1;
    else
        devise_size = 2;

    //devise_size = 2;

    game_area = document.getElementById('game_area');

    canvas = document.getElementById("my_canvas");
    canvas.width = 320*devise_size;
    canvas.height = 480*devise_size;

    stage = new createjs.Stage(canvas);
    createjs.Touch.enable(stage);

    if(window.orientation !== undefined)
    {
        rotate = document.createElement("img");
        rotate.id = "img_rotate";
        rotate.src = "assets/rotate.jpg";
        rotate.unselectable = "on";
        document.body.appendChild(rotate);
    }

    load = new ClassLoad();

    Resize();
    window.addEventListener('resize', Resize, false);
}

var job, game;
var window_controller;

function StartGame()
{
    var j=0;
    for(var i=0; i<config.lvls.length; i++)
    {
        config.lvls[i].job = [];
        config.lvls[i].bonus = [];

        if(config.lvls[i].bonus_lvl == undefined)
            config.lvls[i].bonus_lvl = 0;

        for(j=0; j<config.lvls[i].job_pets.length; j++)
            config.lvls[i].job.push({ ID : 0, value : j, count : config.lvls[i].job_pets[j] });

        if(config.lvls[i].watermelon != 0)
        {
            config.lvls[i].job.push({ ID : 4, value : 0, count : config.lvls[i].watermelon });
            for(j=0; j<config.lvls[i].watermelon; j++)
                config.lvls[i].bonus.push("watermelons");
        }
        if(config.lvls[i].pumpkin != 0)
        {
            config.lvls[i].job.push({ ID : 3, value : 0, count : config.lvls[i].pumpkin });
            for(j=0; j<config.lvls[i].pumpkin; j++)
                config.lvls[i].bonus.push("pumpkin");
        }
        if(config.lvls[i].locks != 0)
        {
            for(j=0; j<config.lvls[i].locks; j++)
                config.lvls[i].bonus.push("key");
        }
        if(config.lvls[i].gifts != 0)
        {
            for(j=0; j<config.lvls[i].gifts; j++)
                config.lvls[i].bonus.push("gift");
        }
        if(config.lvls[i].hearts != 0)
        {
            for(j=0; j<config.lvls[i].hearts; j++)
                config.lvls[i].bonus.push("heart");
        }
    }


    game = new ClassGame();

    /*loc = new ClassLocation();
    stage.addChild(loc.GetImg);

    pre = new ClassPrePanel();
    pre.GetImg.x = 180;
    pre.GetImg.y = 810;
    stage.addChild(pre.GetImg);

    modal_bg = new ClassModalBG();
    stage.addChild(modal_bg.GetImg);

    job = new ClassJob();
    stage.addChild(job.GetImg);


    loc.Show();
    job.Show();  */

    window_controller = new ClassWindowController();
    //window_controller.windows[window_controller.getIDbyName("main_menu")].getImg.visible = true;
    window_controller.next = "s_scr";
    window_controller.ShowPost();
    //window_controller.Upd();

    createjs.Ticker.setFPS(30);
    createjs.Ticker.addEventListener("tick", Upd);
}

function Upd()
{
    window_controller.Drw();
    stage.update();
}

function Resize()
{
    document.body.style.height = ((window.innerHeight * 2) <<0) + 'px';
    window.scrollTo(0, 1);
    width = window.innerWidth;
    height = window.innerHeight;

    if(rotate != undefined && width > height)
    {
        document.bgColor = "#fff";
        var wh = Math.min(height, Math.min(width, 400));
        game_area.style.display = "none";
        rotate.style.display = "block";
        rotate.style.height = wh+"px";
        rotate.style.width = wh+"px";
        rotate.style.marginLeft = wh/(-2)+"px";
        rotate.style.marginTop = wh/(-2)+"px";
    }
    else
    {
        document.bgColor = "#000";
        var newWidthToHeight = width / height;
        var widthToHeight = 640 / 744;
        if (newWidthToHeight >= widthToHeight)
        {
            width = height * (640 / 744);
            height = (height / 744) * 960;
        }
        else
        {
            height = width / (640 / 960);
        }
        if(rotate != undefined) rotate.style.display = "none";
        game_area.style.display = "block";
        game_area.style.width = (width<<0) + 'px';
        game_area.style.height = (height<<0) + 'px';
        game_area.style.marginLeft = ((-width / 2)<<0) + 'px';
        game_area.style.marginTop = ((-height / 2)<<0) + 'px';
    }
}

window.onpageshow = function()
{
};

window.onpagehide = function()
{
};