var OP_TIME = 20;
var JUMP_TIME = 70;

function IMObject( id, imgUrl, width, height, zidx )
{
	var imObj = null;
	var imObji = null;
	var imSrc = imgUrl;
	var imW = width;
	var imH = height;
	var imId = id;
	var imTop = 0;
	var imLeft = 0;
	var imVis = 'hidden';
	var imZindex = zidx;

	this.imH = height;
	this.imW = width;
	this.imId = id;
	this.imTop = 0;
	this.imLeft = 0;

	this.html = function()
	{
		return '<img class="pngva inv" id='+imId+' src='+imSrc+' width='+imW+' height='+imH+
			' border=0 style="position: absolute; top: '+imTop+'px; left: '+
			imLeft+'px; visibility: '+imVis+'; z-index: ' + imZindex +'">';
	}

	this.updateObj = function()
	{
		imObj = findObject( imId );
//		imObji = findObject( imId+'i' );
	}

	this.updatePos = function( newX, newY )
	{
		imTop = newY;
		imLeft = newX;
		imObj.style.left = imLeft+'px';
		imObj.style.top = imTop+'px';
	}

	this.updateSize = function( newW, newH )
	{
		imW = newW;
		imH = newH;
		this.imW = newW;
		this.imH = newH;
		imObj.style.width = imW+'px';
		imObj.style.height = imH+'px';
//		imObji.style.width = imW+'px';
//		imObji.style.height = imH+'px';
	}

	return this;
}

function moveData( srcY, tgtY )
{
	this.sY = srcY;
	this.cY = srcY;
	this.dY = tgtY;

	this.down = function()
	{
		cY -= 10;
		if( cY <= sY )
			cY = sY;
		return cY;
	}

	this.up = function()
	{
		step = 5;
		if( dY - cY < 25 )
			step = 3;
		if( dY - cY < 10 )
			step = 1;
		cY += step;
		if( cY >= dY )
			cY = dY;
		return cY;
	}

	this.doneMoveUp = function()
	{
		return (cY == dY);
	}

	this.doneMoveDown = function()
	{
		return (cY == sY);
	}

	return this;
}

function flatData( srcW, tgtW, srcH, tgtH )
{
	this.sW = srcW;
	this.cW = srcW;
	this.dW = tgtW;
	this.sH = srcH;
	this.cH = srcH;
	this.dH = tgtH;

	this.flatOff = function()
	{
		cW -= 2;
		if( cW <= sW )
			cW = sW;
		cH++;
		if( cH >= sH )
			cH = sH;
	}

	this.flat = function()
	{
		cW += 2;
		if( cW >= dW )
			cW = dW;
		cH--;
		if( cH <= dH )
			cH = dH;
	}

	this.doneFlat = function()
	{
		return (cW == dW) && (cH == dH);
	}

	this.doneFlatOff = function()
	{
		return (cW == sW) && (cH == sH);
	}

	return this;
}

function Ball( src, num, pobj, ico )
{
	var bC = findObject( src );
	var bNum = num;
	var bSrc = src;
	var bSrcIdx = src.substr(2);
	var bP = pobj;
	var bPObj = findObject( pobj );
	var bW, bH;
	var state = 0;

	var BObj = null;
	var BSObj = null;
	var NObj = null;

	var IcoObj = null;
	var IcoSh = null;

	var opTimer = null;
	var callBack = ''; var currFunc = '';

	var fd = null; var md = null; 
	var jd = 0; var jdsteps = 0;
	var currY = 10; var sl = false; var delay = OP_TIME;
	var hover = false; var icovis = false;

	this.updateParentPos = function()
	{
		bC.style.top = (bPObj.offsetTop-100)+'px';
		bC.style.left = (bPObj.offsetLeft-20)+'px';		
	}

	this.init = function( src_obj, ico )
	{
		state = !((bC == null) || (bPObj == null));
		if( !state )
			return;

		this.updateParentPos();
		bC.style.top = (bPObj.offsetTop-100)+'px';
		bC.style.left = (bPObj.offsetLeft-20)+'px';		

		bW = bC.offsetWidth;
		bH = bC.offsetHeight;

		BSObj = new IMObject( src_obj+'_bs', 'img/balls.png', 58, 15, 5 );
		BObj = new IMObject( src_obj+'_ball', 'img/ball.png', 61, 61, 6 );
		NObj = new IMObject( src_obj+'_num', 'img/'+bNum+'.png', 61, 61, 7 );

		IcoObj = new IMObject( src_obj+'_ico', 'img/'+ico+'.png', 61, 61, 6 );
		IcoSh = new IMObject( src_obj+'_icos', 'img/'+ico+'_sh.png', 61, 61, 5 );

		bC.innerHTML = BSObj.html()+BObj.html()+NObj.html()+IcoObj.html()+IcoSh.html();
		opTimer = setTimeout( 'runBall("'+bSrc+'","setupInnerObjects")', delay );
	}

	function stopTimer()
	{
		if( opTimer )
			{
			clearTimeout( opTimer );
			opTimer = null;
			currFunc = '';
			}
		if( callBack )
			{
			currFunc = callBack;
			callBack = '';
			opTimer = setTimeout( 'runBall("'+bSrc+'")', delay );
			}
	}

	function updateTimer()
	{
		if( opTimer )
			{
			clearTimeout( opTimer );
			opTimer = null;
			}
		opTimer = setTimeout( 'runBall("'+bSrc+'")', delay );
	}

	function centerX( O )
	{
		return Math.floor( bW/2-O.imW/2 );
	}
	
	function setY( O, y )
	{
//	    if( oy[bSrcIdx] && oy[bSrcIdx]!=220 )
//	    alert( bH-O.imH-y, ' ', oy[bSrcIdx] );
		return bH-O.imH-y;
	}

	this.setupInnerObjects = function()
	{
		stopTimer();

		BSObj.updateObj();
		BSObj.updatePos( centerX( BSObj )+3, setY( BSObj, currY-5 ) );
		BObj.updateObj();
		BObj.updatePos( centerX( BObj ), setY( BObj, currY ) );
		NObj.updateObj();
		NObj.updatePos( centerX( BObj ), setY( BObj, currY ) );

		IcoObj.updateObj();
		IcoObj.updatePos( centerX( BObj ), setY( BObj, currY ) );
		IcoSh.updateObj();
		IcoSh.updatePos( centerX( BObj ), setY( BObj, currY ) );

		fd = flatData( BObj.imW, BObj.imW+6, BObj.imH, BObj.imH-6 );
		md = moveData( currY, 70 );
//		jd = jumpData( currY );
		
		showElem( BSObj.imId );
		showElem( BObj.imId );
		showElem( NObj.imId );

		fadeOpacity( NObj.imId, '1' );
		fadeOpacity( BObj.imId, '1' );
		fadeOpacity( BSObj.imId, '3' );
	}

	function updateBall()
	{
	    var y = setY( BObj, currY );
		BObj.updateSize( fd.cW, fd.cH );
		BObj.updatePos( centerX( BObj ), y );
		NObj.updateSize( fd.cW, fd.cH );
		NObj.updatePos( centerX( BObj ), setY( BObj, currY ) );
		IcoObj.updatePos( centerX( IcoObj ), setY( IcoObj, currY ) );
//		IcoSh.updatePos( centerX( BObj ), setY( IcoSh, currY ) );
	    dy[bSrcIdx] = oy[bSrcIdx]-currY+10;
	    setCoords( true );
	}

	this.flatOff = function()
	{
		fd.flatOff();
		updateBall();

		if( !fd.doneFlatOff() )
			updateTimer();
		else
			stopTimer();
	}

	this.flatOn = function()
	{
		delay = OP_TIME;
		fd.flat();
		updateBall();

		if( !fd.doneFlat() )
			updateTimer();
		else
			stopTimer();
	}

	this.moveUp = function()
	{
		if( !sl )
			{
			fadeOpacity( BSObj.imId, '2' );
			sl = true;
			}
		if( !fd.doneFlatOff() )
			fd.flatOff();
		currY = md.up();
		updateBall();
		if( !md.doneMoveUp() )
			updateTimer();
		else
		    {
//		    dy[bSrcIdx] += 50;
//		    setCoords( true );
		    opTimer = setTimeout( 'runBall("'+bSrc+'","moveDown")', OP_TIME );
		    }
	}

	this.hideBall = function()
	{
		stopTimer();
		currFunc = 'hideBall';
		callBack = '';
		hover = true;
		if( !sl )
			{
			fadeOpacity( BSObj.imId, 'h' );
			fadeOpacity( BObj.imId, 'h' );
			fadeOpacity( NObj.imId, 'h' );
			sl = true;
			}
		if( !fd.doneFlatOff() )
			fd.flatOff();
		currY = md.up();
		updateBall();
		if( !md.doneMoveUp() )
			updateTimer();
		else
			{
			opTimer = setTimeout( 'runBall("'+bSrc+'","showIcon")', OP_TIME );
			}
	}

	this.showBall = function()
	{
		stopTimer();
		currFunc = 'showBall';
		callBack = '';
		hover = true;
		if( icovis )
			{
			fadeOpacity( IcoObj.imId, 'h' );
			fadeOpacity( IcoSh.imId, 'h' );
			icovis = false;
			}
		currY = md.up();
		updateBall();
		if( !md.doneMoveUp() )
			updateTimer();
		else
			{
			opTimer = setTimeout( 'runBall("'+bSrc+'","hideIcon")', OP_TIME );
			}
	}

	this.showIcon = function()
	{
		showElem( IcoObj.imId );
		showElem( IcoSh.imId );
		fadeOpacity( IcoObj.imId, '1' );
		fadeOpacity( IcoSh.imId, '3' );
		icovis = true;
		opTimer = setTimeout( 'runBall("'+bSrc+'","moveIconDown")', OP_TIME );
	}

	this.hideIcon = function()
	{
		fadeOpacity( BObj.imId, '1' );
		fadeOpacity( NObj.imId, '1' );
		fadeOpacity( BSObj.imId, '3' );
		hover = false;
		opTimer = setTimeout( 'runBall("'+bSrc+'","moveDown")', OP_TIME );
	}


	this.moveDown = function()
	{
		if( sl )
			{
			fadeOpacity( BSObj.imId, '3' );
			sl = false;
			}
		if( !fd.doneFlatOff() )
			fd.flatOff();
		currY = md.down();
		updateBall();
		if( !md.doneMoveDown() )
			updateTimer();
		else
			{
			jd = 10; jdsteps = 7;
			opTimer = setTimeout( 'runBall("'+bSrc+'","jumps")', OP_TIME );
			}
	}

	this.moveIconDown = function()
	{
		currY = md.down();
		updateBall();
		if( !md.doneMoveDown() )
			updateTimer();
		else
			stopTimer();
	}

	this.jumps = function()
	{
		delay = JUMP_TIME;
		if( jd > 0 )
			currY = Math.round( currY+jd );
		else
			currY = 10;
		updateBall();
		if( jd > 0 )
			jd = -jd;
		else
			{
			jd = -jd/2;
			jdsteps--;
			}
		if( jdsteps > 0 )
			updateTimer();
		else
			stopTimer();
	}

	this.setupFuncs = function( func, cb )
	{
		if( cb )
			callBack = cb;
		if( func )
			currFunc = func;
	}

	this.process = function()
	{
		if( currFunc )
			eval( 'this.'+currFunc+'()' );
	}
	
	this.hover = function()
	{
		return hover;
	}

	this.init( src, ico );
}

fadeOpacity.addRule('1',0,1,15);
fadeOpacity.addRule('2',0.8,0.1,10);
fadeOpacity.addRule('3',0,0.8,15);

fadeOpacity.addRule('h',1,0,15 );
fadeOpacity.addRule('h2',1,0,15);

fadeOpacity.addRule('s',0,1,15);
fadeOpacity.addRule('hlf',0.5,1,30);

ballsObjects = {};

function addBall( src, num, pobj, ico )
{
	var newBall = new Ball( src, num, pobj, ico );
	ballsObjects[src] = newBall;
}

function runBall( src, func, cb )
{
	ballsObjects[src].setupFuncs( func, cb );
	ballsObjects[src].process();	
}

function show()
{
	if( hoveIt )
		return;
	bid = Math.round(Math.random()*4+1);
	bc = 'bc'+bid;
	if( !ballsObjects[bc].hover() )
		runBall( bc, 'flatOn', 'moveUp' );
//	dy[bid] -= 50;
//	setCoords();
}
var hoveIt = false;

function ballHover( id )
{
	hoveIt = true;
//	if( !ballsObjects['bc'+id].hover() )
//		runBall( 'bc'+id, 'hideBall' );
}

function ballOut( id )
{
	hoveIt = false;
//	if( ballsObjects['bc'+id].hover() )
//		runBall( 'bc'+id, 'showBall' );
}