
// MacJams Profile Background and Appearance
// v5
// you are free to do anything you wish with this code
// Bill Grundmann http://www.macjams.com/artist/SmokeyVW
//
// allows users to set the background and control appearance of portions of their artist page

// 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, install_mjBackground );

function install_mjBackground()
{
	mjBackground_set_all_transparent();

	mjBackground_set_div_bg("Past30Comments","white");

	var userspec = document.getElementById("mjBackground");
	var words;
	if (userspec == null) {
		words = new Array();
	} else {
		words = userspec.title.split(" ");
	}
	var keyword = "";
	var color;
	var retval;
	for (i=0; i<words.length; i++) {
		if (keyword == "") {
			keyword = words[i];
		} else {
			color = words[i];
			retval = mjBackground_set_div_bg(keyword,color);
			keyword = "";
			if (!retval) {
				break;
			}
		}
	}
	if (keyword != "") {
		mjBackground_warn("in the span code, after '"+keyword+"', no color name or transparent was provided.");
	}
}

function mjBackground_set_div_bg(id,color)
{
	var x0;
	var x1 = null;
	var id2 = ""+id;
	id2 = id2.toLowerCase().replace(/[0-9]+/,"");
	switch (id2) {
		case "pagebackground":
			document.body.style.background = "url(" + color + ")";
			return 1;
		break;
		case "pagebackgroundcolor":
			document.body.style.backgroundColor = color;
			return 1;
		break;
		case "profile":
			x0 = document.getElementById("artist_profile");
		break;

		case "stats":
			x0 = document.getElementById("bio");
			x0 = x0.parentNode;
		break;

		case "bio":
			x0 = document.getElementById("bio");
		break;

		case "songs":
			x0 = document.getElementById("songs");
			x1 = x0.firstChild;
		break;

		case "favoritesongs":
			x0 = document.getElementById("favorite_songs");
			if (!mjBackground_set_bg_by_legend(id2,color)) return 0;
		break;

		case "favoritemacjammers":
			x0 = document.getElementById("favorite_artists");
			x1 = x0.firstChild;
			//if (!mjBackground_set_bg_by_legend(id2,color)) return 0;
		break;

		case "fans":
			x0 = document.getElementById("fans");
			x1 = x0.firstChild;
			//if (!mjBackground_set_bg_by_legend(id2,color)) return 0;
		break;

		case "musicbackground":
		case "musicskills":
		case "musichardware":
		case "musicsoftware":
		case "keywords":
		case "pastcomments":
			return mjBackground_set_bg_by_legend(id2,color);
		break;

		default:
			mjBackground_warn("in the span code, '"+id+"' should be one of these: Profile, Stats, Bio, Songs, FavoriteSongs, FavoriteMacJammers, Fans, MusicBackground, MusicSkills, MusicHardware, MusicSoftware, Keywords, or Past30Comments, followed by a color name or transparent. Transparent is the default except Past30Days is white. You can also set PageBackground to a URL, or set PageBackgroundColor to a color.");
			return 0;
		break;
	}
	if (color == "transparent") {
		x0.style.background = color;
	} else {
		x0.style.backgroundColor = color;
	}
	if (x1 != null) {
		if (color == "transparent") {
			x1.style.background = color;
		} else {
			x1.style.backgroundColor = color;
		}
	}
	return 1;
}

function mjBackground_set_bg_by_legend(id,color)
{
	var x0 = document.getElementsByTagName("legend");
	var t;
	for (var i=0; i<x0.length; i++) {
		// Safari and IE
		t = x0[i].innerText;
		if (typeof t == 'undefined') {
			// Firefox
			t = x0[i].textContent;
		}
		if (t.toLowerCase().replace(/[0-9]+/,"").replace(/ +/g,"").replace(/\(/,"").replace(/\)/,"") == id) {
			if (color == "transparent") {
				x0[i].style.background = color;
				x0[i].parentNode.style.background = color;
			} else {
				x0[i].style.backgroundColor = color;
				x0[i].parentNode.style.backgroundColor = color;
			}
			return 1;
			break;
		}
	}
	if (id == "pastcomments") {
		// OK, if it's not your own page, or you are not logged on
		return 1;
	}
	return 0;
}

function mjBackground_set_all_transparent()
{
	var x0 = document.getElementById("bio");
	x0.parentNode.style.background = "transparent";

	var x1 = document.getElementsByTagName("fieldset");
	mjBackground_set_bg_upwards(x1[0].parentNode.parentNode.parentNode,3);
	for (var i=0; i<x1.length; i++) {
		x1[i].style.background = "transparent";
	}
}

function mjBackground_set_bg_upwards(x1,rec)
{
	x1.style.background = "transparent";
	if (rec <= 0) return;
	x1 = x1.parentNode;
	mjBackground_set_bg_upwards(x1,rec-1);
}

function mjBackground_warn(msg)
{
	mjBackground_msg("Problem in mjBackground: "+msg,"yellow");
}

function mjBackground_alert(msg)
{
	mjBackground_msg(msg,"");
}

function mjBackground_msg(msg,color)
{
	var x0 = document.getElementById("artist_username");

	element=document.createElement("p");
	if (color != "") element.style.backgroundColor = color;
	element.style.fontSize = "8pt";
	element.innerHTML = msg;
	x0.appendChild(element);
}
