//contains global variables used
var Global = {};

//contains all the rotations of images
var Images = {
  Spaceship : []
};

//everything that runs the game
var Game = {
  Timer  : null,
  Player : null,
  playerAccel : 0,
  
  Init : function () {
    var startX  = Global.winWidth/2 - 12;
    var startY  = Global.winHeight/2 - 12;
    Game.Player = new Spaceship(startX, startY);
    Game.Timer  = window.setInterval(Game.movePlayer, 30);
  },
  
  movePlayer : function () {
    var ang = 0;
    if (Key.isDown(Key.RIGHT)) ang += 4;
    if (Key.isDown(Key.LEFT))  ang -= 4;
    if (ang != 0) Game.Player.rotate(ang);
    
    if (Key.isDown(Key.UP)) {
      if (Game.playerAccel < 10) {
        Game.playerAccel++;
      }
    } else {
      if (Game.playerAccel > 0 && Game.playerAccel < 0.25) {
        Game.playerAccel = 0;
      } else {
        Game.playerAccel *= 0.9
      }
    }
    
    Game.Player.move(Game.playerAccel);
  }
};

//loads the spaceship images
var SpaceshipLoader = {
  loading  : null,
  counter  : 0,
  commence : function () {
    //create loading text
    SpaceshipLoader.loading = document.createElement("div");
    SpaceshipLoader.loading.className = "Loading";
    SpaceshipLoader.loading.appendChild(document.createTextNode("Loading..."));
    document.body.appendChild(SpaceshipLoader.loading);
    
    //load images
    for (var i = 0; i <= 364; i += 4) {
      Images.Spaceship[i] = new Image(24, 24);
      Images.Spaceship[i].onload = SpaceshipLoader.increment;
      Images.Spaceship[i].src    = "src/rotate.php?a="+ i;
    }
  },
  
  destroy : function () {
    document.body.removeChild(SpaceshipLoader.loading);
    Game.Init();
  },
  
  increment : function () {
    SpaceshipLoader.counter++;
    if (SpaceshipLoader.counter == 91) {
      SpaceshipLoader.destroy();
    }
  }
};

//the constructor for the spaceship(s)
function Spaceship (x, y, p) {
  //public methods
  this.move = function(dist) {
    x += dist * Math.cos(angle * Math.PI/180);
    y += dist * Math.sin(angle * Math.PI/180);
    
    if (x < 0) x = Global.winWidth - 24;
    if (x > Global.winWidth - 24) x = 0;
    if (y < 0) y = Global.winHeight - 24;
    if (y > Global.winHeight - 24) y = 0;
    
    elements.container.style.left = x +"px";
    elements.container.style.top  = y +"px";
  };
  
  this.rotate = function (a) {
    if (angle + a != angle) {
      angle += a;
      if (angle >= 360) angle -= 360;
      if (angle < 0)    angle += 360;
      elements.ship.style.left = "-"+ ((360 - angle) * 6) +"px";
    }
  };
  
  //private variables
  var angle  = 0;
  var x = x || 0;
  var y = y || 0;
  var elements = {
    ship : document.createElement("div"),
    container : document.createElement("div"),
    parent : p ? p : document.body
  };
  
  //spaceship images
  var current_image;
  for (var i = 0; i < 364; i+= 4) {
    current_image = Images.Spaceship[i].cloneNode(false);
    setStyles(current_image, {
      position : "absolute",
      left     : i * 6 +"px",
      top      : "0px"
    });
    elements.ship.appendChild(current_image);
  }
  
  //styling for container objects
  setStyles(elements.ship, {
    position : "absolute",
    left     : "0px",
    top      : "0px"
  });
  setStyles(elements.container, {
    position : "absolute",
    left     : x +"px",
    top      : y +"px",
    width    : "24px",
    height   : "24px",
    overflow : "hidden"
  });
  
  //append elements
  elements.container.appendChild(elements.ship);
  elements.parent.appendChild(elements.container);  
};

//other functions
function setStyles (o, s) {
  for (var prop in s) {
    o.style[prop] = s[prop];
  }
};

function getWindowDimensions () {
  if (document.documentElement) {
    Global.winWidth  = document.documentElement.clientWidth;
    Global.winHeight = document.documentElement.clientHeight;
  } else {
    Global.winWidth  = document.body.clientWidth;
    Global.winHeight = document.body.clientHeight;
  }
};

window.onload = function () {
  getWindowDimensions();
  window.onresize = getWindowDimensions;
  SpaceshipLoader.commence();
};