 var w = 100;
 var h = w * 2;
 var buffer = h / 4;
 var acceleration = .5;
 var decelerationFactor = .9;
 var paddleMax = 4;
 var speedFactor = 2;

 function Paddle (px,py,name,ball,manual)
 {
  if (manual)
  {
   this.move=getPosition;
  } else {
   this.move=move;
  }
  function move()
  {
   if (this.ball.dx > 0 && this.left + w < this.ball.left || this.ball.dx < 0 && this.left > this.ball.left)
    return;
   if (this.ball.top + w < this.top + buffer )
    this.dy = Math.max(this.dy - acceleration * speedFactor,-paddleMax * speedFactor);
   else if (this.ball.top > this.top + h - buffer)
    this.dy = Math.min(this.dy + acceleration * speedFactor,paddleMax * speedFactor);
   else
    this.dy = this.dy * decelerationFactor;
   var x = this.left;
   var y = (this.top += this.dy);
   this.oMe.resizeTo(w,h);
   this.oMe.moveTo(x,y);
  }
  function getPosition()
  {
   this.oMe.resizeTo(w,h);
   this.oMe.moveTo(this.left,this.top);
  }
  this.score = score;
  function score()
  {
   this.points++;
   this.oMe.document.body.innerHTML = name + ": " + this.points;
   speedFactor += (.5)
  }
  this.ball = ball;
  ball.addPaddle(this);
  this.left = px;
  this.top = py;
  this.name = name;
  this.points = 0;
  this.dy = 0;
  this.oMe = window.open("",name,"resizable=no,scrollbars=no,status=no,toolbar=no,directories=no,menubar=no");
  this.oMe.document.body.innerHTML = name + ": " + this.points;
  this.oMe.resizeTo(w,h);
  this.oMe.moveTo(this.left,this.top);
  if (manual)
   {
    this.moving=false;
    this.oMe.document.body.style.width= w +"px";
    this.oMe.document.body.style.height= h +"px";
    this.oMe.document.body.p = this;
    this.oMe.document.body.onmousedown = function onmousedown(e)
    {
     this.p.mouseY = e.clientY;
     this.p.moving = true;
    };
    this.oMe.document.body.onmousemove = function onmousemove(e)
    {
     if (!this.p.moving)
      return;
     this.p.top = e.screenY - this.p.mouseY;
    };
   }
   this.oMe.document.body.onmouseup = 
   this.oMe.document.body.onmouseleave = 
   function ()
   {
    this.p.moving = false;
   }
 }
 function Ball (px,py,maxdx,maxdy)
 {
  this.move=move;
  function move()
  {
   var x = this.left;
   var y = this.top;
   if (x + w >= screen.availWidth)
   {
    this.paddles[0].score();
    this.reset();
    return;
   }
   if (x <= 4)
   {
    this.paddles[1].score();
    this.reset();
    return;
   }
   if (y + w >= screen.availHeight)
   {
    this.dy = -this.dy
   }
   if (y <= 20)
   {
    this.dy = -this.dy;
   }
   for (i = 0; i < this.paddles.length; i++)
   {
    p = this.paddles[i];
    if (this.top < p.top + h && this.top + w > p.top)
     if ((this.dx > 0 && this.left + w >= p.left && this.left < p.left) || (this.dx < 0 && this.left <= p.left + w && this.left > p.left))
      this.dx = -this.dx;
   }
   this.dx += (this.dx < 0 ? -.05 : .05);
   this.dy += (this.dy < 0 ? -.05 : .05)
   this.oMe.document.body.innerHTML = this.dx + ", " + this.dy;
   x += this.dx;
   y += this.dy;
   this.oMe.moveTo(x,y);
   this.left = x;
   this.top = y;
  }
  this.addPaddle=addPaddle;
  function addPaddle(oPaddle)
  {
   this.paddles.push(oPaddle);
  }
  this.reset=reset;
  function reset()
  {
   this.left = this.defaultLeft;
   this.top = this.defaultTop;
   this.oMe.resizeTo(w,w);
   this.oMe.moveTo(this.left,this.top);
	this.dx = this.maxdx * (Math.random() > .55 ? 1 : -1);
	this.dy = this.maxdy * (Math.random() > .55 ? 1 : -1);
  }
  this.paddles = new Array();
  this.left = px;
  this.top = py;
  this.defaultLeft = this.left;
  this.defaultTop = this.top;
  this.oMe = window.open("","ball","resizable=no,scrollbars=no,status=no,toolbar=no,directories=no,menubar=no");
  this.oMe.document.body.style.backgroundColor = "#000000";
  this.oMe.document.body.style.color = "#fffff0"
  this.oMe.resizeTo(w,w);
  this.oMe.moveTo(this.left,this.top);
  this.maxdx = maxdx;
  this.maxdy = maxdy;
  this.dx = this.maxdx * (Math.random() > .55 ? 1 : -1);
  this.dy = this.maxdy * (Math.random() > .55 ? 1 : -1);
 }
 function Game()
 {
  this.ball = new Ball(screen.width/2,screen.height/2,5 * speedFactor,5 * speedFactor);
  this.leftPaddle = new Paddle(0,0,"left",this.ball,true);
  this.rightPaddle = new Paddle(screen.width-w,screen.height-h,"right",this.ball,false);
  this.step=step;
  function step()
  {
   this.ball.move();
   this.leftPaddle.move();
   this.rightPaddle.move();
  }
 }
 var oGame;
 function playGame()
 {
  oGame.step();
  timeout = setTimeout(playGame,10);
 }
 function togglePlaying()
 {
	 playing = !playing;
	 if (playing)
	 {
     oGame = new Game();
	  playGame();
	 }
	 else
	 {
     clearTimeout(timeout);
     oGame.leftPaddle.oMe.close();
	  oGame.rightPaddle.oMe.close();
	  oGame.ball.oMe.close();
	 }
 }
 var playing = false;
 var timeout;
