$(function() {
	soundManager.url = '/fileadmin/html/_common/soundmanager2/swf/soundmanager2.swf';
	soundManager.onready(function(oStatus) {

		var elem = $('a.audio_content').first();

		var setStopCookie = function (stopped) {
			document.cookie = "disable_autoplay=" + stopped + "; expires=" + (new Date((new Date()).getTime() + (90*24*60*60*1000))).toGMTString() + "; path=/";
		};

		var setPlayStatus = function (playing) {
			if (playing) {
				elem.removeClass('audio_stopped');
				elem.addClass('audio_playing');
				elem.attr('title', 'Stop');
			} else {
				elem.removeClass('audio_playing');
				elem.addClass('audio_stopped');
				elem.attr('title', 'Abspielen');
			}
		};

		// check if SM2 successfully loaded..
		if (oStatus.success) {
			// SM2 has loaded - now you can create and play sounds!
			var audio_content = soundManager.createSound({
					id: 'audio_content',
					url: elem.attr('href'),
					onplay: function() {
						setStopCookie(false);
						setPlayStatus(true);
					},
					onstop: function() {
						setStopCookie(true);
						setPlayStatus(false);
					},
					onfinish: function() {
						setPlayStatus(false);
					}
			});

			elem.click(function() {
				if (audio_content.playState) {
					audio_content.stop();
				} else {
					audio_content.play();
				}
				return false;
			});

			/* Add and remove "useless" classes in order to make IE6 update the images and
			 * correctly apply the style. */
			elem.hover(function() {
				elem.children('img').addClass('hover');
			}, function() {
				elem.children('img').removeClass('hover');
			});

			if (document.cookie.indexOf('disable_autoplay=true') == -1) {
				audio_content.play();
			}
		}
	});
});

