var scrolling, intro, fighting, hitting, blocking;
var mus = [], active;
var musicVol = 100, soundVol = 100;

// Load the SWFSound Flash Engine
function initSWF() {
	swfsound.embedSWF( './swfsound/swfsound.swf' );
}

// When the engine is loaded, preload sounds
swfsound.onload = function() {
	scrolling = swfsound.loadSound( './music/scroll.mp3', streamAndAutoPlay = false );
	intro = swfsound.loadSound( './music/intro.mp3', streamAndAutoPlay = false );
	fighting = swfsound.loadSound( './music/fight.mp3', streamAndAutoPlay = false );
	hitting = swfsound.loadSound( './music/hit.mp3', streamAndAutoPlay = false );
	blocking = swfsound.loadSound( './music/block.mp3', streamAndAutoPlay = false );
	
	player.setSounds();
	robberLeader.setSounds();
	robber.setSounds();
}

function setMusicVolume(vol) {
	musicVol = vol;
	
	try {
		swfsound.setVolume(mus[active], musicVol);
	}
	catch (e) {}
}

function setSoundVolume(vol) {
	soundVol = vol;
	//TODO	Sample abspielen
}

function playSound(sound) {
	if (sound == null) {
		return;
	}
	else if (typeof sound == 'string') {
		swfsound.loadSound( sound, streamAndAutoPlay = true );
	}
	else {
		swfsound.setVolume(sound, soundVol);
		swfsound.startSound(sound);
	}
}

function infLoop(obj) {
	swfsound.startSound(obj, 0.0, 0, 'infLoop(' + obj + ')');
}

function playMusic(obj, volume) {
	if (obj == mus[active]) {
		return;
	}
	
	if (active == null) {
		active = 0;
	}
	else {
		sndfadeOut(mus[active]);
		active = 1-active;
	}
	
	var vol = (volume) ? musicVol * volume / 100 : musicVol;
	
	mus[active] = obj;
	infLoop(mus[active]);
	swfsound.setPan( mus[active], 0);
	swfsound.setVolume( mus[active], vol);
}

function sndfadeOut(obj) {
	var fadeStep = 5;
	var val = swfsound.getVolume(obj);
	var sndfading = window.setInterval(
		function() {
			val -= fadeStep;
			
			if (val < fadeStep) {
				val = 0;
				window.clearInterval(sndfading);
				swfsound.stopSound(obj);
			}
			swfsound.setVolume(obj, val);
		}, 50 );
}
