/**
 * All rights reserved
 * 
 * @author Kozicki Jakub <kuba.kozicki@gmail.com>
 */

soundManager.url = 'flashes/';
soundManager.waitForWindowLoad = true;
soundManager.defaultOptions.volume = 100;
soundManager.debugMode = false;
soundManager.useConsole = false;
soundManager.onerror = function() {
	
	  var body = Element.extend(document.body);
	  body.insert({top: '<div id="sm2-flashblock" style="position:fixed;left:0px;top:0px;width:100%;min-height:24px;z-index:99;background:#fff;color:#3A3A3A;font-family:helvetica,verdana,arial;font-size:11px;border-bottom:1px solid #333;opacity:0.95">'
		     + '<div style="float:right;display:inline;margin-right:0.5em;color:#fff;line-height:24px">[<a href="#noflashblock" onclick="document.getElementById(\'sm2-flashblock\').style.display=\'none\'" title="Zamknij" style="color:#999;text-decoration:none">x</a>]</div>'
		     + '<div id="sm2-flashmovie" style="float:left;display:inline;margin-left:0.5em;margin-right:0.5em"><!-- [flash] --></div>'
		     + '<div style="padding-left:0.5em;padding-right:0.5em;line-height:24px">Nie używasz flasha? Jeśli chcesz aby strona była w pełni multimedialna zezwól na używanie obiektów flash w celu aktywowania efektów dźwiękowych, a następnie odśwież stronę w swojej przeglądarce.</div>'
		     + '</div>'});
};

soundManager.onload = function() {
	soundManager.createSound({
		'id': 'ball',
		'url': 'sounds/ball.mp3',
		'autoLoad': true,
		'volume' : 10
	});
	
	new SoundPlayer();
};

var SoundPlayer = Class.create({
	defaultOptions : {
		id : {
			progressBar : 'progress-bar',
			progressPercent : 'progress-percent',
			musicBar : 'music-bar',
			musicBarControl : 'music-bar-control'
		}
	},
	initialize : function() {
		Object.extend(this, arguments[0] || this.defaultOptions);
		
		this.progressBar = new ProgressBar(this.id.progressBar,{  
			interval: 0.15  
		});
		this.musicBar = $(this.id.musicBar);
		this.musicBarControl = $(this.id.musicBarControl);
		this.progressPercent = $(this.id.progressPercent);
		
		this.progressBar.container.show();
		this.progressPercent.show();
		this.musicBar.show();
		
		this.autoStart = false;
		
		this.sound = soundManager.createSound({
			'id': 'music',
			'url': 'sounds/sound.mp3',
			'autoLoad': true,
			'autoPlay': false,
			'multiShot': true,
			'whileloading': this.whileloading.bind(this),
			'onload': this.onLoad.bind(this),
			'onfinish': this.onFinish.bind(this),
			'volume': 85
		});
		
		Event.observe(this.id.musicBarControl, 'click', this.controlClick.bindAsEventListener(this));
	},
	
	whileloading : function() {
		var percent = Math.round(Math.max(0, this.sound.bytesLoaded/this.sound.bytesTotal) * 100, 0);
		
		this.progressBar.setProgress(percent);
		this.progressPercent.update(percent + "%");
		
		if(percent > 75 && this.autoStart == false && this.sound.playState == 0) {
			this.sound.play();
			this.togglePause();
		}
	},
	
	onLoad: function() {
		this.progressBar.container.squish();
		this.progressPercent.squish();
	},
	
	onFinish: function() {
		this.sound.setPosition(0);
		this.sound.play();
	},
	
	controlClick: function() {
		this.sound.togglePause();
		this.togglePause();
	},
	
	togglePause: function() {
		this.musicBarControl.toggleClassName('play');
		this.musicBar.toggleClassName('play');
		this.autoStart = true;
	}
});
