// ==PREPROCESSOR==
// @name "GetAlbumArtV2"
// @author "T.P Wang"
// @import "%fb2k_component_path%docs\flags.txt"
// @import "%fb2k_component_path%docs\helpers.txt"
// ==/PREPROCESSOR==

var g_img = null;
var ww = 0, wh = 0;
on_playback_new_track();

function on_paint(gr) {
	// RGB function is defined in docs\helpers.txt
	gr.FillSolidRect(0, 0, ww, wh, RGB(255, 255, 255));
	if (g_img) {
		// Keep aspect ratio
		var scale_w = ww / g_img.Width;
		var scale_h = wh / g_img.Height;
		var scale = Math.min(scale_w, scale_h);
		var pos_x = 0, pos_y = 0;
		if (scale_w < scale_h)
			pos_y = (wh - g_img.height * scale) / 2;
		else if (scale_w > scale_h)
			pos_x = (ww - g_img.Width * scale) / 2;
		gr.DrawImage(g_img, pos_x, pos_y, g_img.Width * scale, g_img.Height * scale, 0, 0, g_img.Width, g_img.Height);
	}
}

function on_size() {
	ww = window.Width;
	wh = window.Height;
}

function on_playback_new_track() {
	var handle = fb.GetNowPlaying();
	if (!handle)
		return;
	if (g_img)
		g_img.Dispose();
	// AlbumArtId.front is defined in docs\flags.txt
	g_img = utils.GetAlbumArtV2(handle, AlbumArtId.front);
	window.Repaint();
}
