function toMinutes(msec) {
	sec = Math.round(msec/1000)
	min = Math.round(sec/60)
	if (min*60 > sec) sec += 60
	sec -= min*60
	if (sec < 10) sec = "0"+sec
	return min+":"+sec
}
soundManager.url = '/js/'; // directory where SM2 .SWFs live
var mp3url = ""
var volume = 100

// disable debug mode after development/testing..
soundManager.debugMode = false;


soundManager.onload = function() {
	var som = soundManager.createSound({
		id: 'mp3musica',
		url: mp3url,
		autoPlay: false,
		autoLoad: true,
		onload: load,
		onid3: id3,
		whileloading: loading,
		whileplaying: playing
	})

	function load() {
		$("span.titulo").text( this.id3['songname'] + ' ['+ toMinutes(this.duration) +']' )
	}

	// ID3
	function id3() {
		$("span.titulo").text( this.id3['songname'] )
	}

	// playing
	function playing() {
		var positionPorcent = (this.position*100 / this.duration)
		$("#bar").css('left', positionPorcent+"%")
	}

	// loading
	function loading() {
		var loaded = this.bytesLoaded*100/this.bytesTotal
		$("span.titulo").text( "Carregando: "+ Math.round(loaded) +"%" )
	}

}

$(document).ready( function() {
	mp3url = $("#mp3").text()
	
	// play button
	$("#play").click( function() {	soundManager.play('mp3musica'); })

	// stop button
	$("#stop").click( function() {	soundManager.stop('mp3musica'); })
	
	// menos volume button
	$("#vol_men").click( function() {
		volume -= 10
		if (volume < 0) volume = 0
		soundManager.setVolume( 'mp3musica', volume )
		$("#volumeatual").css('left', volume+"%")
		$("#vol_men").css('width', Math.round(65*volume/100) )
		$("#vol_mais").css('width', 65 - Math.round(65*volume/100) )
	})

	// mais volume button
	$("#vol_mais").click( function() {
		volume += 10
		if (volume > 100) volume = 100
		soundManager.setVolume( 'mp3musica', volume )
		$("#volumeatual").css('left', volume+"%")
		$("#vol_men").css('width', Math.round(65*volume/100) )
		$("#vol_mais").css('width', 65 - Math.round(65*volume/100) )
	})

});
