played = 0;
played_track = -1;
previos_track = -1;
nextTrack = 0;
timers = [];
delimeter = "'";
load_vars();
function get_id(object, name = "player"){
var i = object.id.toString().replace(name,"");
return i;}
function load_vars(){
$.each($("audio"), function (i, player){
var id = player.id.toString().replace("player","");
t = setInterval(function(){
timer_tik(id);
}, 1000);
timers.push(t);
});
$.each($("a[class=link]"), function (i, link){
var id = get_id(link, "counter");
$("#counter" + id).click(function(){
play(id);
});});}
function timer_tik(id){
if (played_track == id){
var pl = document.getElementById( 'player' + id );
var current_time = parseInt(pl.duration) - parseInt(pl.currentTime);
var link = document.getElementById( 'counter' + id );
// var value = link.textContent;
// var a = value.split(delimeter);
// var seconds = (+parseInt(a[0]) * 60 + parseInt(a[1]));
// seconds = seconds -1 ;
// var minutes = parseInt(seconds/60);
// var sec = parseInt(seconds - minutes * 60);
// if (sec.toString().length == 1) {
// sec = "0" + sec.toString();
// }
var minutes = parseInt(current_time/60);
var sec = parseInt(current_time - minutes * 60);
if (sec.toString().length == 1) {
sec = "0" + sec.toString();
}
link.textContent = minutes + delimeter + sec;
// if (seconds == 0){
// stop(id);
// if (id == timers.length){
// play(1);
// }else{
// play(parseInt(id) + 1);
// }
// }
if (current_time == 0){
stop(id);
if (id == timers.length){
play(1);
}else{
play(parseInt(id) + 1);
}}}}
function mute(){
$.each($("audio"), function (i, player){
player.pause();
player.currentTime=0;
});
$.each($("a[class=link]"), function (i, link){
link.textContent = link.getAttribute('alt');
});
previos_track = played_track;
played_track = -1;
}
function stop(id){
mute();
player = document.getElementById( 'player' + id );
player.pause();
player.currentTime = 0;
// console.log("debug");
}
function play (id){
// console.log(id);
if (played_track == id){
// mute();
played_track = -1;
player = document.getElementById( 'player' + id );
player.pause();
} else {
// console.log(played_track, previos_track);
if (played_track == previos_track){
// mute();
player = document.getElementById( 'player' + id );
player.play();
played_track = id;
// console.log(played_track, previos_track);
}else{
mute();
player = document.getElementById( 'player' + id );
player.play();
played_track = id;
// console.log(played_track, previos_track);
};
}}