
var click_counter = 0;

function init_title_screen()
{

}

var title_image = new Image();
title_image.src = "img/title/title.png";

var title_timer = 0;


var char_image = [];
char_image[0] = new Image();
char_image[0].src = "img/title/warrior.png";
char_image[1] = new Image();
char_image[1].src = "img/title/thief.png";
char_image[2] = new Image();
char_image[2].src = "img/title/priestess.png";
char_image[3] = new Image();
char_image[3].src = "img/title/witch.png";
char_image[4] = new Image();
char_image[4].src = "img/title/bard.png";
char_image[5] = new Image();
char_image[5].src = "img/title/spellblade.png";
char_image[6] = new Image();
char_image[6].src = "img/title/knight.png";
char_image[7] = new Image();
char_image[7].src = "img/title/illusionist.png";
char_image[8] = new Image();
char_image[8].src = "img/title/engineer.png";
char_image[9] = new Image();
char_image[9].src = "img/title/ranger.png";
char_image[10] = new Image();
char_image[10].src = "img/title/psychic.png";
char_image[11] = new Image();
char_image[11].src = "img/title/monk.png";
char_image[12] = new Image();
char_image[12].src = "img/title/dancer.png";
char_image[13] = new Image();
char_image[13].src = "img/title/necromancer.png";
char_image[14] = new Image();
char_image[14].src = "img/title/merchant.png";
char_image[15] = new Image();
char_image[15].src = "img/title/trickster.png";
char_image[16] = new Image();
char_image[16].src = "img/title/barbarian.png";
char_image[17] = new Image();
char_image[17].src = "img/title/inquisitor.png";
char_image[18] = new Image();
char_image[18].src = "img/title/druid.png";
char_image[19] = new Image();
char_image[19].src = "img/title/alchemist.png";
char_image[20] = new Image();
char_image[20].src = "img/title/magus.png";

var lock_char_image = [];
lock_char_image[0] = new Image();
lock_char_image[0].src = "img/title/warrior.png";
lock_char_image[1] = new Image();
lock_char_image[1].src = "img/title/thief.png";
lock_char_image[2] = new Image();
lock_char_image[2].src = "img/title/priestess.png";
lock_char_image[3] = new Image();
lock_char_image[3].src = "img/title/witch.png";
lock_char_image[4] = new Image();
lock_char_image[4].src = "img/title/lock_bard.png";
lock_char_image[5] = new Image();
lock_char_image[5].src = "img/title/lock_spellblade.png";
lock_char_image[6] = new Image();
lock_char_image[6].src = "img/title/lock_knight.png";
lock_char_image[7] = new Image();
lock_char_image[7].src = "img/title/lock_illusionist.png";
lock_char_image[8] = new Image();
lock_char_image[8].src = "img/title/lock_engineer.png";
lock_char_image[9] = new Image();
lock_char_image[9].src = "img/title/lock_ranger.png";
lock_char_image[10] = new Image();
lock_char_image[10].src = "img/title/lock_psychic.png";
lock_char_image[11] = new Image();
lock_char_image[11].src = "img/title/lock_monk.png";
lock_char_image[12] = new Image();
lock_char_image[12].src = "img/title/lock_dancer.png";
lock_char_image[13] = new Image();
lock_char_image[13].src = "img/title/lock_necromancer.png";
lock_char_image[14] = new Image();
lock_char_image[14].src = "img/title/lock_merchant.png";
lock_char_image[15] = new Image();
lock_char_image[15].src = "img/title/lock_trickster.png";
lock_char_image[16] = new Image();
lock_char_image[16].src = "img/title/lock_barbarian.png";
lock_char_image[17] = new Image();
lock_char_image[17].src = "img/title/lock_inquisitor.png";
lock_char_image[18] = new Image();
lock_char_image[18].src = "img/title/lock_druid.png";
lock_char_image[19] = new Image();
lock_char_image[19].src = "img/title/lock_alchemist.png";
lock_char_image[20] = new Image();
lock_char_image[20].src = "img/title/lock_magus.png";

var credits_button_image = new Image();
credits_button_image.src = "img/ui/credits.png";


var bgm_button = [];
bgm_button[0] = new Image();
bgm_button[0].src = "img/ui/bgm0.png";
bgm_button[1] = new Image();
bgm_button[1].src = "img/ui/bgm1.png";
bgm_button[2] = new Image();
bgm_button[2].src = "img/ui/bgm2.png";
bgm_button[3] = new Image();
bgm_button[3].src = "img/ui/bgm3.png";

var sfx_button = [];
sfx_button[0] = new Image();
sfx_button[0].src = "img/ui/sfx0.png";
sfx_button[1] = new Image();
sfx_button[1].src = "img/ui/sfx1.png";
sfx_button[2] = new Image();
sfx_button[2].src = "img/ui/sfx2.png";
sfx_button[3] = new Image();
sfx_button[3].src = "img/ui/sfx3.png";

var logo_image = new Image();
logo_image.src = "img/title/logo.png";

function update_title_screen()
{
    title_timer += get_frame_time();

    //console.log("title");
    //draw_text("Eat the Dungeon!", 150, 200, 100);


    draw_center(title_image, .5 * CANVAS_WIDTH, .5 * CANVAS_HEIGHT, CANVAS_WIDTH, CANVAS_HEIGHT);


    /*__adventurer_list[7].draw(
		.5 * CANVAS_WIDTH, .5 * CANVAS_HEIGHT,
        .9 * .9 *CANVAS_HEIGHT,  .9 * CANVAS_HEIGHT, 1.0, 0, false);
         
        return;*/
        
    var char_pf = 2.0 * title_timer;
    //var char_sf = 3.0 * title_timer - 1.0;
    //char_sf = clamp(char_sf, 0.0, 1.0);
    
    //console.log(char_pf + "|" + char_sf);

    for(var i in char_image)
    {
        var temp_pf = char_pf - i * .1;

        temp_pf = clamp(temp_pf, 0.0, 1.0);
        var x = Math.pow(1.0 - temp_pf, 2.0) * 2160.0;
        if(i % 2 == 0)
            x *= -1.0;

        var y = (1 - 2 * (i % 2)) * -300.0 * (-4 * Math.pow(temp_pf -.5, 2.0) + 1);

        //var ws = 1.0 + (-4 * Math.pow(char_sf-.5, 2.0) + 1)

        var s = 1.0 + .05 * Math.sin(2 * title_timer + 4 * i);
        if(s < 1.0)
            s = 1.0;

        if (__adventurer_list[i].unlocked)
            draw_center(char_image[i], .5 * CANVAS_WIDTH + x, .5 * CANVAS_HEIGHT + y,
                s * CANVAS_WIDTH, s * CANVAS_HEIGHT);
                else
        draw_center(lock_char_image[i], .5 * CANVAS_WIDTH + x, .5 * CANVAS_HEIGHT + y,
            s * CANVAS_WIDTH, s * CANVAS_HEIGHT);
    }

    var hs = Math.sin(Math.PI * (3.0 * title_timer - 1.0)) + (3.0 * title_timer - 1.0);
    if(hs < 0)
        hs = 0;
    if(title_timer > .66)
    {
        hs = 1;
        //if(title_timer > 1)
        {

    }
}

    hs *= 1.0 + .1 * Math.sin(Math.PI * title_timer);
    if(title_timer > .66 &&  hs < 1)
        hs = 1;

        //console.log(hs);
        
        var ws = 2.0 - hs;

    draw_center(logo_image, .5 * CANVAS_WIDTH, .5 * CANVAS_HEIGHT , CANVAS_WIDTH*ws, CANVAS_HEIGHT* hs);

    //var button_size = 25;
    //if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, 450, 400, 100, 50))
    //  button_size = 50;

    if (click_counter == 1)
        draw_text("Click again to start the game", .425 * CANVAS_WIDTH - 7 * 25, .9 * CANVAS_HEIGHT, 50);
    else
        draw_text("Click to start music", .5 * CANVAS_WIDTH - 7 * 25, .9 * CANVAS_HEIGHT, 50);

    //if(INPUT.left_pressed)
            //draw_text("TEST", .5 * CANVAS_WIDTH, .5 * CANVAS_HEIGHT, 50);
    
    draw_text("version 0.16", .85 * CANVAS_WIDTH, .975 * CANVAS_HEIGHT, 25 * frame_scale);

    if (draw_button(credits_button_image, 0, 55 * frame_scale, CANVAS_HEIGHT - 30 * frame_scale,
         100 * frame_scale, 45* frame_scale, ""))
    {
        //console.log("dfsklasadjkf");
        next_gamestate = GAMESTATE_ENUM.CREDITS_SCREEN;
    }


    if (draw_button(bgm_button[get_bgm_volume()], 1, 165 * frame_scale, CANVAS_HEIGHT - 30 * frame_scale,
            100 * frame_scale, 45 * frame_scale, "")) {
        toggle_bgm_volume();
    }

    if (draw_button(sfx_button[get_sfx_volume()], 2, 275 * frame_scale, CANVAS_HEIGHT - 30 * frame_scale,
            100 * frame_scale, 45 * frame_scale, "", null, null, null, true)) {
        toggle_sfx_volume();
        button_sfx.play();
    }

    
    if(next_gamestate == current_gamestate)
    {
        if(INPUT.left_triggered && INPUT.mouse_screen.x > 0 && INPUT.mouse_screen.y > 0 &&
        INPUT.mouse_screen.x < CANVAS_WIDTH && INPUT.mouse_screen.y < CANVAS_HEIGHT)
        {
            if (INPUT.mouse_screen.x > 350 * frame_scale || INPUT.mouse_screen.y < CANVAS_HEIGHT - 60 * frame_scale)
            {
                click_counter += 1;

                if (click_counter == 1)
                {
                    switch_bgm(SONG_ENUM.BGM_TITLE);

                }
                else
                {
                    try
                    {
			            var test = localStorage.getItem("current_fat");

			            start_sfx.play();

                        if(test && !isNaN(test)) //savefile is valid
                        {
                            //console.log("click");
                            next_gamestate = GAMESTATE_ENUM.UPGRADES;
                        }
                        else
                        {
                            adventurer_field[0] = __adventurer_list[0];
                            adventurer_field[1] =__adventurer_list[1];
                            adventurer_field[2] =__adventurer_list[2];
                            adventurer_field[3] =__adventurer_list[3];

                            //console.log("click");
                            //start on cutscene for level 0 tutorial
                            next_gamestate = GAMESTATE_ENUM.UPGRADES;
                            current_level = 0;
                            enter_cutscene(0);
                            switch_gamestate_flag = true;

                        }
        	            }
                    catch(e)
                    {
                        console.log(e.message);
                    }
                }
            }
        }
    }
    
    //__adventurer_list[4].draw_small(CANVAS_WIDTH * .5, CANVAS_HEIGHT * .5, CANVAS_WIDTH * .5, 1.0);
}


function exit_title_screen()
{

}