
// MacJams Album Support
// v11
// you are free to do anything you wish with this code
// Bill Grundmann http://www.macjams.com/artist/SmokeyVW

//
// allow for users to mix and match scripts as they wish
// see http://blog.roberthahn.ca/articles/2007/02/02/how-to-use-window-onload-the-right-way
// each script that directly uses window.onload should instead use this code.
//
function makeDoubleDelegate(function1, function2) {
    return function() {
        if (function1) function1();
        if (function2) function2();
    }
}
window.onload = makeDoubleDelegate(window.onload, formatCustomSongList );

function formatCustomSongList()
{
	var font=document.getElementsByTagName("font");

	var songs,albumName,songName,i,j,start,colHeader,k,hit,tr,tbody;

	var userspec=document.getElementById("albums");
	var lines;
	var totalAlbums=0;
	if (userspec==null) {
		lines=new Array();
	} else {
		// server adds "<br \>" at the end of each line - remove them
		lines = userspec.title.replace(/<br\ \/>/g,"").split(/\n/);
	}

	var highlightsongs=document.getElementById("highlight_songs");
	var hlsongs;
	if (highlightsongs==null) {
		hlsongs=new Array();
	} else {
		hlsongs=(highlightsongs.title).split("|");
	}

	var debug      = 0;
	var latest     = 6;
	var maxheight  = "";
	var imageheight= "100px";
	var imagewidth = "100px";

	var dividerfg  = "#bbbbcc";
	var titlefg    = "#0000ff";
	var titlebg    = "#ddddee";
	var contentbg  = "#eeeeee";
	var highlightbg= "#ffffdd";

	// find the song table
	tbody=null;
	for (i=0; i<font.length; i++) { 
		if ((font[i].color=="green" || font[i].color=="#008000") && font[i].firstChild.nodeValue!=null && font[i].firstChild.nodeValue.toLowerCase()=="active") {
			tr=font[i].parentNode.parentNode;
			tbody=tr.parentNode;
			break;
		}
	}
	// if user has no songs, the table can't be found
	// could try looking harder, perhaps at column headers

	// couldn't find the songlist
	if (tbody==null) return;

	// gather an array of song numbers and their rows and a flag to track if the song got used
	row_array = [];
	rows = tbody.rows;

	// find the real row with headers
	// it's usually at row 0
	// but if newComments() has run, it has been shifted down
	start=1;
	for (j=0; j<15 && j<rows.length; j++) {
		colHeader=rows[j].cells[0].textContent;
		if (typeof colHeader == 'undefined') {
			colHeader=rows[j].cells[0].text;
			if (typeof colHeader == 'undefined') {
				colHeader=rows[j].cells[0].innerText;
				if (typeof colHeader == 'undefined') {
					colHeader="";
				}
			}
		}
		if (colHeader=="Date") {
			start = j+1;
			break;
		}
	}

	// scan through all the data in the songlist
	for (j=start; j<rows.length; j++) {
		if (rows[j].cells.length<2) {
			// ignore unknown stuff in table (a previous run of this script?)
			continue;
		}

		// different data model on different interps...
		songName=rows[j].cells[1].textContent;
		if (typeof songName == 'undefined') {
			songName=rows[j].cells[1].text;
			if (typeof songName == 'undefined') {
				songName=rows[j].cells[1].innerText;
				if (typeof songName == 'undefined') {
					songName="";
				}
			}
		}

		link=""+rows[j].cells[1].getElementsByTagName('a')[0];
		songNumber=link.split("/")[4];
		row_array[row_array.length] = [songNumber,rows[j],0];

		songname=songName.toLowerCase();
		for (k=0; k<hlsongs.length; k++) {
			if (songname.indexOf(hlsongs[k].toLowerCase())>=0) rows[j].cells[1].style.backgroundColor=highlightbg;
		}
	}

	// gather an array of song titles and artists
	collab_array1 = [];
	collab_array2 = [];

	// parse through the user's album specification
	// pass 1 -- obtain custom song names and artist names
	if (userspec.title.indexOf("songtitle")>=0) {
		// performance: don't even bother with this if the songtitle keyword isn't present
		for (i=0; i<lines.length; i++) {

			line = lines[i].replace(/^\s*/,"").replace(/\s*$/,"").replace(/\s+/g," ");
			keyword = line.split(" ")[0];

			if (keyword=="songtitle") {
				//
				// user will add external song
				// save the title
				//
				aaa = line.replace(/songtitle /,"");
				songNumber = ""+aaa.split(" ")[0];
				songName = aaa.substring(songNumber.length,999);
				collab_array1[collab_array1.length] = songNumber;
				collab_array1[collab_array1.length] = songName;
				continue;
			}
			if (keyword=="songartist") {
				//
				// user will add external song
				// save the artist
				//
				aaa = line.replace(/songartist /,"");
				songNumber = ""+aaa.split(" ")[0];
				songArtist = aaa.substring(songNumber.length,999);
				collab_array2[collab_array2.length] = songNumber;
				collab_array2[collab_array2.length] = songArtist;
				continue;
			}
			// skip all else
			// error checking is done during pass 2
		}
	}

	// parse through the user's album specification
	// pass 2
	image = "";
	var description = "";
	for (i=0; i<lines.length; i++) {

		line = lines[i].replace(/^\s*/,"").replace(/\s*$/,"").replace(/\s+/g," ");
		keyword = line.split(" ")[0];

		if (keyword=="image") {
			// save image info to possibly combine with description
			image = line.replace(/image /,"");
			continue;
		}
		if (keyword=="description") {
			// save description info to possibly combine with image
			description = line.replace(/description /,"");
			continue;
		}
		if (image=="") {
			if (description=="") {
				// nothing
			} else {
				// display album description
				// add to the end of the table
				// create a row
				element=document.createElement("tr");
				element.className="mjAlbum";
				element.style.backgroundColor=contentbg;
				td=document.createElement("td");
				td.setAttribute("colspan","9");
				td.colSpan="9";
				td.appendChild(document.createTextNode(description));
				element.appendChild(td);
				tbody.appendChild(element);
				description = "";
			}
		} else {
			if (description=="") {
				// display album cover art
				// add to the end of the table
				// create a row
				element=document.createElement("tr");
				element.className="mjAlbum";
				element.style.backgroundColor=contentbg;
				td=document.createElement("td");
				td.setAttribute("colspan","9");
				td.colSpan="9";
				td.style.backgroundColor=contentbg;
				td.style.backgroundImage="url('"+image+"')";
				td.style.backgroundRepeat="no-repeat";
				td.style.height=imageheight;
				td.style.width=imagewidth;
				element.appendChild(td);
				tbody.appendChild(element);
			} else {
				// display album cover art and description
				// add to the end of the table
				// create a row
				element=document.createElement("tr");
				element.className="mjAlbum";
				element.style.backgroundColor=contentbg;
				td=document.createElement("td");
				td.setAttribute("colspan","3");
				td.colSpan="3";
				td.style.backgroundColor=contentbg;
				td.style.backgroundImage="url('"+image+"')";
				td.style.backgroundRepeat="no-repeat";
				td.style.height=imageheight;
				td.style.width=imagewidth;
				element.appendChild(td);
				td=document.createElement("td");
				td.setAttribute("colspan","6");
				td.colSpan="6";
				td.appendChild(document.createTextNode(description));
				element.appendChild(td);
				tbody.appendChild(element);
				description = "";
			}
			image = "";
		}

		if (keyword=="album") {
			//
			// start a new album
			//
			totalAlbums++;
			albumName = line.replace(/album /,"");

			// fake a divider
			// add to the end of the table
			// create a row
			element=document.createElement("tr");
			element.className="mjAlbum";
			element.style.backgroundColor=dividerfg;
			td=document.createElement("td");
			td.setAttribute("colspan","9");
			td.colSpan="9";
			td.appendChild(document.createTextNode(""));
			element.appendChild(td);
			tbody.appendChild(element);

			// display album title
			// add to the end of the table
			// create a row
			element=document.createElement("tr");
			element.className="mjAlbum";
			element.style.backgroundColor=titlebg;
			element.style.color=titlefg;
			element.style.fontSize="1.35em";
			element.style.fontWeight="bold";
			element.style.fontFamily="comic sans ms";
			td=document.createElement("td");
			td.setAttribute("colspan","9");
			td.colSpan="9";
			td.appendChild(document.createTextNode(albumName));
			element.appendChild(td);
			tbody.appendChild(element);

			continue;
		}
		if (keyword=="songtitle") {
			// already done in pass 1
			continue;
		}
		if (keyword=="songartist") {
			// already done in pass 1
			continue;
		}
		if (keyword=="songs") {
			//
			// display songs within the album
			//
			songs = line.replace(/songs /,"").split(" ");

			for (j=0; j<songs.length; j++) {
				songNumber=songs[j];
				hit=0;
				for (k=0; k<row_array.length; k++) {
					if (row_array[k][0]==songNumber) {
						//
						// found song, move it to the end of the table
						//
						hit=1;
						tbody.appendChild(row_array[k][1]);
						row_array[k][1].style.backgroundColor=contentbg;
						// remember we used this song
						row_array[k][2]=1;
						break;
					}
				}
				if (!hit) {
					//
					// song is not in user's table, reference to external song
					// create a new line for it

					songName=songNumber;
					songArtist="---";
					for (k=0; k<collab_array1.length; k+=2) {
						if (collab_array1[k] == songNumber) {
							songName = collab_array1[k+1];
							break;
						}
					}
					for (k=0; k<collab_array2.length; k+=2) {
						if (collab_array2[k] == songNumber) {
							songArtist = collab_array2[k+1];
							break;
						}
					}

					// start new row
					// create a row
					element=document.createElement("tr");
					element.className="mjAlbum";
					element.style.backgroundColor=contentbg;
					element.style.color="#000000";

					// add each column left to right

					td=document.createElement("td");
					td.appendChild(document.createTextNode("00.00.00"));
					element.appendChild(td);

					//td=document.createElement("td");
					//td.appendChild(document.createTextNode(""));
					//element.appendChild(td);

					td=document.createElement("td");
					link=document.createElement("a");
					link.setAttribute("href","http://macjams.com/song/"+songNumber);
					link.appendChild(document.createTextNode(songName));
					td.appendChild(link);
					element.appendChild(td);

					td=document.createElement("td");
					td.appendChild(document.createTextNode("---"));
					element.appendChild(td);

					td=document.createElement("td");
					td.appendChild(document.createTextNode("---"));
					element.appendChild(td);

					td=document.createElement("td");
					td.appendChild(document.createTextNode(songArtist));
					element.appendChild(td);

					td=document.createElement("td");
					td.appendChild(document.createTextNode("00.00.00"));
					element.appendChild(td);

					td=document.createElement("td");
					td.appendChild(document.createTextNode("-"));
					element.appendChild(td);

					td=document.createElement("td");
					td.appendChild(document.createTextNode(""));
					element.appendChild(td);

					// add this new row to the end of table
					tbody.appendChild(element);
				}
			}
			continue;
		}
		if (keyword=="latest") {
			latest = line.replace(/latest /,"");
			continue;
		}
		if (keyword=="maxheight") {
			maxheight = line.replace(/maxheight /,"");
			continue;
		}
		if (keyword=="hide") {
			var hide = line.replace(/hide /,"");
			var x1 = document.getElementById(hide);
			if (x1!=null) {
				x1.parentNode.removeChild(x1);
			}
			continue;
		}
		if (keyword=="imagewidth") {
			imagewidth = line.replace(/imagewidth /,"");
			continue;
		}
		if (keyword=="imageheight") {
			imageheight = line.replace(/imageheight /,"");
			continue;
		}
		if (keyword=="debug") {
			debug = line.replace(/debug /,"");
			continue;
		}
		if (keyword=="") {
			continue;
		}
		if (1) {
			// create a row
			element=document.createElement("tr");
			element.className="mjAlbum";
			element.style.backgroundColor="#ff0000";
			element.style.color="#ffffff";
			element.style.fontSize="2.0em";
			element.style.fontWeight="bold";
			element.style.fontFamily="comic sans ms";
			td=document.createElement("td");
			td.setAttribute("colspan","9");
			td.colSpan="9";
			td.appendChild(document.createTextNode(line));
			element.appendChild(td);
			tbody.appendChild(element);
		}
	}

	// in case there is left-over info, add it at the end
	if (image=="") {
		if (description=="") {
			// nothing
		} else {
			// display album description
			// add to the end of the table
			// create a row
			element=document.createElement("tr");
			element.className="mjAlbum";
			element.style.backgroundColor=contentbg;
			td=document.createElement("td");
			td.setAttribute("colspan","9");
			td.colSpan="9";
			td.appendChild(document.createTextNode(description));
			element.appendChild(td);
			tbody.appendChild(element);
			description = "";
		}
	} else {
		if (description=="") {
			// display album cover art
			// add to the end of the table
			// create a row
			element=document.createElement("tr");
			element.className="mjAlbum";
			element.style.backgroundColor=contentbg;
			td=document.createElement("td");
			td.setAttribute("colspan","9");
			td.colSpan="9";
			td.style.backgroundColor=contentbg;
			td.style.backgroundImage="url('"+image+"')";
			td.style.backgroundRepeat="no-repeat";
			td.style.height=imageheight;
			td.style.width=imagewidth;
			element.appendChild(td);
			tbody.appendChild(element);
		} else {
			// display album cover art and description
			// add to the end of the table
			// create a row
			element=document.createElement("tr");
			element.className="mjAlbum";
			element.style.backgroundColor=contentbg;
			td=document.createElement("td");
			td.setAttribute("colspan","3");
			td.colSpan="3";
			td.style.backgroundColor=contentbg;
			td.style.backgroundImage="url('"+image+"')";
			td.style.backgroundRepeat="no-repeat";
			td.style.height=imageheight;
			td.style.width=imagewidth;
			element.appendChild(td);
			td=document.createElement("td");
			td.setAttribute("colspan","6");
			td.colSpan="6";
			td.appendChild(document.createTextNode(description));
			element.appendChild(td);
			tbody.appendChild(element);
			description = "";
		}
		image = "";
	}

	if (totalAlbums>0) {
		// fake a final divider
		// add to the end of the table
		// create a row
		element=document.createElement("tr");
		element.className="mjAlbum";
		element.style.backgroundColor=dividerfg;
		td=document.createElement("td");
		td.setAttribute("colspan","9");
		td.colSpan="9";
		td.appendChild(document.createTextNode(""));
		element.appendChild(td);
		tbody.appendChild(element);
	}

	// unaccounted for songs remain at the top of the table
	// if there are more than 'latest' the older ones drop to the bottom of the list
	// it's possible none will appear if recent ones all went into albums
	for (k=latest; k<row_array.length; k++) {
		if (row_array[k][2]==0) {
			tbody.appendChild(row_array[k][1]);
		}
	}

	delete row_array;
	delete collab_array1;
	delete collab_array2;

	if (maxheight != "") {
		var x0 = document.getElementById("songs");
		if (typeof x0.children != 'undefined') {
			if (typeof x0.children[1] != 'undefined') {
				//old safari?
				x0.children[1].style.maxHeight = maxheight;
			}
		} else if (typeof x0.childNodes != 'undefined') {
			if (typeof x0.childNodes[2] != 'undefined') {
				//safari firefox
				x0.childNodes[2].style.maxHeight = maxheight;
			} else if (typeof x0.childNodes[1] != 'undefined') {
				// ie7
				x0.childNodes[1].style.maxHeight = maxheight;
			}
		}
	}
}
