window.onload = makeDoubleDelegate(window.onload, init );

function makeDoubleDelegate(function1, function2) 
{
	return function() 
	{
		if (function1) function1();
		if (function2) function2();
	}
}

function init()
{
	var favSongs=document.getElementById("favorite_songs");
	var div=favSongs.getElementsByTagName("div")[0];
	var textField,i;
	div.style.display="none";
	favSongs.style.border="5px #aaaadd outset";
	findArtistOrSong();
	textField=document.getElementById("find_this");
	textField.value="";
	dropDown();
	listOfFavorites();
}// end init

function findArtistOrSong()
{
	var favSongs=document.getElementById("favorite_songs");
	var text=document.createElement("input");
	text.setAttribute("type","text");
	text.setAttribute("id","find_this");
	text.onkeyup=listOfFavorites;
	favSongs.appendChild(document.createTextNode("Find this: "));
	favSongs.appendChild(text);
}// end find ArtistOrSong

function dropDown()
{
	var favSongs=document.getElementById("favorite_songs");
	var dd=document.createElement("select");
	//var op=document.createElement("option");
	dd.setAttribute("id","select_this");
	//op.setAttribute("value","0");
	//op.appendChild(document.createTextNode("Please select..."));
	dd.onchange=goToSong;
	//dd.appendChild(op);
	favSongs.appendChild(document.createElement("br"));
	favSongs.appendChild(dd);
}// end dropDown

function listOfFavorites()
{
	var select=document.getElementById("select_this");
	var options=select.getElementsByTagName("option");
	var favSongs=document.getElementById("favorite_songs");
	var findThis=document.getElementById("find_this");
	var i,a,option,text,count;
	if (options.length>0)
	{
		for (i=options.length-1; i>=0; i--)
		{
			select.removeChild(options[i]);
		}// end for i
	}// end if
	a=favSongs.getElementsByTagName("a");
	for (i=0; i<a.length; i+=3)
	{
		option=document.createElement("option");
		option.setAttribute("value",a[i].href);
		text="";
		if (a[i+1].getElementsByTagName("b")[0].firstChild!=null)
		{
			text+=a[i+1].getElementsByTagName("b")[0].firstChild.nodeValue;
		}
		text+=" - "+a[i].getElementsByTagName("b")[0].firstChild.nodeValue;
		option.appendChild(document.createTextNode(text));
		if (text.toLowerCase().indexOf(findThis.value.toLowerCase())>=0 || findThis.value=="")
		{
			select.appendChild(option);
		}
	}// end for a
	options=select.getElementsByTagName("option");
	count=options.length;
	option=document.createElement("option");
	option.setAttribute("value","0");
	text="Please select... ("+count+")";
	option.appendChild(document.createTextNode(text));
	if (count>0)
	{
		select.insertBefore(option,options[0]);
		select.value="0";
	}
	else select.appendChild(option);
}// end listOfFavorites

function goToSong()
{
	var select=document.getElementById("select_this");
	location.href=select.value;
}// end goToSong

