# Bryan Schumaker (11/17/2010)

from libsaria.sources import library
from libsaria import path
splitext = path.splitext

def header(wfile):
	wfile.write("<html><head>")
	wfile.write("<script type=\"text/javascript\" src=\"controls.js\"></script>\n")
	wfile.write("<script type=\"text/javascript\" src=\"utils.js\"></script>\n")
	wfile.write("<script type=\"text/javascript\">")
	wfile.write("function playSong(id){")
	wfile.write("load_id(id);")
	#wfile.write("var src=id+ext;")
	#wfile.write("var audio = document.getElementById(\"audio\");")
	#wfile.write("audio.innerHTML=\"<audio src=\\\"\" + src + \"\\\" controls autoplay></audio>\";")
	#wfile.write("return false;")
	wfile.write("}")
	wfile.write("</script><body>")

def footer(wfile):
	wfile.write("</body></html>")

def body(wfile, args):
	artist = library.tag_tree[args["artist"]].value
	album  = library.tag_tree[args["artist"]][args["album"]].value
	wfile.write("<table align=center width=75%>")

	wfile.write("<tr><td><table align=center>")
	wfile.write("<tr><td><font size=7>%s</font></td></tr>" % album)
	wfile.write("<tr><td><font size=4>by %s</font></td></tr>" % artist)
	wfile.write("</table></td></tr>")

	img_ar = artist.replace("/", "%2F")
	img_al = album.replace("/", "%2F")
	wfile.write("<tr><td><table align=center width=100%><tr>")
	wfile.write("<td align=center><table align=center width=100%>")
	wfile.write("<tr><td align=center>")
	wfile.write("<img src=\"artwork/%s/%s.jpg\" height=192/></td></tr>" % (img_ar, img_al))
	wfile.write("<tr><td align=center id=\"audio\"></td></tr></table>")
	wfile.write("<td align><table align=left>\n")

	titles = []
	for title, key in library.titles(args["artist"], args["album"]):
		sid = library.song_id(args["artist"], args["album"], key)
		track = library.get_attrs(sid, "track")
		titles += [(track, sid, title)]
	titles.sort()
	tags = "<tr><td align=right>%s.</td><td><font onclick=\"playSong('%s');\">%s</font></td></tr>\n"
	for id, sid, title in titles:
		wfile.write(tags % (id, sid, title))
	wfile.write("</table></td>")
	wfile.write("</tr></table></td></tr>")

	wfile.write("</table>")


def to_html(wfile, args):
	header(wfile)
	body(wfile, args)
	footer(wfile)
