
soundManager.fadeOut = function(soundObj){
  if(typeof soundObj == "string"){
	  soundManager.getSoundById(id);
	}
	var lower = function(){
		if(soundObj.volume > 0){
			var decreaseBy = soundObj.volume >= 1 ? 1 : soundObj.volume;
			soundObj.setVolume(soundObj.volume - decreaseBy);
			setTimeout(function() {
				lower();
			}, 10);
		}else{
			soundObj.stop();
			soundObj.setVolume(100);
		}
	};
	lower();
}

soundManager.url = 'swf/';
// soundManager.debugMode = true;
soundManager.onload = function() {
    
    $(settings.songs).each(function(){
        soundManager.createSound(this)
    });
}

soundManager.onerror = function() { alert("There was an error while loading the audio player on this page. One possible reason for the problem is if you have an old version of Flash installed."); }

var getSo = function(obj) {
    return soundManager.getSoundById(
    $(".id", obj).text()
    );
}

$(".playLink").live("click",
    function() {
        var obj = $(this);
        var so = getSo(this)
        var position = $("<div class='position'><div class='indicator'></div></div>").appendTo(obj);

        so.options.whileplaying = function() {

            var indicatorWidth = (so.bytesLoaded / so.bytesTotal) * settings.positionWidth;

            position.css("width", indicatorWidth);

            $(".indicator", position).css("left",
                indicatorWidth
                * (so.position / so.duration)
            );

        };
        soundManager._mp_currentSO = so;
        so.play();
        $(".playStop", this).text("stop");
        $(this).removeClass("playLink");
        $(this).addClass("playLinkPlaying");
});

$(".playLinkPlaying").live("click",
	function() {
    var obj = this;
    soundManager.fadeOut($(".id", this).text());
    $(".playStop", this).text("play");
    $(this).addClass("playLink");
    $(this).removeClass("playLinkPlaying");
	var pos = $(".position", obj);
	pos.animate(
		{height: 0},
		500,
		"swing",
		function(){
	    	$(".position", obj).remove();
		}
	);
});

$(".position").live("click",
function(e) {

    var getOffX = function(o) {
        // http://www.xs4all.nl/~ppk/js/findpos.html
        var curleft = 0;
        if (o.offsetParent) {
            while (o.offsetParent) {
                curleft += o.offsetLeft;
                o = o.offsetParent;
            }
        }
        else if (o.x) curleft += o.x;
        return curleft;
    }
    var leftPos = e.pageX - getOffX(this);
    $(".indicator", this).css("left", leftPos);
    var so = getSo($(this).parents(".playLinkPlaying"));
    so.setPosition(
    so.duration * leftPos / $(this).width()
    );
    return false;
});


