// GuitarStringObject Class
//****************************************************************************//

function GuitarStringObject( x1, y1, x2, y2, lineWidth, wibble, numStr, delayms, freqpms, durationms ) {
  this.strnum = numStr;
  this.canvasname = "c" + this.strnum;

  this.left = Math.min( x1, x2 ) - wibble;
  this.right = Math.max( x1, x2 ) + wibble;
  this.top = Math.min( y1, y2 ) - wibble;
  this.bottom = Math.max( y1, y2 ) + wibble;
  this.width = this.right - this.left;
  this.height = this.bottom - this.top;
  this.x1 = x1 - this.left;
  this.x2 = x2 - this.left;
  this.y1 = y1 - this.top;
  this.y2 = y2 - this.top;
  this.xm = (this.x1 + this.x2)/2.0;
  this.ym = (this.y1 + this.y2)/2.0;
  
  this.strpos = 0;
  
  this.delayms = delayms;
  this.freqpms = freqpms;
  this.durationms = durationms;
  
  document.write(
    "<canvas OnMouseOver='javascript:guitarStrings["+this.strnum+"].strum()' "+
    "id='" + this.canvasname + "' class='guitarString'></canvas>"
  );

  this.init = function() {
    this.canvas = getObjectRef( this.canvasname );
    this.canvas.style.top = parseInt(this.top) + "px";
    this.canvas.style.left = parseInt(this.left) + "px";
    this.canvas.height = parseInt(this.height);
    this.canvas.width = parseInt(this.width);
    this.ctx = this.canvas.getContext('2d');
  }
    
  this.strum = function() {
    this.ctx.save();

    var mag= (durationms-this.strpos)/durationms;
    
    this.ctx.clearRect(0,0,1000,1024);
    this.ctx.lineCap = "round";
    this.ctx.strokeStyle = "#a29c7f";// ptn;
  
    this.ctx.lineWidth = lineWidth;
    this.ctx.beginPath();
    this.ctx.moveTo(this.x1,this.y1);
    this.ctx.quadraticCurveTo(
      this.xm+mag*wibble*Math.sin(this.strpos * 2 * Math.PI * freqpms), this.ym,
      this.x2, this.y2
    );
    this.ctx.stroke();

    this.ctx.restore();

    this.strpos+=delayms;
    
    if( mag > 0 ) {
      setTimeout("guitarStrings["+this.strnum+"].strum()",delayms);
    }else{
      this.strpos=0;
    }
  }
  
  return this;
}
