var soundManager;
$(document).ready(function(){
	
	spawn_bat();
	
});


function spawn_bat() {

	soundManager.onload = function() {

	
		for (i = 0; i < 10; i++) {
			left_to_right(random());
			right_to_left(random());
		}
	}

	
}

function left_to_right(bottom) {
	/* move bat from left to right */
	
//	soundManager.play('fart1','/files/fart1.mp3');
	$("#bat2").animate({ 
		left: '140%',
		bottom: bottom + '%',
	 }, 1100);
		$('#bat2').animate({ display: 'inline' }, 1100, false, function() { switch_bat(1); soundManager.play('fart1','/files/fart1.mp3') })
}

function right_to_left(bottom) {
	/* move bat from right to left */
	 
	$("#bat2").animate({ 
		left: "-400px",
		bottom: bottom + '%'
	 }, 1100);
	$('#bat2').animate({ display: 'inline' }, 1100, false, function() { switch_bat(2); soundManager.play('fart0','/files/fart0.mp3'); })
}

function switch_bat(batNumber) {
	$('#bat2').attr('src','/img/giantbatfarts/bat' + batNumber + '.jpg');
}

function sleep(ms) {
	var dt = new Date();
	dt.setTime(dt.getTime() + ms);
	while (new Date().getTime() < dt.getTime());
}

function random() {
	return Math.floor(Math.random()*91)
}



