var SoundObj = Class.create({
	initialize: function( p_url, p_volume){
		this.url = p_url;
		this.audio = false;
		this.volume = p_volume || 1.0;
		this.looped = false;
		this.playing = false;
		this.time = 0;
		this.Create();
		g_sound.sounds[this.url] = this;
	},
	Create: function(){
		this.audio = new Audio();
		this.audio.src = '/s/' + this.url + (g_sound.support_mp3?'.mp3':'.ogg');
		if (!!this.audio.autobuffer)
		{
			this.audio.autobuffer = true;
		}
		if (!!this.audio.preload)
		{
			this.audio.preload = true;
		}
	},
	Stop: function( p_noActEnd){
		this.playing = false;
		if (!this.audio.ended)
		{
			this.audio.pause();
			if (this.audio.readyState > 0)
			{
				this.audio.currentTime = 0;
			}
		}
	},
	Play: function( p_func){
		if (this.audio.networkState > 0)
		{
			this.func = p_func;
			this.audio.play();
			this.audio.volume = (this.looped?g_sound.musicVol:g_sound.voiceVol) * this.volume;
			this.playing = true;
			this.audio.stopObserving('ended');
			this.audio.observe( 'ended',this.Finish.bind(this));
		}
		else if(p_func)
		{
			p_func();
		}
	},
	Finish: function(){
		this.playing = false;
		if (this.func){this.func();this.func = false;}
		this.audio.stopObserving('ended');
		this.Stop();
	},
	Update: function(){
		++ this.time;
		if (this.playing && ! this.looped && this.time > 1 && this.audio.networkState == 3){
			this.Finish();
			return true;
		}
		if (this.playing && ! this.looped && this.audio.currentTime == 0 && this.time > 5){
			this.Finish();
			return true;
		}
		if ( ! this.playing && this.time > 10){return true;}
		return false;
	},
	Loop: function(){
		if (this.audio.readyState > 0)
		{
			this.audio.currentTime = 0;
		}
		this.looped = true;
		this.Play(this.Loop.bind(this));
	}
});

var g_sound = {
	support_mp3: false,
	support_ogg: false,
	supported: false,
	sounds: {},
	currentMusic: false,
	mutedMusic: false,
	mutedVoice: false,
	voiceVol: 1.0,
	musicVol: 0.1,
	checking: false,
	MuteMusic: function( p_mute){
		if (this.mutedMusic == p_mute){return;}
		this.mutedMusic = p_mute;
		if (!this.supported || ! this.currentMusic){return;}
		
		if (this.mutedMusic)
		{
			this.sounds[this.currentMusic].Stop();
		}
		else
		{
			this.sounds[this.currentMusic].Loop();
		}
	},
	MuteVoice: function( p_mute){
		if (this.mutedVoice == p_mute){return;}
		this.mutedVoice = p_mute;
		if (!this.supported){return;}
		if (this.mutedVoice)
		{
			for (var i in this.currentPlays)
			{
				fireEvent(this.sounds[i],'ended');
			}
		}
	},
	Test: function(){
		try{
			var l_tau = document.createElement('audio'); 
			if (l_tau.canPlayType){
			   this.support_mp3 = !!l_tau.canPlayType && "" != l_tau.canPlayType('audio/mpeg');
			   this.support_ogg = !!l_tau.canPlayType && "" != l_tau.canPlayType('audio/ogg; codecs="vorbis"');
			}
		}
		catch(e)
		{
			this.supported = false;
		}
		this.supported = this.support_mp3 || this.support_ogg;
		if ( ! this.supported)
		{
			this.muted = true;
		}
	},
	SetBackground: function( p_music){
		if (this.currentMusic == p_music){return;}
		if (this.currentMusic && this.sounds[this.currentMusic]){this.sounds[this.currentMusic].Stop();}
		this.currentMusic = p_music;
		if (this.mutedMusic){return;}
		this.Load( p_music);
		this.sounds[p_music].Loop();
	},
	Check: function(){
		var num = 0;
		for (var i in this.sounds)
		{
			if (this.sounds[i].Update())
			{
				delete this.sounds[i];
			}
			else
			{
				++num;
			}
		}
		if (num == 0)
		{
			clearInterval(this.checking);
			this.checking = false;
		}
	},
	Load: function( p_src){
		if (this.sounds[p_src]){return;}
		if (!this.checking)
		{
			var num = 0;
			for( var i in this.sounds){
				++ num;
			}
			if (num == 0)
			{
				this.checking = setInterval(this.Check.bind(this),1000);
			}
		}
		var o = new SoundObj(p_src);
		return o;
	},
	Pause: function( p_src, p_noActEnd){
		if (!this.sounds[p_src]){return;}
		this.sounds[p_src].Stop( p_noActEnd);
	},
	Play: function( p_src, p_ret, p_volume){
		if (!this.supported || this.mutedVoice){if(p_ret){p_ret();}return};
		this.Load(p_src);
		this.sounds[p_src].Play( p_ret);
	},
	ChangeVoiceVol: function( p_val){
		this.voiceVol = p_val;
		if($('VolVoiceIcon')){$('VolVoiceIcon').src=(p_val == 0?'/i/iconesanstexte/voix_off.png':'/i/iconesanstexte/voix_on.png');}
		if($('VolVoiceTxt')){$('VolVoiceTxt').update(Math.floor(100*p_val));}
		this.MuteVoice(p_val == 0);
	},
	ChangeMusicVol: function( p_val){
		this.musicVol = 0.2 * p_val;
		if($('VolMusicTxt')){$('VolMusicTxt').update(Math.floor(100*p_val));}
		if($('VolMusicIcon')){$('VolMusicIcon').src=(p_val == 0?'/i/iconesanstexte/music_off.png':'/i/iconesanstexte/music_on.png');}
		if (this.background)
		{
			try{
				this.sounds[this.background].volume = this.musicVol;
			}catch (e){}
		}
		this.MuteMusic(p_val == 0);
	}
};
g_sound.Test();
