/*
 * Copyright 2009 Jon Haikarainen
 *
 */

//<Point>

function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.rotate = function (angle) {
  var x = (this.x * Math.cos(angle)) + (this.y * Math.sin(angle));
  var y = (-this.x * Math.sin(angle)) + (this.y * Math.cos(angle));
  this.x = x;
  this.y = y;
}

Point.prototype.transform = function (deltaX, deltaY) {
  this.x += deltaX;
  this.y += deltaY;
}

//</Point>

function drawBg() {
  var canvas = document.getElementById("canvas");
  if (!canvas || !canvas.getContext)
    return;

  var ctx = canvas.getContext("2d");

  var numberOfLines = 200;

  winWidth = getPageWidth();
  winHeight = getPageHeight();

  canvas.width = winWidth;
  canvas.height = winHeight;

  var x0 = Math.round(winWidth / 2);
  var y0 = 330; //Math.round(winHeight / 2);

  for (i = 0; i < numberOfLines; i++) {
    var angle = Math.round(Math.random() * 360);
    var len = Math.round(Math.random() * winWidth * 0.55) + 300;
    var width = Math.round(1/Math.sqrt((Math.random() * 9.9 + 0.1)/10)) + 1;
    //var width = Math.round(Math.log(Math.random() * 10 + 1) * 10);
    //var width = Math.round(Math.random() * 10);
    ctx.fillStyle = (Math.random() < 0.1) ? "rgb(200,80,0)" : "rgb(0,0,0)";
    drawArrow(ctx, x0,y0, angle, len, width);
  }
}

function rotatePoint(point, angle) {
  var x = (point.x * Math.cos(angle)) + (point.y * Math.sin(angle));
  var y = (-point.x * Math.sin(angle)) + (point.y * Math.cos(angle));
  point.x = x;
  point.y = y;
  return point;
}

function transformPoint(point, deltaX, deltaY) {
  point.x += deltaX;
  point.y += deltaY;
  return point;
}

function drawArrow(ctx, x, y, angle, len, width) {
  //init
  var point = new Point(len, 0);
  var pointL = new Point(len - width, width);
  var pointR = new Point(len - width, -width);

  //rotate
  point.rotate(angle);
  pointL.rotate(angle);
  pointR.rotate(angle);

  //transform
  point.transform(x, y);
  pointL.transform(x, y);
  pointR.transform(x, y);

  //draw
  ctx.beginPath();
  ctx.moveTo(x, y);
  ctx.lineTo(pointL.x, pointL.y);
  ctx.lineTo(point.x, point.y);
  ctx.lineTo(pointR.x, pointR.y);
  ctx.lineTo(x, y);
  ctx.closePath();
  ctx.fill();

}

function getPageWidth() {
  return (window.innerWidth != null) ? window.innerWidth :
    (document.documentElement && document.documentElement.clientWidth) ?
      document.documentElement.clientWidth : (document.body != null) ?
        document.body.clientWidth : null;
}

function getPageHeight() {
  return (window.innerHeight != null) ? window.innerHeight :
    (document.documentElement && document.documentElement.clientHeight) ?
      document.documentElement.clientHeight : (document.body != null) ?
        document.body.clientHeight : null;
}


