var elephantState = 1;
var elephantRepetitions = 0;

$('#boat').sprite({fps: 2, no_of_frames: 4});
$('#penguins').sprite({fps: 12, no_of_frames: 135});
$('#plane').sprite({fps: 4, no_of_frames: 4});
$('#elephant').sprite({
    fps: 10, 
    no_of_frames: 25, 
    on_last_frame: function(obj) { 

    	if ( elephantState == 2 ) {
		if ( elephantRepetitions > 5 ) { 
			obj.spState(1); 
			elephantState = 1; 
			elephantRepetitions = 0
			for (n=1;n<4;n++) { document.getElementById( 'screech' + n ).src = document.getElementById( 'screech' + n ).src };
			if (playing) { document.getElementById('screech' + ( Math.floor( Math.random() * 3 ) + 1 ) ).play() };
		}
		else { elephantRepetitions++ }
	}
	else {obj.spState(2); elephantState = 2 };

    }
});

var plane = document.getElementById('plane');
plane.style.left = window.innerWidth + 100 + 'px';

gameLoop = function() {
	if (animating) {
		plane.style.left = ( plane.offsetLeft - 1 ) + 'px';
		if ( plane.offsetLeft == window.innerWidth - 200 ) { if (playing) { document.getElementById('planeSound').play() }};
		if ( plane.offsetLeft + plane.offsetWidth < -1000 ) { 
			plane.style.left = ( window.innerWidth + 1000 ) + 'px';
			document.getElementById('planeSound').pause();
			document.getElementById('planeSound').src = document.getElementById('planeSound').src }}};

setInterval(gameLoop, 30);
