(function($) {
	$.fn.currentRadio = function (options) {

		return this.each(function () {
			var $current = $(this).find('.current');
			var $upcoming = $(this).find('.upcoming');
			var $sentence = $('#peak div.nav p.status .artist');
			var $cover = $(this).find('.cover');
			var $error = $(this).find('.error-message');
			
			var currentTrack = '';
			
			function fetch () {

				// Get tracks from MPD
				$.getJSON('http://www.greyhousecoffee.com/music.app.php?jsoncallback=?', { action:'getqueuedrefactored',num: 10 }, function (data) {
					if (data.current == null) {
						
						// $current.fadeOut('slow',function () {
						// 	$error.fadeIn('slow');
						// });
						// $upcoming.fadeOut('slow');
						$('#radio h3').hide();
						$current.html('We\'re not listening to music at the moment.');
						$('#radio .line').fadeOut('slow');
						return;
					}
					// $error.fadeOut('slow',function () {
					// 						$current.fadeIn('slow');
					// 						$upcoming.fadeIn('slow');
					// 					});
					$('#radio .line').fadeIn('slow');
					$('#radio h3').fadeIn('slow');
					var track = data.current.Title + data.current.Album + data.current.Artist;

					if (currentTrack != track) {

						currentTrack = track;
						
						// Fetch new album art from last.fm
						$.getJSON('http://www.greyhousecoffee.com/music.app.last.fm.php?callback=?', {
							method: 'album.getinfo',
							api_key: 'b25b959554ed76058ac220b7b2e0a026',
							artist: data.current.Artist,
							album: data.current.Album,
							format: 'json'
						}, function (data) {

							src = '';
							showRaster = true;
							$.each(data.album.image, function(i, image) {
								if (image.size == 'extralarge') {
									src = image['#text'];
									showRaster = false;
								} else {
									showRaster = true;
									src = data.album.image[data.album.image.length - 1];
								}
								// console.log(image.size + ' : ' + image['#text']);
							});
							
							if (!showRaster) {
								$cover.find('.raster').fadeOut();
							} else {
								$cover.find('.raster').fadeIn();
							}
							
							// console.log(src);
							
							// Preload just a little
							// TODO: If you ever have time to waste,
							// make it so the previous song's art doesn't fade
							// until this one is loaded.  Load this one behind
							// the previous with absolute positioning and z-index
							// then fade through.
							$('<img/>').attr('src', src);
							
							$cover.find('img').fadeOut('slow', function () {
								$(this).remove();
								
								$img = $(new Image());
								
								$img.hide().load(function () {
									$(this).fadeIn('slow');
									$cover.find('div').wrap('<a href="' + data.album.url + '" target="_blank"></a>');
								});

								$img.attr('src', src);
								
								if ($img.width() < 300) {
									$cover.find('.raster').fadeOut();
								} else {
									$cover.find('.raster').fadeIn();
								}

								$img.css({width:'300px',height:'300px'}).appendTo($cover);
							});
						});
						
						$current.find('.artist').html(data.current.Artist);
						$current.find('.title').html(data.current.Title);
						
						$current.html(data.current.Title + ' <span class="by">by</span> ' + data.current.Artist);
						
						$sentence.html(data.current.Artist);
						$upcoming.empty();
						$.each(data.queued, function (i, track) {
							$('<li/>').html(track.Title + ' <span class="by">by</span> ' + track.Artist)
								.css({opacity:1 / data.queued.length * (data.queued.length - i) - (1 / data.queued.length)})
								.appendTo($upcoming);
						});
					}
				});
				setTimeout(fetch, 15000);
			}
			
			fetch();
			
		});
	}
})(jQuery);