$(document).ready(function(){
    
    $("#audio-player").jPlayer( {
        swfPath: "/js/",
        //nativeSupport: false,
        ready: function(){
            //var file = $("ul.messages>li:first .message-actions li.download a").attr('href');
            //this.element.jPlayer("setFile", file);
        }
    }).jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
        $("#jplayer_play_time").text($.jPlayer.convertTime(playedTime));
        $("#jplayer_total_time").text($.jPlayer.convertTime(totalTime));
        
        //demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page
    });
    
    $(".jp-interface").hide();
    
    $(".message-actions li.listen a").click(function(){
        var file = $(this).parent().siblings('.download').find('a').attr('href');
        var $that = $(this);
        
        if($(".jp-interface", $that.parent().parent().parent()).length && $("#audio-player").jPlayer("getData", "isFileSet")) { // already here
            
            if ($(".jp-interface:visible").length) {
                $(".jp-interface").hide();
                $("#audio-player").jPlayer('pause');
            } else {
                $(".jp-interface").show();
                $("#audio-player").jPlayer('play');
            };
            
        } else {
            
            var file = $that.parent().siblings("li.download").children('a').attr('href');
            $(".jp-interface").appendTo($that.parent().parent().parent()).show();
            $("#audio-player").jPlayer("setFile", file).jPlayer("play");
        
        }

        return false;
    });
    
    // Track event
    $(".message-actions li a, .message-actions li input").click(function(){
        var action = 'no action';
        if($(this).is("a")) {
            action = $(this).text();
        } else if($(this).is("input")) {
            action = $(this).val();
        }
        
        _gaq.push(['_trackEvent', 'Message', action, $(this).attr("title")]);
        
    });
    
});
