///scr_Book_StepEvent_Keyboard
//Place this script within the Step Event of the obj_Book or equivalent.
//This script takes no arguments.

var RandomPageTurningSound;
var Random = irandom_range(1,3);
if (Random = 1) {RandomPageTurningSound = PageTurnAudio1;}
if (Random = 2) {RandomPageTurningSound = PageTurnAudio2;}
if (Random = 3) {RandomPageTurningSound = PageTurnAudio3;}

//If first page key is released, go to title page (-1)...
if keyboard_check_released(Keyboard_FirstPage1) or keyboard_check_released(Keyboard_FirstPage2) or keyboard_check_released(Keyboard_FirstPage3){
    PreviousPage = p; //Keep at beginning
    p = -1;
    if sound_isplaying(Narration[PreviousPage]){ sound_stop(Narration[PreviousPage]); }
    HeardIt = false;
    sound_play(RandomPageTurningSound);
}
//If last page key is released, go to the last page...
if keyboard_check_released(Keyboard_LastPage1) or keyboard_check_released(Keyboard_LastPage2) or keyboard_check_released(Keyboard_LastPage3){
    PreviousPage = p; //Keep at beginning
    p = TotalPages-1;
    if sound_isplaying(Narration[PreviousPage]){ sound_stop(Narration[PreviousPage]); }
    HeardIt = false;
    sound_play(RandomPageTurningSound);
}

//If page forward keys are used and not on last page...
if (p < (TotalPages-2)){
    if keyboard_check_released(Keyboard_PageForward1) or keyboard_check_released(Keyboard_PageForward2) or keyboard_check_released(Keyboard_PageForward3) or keyboard_check_released(Keyboard_PageForward4){
        PreviousPage = p; //Keep at beginning
        p += 2; 
        if sound_isplaying(Narration[PreviousPage]){ sound_stop(Narration[PreviousPage]); }
        HeardIt = false;
        sound_play(RandomPageTurningSound);
    }
}

//If page backward keys are used and not on title page...
if (p > 0){
    if keyboard_check_released(Keyboard_PageBackward1) or keyboard_check_released(Keyboard_PageBackward2) or keyboard_check_released(Keyboard_PageBackward3) or keyboard_check_released(Keyboard_PageBackward4){ 
        PreviousPage = p; //Keep at beginning
        p -= 2;
        if sound_isplaying(Narration[PreviousPage]){ sound_stop(Narration[PreviousPage]); }
        HeardIt = false;
        sound_play(RandomPageTurningSound);
    }
}

//If bookmark key is released...
if keyboard_check_released(Keyboard_Bookmark1) or keyboard_check_released(Keyboard_Bookmark2){
    ini_open(Book);
        ini_write_real("Settings","Bookmark",p);
    ini_close();
    if (BackgroundAudioOn = "true"){ sound_play(BookmarkAudio); }
    Bookmark = p;
}

//If toggle background music mute on/off is used...
if keyboard_check_released(Keyboard_ToggleMusic){
    if (BackgroundAudioOn = "false"){ BackgroundAudioOn = "true"; }
    else{ BackgroundAudioOn = "false"; }
    ini_open(working_directory+"Settings/Audio.ini");
        ini_write_string("Books","BackgroundAudioOn",string(BackgroundAudioOn));
    ini_close();
    keyboard_clear(ord('M'));
}

//If narration on/off keys are used...
if keyboard_check_released(Keyboard_ToggleNarration){
    if (NarrationOn = "true"){ NarrationOn = "false"; }
    else{ NarrationOn = "true"; }
    ini_open(working_directory+"Settings/Audio.ini");
        ini_write_string("Books","NarrationOn",string(NarrationOn));
    ini_close();
    keyboard_clear(ord('N'));
}

//If exit keys used...
if keyboard_check_released(Keyboard_Exit1) or keyboard_check_released(Keyboard_Exit2){
    script_execute(scr_Book_CloseBook);
}
