/*
	JJBM,   25/08/2008, JavaScript_include.js

	Width XHTML 1.1 document.body.clientHeight not return height of client area (of the window).
	Change code for run into a square.
*/

var k_ball_large = 50;
var k_ball_spring = -0.8;
var k_sys_friction = 0.999;
var k_sys_time_inc = 0.3;
var k_sys_gravity = -9.8;

var balls = new Array();
var wx, wy, wxtop; // tamaņo ventana y cota x
var sx, sy; // scroll ventana

	function Ball_Move() {

		// simple euler

		this.x += this.vx * k_sys_time_inc;
		this.y += this.vy * k_sys_time_inc;

		this.vx += this.ax * k_sys_time_inc;
		this.vy += this.ay * k_sys_time_inc;

		this.vx *= k_sys_friction;
		this.vy *= k_sys_friction;

		this.ax = 0;
		this.ay = 0;

		if( this.y < k_ball_large ) {
			this.y = k_ball_large
			this.vy *= k_ball_spring
		}
		if( this.x < 0 ) {
			this.x = 0
			this.vx *= k_ball_spring
		}
		if( this.x > wxtop ) {
			this.x = wxtop
			this.vx *= k_ball_spring
		}

		this.ay += k_sys_gravity;

		this.c.style.left = (sx + this.x) + "px";
		this.c.style.top = (sy + wy - this.y) + "px"; // flip Y world axis

	}
	function Ball_OnClick(e) {
		this.c.style.display = 'none';
		balls[ this.idx ] = null;
	}
	function Ball_Img_OnClick(e) {
		balls[ this.idx ].OnClick(e);
	}
function Ball( idx ) {

	this.Move = Ball_Move;
	this.OnClick = Ball_OnClick;

	this.idx = idx;

	// position, velocity and aceleration (the forces are implicit)
	this.x = 0.0;
	this.y = wy;
	this.vx = 0.0;
	this.vy = 0.0;
	this.ax = 60.0 + Math.random() * 100.0;
	this.ay = 0.0;

	// new DIV
	this.c = document.createElement('div');
	// new IMG
	var i = new Image();
	i.src = "data/articulo/pelota-" + ( parseInt( Math.random() * 100 ) % 10 ) + ".png";
	i.idx = this.idx;
	i.onclick = Ball_Img_OnClick;
	// IMG inside DIV
	this.c.appendChild( i );
	this.c.style.position = "absolute";
	this.c.style.left = sx + "px";
	this.c.style.top = sy + "px";
	// DIV inside BODY
	document.body.appendChild( this.c );
}

function GetWindowWidth() {
	return document.getElementById('peloteo').clientWidth;
}
function GetWindowHeight() {
	return document.getElementById('peloteo').clientHeight;
}

function GetOffsetLeft( o ) {
	if( o == undefined || o == null )
		return 0;
	return GetOffsetLeft( o.offsetParent ) + o.offsetLeft;
}

function GetOffsetTop( o ) {
	if( o == undefined || o == null )
		return 0;
	return GetOffsetTop( o.offsetParent ) + o.offsetTop;
}

function BallManager() {
	var i
	for( i = 0; i < balls.length; i++ )
		if( balls[i] != null )
			balls[i].Move();
}

function NewBall() {
	var i;
	// we search empty
	for( i = 0; i < balls.length; i++ )
		if( balls[i] == null )
			break;
	// i-th are free
	balls[i] = new Ball(i);
}

function myOnLoad() {

	wx = GetWindowWidth();
	wy = GetWindowHeight();
	wxtop  = wx - k_ball_large;

	sx = GetOffsetLeft( document.getElementById('peloteo') );
	sy = GetOffsetTop( document.getElementById('peloteo') );

	if( balls.length <= 0 )
		setInterval( "BallManager()", 20 );

	NewBall();

	setInterval( "NewBall()", 3000 );
}

