/*
 * Copyright 2010 Bobby Powers
 * released under the MIT License
 * <http://www.opensource.org/licenses/mit-license.php>
 */
var options = {
    content: '#details',
};

bp.media = function () {

    return {
	playing: function() {
	    $('#selected').attr('id', '');
	    var song = $('#audio')[0].src;
	    $(options.content + ' .row a').filter(function (i, a) {
		if (a.href === song)
		    return true;
		return false;
	    }).parent().attr('id', 'selected');
	},
	canplay: function() {
	    $('#audio')[0].play();
	},
	ended: function() {
	    $('#selected').next().children('a').click();
	}
    }
}();

var infoCb = function(type, clickAction) {
    return function(data, status) {
        var html = '';
        if (!data)
            html = 'no data in response.';
        else
            data.map(function (a) {
                if (!a)
                    return;
                html += '<div class="row"><a href="#' + type+ '=' + a + '">' +
                    decodeURIComponent(a) + '</a></div>';
            });

        $('#' + type).html(html);
        $('#' + type + ' .row a').bind('click', function (event) {
            event.preventDefault();
            clickAction(type, $(this).text());
        });
    }
}

var loadBrowse = function(type) {
    $.ajax({
        url: '/api/' + type,
        dataType: 'json',
        success: infoCb(type, loadDetails)
    });
}

var loadDetails = function(type, target) {
    $.ajax({
        url: '/api/' + type + '/' + encodeURIComponent(target),
        dataType: 'json',
        success: function(data, status) {
	    var html = '';
	    if (!data)
		html = 'no data in response.';
	    else
		data.map(function (song) {
		    if (!song)
			return;
		    html += '<div class="row"><a href="/music/' +
			encodeURIComponent(song.path) + '">play</a> ' +
			decodeURIComponent(song.album) +
			' - ' + decodeURIComponent(song.title) + '</div>';
		});
	    $(options.content).html(html);
	    $(options.content + ' .row a').bind('click', function(event) {
		event.preventDefault();
		$('#now-playing').html('<audio src="' + $(this).attr('href') +
				       '" id="audio" controls preload ' + 
				       'oncanplay="bp.media.canplay()" ' +
				       'onplaying="bp.media.playing()" ' +
				       'onended="bp.media.ended()" ' +
				       '></audio>');
		document.getElementById("audio").muted = false;
	    });
	}
    });
}

// called on document.ready, sets the initial state of the FSM.
$(function () {
    loadBrowse('artist');
    loadBrowse('album');
});

@@ -1381,24 +1381,526 @@
 %0A    %7D);%0A%7D%0A%0A
+// returns the straight up path we got from cnote and encode each%0A// component, so that both the browser and nginx will handle things%0A// correctly.  The browser (or maybe just jquery) will truncate%0A// anything after '#' in a URI for an ajax request, and nginx might%0A// mess stuff up too.%0Avar escapePath = function(path) %7B%0A    var parts = path.split(%22/%22);%0A    var escapedPath = %22%22;%0A    for (i in parts) %7B%0A%09escapedPath += %22/%22 + encodeURIComponent(parts%5Bi%5D);%0A    %7D%0A    return escapedPath.substring(1);%0A%7D%0A%0A
 var loadDeta
@@ -2211,16 +2211,59 @@
 return;%0A
+%09%09    escapedPath = escapePath(song.path);%0A
 %09%09    ht
@@ -2314,36 +2314,18 @@
 %09%09%09e
-ncodeURIComponent(song.p
+scapedP
 ath
-)
  + '

fix media url path escaping