(function($) {
    "use strict";
    var $actual= null;
    var opened=false;
    $(".open-disc").click(function() {
        open($(this).data('url'));
        $actual=$(this);
    });

    function open(e){
    $.ajax({
        url: e,
        success: function(data) {
            $('.project-content').html(data);
            $(".project-content").hide(0)
            $('.project-window').hide(0)
            close();
            $("html, body").animate({ scrollTop: $('#project-show').offset().top - (200) }, 300, function(){
                $('.project-window').show(0);
                $('.project-window').css('height','0');
                var heightAjax = $('.project-content').height();
                $('.project-window').animate({height:heightAjax}, 500,function(){
                    $('.project-window').css('height',heightAjax);
                    $(".project-content").fadeIn("slow");
                });
            });
            prepareAudio();
            $(".info-album a").attr("target","_blank");
        }
    });
    }

    function close(){
        $(".close-btn").click(function() {
            $(".project-window").slideUp("slow");
            $(".project-content").fadeOut("slow");
            $("html, body").animate({ scrollTop: $('#discography').offset().top -(50) }, 1000);
            opened=false;
        });
    }

    function prepareAudio() {
        var playlist0 = $('#playlist0');
        var a0 = audiojs.create(playlist0, {
          trackEnded: function() {
            var next = $('ol.playlist0 li.playing').next();
            if (!next.length) next = $('ol.playlist0 li').first();
            next.addClass('playing').siblings().removeClass('playing');
            audio0.load($('a', next).attr('data-src'));
            audio0.play();
          }
        });

        // Load in the first track
        var audio0 = a0[0];
        var first = $('ol.playlist0 a').attr('data-src');
        $('ol.playlist0 li').first().addClass('pause');
        audio0.load(first);

        // Load in a track on click
        $('ol.playlist0 li').click(function(e) {
            e.preventDefault();

            if( $(this).attr('class') == 'playing' ) {
                $(this).addClass('pause');
                audio0.playPause();
            } else {
                $(this).removeClass('pause').addClass('playing').siblings().removeClass('playing').removeClass('pause');
                audio0.load($('a', this).attr('data-src'));
                audio0.play();
            }

        });
    }

})(jQuery);
