/**
 * @projectDescription 
 * @author _keisari
 * @version 1.0 
 * @license MIT 
 * @copyright 2007 Dennis Kayser - www.keisari.dk | www.elektronramp.com
 */
 
var canvasX = 0;
var canvasY = 0;
var timer = null;
var G_WIDTH = 34;
var G_HEIGHT = 34;
var vX = 2;
var vY = 4;
var bounce = false;

function fillImageArray() // creates the image array
{
	// create object
    imageObj = new Image();


    // set image list
    images = new Array();
    images[0]="images/Benzo.gif"
    images[1]="images/Blubba.gif"
    images[2]="images/Ghost.gif"


    // start preloading
    for(i=0; i<=3; i++) 
    {
    	imageObj.src=images[i];
    }
}

// create random number and round to Int
function rnd(n) {
	return Math.floor(Math.random()*n);
}

// move guy back and forth along x-axis
function moveGuy() {
  guy._x += vX;
  guy.style.left = guy._x + 'px';
  if ((guy._x > 0 && guy._x + G_WIDTH > canvasX) || (guy._x < 0)) { vX *= -1;}
  if (bounce) {bounceGuy();}

}

// make guy bounce once
function bounceGuy() {
		guy._y += vY;
		guy.style.bottom = guy._y + 'px';
		if (guy._y > 0 && guy._y > 50) {vY *= -1;}
		if (guy._y < 16 && vY < 0) 
		{
			bounce = false;
			vY *= -1;
		}
}

// initiate bounce
function startBounce() {
	bounce = true;
}

// start animation timeline
function startAnimation() {
  if (!timer) timer = setInterval(moveGuy, 20);
}

// init animation code
function init() {
	getWindowCoords();
	fillImageArray();
	guy = document.getElementById("guyContainer");
	guy.style.left = '0px';
	guy.style.bottom = '15px';
	guy._x = 0;
	guy._y = 15;
	guy.src=images[rnd(3)];
	startAnimation();
	document.onmousedown = startBounce;
}

// get size of canvas
getWindowCoords = (navigator.userAgent.toLowerCase().indexOf('opera') > 0 || navigator.appVersion.toLowerCase().indexOf('safari') != -1)?function() {
  canvasX = window.innerWidth;
  canvasY = window.innerHeight;
}:function() {
  canvasX = document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth;
  canvasY = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
}

window.onresize = getWindowCoords;
window.onload = init;