/**
 * Adopt game dimensions to current screen size
 */
jQuery(document).ready( function($) {
  var game_width;
  var game_height;
  var game_ratio;   /* Game height/width ratio */
  var resized;      /* Set true if the game has been resized */
  var game;         /* Game object */

  /**
   * Resize game object
   *
   * @version 1.2.0
   * @since   1.2.0
   * @return  void
   */
  function game_resize() {
    // Get the new body width
    var new_width = $("body").width();
    // Add some space (for frames or borders)
    var check_width = parseInt(game_width);

    // Check defined width
    if ( new_width <= 984 && new_width >= 759 ) {
      new_width = 690;
    }

    // Check if the body width is smaller than the game
    // If so, resize the game
    if ( new_width <= check_width  ) {
      game.attr("width", parseInt(new_width) - 50 );
      game.attr("height", (parseInt(new_width) - 50 ) * game_ratio);
      resized = true;
    }
    else {
      if ( resized ) {
        // Resize back
        game.attr("width", game_width);
        game.attr("height", game_height);
        resized = false;
      }
    }
  }

  /**
   * Determinate game dimensions
   * Search for a game object and try to get dimension informatino
   *
   * @version 1.2.0
   * @since   1.2.0
   * @return  void
   */
  function determinate_dimension() {
    // Set elemtets to search for
    search = $("embed, iframe, object");
    game = $("#myarcade_game").find( search );
    game_width = game.attr("width");
    game_height = game.attr("height");
    game_ratio = game_height / game_width;
  }

  // Bind resize event with the resize function
  $(window).resize( function(event) {
    game_resize();
  });

  // Initial call
  determinate_dimension();
  game_resize();
});