var animation_images = ["car.png", "plane.png", "boat.png", "helicopter.png"];
var animation_x_vector = [-4, -8, 6, 0]
var animation_y_vector = [2, 4, 1, -2]

var animation_number = 3;

var animation_objects = new Array(animation_number);

function animation_move() {
	for ( var i = 0 ; i < animation_number ; i++ ) {
		var new_x = parseInt(animation_objects[i].style.left.replace(/px/g,''),10) + parseInt(animation_objects[i].getAttribute("x_vector"),10);
		var new_y = parseInt(animation_objects[i].style.top.replace(/px/g,''),10) + parseInt(animation_objects[i].getAttribute("y_vector"),10);

		var opacity = 100;

		if ( new_x < 100 ) {
			opacity = new_x;
		} else if ( new_x > document.body.clientWidth - 250 ) {
			opacity = document.body.clientWidth - 150 - new_x;
		}

		animation_objects[i].style.opacity = opacity/100;
		animation_objects[i].style.filter = 'alpha(opacity=' + opacity + ')';


		
		if ( new_x < -150 ) {
			new_x = document.body.clientWidth - 150;
		} else if ( new_x > document.body.clientWidth - 150 ) {
			new_x = -150;
		}

		if ( new_y < -100 ) {
			new_y = document.body.clientHeight;
		} else if ( new_y > document.body.clientHeight ) {
			new_y = -100;
		}
		
		animation_objects[i].style.top =  new_y + "px";
		animation_objects[i].style.left = new_x + "px";

	}
	setTimeout("animation_move()", 50);
}

function setup_animation() {

	for ( var i = 0 ; i < animation_number ; i++ ) {
	
		var rand = Math.floor( Math.random()* animation_images.length );
	
		var div = document.createElement("div");
		div.id = "animation_" + i;
	//	div.style.zIndex = -100;
		div.style.position = "absolute";
		div.style.top = ( -100 - Math.floor( Math.random( )* 100 ) ) + "px"
		div.style.left =  Math.floor( Math.random( )* document.body.clientWidth ) + "px";
		div.setAttribute("x_vector", animation_x_vector[rand] * ( ( i / 3 ) + 1 ) );
		div.setAttribute("y_vector", animation_y_vector[rand] * ( ( i / 3 ) + 1 ) );
		var img = document.createElement("img");
		img.src = "/animation/" + animation_images[rand];
		div.appendChild(img);
		document.body.appendChild(div);
		animation_objects[i] = div;
	}
	setTimeout("animation_move()", 10000);

}

window.onload = setup_animation;