function IGO_SpaceShip() 
{
  this.spaceShipImageDir = "../images/spaceship/";
  this.laserImageDir = "../images/lasers/";
  this.spaceShipImageIndex = 0;
  this.rotationInc=22.5;
  this.rotationPos=0;
  this.scale_x=0;
  this.scale_y=0;
  this.spaceShipState=1;
  this.xPos = 0;
  this.xVel = 0;
  this.xVelMax = 5;
  this.yPos = 0;
  this.yVel = 0;
  this.yVelMax = 5;
  this.xAcc = 0;
  this.yAcc = 0;
  
  this.xLaserVel=0;
  this.yLaserVel=0;
  this.xLaserPos=0;
  this.yLaserPos=0;
  this.laserDuration=0;
  this.laserFrame=0;
  this.isFiringLaser=false;
  
  this.spaceShipObj="";
  this.laserObj="";
  this.updateFrameId=0;
  this.updateFrameLaserId=0;
  this.hittingTheGas = 0;
  this.width = 64;
  this.height = 64;
  this.windowWidth=800;
  this.windowHeight=600;
}

function IGO_SpaceShip_Init() {
  //console.log("Entering Init");
  clearTimeout(this.updateFrameId);
  this.updateDisplay();
  this.rememberWindowSize();
  this.scale_x = Math.cos( ((this.rotationPos*Math.PI) / 180) );
  this.scale_y = Math.sin( ((this.rotationPos*Math.PI) / 180) );
  var theObj = this;
  this.updateFrameId=setTimeout(function(){theObj.updatePosition();}, 50);
}
function IGO_SpaceShip_IdentifySpaceShipDiv(d) {
  this.spaceShipObj = document.getElementById(d);
}
function IGO_SpaceShip_IdentifyLaserDivs(d) {
  this.laserObj = document.getElementById(d);
  this.laserObj.style.display="none";
}

function IGO_SpaceShip_FireLaser() {
  
  if (this.isFiringLaser) {return;} // return if we're already firing a laser
  
  this.isFiringLaser = true;
  this.laserDuration=20;
  
  this.xLaserPos=this.xPos+25+(this.scale_x * (this.width/2));
  this.yLaserPos=this.yPos+25+(this.scale_y * (this.height/2));
  this.laserObj.style.left = this.xLaserPos + "px";
  this.laserObj.style.top = this.yLaserPos + "px";
  this.laserObj.style.display="block";
  
  var imageLoc = this.laserImageDir+"circle-blue-0.png";
  this.laserObj.style.backgroundImage="url(" + imageLoc + ")";
  this.xLaserVel=this.scale_x*20;
  this.yLaserVel=this.scale_y*20;
  
  this.laserFrame=0;
  
  var theObj = this;
  this.updateFrameLaserId=setTimeout(function(){theObj.updateLaserPosition();}, 50);
}

function IGO_UpdateLaserPosition() {
  
  this.laserDuration--;
  
  if (this.laserDuration <= 0)
  {
    this.laserObj.style.display="none";
    this.isFiringLaser=false;
    return; 
  }
  
  this.laserFrame++;
  if (this.laserFrame>15){
    this.laserFrame=0; 
  }
  var frameToUse=Math.round(this.laserFrame/3);
  var imageLoc = this.laserImageDir+"circle-blue-"+frameToUse+".png";
  this.laserObj.style.backgroundImage="url(" + imageLoc + ")";
  
  this.xLaserPos = this.xLaserPos + this.xLaserVel;
  this.yLaserPos = this.yLaserPos + this.yLaserVel;

  if (this.xLaserPos > this.windowWidth) {
    this.xLaserPos = 0;
  }
  if (this.yLaserPos > this.windowHeight) {
    this.yLaserPos = 0;
  }
  if (this.xLaserPos < -64) {
    this.xLaserPos = this.windowWidth;
  }
  if (this.xLaserPos < -64) {
    this.xLaserPos = this.windowHeight;
  }

  this.laserObj.style.left = this.xLaserPos + "px";
  this.laserObj.style.top = this.yLaserPos + "px";
  
  var theObj = this;
  this.updateFrameLaserId=setTimeout(function(){theObj.updateLaserPosition();}, 50);
}

function IGO_SpaceShip_RotateCCW() {
  this.rotationPos=this.rotationPos-this.rotationInc;
  if (this.rotationPos < 0) {this.rotationPos=360-this.rotationInc;}
  this.scale_x = Math.cos( ((this.rotationPos*Math.PI) / 180) );
  this.scale_y = Math.sin( ((this.rotationPos*Math.PI) / 180) );
  this.updateDisplay();
}
function IGO_SpaceShip_RotateCW() {
  this.rotationPos=this.rotationPos+this.rotationInc;
  if (this.rotationPos >= 360) {this.rotationPos=0;}
  this.scale_x = Math.cos( ((this.rotationPos*Math.PI) / 180) );
  this.scale_y = Math.sin( ((this.rotationPos*Math.PI) / 180) );
  this.updateDisplay();
}
function IGO_SpaceShip_UpdateDisplay() {
  var imageLoc = this.spaceShipImageDir+"spaceship-"+this.spaceShipState+"-"+this.rotationPos+".png";
  //console.log("imageLoc = " + imageLoc);
  //console.log("this.spaceShipDiv = " + this.spaceShipDiv);
  this.spaceShipObj.style.backgroundImage="url(" + imageLoc + ")";
}
function IGO_SpaceShip_HitTheGas() {

  this.xAcc = this.scale_x * 1;
  this.yAcc = this.scale_y * 1;  

  this.xVel = this.xVel + this.xAcc;
  this.yVel = this.yVel + this.yAcc;

  if (this.xVel > 5) {this.xVel=5;}
  if (this.yVel > 5) {this.yVel=5;}

  if (this.xVel < -5) {this.xVel=-5;}
  if (this.yVel < -5) {this.yVel=-5;}
  
  this.hittingTheGas = 3;
  
  if (this.spaceShipState == 1) {
    this.spaceShipState = 2;
    this.updateDisplay();
  } else if (this.spaceShipState == 2) {
    this.spaceShipState = 3;
    this.updateDisplay();    
  } else if (this.spaceShipState == 3) {
    this.spaceShipState = 2;
    this.updateDisplay();    
  }
}
function IGO_SpaceShip_UpdatePosition() {

  if (this.hittingTheGas > 0) {
    this.hittingTheGas--;
    if (this.hittingTheGas == 0) {
      this.spaceShipState = 1;
      this.updateDisplay();
    }
  }

  this.xPos = this.xPos + this.xVel;
  this.yPos = this.yPos + this.yVel;

  if (this.xPos > this.windowWidth) {
    this.xPos = 0;
  }
  if (this.yPos > this.windowHeight) {
    this.yPos = 0;
  }
  if (this.xPos < -64) {
    this.xPos = this.windowWidth;
  }
  if (this.yPos < -64) {
    this.yPos = this.windowHeight;
  }

  this.spaceShipObj.style.left=this.xPos+"px";
  this.spaceShipObj.style.top=this.yPos+"px";
  
  var theObj = this;
  this.updateFrameId=setTimeout(function(){theObj.updatePosition();}, 50);
}

function IGO_SpaceShip_GetWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  this.windowWidth = myWidth;
  this.windowHeight = myHeight;
}

IGO_SpaceShip.prototype.identifyLaserDivs = IGO_SpaceShip_IdentifyLaserDivs;
IGO_SpaceShip.prototype.identifySpaceShipDiv = IGO_SpaceShip_IdentifySpaceShipDiv;
IGO_SpaceShip.prototype.rotateCW = IGO_SpaceShip_RotateCW;
IGO_SpaceShip.prototype.rotateCCW = IGO_SpaceShip_RotateCCW;
IGO_SpaceShip.prototype.updateDisplay = IGO_SpaceShip_UpdateDisplay;
IGO_SpaceShip.prototype.updatePosition = IGO_SpaceShip_UpdatePosition;

IGO_SpaceShip.prototype.hitTheGas = IGO_SpaceShip_HitTheGas;
IGO_SpaceShip.prototype.init = IGO_SpaceShip_Init;
IGO_SpaceShip.prototype.rememberWindowSize = IGO_SpaceShip_GetWindowSize;
IGO_SpaceShip.prototype.fireLaser = IGO_SpaceShip_FireLaser;
IGO_SpaceShip.prototype.updateLaserPosition = IGO_UpdateLaserPosition;