var mb_section;
var mb_artist;
var mb_div;

$(function(){
	
	mb_section = 'artist';
	mb_artist = '';
	mb_div = $('#musicBrowser');
	
	
	listen();
});

function listen() {
	mb_div.find('#col1 li').click(function() {
		mb_section = $(this).attr('rel');
		mb_artist = '';
		mb_reload();
	});

	mb_div.find('#col2 li').click(function() {
		mb_artist = $(this).attr('rel');
		mb_reload();
	});
	
	mb_div.find('.thumbsUp').click(function() {
		if(!logged_in) { mb_login_msg(); }
		else { mb_rate('up', $(this).attr('rel')); }
		return false;
	})
	
	mb_div.find('.thumbsDown').click(function() {
		if(!logged_in) { mb_login_msg(); }
		else { mb_rate('down', $(this).attr('rel')); }
		return false;
	})
}

function mb_rate(vote, id) {
	$.get('/media/music/rate:' + id + ':' + vote);
	$('#vote_up_track_' + id).removeClass('active');
	$('#vote_down_track_' + id).removeClass('active');
	
	$('#vote_' + vote + '_track_' + id).addClass('active');
}

function mb_login_msg() {
	mb_msg_error_div = $('.browser.msg.error');
	mb_msg_error_div.html('You must be <a href="/user/login">logged in</a> to rate music.').slideDown();
	setTimeout("mb_msg_error_div.slideUp()", 5000);
}

function mb_reload() {
	mb_div.load('/media/music/' + mb_section + '/' + mb_artist, function() { 

		listen(); 

		if (soundManager.supported()) {
			// Kill the sound when the page is changed.
			if(threeSixtyPlayer.lastSound != null) {
				threeSixtyPlayer.lastSound.pause();
				threeSixtyPlayer.lastSound = null;
			}

			// soundManager.createSound() etc. may now be called
			threeSixtyPlayer.init();
		}
	});
}
