
soundManager.url = '/flash/'; 
soundManager.debugMode = false;

function playFadedSound(soundClip, imageId)
{
  	var imageObject = document.getElementById(imageId);
	if(imageObject.src.indexOf('start') != -1) 
	{
  
	  	var mySound = soundManager.createSound({
		id: soundClip,
	  	url: soundClip,
	  	autoLoad: true,
	  	autoPlay: true,
	  	stream: true,
		onfinish: function ()
			  {
				var imageObject = document.getElementById(imageId);
				var imageBaseURL = imageObject.src.substring(0, imageObject.src.indexOf('stop')); 
				imageObject.src = imageBaseURL + "start.gif";
				soundManager.unload(soundClip);
			  }
		});

		mySound.play();
		var imageBaseURL = imageObject.src.substring(0, imageObject.src.indexOf('start')); 
		imageObject.src = imageBaseURL + "stop.gif";
		t = setTimeout(function () {
fadeOutSound(soundClip, imageId);
}, 10000);

	}
	
	else if(imageObject.src.indexOf('stop') != -1) 
	{
		soundManager.stop(soundClip);
		soundManager.unload(soundClip);
		var imageBaseURL = imageObject.src.substring(0, imageObject.src.indexOf('stop')); 
		imageObject.src = imageBaseURL + "start.gif";
		clearTimeout(t);
	}
}

function fadeOutSound(soundClip, imageId) {
                var s = soundManager.getSoundById(soundClip);
                var vol = s.volume;
			    if (vol == 0) {
				playFadedSound(soundClip, imageId);
				}
				  else
				{
                s.setVolume(Math.max(0,vol-5));
				setTimeout(function () {
fadeOutSound(soundClip, imageId);
}, 200);
				}
}
