soundManager.url = '/sound/soundmanager/swf/';
soundManager.flashVersion = 9;
soundManager.useFlashBlock = false;
soundManager.debugMode = false;

var volume = $.cookie('volume') ? $.cookie('volume') : 30;
var position = $.cookie('position') ? $.cookie('position') : 0;

var isPlaying = false;
var isPlayableBackground = ($.cookie('background') == 0) ? false : true;
//var isPlayableBackground = true;
var isError	= false;
var isVolumeHover = false;

soundManager.onready(function() {
	soundManager.createSound({
		id: 'background',
		url: '/sound/background.mp3',
		autoPlay: isPlayableBackground,
		position: 0,
		loops: 3,
		volume: volume,
		onload: function() {
			soundManager.setPosition('background', position);
		}
	});

	soundManager.createSound({
		id: 'advertisement',
		url: '/sound/advertisement.mp3',
		volume: 60, // volume
		onfinish: function() {
			soundManager.play('background');
		}
	});
});

soundManager.ontimeout(function(status) {
	isError = true;
	$('#sound').html('Для воспроизведения презентации установите <a style="text-decoration:underline;" href="http://get.adobe.com/ru/flashplayer/" target="_new">Flash Player</a>');
});

$(function() {
/*
	setVolume(volume);
	$('#volume').slider({
		orientation: "vertical",
		range: "min",
		min: 0,
		max: 100,
		value: volume,
		slide: function(event, ui) {
			setVolume(ui.value);
		}
	});

	$('#volume-wrapper').css('left', $('#volume-control').position().left + 18).css('top', $('#volume-control').position().top + 18);
	$('#volume-wrapper').hover(function() { isVolumeHover = true; }, function() { isVolumeHover = false; });
	$('#volume-control').hover(function() { isVolumeHover = true; }, function() { isVolumeHover = false; });


	$(window).click(function() {
		if (!isVolumeHover) {
			$('#volume-wrapper').hide();
		}
	});
	$('#volume-control').mousedown(function() { $('#volume-wrapper').toggle(); });
*/
	if (isPlayableBackground) {
		$('#volume-control').css('backgroundImage', 'url(/sound/volume-high.png)');
	} else {
		$('#volume-control').css('backgroundImage', 'url(/sound/volume-muted.png)');
	}

	$('#volume-control').click(function () {
		if (!isPlayableBackground) {
			if (!isPlaying) {
				soundManager.play('background');
			}
			$(this).css('backgroundImage', 'url(/sound/volume-high.png)');
			isPlayableBackground = true;
		} else {
			soundManager.pause('background');
			$(this).css('backgroundImage', 'url(/sound/volume-muted.png)');
			isPlayableBackground = false;
		}
		$.cookie('background', isPlayableBackground ? 1 : 0);
		return false;
	});


	$('#advertisement').click(function () {
		if (!isError) {
			if (!isPlaying) {
				if (0 == volume) {
					setVolume(60);
					$('#volume').slider('value', 60);
				}
				soundManager.pause('background');
				soundManager.play('advertisement');
				$(this).html('Остановить воспроизведение');
				isPlaying = true;
			} else {
				if (isPlayableBackground) {
					soundManager.play('background');
				}
				soundManager.pause('advertisement');
				$(this).html('Продолжить воспроизведение');
				isPlaying = false;
			}
		}
		return false;
	});

	$(window).unload(function() {
		$.cookie('position', soundManager.getSoundById('background').position);
	});

	$('#slider1').lemmonSlider({
		infinite: true
	});
	sliderAutoplay();

});

function sliderAutoplay() {
	$('#slider1').trigger('nextSlide');
	setTimeout(function() { sliderAutoplay(); }, 3000);
}
/*
function setVolume(value) {
	volume = value;
	soundManager.setVolume('background', value);
	soundManager.setVolume('advertisement', value);
	$.cookie('volume', value);
	if (0 == value) {
		$('#volume-control').css('backgroundImage', 'url(/sound/volume-muted.png)');
	} else if (60 > value) {
		$('#volume-control').css('backgroundImage', 'url(/sound/volume-medium.png)');
	} else {
		$('#volume-control').css('backgroundImage', 'url(/sound/volume-high.png)');
	}
}
*/
