AnimalsGame.SoundEffect = new Class({
	
	extend: {
		HTML5_AUDIO_SUPPORT: document.createElement('audio').canPlayType !== undefined,
		BGSOUND_SUPPORT: document.createElement('bgsound').src !== undefined,
		FLASH_PLAYER_PATH: 'lib/niftyplayer/niftyplayer.swf',
		SOUND_FILES_PATH: 'assets/sounds'
	},
	
	initialize: function(opts) {
		AnimalsGame.SoundEffect.AUDIO_PLATFORM = AnimalsGame.SoundEffect.HTML5_AUDIO_SUPPORT ? 'html5' : AnimalsGame.SoundEffect.BGSOUND_SUPPORT ? 'bgsound' : 'flash';
		
		this.game = opts['game'];
		
		this.playing = false;
		this.container = document.createElement('div');
		$(document.body).append( this.container );
	},
	
	play: function(soundName, times, soundDuration) {
		var this_ = this;
		
		if (!this.game.muted) {
		
			this.container.innerHTML = '';
			this.playing = true;
		
			switch (AnimalsGame.SoundEffect.AUDIO_PLATFORM) {
				case 'html5':
					// check if browser supports the `loop` attribute on HTMLAudioElement (looking at you, Firefox)
					if (document.createElement('audio').loop !== undefined || !isNumber(soundDuration)) {
						this.container.innerHTML =
							'<audio' + (times === 0 ? ' loop' : '') + '>' +
								'<source src="'+ AnimalsGame.SoundEffect.SOUND_FILES_PATH +'/'+ soundName +'.mp3">' +
								'<source src="'+ AnimalsGame.SoundEffect.SOUND_FILES_PATH +'/'+ soundName +'.ogg">' +
							'</audio>';
						this.container.childNodes[0].play();
					} else {
						var generateAudioElement = function() {
							this_.container.innerHTML =
								'<audio>' +
									'<source src="'+ AnimalsGame.SoundEffect.SOUND_FILES_PATH +'/'+ soundName +'.mp3">' +
									'<source src="'+ AnimalsGame.SoundEffect.SOUND_FILES_PATH +'/'+ soundName +'.ogg">' +
								'</audio>';
							this_.container.childNodes[0].play();
						};
						generateAudioElement();
						this.interval = setInterval(generateAudioElement, soundDuration);
					}
				break;
		
				case 'bgsound':
					var bgsound = document.createElement('bgsound');
			
					bgsound.src = AnimalsGame.SoundEffect.SOUND_FILES_PATH +'/'+ soundName +'.mp3';
					if (times === 0 || times === -1) {
						bgsound.loop = -1;
					} else if (times !== undefined) {
						bgsound.loop = times;
					}
					bgsound.autostart = 'autostart';
					$(this.container).append(bgsound);
				break;
		
				default:
					var generateAudioElement = function() {
						this_.container.innerHTML =
							'<object width="0" height="0">' +
								'<param name="movie" value="'+ AnimalsGame.SoundEffect.FLASH_PLAYER_PATH +'?file='+ AnimalsGame.SoundEffect.SOUND_FILES_PATH +'/'+ soundName +'.mp3&amp;as=1">' +
								'<embed src="'+ AnimalsGame.SoundEffect.FLASH_PLAYER_PATH +'?file='+ AnimalsGame.SoundEffect.SOUND_FILES_PATH +'/'+ soundName +'.mp3&amp;as=1" width="0" height="0" type="application/x-shockwave-flash" />' +
							'</object>';
					};
					generateAudioElement();
					if (times === 0 && isNumber(soundDuration)) {
						this.interval = setInterval(generateAudioElement, soundDuration);
					}
				break;
			}
		}
	},
	
	stop: function() {
		this.container.innerHTML = '';
		this.playing = false;
		clearInterval(this.interval);
	},
	
	toggle: function(soundName, times, soundDuration) {
		if (this.playing) {
			this.stop();
			return false;
		} else {
			this.play(soundName, times, soundDuration);
			return true;
		}
	}
	
});
