var command_history = [];
var command_history_position = -1;
var command_in_progress = "";

$(document).ready( function() {

  $("#keyboard_input").focus();

  command_history = initial_history;

  $("#input_form").submit( function () {
    dismiss_errors_and_hints();
    $("#keyboard_input").css( 'background-color', '#eee' );

    command_history.unshift( $("#keyboard_input").val() );
    command_history_position = -1;

    $.post( "/i", $(this).serialize(), update_story, "json" );

    return false;
  } );

  $("#keyboard_input").keydown( function( e ) {
    if( e.which == 38 ) {       /* up */
      if( command_history_position < command_history.length -1  ) {
        if( command_history_position == -1 ) {
          command_in_progress = $("#keyboard_input").val();
        }
        command_history_position++;
        $("#keyboard_input").val( command_history[command_history_position] );
      }
    }
    else if( e.which == 40 ) {  /* down */
      if( command_history_position > 0 && command_history.length > 0 ) {
        command_history_position--;
        $("#keyboard_input").val( command_history[command_history_position] );
      }
      else if( command_history_position == 0 ) {
        command_history_position = -1;
        $("#keyboard_input").val( command_in_progress );
      }
    }
  } );

} );

function cmd( i ) {
  $("#keyboard_input").val( i );
  $("#input_form").submit();
} 

function update_story( json ) {
  $("#keyboard_input").val( "" ).focus();
  $("#keyboard_input").css( 'background-color', '#fff' );

  if( json.error ) {
    $("#error").html( json.error );
    $("#error").css( 'display', 'block' );
  }
  else {
    $("#story").fadeOut( 300, function() {
      this.innerHTML = json.html;
      $(this).fadeIn( 300, function() {
        if( json.save_id ) {
          $("#restart").css( 'display', 'inline' );
        }

        $("#error").css( 'display', 'none' );
        $("#error").html( "" );
      } );
    } );

    $("#save_id").val( json.save_id );

    if( json.location_name ) {
      $("#location_name_link").html( json.location_name );
    }

    if( json.score != undefined ) {
      $("#score").html( "" + json.score );
    }

    if( json.turns != undefined ) {
      $("#turns").html( "" + json.turns );
    }

    if( json.hours != undefined ) {
      $("#hours").html( "" + json.hours );
    }

    if( json.minutes != undefined ) {
      $("#minutes").html( "" + json.minutes );
    }

    if( ! json.save_id ) {
      $("#restart").css( 'display', 'none' );
    }
  }
}

function restart_machine() {
  var save_id = $("#save_id").val();

  dismiss_errors_and_hints();
  $.post( "/r", "save_id=" + save_id, update_story, "json" );
}

function hints( story_name ) {
  if( $("#hints").css( 'display' ) == 'none' ) {
    var save_id = $("#save_id").val();
    var params = "story_name=" + story_name + "&save_id=" + save_id;

    $.post( "/h", params, update_hints, "html" );
  }
  else {
    dismiss_errors_and_hints();
  }

  $("#keyboard_input").val( "" ).focus();
}

function update_hints( html ) {
  dismiss_errors_and_hints();
  if( html ) {
    $("#hints").html( html );
    $("#hints").show( 300 );
    $("#hints_link").addClass( 'selected' );
  }
}

function exits( story_name ) {
  if( $("#exits").css( 'display' ) == 'none' ) {
    var save_id = $("#save_id").val();
    var params = "story_name=" + story_name + "&save_id=" + save_id;

    $.post( "/e", params, update_exits, "html" );
  }
  else {
    dismiss_errors_and_hints();
  }

  $("#keyboard_input").val( "" ).focus();
}

function update_exits( html ) {
  dismiss_errors_and_hints();
  if( html ) {
    $("#exits").html( html );
    $("#exits").show( 300 );
    $("#exits_link").addClass( 'selected' );
  }
}

function history() {
  if( $("#history").css( 'display' ) == 'none' ) {
    var save_id = $("#save_id").val();
    var params = "save_id=" + save_id;

    $.post( "/ch", params, update_history, "html" );
  }
  else {
    dismiss_errors_and_hints();
  }

  $("#keyboard_input").val( "" ).focus();
}

function update_history( html ) {
  dismiss_errors_and_hints();
  if( html ) {
    $("#history").html( html );
    $("#history").show( 300 );
    $("#history_link").addClass( 'selected' );
  }
}

function dismiss_errors_and_hints() {
  if( $("#errors").css( 'display' ) == 'block' ) {
    $("#errors").hide( 300 );
  }
  else if( $("#hints").css( 'display' ) == 'block' ) {
    $("#hints").hide( 300 );
    $("#hints_link").removeClass( 'selected' );
  } 
  else if( $("#exits").css( 'display' ) == 'block' ) {
    $("#exits").hide( 300 );
    $("#exits_link").removeClass( 'selected' );
  }
  else if( $("#history").css( 'display' ) == 'block' ) {
    $("#history").hide( 300 );
    $("#history_link").removeClass( 'selected' );
  }
}

