function RGB(r,g,b){ return (0xff000000|(r<<16)|(g<<8)|(b)); }

var g_drag = 0;
var bool;
var g_metadb;
var imgname;
var rating;
var nrating;
var lrating;
var img;
var hofset=0;
var imgw=15;
var g_tfo = fb.TitleFormat("%rating%");

on_item_focus_change();

function on_paint(gr){
//var info = g_metadb.GetFileInfo();
if (g_metadb) {
for (i = 1; i < 6; i++) {
img = gdi.image(fb.FoobarPath + "images\\rating\\"
+ ((i > (g_drag ? lrating : rating)) ? "star-off" : "star" + (g_drag ? ((i==rating)?((i==lrating)?"-kill":"-hover") :"-hover") : "")) + ".png");
// + ((i > (g_drag ? lrating : rating)) ? "No" : "") +"Star" + (g_drag ? ((rating != lrating) ? "-hover" : "") : "") + ".png");
// + ((i > (g_drag ? lrating : rating)) ? "No" : (g_drag ? ((rating != lrating) ? "Red" : "") : "")) +"Star.png");
// + (g_drag ? ((rating != lrating) ? "Blue" : "") : "") + ((i > (g_drag ? lrating : rating)) ? "No" : "") +"Star.png");
gr.DrawImage(img, hofset+imgw*(i-1), 1, 16, 16, 0, 0, 16, 16);
}
}
}

function on_mouse_wheel(delta){}

//function on_mouse_lbtn_up(x,y){
//if (lrating !=rating) {if (g_metadb) {
//bool = g_metadb.UpdateFileInfoSimple("RATING",lrating);
//}}
//else {
//bool = g_metadb.UpdateFileInfoSimple("RATING",0);}
//}

function on_mouse_lbtn_up(x,y){
// fb.trace("button up");
if (lrating !=rating) {if (g_metadb) {
bool = fb.RunContextCommandWithMetadb("Rating/"+((lrating==0) ? "<not set>" : lrating),g_metadb);
}}
else {
bool = fb.RunContextCommandWithMetadb("Rating/<not set>",g_metadb);}
}

function on_mouse_move(x, y) {
if (g_metadb) {
g_drag = 1;
nrating = Math.ceil((x-hofset)/imgw);
if (nrating > 5) nrating = 5;
if (nrating != lrating) {
lrating = nrating;
window.Repaint();
}
}
}
function on_mouse_leave() {
on_metadb_changed()
}

function on_item_focus_change() {
fb.trace("itm focus changed");
if (g_metadb) {window.UnwatchMetadb();}
//g_metadb = fb.GetFocusItem();
if (fb.IsPlaying | fb.IsPaused) g_metadb = fb.GetNowPlaying();
if (g_metadb) {
on_metadb_changed();
window.WatchMetadb(g_metadb);
}

}
function on_metadb_changed() {
g_drag = 0;
// fb.trace("changed");
rating = g_tfo.EvalWithMetadb(g_metadb);
if (rating == "?") {rating = 0;}
lrating = rating;
window.Repaint();
}

function on_playback_new_track(metadb) {on_item_focus_change();}
//EOF
