$(document).ready(function(){
    $('div.pesme h2.gray').click(function(){
        f_SH(this);
    });
    $('body').data('mp3', false);
    $('div.pesmaPlay a').click(function(){
        $('div.pesmaPlay').show();
        $('div.pesmaPause').hide();
        $(this).parent().hide();
        $(this).parent().parent().children('div.pesmaPause').show();
        $('body').data('mp3', this);
        f_SecPlay($(this).attr('href'));
        return false;
    });
    $('div.download a.download').click(function(){
        var href = $(this).parent().parent().children('div.pesmaPlay').children('a').attr('href');
        $(this).attr('href', href);
    });
    $('div.pesmaPause a.pause').click(function(){
        $("#jPlayers").pause();
        $(this).hide();
        $('div.pesmaPause a.play').show();
        return false;
    });
    $('div.pesmaPause a.play').click(function(){
        $("#jPlayers").play();
        $(this).hide();
        $('div.pesmaPause a.pause').show();
        return false;
    });
    $("#jPlayers").jPlayer({
        ready: function() {
            try {
                $('#jPlayer').stop();
            } catch(e){}
        }
    }).onProgressChange(function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
        var obj = $('body').data('mp3');

        var ppaInt = parseInt(playedPercentAbsolute * 0.96);
        var myPlayedTime = new Date(playedTime);
        var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
        var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
        $(obj).parent().parent().children('div.pesmaPause').children('div.pesmaDuration').text(ptMin + ":" + ptSec);

        var myTotalTime = new Date(totalTime);
        var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
        var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
        //$(obj).parent().parent().children('div.pesmaPause').children('div.pesmaDuration').text(ttMin + ":" + ttSec);

        try {
            var obj = $('body').data('mp3');
            $(obj).parent().parent().children('div.pesmaPause').children('div.pesmaLine').css('left', 38 + ppaInt);
        } catch(e) {}
    }).onSoundComplete(function() {
        try {
            var obj = $('body').data('mp3');
            $(obj).parent().show();
            $(obj).parent().parent().children('div.pesmaPause').hide();
        } catch(e){}
    });
});

function f_SecPlay(mp3) {
    try {
        $('#jPlayer').stop();
    } catch(e){}
    $('#jPlayers').setFile(mp3);
    $('#jPlayers').play();
};
function f_SH(obj) {
    var id = $(obj).attr('id');
    $('div.pesme h2.gray').removeClass('hStraneActive hZabavneActive hNarodneActive');
    $('#' + id).addClass(id + 'Active');
    $('div.collection').css('display', 'none');
    $('div.' + id).css('display',  'block');
};
