// (C) Jan 2004 Yuri Yakimenko, Princeton NJ
//	e-mail: xpong@xpong04.com

window.onresize= setSizes;
var YOFFSET=30;

var xpos1_max;
var xpos2_max;

var ypos1_max;
var ypos2_max;
var xpos1=0;
var ypos1=100;
var speed1 = Math.floor(Math.random()*10)+2;
var yspeed1=0;

var objBall1, objBall2;

var xpos2=0;
var ypos2=70;

var speed2 = speed1 + 2; //Math.floor(Math.random()*10)+2;
var yspeed2=0;
var xdiff, ydiff;
var started = 0;
var last_dist=1000, dist=1000;

var last_ypos1,   last_xpos1,  last_ypos2,  last_xpos2 ;

var winW = 815, winH = 460;

function BallsCollapse ()
{
   vx1 = (xpos1_max - last_xpos1);
   vx2 = (xpos2_max - last_xpos2);
   vy1 = (ypos1_max - last_ypos1);
   vy2 = (ypos2_max - last_ypos2);

   v_mut = (vx1 - vx2) * (vx1 - vx2) + (vy1 - vy2) * (vy1 - vy2);
   if (v_mut == 0)
	return 0;

   k = ((last_xpos2 - last_xpos1) * (vx1 - vx2) + (last_ypos2 - last_ypos1) * (vy1 - vy2)) / v_mut;

   if (k > 0 && k < 1)
	{
	x1 = last_xpos1 + k * vx1;
        x2 = last_xpos2 + k * vx2;
        y1 = last_ypos1 + k * vy1;
        y2 = last_ypos2 + k * vy2;
	if ((x1 - x2) *(x1 - x2) + (y1 - y2)*(y1-y2) < 169)
		{
//		window.alert ("collapse");
		return 1;
		}
	else
		return 0;
	}
   else 
	return 0;

}

function GetKoef ()
{
   vx1 = (xpos1_max - last_xpos1);
   vx2 = (xpos2_max - last_xpos2);
   vy1 = (ypos1_max - last_ypos1);
   vy2 = (ypos2_max - last_ypos2);

   a = (vx1 - vx2) * (vx1 - vx2) + (vy1 - vy2)*(vy1 - vy2) ;
   b = 2 * (vx1 - vx2) * (last_xpos1 - last_xpos2) + 2 * (vy1 - vy2) *(last_ypos1 - last_ypos2);
   c = (last_xpos1 - last_xpos2) * (last_xpos1 - last_xpos2) + (last_ypos1 - last_ypos2) * (last_ypos1 - last_ypos2) - 169;

   rt = Math.sqrt (b * b - 4 * a * c);

   k1 = (-b + rt) / ( 2 * a);
   k2 = (-b - rt) / ( 2 * a);

   if (k2 > 0 && k2 < 1)
	{
        return k2;
	}

   if (k1 > 0 && k1 < 1)
	{
        return k1; 
	}
//  window.alert ("bad k");
  
   return 1;
}


function setSizes() 
{
  winW = 240;

  if (1) //parseInt(navigator.appVersion) > 3) 
	{
	if (navigator.appName=="Netscape") 
		{
		winW = window.innerWidth-16;
		winH = window.innerHeight-16;
		}
	else if (navigator.appName.indexOf("Microsoft")!=-1) 
		{
		if (document.body)
			{
			winW = document.body.offsetWidth-20;
			winH = document.body.offsetHeight-20;
			}
		}
	}

   winW -= 15; // WIDTH OF THE BALL

   if (started == 0)
	{
        window.setTimeout("ShowBalls();",200);
	}

   started = 1;
}

function ShowBalls ()
{
   if (document.all)
	{
	objBall1 = document.all.ball1.style;
	objBall2 = document.all.ball2.style;
	if (objBall1)
		objBall1.visibility = 'visible';
	if (objBall2)
		objBall2.visibility = 'visible';
	}

   else if (document.layers)
	{
	objBall1 = document.layers.ball1;
	objBall2 = document.layers.ball2;

	if (objBall1)
		objBall1.visibility = 'visible';
	if (objBall2)
		objBall2.visibility = 'visible';
	}
   else
	{
	if (document.getElementById("ball1"))
		{
		objBall1 = document.getElementById("ball1").style;
		objBall2 = document.getElementById("ball2").style;

		if (objBall1)
			objBall1.visibility = 'visible';
		if (objBall2)
			objBall2.visibility = 'visible';
		}
	}
     next();
}

function next() {

   xpos1 += speed1;

   ypos1 -= yspeed1;

   if (xpos1 > winW && speed1 > 0) speed1 = -speed1;
   if (xpos1 < 0 && speed1 < 0) speed1 = -speed1;

   yspeed1-=1;

   if (ypos1 > 400 + YOFFSET && yspeed1 < 0)
	{
	yspeed1+=1;
	yspeed1 = -0.9 * yspeed1;
        speed1 = speed1 * 0.99;

	   if ((yspeed1 < 0.01) && (yspeed1 > -0.01) && (speed1 < 0.2) && (speed1 >-0.2))
		{
		if (speed1 < 0)
			{
			xpos1 = 0;
			}
		else
			{
			xpos1 = winW;
			}
		ypos1 = 100;

		last_xpos1 = xpos1;
		last_ypos1 = ypos1;

		yspeed1 =0;
	 	speed1 = Math.floor(Math.random()*10)+2;
		if (xpos1 != 0)
			speed1 = -speed1;
		}
	}


   xpos2 += speed2;

   ypos2 -= yspeed2;

   if (xpos2 > winW && speed2 > 0) speed2 = -speed2;
   if (xpos2 < 0 && speed2 < 0) speed2 = -speed2;

   yspeed2-=1;

   if (ypos2 > 400 + YOFFSET && yspeed2 < 0)
	{
	yspeed2+=1;
	yspeed2 = -0.8 * yspeed2;
        speed2 = speed2 * 0.99;

	   if ((yspeed2 < 0.001) && (yspeed2 > -0.001) && (speed2 < 0.2) && (speed2 > -0.2))
		{
		if (speed2 < 0)
			{
			xpos2 = 0;
			}
		else
			{
			xpos2 = winW;
			}
		ypos2 = 100;

		last_xpos2 = xpos2;
		last_ypos2 = ypos2;

		yspeed2 = 0;
	 	speed2 = Math.floor(Math.random()*10)+2;
		if (xpos2 != 0)
			speed2 = -speed2;
		}
	}

   ypos1_max = ypos1;
   if (ypos1_max > 400 + YOFFSET) ypos1_max = 400 + YOFFSET;

   ypos2_max = ypos2;
   if (ypos2_max > 400 + YOFFSET) ypos2_max = 400 + YOFFSET;

   xpos1_max = xpos1;
   if (xpos1_max > winW)
      xpos1_max = winW;

   xpos2_max = xpos2;

   if (xpos2_max > winW)
      xpos2_max = winW;  
	

   dist = (xpos1_max - xpos2_max) * (xpos1_max - xpos2_max) + (ypos1_max - ypos2_max) * (ypos1_max - ypos2_max) ;

   if ((dist < last_dist && dist < 225)) // || (last_dist > 170 && dist > 170 && BallsCollapse()==1))
	{
	if (dist < 169) // && last_dist > 169)
		{
		// this is linear approximation of a quadratic function
		// call to save some CPU time

		// var last_dist2 = Math.sqrt(last_dist);
		// var dist2 = Math.sqrt(dist);
		// k = (last_dist2 - 13)/(last_dist2 - dist2);

		// otherwise, get more accurate k
		k = GetKoef ();	

		ypos1_max = last_ypos1 + k * (ypos1_max - last_ypos1);
		xpos1_max = last_xpos1 + k * (xpos1_max - last_xpos1);
		
		ypos2_max = last_ypos2 + k * (ypos2_max - last_ypos2);
		xpos2_max = last_xpos2 + k * (xpos2_max - last_xpos2);
		
		}

        xdiff = speed1 - speed2;
	speed1 -= xdiff;
	speed2 += xdiff;

	ydiff = yspeed1 - yspeed2;
	yspeed1 -= ydiff;
	yspeed2 += ydiff;
	dist = 0;
	}
   last_dist = dist;

   last_xpos1 = xpos1_max;
   last_ypos1 = ypos1_max;

   last_xpos2 = xpos2_max;
   last_ypos2 = ypos2_max;

   if (document.layers)
	{
	objBall1.left = xpos1_max;
	objBall1.top = ypos1_max;

	objBall2.left = xpos2_max;
	objBall2.top = ypos2_max;
	}
   else
	{
	if (document.getElementById("ball1"))
		{
		objBall1.left = xpos1_max;
		objBall1.top = ypos1_max;

		objBall2.left = xpos2_max;
		objBall2.top = ypos2_max;
		}
	else
		{
		objBall1.posLeft = xpos1_max;
		objBall1.posTop = ypos1_max;

		objBall1.posLeft = xpos2_max;
		objBall1.posTop = ypos2_max;
		}
	}

   window.setTimeout("next();",15);
}

