
var INPUT = {left_pressed:false, right_pressed:false, left_triggered:false, right_triggered:false, left_released:false, mouse_screen:{}};


document.onmousemove = function (mouse) {

    /*
    if (init_sound)
        draw_text("TRUE", 20, 50);
    draw_text("onmousemove", 5, 20, 20);
    */

	INPUT.mouse_screen.x = ((mouse.clientX - canvas.getBoundingClientRect().left) / canvas.getBoundingClientRect().width) * CANVAS_WIDTH;
	INPUT.mouse_screen.y = ((mouse.clientY - canvas.getBoundingClientRect().top) / canvas.getBoundingClientRect().height) * CANVAS_HEIGHT;
}

document.onmousedown = function (mouse) {
    /*
    if (init_sound)
        draw_text("TRUE", 20, 50);*/
    //audio_context.resume();

    //draw_text("onmousedown", 5, 20, 20);

	if(mouse.which === 1)
	{
		INPUT.left_pressed = true;
		INPUT.left_triggered = true;
		//console.log("mouse");
	}
	else
	{
		INPUT.right_pressed = true;
		INPUT.right_triggered = true;
	}
}
document.onmouseup = function (mouse) {

    //if (init_sound)
   //     draw_text("TRUE", 20, 50);
    //draw_text("onmouseup", 5, 20, 20);

	if (mouse.which === 1) {

		INPUT.left_pressed = false;
		INPUT.left_released = true;
	}
	else
		INPUT.right_pressed = false;
}
//document.oncontextmenu = function(mouse){
//	mouse.preventDefault();
//}
//*

var init_sound = true;

document.ontouchstart = function (event)
{

   // if (init_sound)
    //    draw_text("TRUE", 20, 50);
    //draw_text("ontouchstart", 5, 20, 20);

	INPUT.left_pressed = true;
	INPUT.left_triggered = true;
	//console.log("mouse");
	INPUT.mouse_screen.x = ((event.changedTouches[0].pageX - canvas.getBoundingClientRect().left) / canvas.getBoundingClientRect().width) * CANVAS_WIDTH;;
	INPUT.mouse_screen.y = ((event.changedTouches[0].pageY - canvas.getBoundingClientRect().top) / canvas.getBoundingClientRect().height) * CANVAS_HEIGHT;;
	
	if (init_sound) {
	    //*
        bgm_array[SONG_ENUM.BGM_MAIN].sound.play();
	    bgm_array[SONG_ENUM.BGM_BOSS].sound.play();
	    bgm_array[SONG_ENUM.BGM_BONUS].sound.play();
	    bgm_array[SONG_ENUM.BGM_VICTORY].sound.play();
	    bgm_array[SONG_ENUM.BGM_WORLD].sound.play();
	    bgm_array[SONG_ENUM.BGM_TITLE].sound.play();

	    bgm_array[SONG_ENUM.BGM_MAIN].sound.pause();
	    bgm_array[SONG_ENUM.BGM_BOSS].sound.pause();
	    bgm_array[SONG_ENUM.BGM_BONUS].sound.pause();
	    bgm_array[SONG_ENUM.BGM_VICTORY].sound.pause();
	    bgm_array[SONG_ENUM.BGM_WORLD].sound.pause();
	    bgm_array[SONG_ENUM.BGM_TITLE].sound.pause();//*/

	    button_sfx.play();
	    /*no_hit_sfx.sound.play();
	    weapon_sfx.sound.play();
	    magic_sfx.sound.play();
	    faith_sfx.sound.play();
	    sneak_sfx.sound.play();
	    heal_sfx.sound.play();
	    armor_sfx.sound.play();
	    buff_sfx.sound.play();
	    ko_sfx.sound.play();
	    type_change_sfx.sound.play();
	    ready_sfx.sound.play();
	    enemy_death_sfx.sound.play();
	    run_sfx.sound.play();
	    advance_sfx.sound.play();
	    grade_sfx.sound.play();
	    level_up0_sfx.sound.play();
	    level_up1_sfx.sound.play();
	    level_up2_sfx.sound.play();
	    level_up3_sfx.sound.play();
	    extract_sfx.sound.play();
	    reset_sfx.sound.play();
	    start_sfx.sound.play();*/

	    //button_sfx.pause();
	    /*no_hit_sfx.sound.play();
	    weapon_sfx.sound.play();
	    magic_sfx.sound.play();
	    faith_sfx.sound.play();
	    sneak_sfx.sound.play();
	    heal_sfx.sound.play();
	    armor_sfx.sound.play();
	    buff_sfx.sound.play();
	    ko_sfx.sound.play();
	    type_change_sfx.sound.play();
	    ready_sfx.sound.play();
	    enemy_death_sfx.sound.play();
	    run_sfx.sound.play();
	    advance_sfx.sound.play();
	    grade_sfx.sound.play();
	    level_up0_sfx.sound.play();
	    level_up1_sfx.sound.play();
	    level_up2_sfx.sound.play();
	    level_up3_sfx.sound.play();
	    extract_sfx.sound.play();
	    reset_sfx.sound.play();
	    start_sfx.sound.play();*/

	    init_sound = false;
	}

	if(INPUT.mouse_screen.x > 0 && INPUT.mouse_screen.y > 0 &&
		INPUT.mouse_screen.x < CANVAS_WIDTH && INPUT.mouse_screen.y < CANVAS_HEIGHT)
	{
		event.preventDefault();
	}
}

document.ontouchmove = function (event) {
    
    //if (init_sound)
    //    draw_text("TRUE", 20, 50);

    //draw_text("ontouchmove", 5, 20, 20);

	INPUT.mouse_screen.x = ((event.changedTouches[0].pageX - canvas.getBoundingClientRect().left) / canvas.getBoundingClientRect().width) * CANVAS_WIDTH;;
	INPUT.mouse_screen.y = ((event.changedTouches[0].pageY - canvas.getBoundingClientRect().top) / canvas.getBoundingClientRect().height) * CANVAS_HEIGHT;;

	if (init_sound)
	{
	    //*
        bgm_array[SONG_ENUM.BGM_MAIN].sound.play();
	    bgm_array[SONG_ENUM.BGM_BOSS].sound.play();
	    bgm_array[SONG_ENUM.BGM_BONUS].sound.play();
	    bgm_array[SONG_ENUM.BGM_VICTORY].sound.play();
	    bgm_array[SONG_ENUM.BGM_WORLD].sound.play();
	    bgm_array[SONG_ENUM.BGM_TITLE].sound.play();

	    bgm_array[SONG_ENUM.BGM_MAIN].sound.pause();
	    bgm_array[SONG_ENUM.BGM_BOSS].sound.pause();
	    bgm_array[SONG_ENUM.BGM_BONUS].sound.pause();
	    bgm_array[SONG_ENUM.BGM_VICTORY].sound.pause();
	    bgm_array[SONG_ENUM.BGM_WORLD].sound.pause();
	    bgm_array[SONG_ENUM.BGM_TITLE].sound.pause();//*/


	    button_sfx.play();
	    //button_sfx.pause();

	    init_sound = false;
	}

	//draw_text("BLUH", 5, 20, 20);

	if(INPUT.mouse_screen.x > 0 && INPUT.mouse_screen.y > 0 &&
		INPUT.mouse_screen.x < CANVAS_WIDTH && INPUT.mouse_screen.y < CANVAS_HEIGHT)
	{
		event.preventDefault();
	}
}

document.ontouchend = function (event)
{
    //if (init_sound)
    //    draw_text("TRUE", 20, 50);
	//draw_text("ontouchend", 5, 20, 20);
	INPUT.left_pressed = false;
	INPUT.left_released = true;
	//console.log("mouse");
    
	if (init_sound) {
	    //*
        
        bgm_array[SONG_ENUM.BGM_MAIN].sound.play();
	    bgm_array[SONG_ENUM.BGM_BOSS].sound.play();
	    bgm_array[SONG_ENUM.BGM_BONUS].sound.play();
	    bgm_array[SONG_ENUM.BGM_VICTORY].sound.play();
	    bgm_array[SONG_ENUM.BGM_WORLD].sound.play();
	    bgm_array[SONG_ENUM.BGM_TITLE].sound.play();

	    bgm_array[SONG_ENUM.BGM_MAIN].sound.pause();
	    bgm_array[SONG_ENUM.BGM_BOSS].sound.pause();
	    bgm_array[SONG_ENUM.BGM_BONUS].sound.pause();
	    bgm_array[SONG_ENUM.BGM_VICTORY].sound.pause();
	    bgm_array[SONG_ENUM.BGM_WORLD].sound.pause();
	    bgm_array[SONG_ENUM.BGM_TITLE].sound.pause();//*/

	    button_sfx.play();
	    //button_sfx.pause();

	    init_sound = false;
	}

	//draw_text("BLEH", 5, 20, 20);

}//*/

INPUT.update = function ()
{
	INPUT.left_triggered = false;
	INPUT.right_triggered = false;
	INPUT.left_released = false;
}