function insertAudioplayer() {
	$f("player", "/swf/flowplayer.commercial-3.0.7.swf", {
		onLoad: audioplayerLoaded,
		onFinish: playerDone,
	
		key: '#$c4451f341c2b602a6c5',
		clip : {
			autoPlay : false
		},
		plugins: { 
	        controls: { 
	            fullscreen: false,
				volume : false, 
	            height: 25,
	            width:254,
	            mute: false,
	            time: false,
	            backgroundColor: '#7bcde0',
	            sliderColor: '#7bcde0',
				buttonColor: '#7bcde0',
		        buttonOverColor: '#728B94',
		        bufferColor: '#d9f2f7',
		        progressColor: '#ffffff',
		        progressGradient: 'none',
				bufferGradient: 'none',
				backgroundGradient: 'none',
	            borderRadius: 0
	        },
	        audio: { 
	            url: '/swf/flowplayer.audio-3.0.4.swf' 
	        } 
	    }
	});
}

function audioplayerLoaded() {
	playCurrentAudiofile();
	
	var audiofileLinks = $$('.audiofile');
	audiofileLinks.each(function(a) {
		a.observe('click', function(event) {
			if (event.element().hasClassName('shoplink')) {
				return; 
			}
			event.stop();
			audiofileLinks.each(function(al) {
				al.removeClassName('current');
			});
			a.addClassName('current');
			playCurrentAudiofile();
		})
	})
}

function playCurrentAudiofile() {
	var audiofile = $$('.audiofile.current').first();
	$f().close();
	$f().play(audiofile.getAttribute('href'));
	adjustScroll();
	new Ajax.Request('/player_counter', {
		parameters: {
			'id': audiofile.id.substring(10)
		}
	});
}

function adjustScroll() {
	var audiofile = $$('.audiofile.current').first();
	var afTop = audiofile.positionedOffset().top; 
	if (afTop < $('audiofileList').scrollTop) {
		$('audiofileList').scrollTop = afTop; 
	}
	var afHeight = audiofile.getHeight();
	var listHeight = $('audiofileList').getHeight();
	//alert(afTop + ' ' + afHeight +' ' + listHeight + ' ' + $('audiofileList').scrollTop);
	if (afTop + afHeight > $('audiofileList').scrollTop + listHeight) {
		$('audiofileList').scrollTop = afTop + afHeight - listHeight;
	}
}

function playerDone() {
	var current = $$('.audiofile.current').first();
	current.removeClassName('current');
	if (current.next('.audiofile')) {
		current.next('.audiofile').addClassName('current');
	} else {
		$$('.audiofile').first().addClassName('current');
	}
	playCurrentAudiofile();
}

