/*************************************************
 * Created by Hedde Bosman aka Tex-nd            *
 * Free to copy when metioning my name.          *
 * http://www.breakcore.nl/                      *
 *************************************************/

// ArtistSelect class. displays a <select> style dropdown

function ArtistSelect(name) {
	this.name = name;
	this.artistSelectList = new Array();
	this.artistSelectIndex = -1;
}



ArtistSelect.prototype.getSelected = function() {
	if (this.artistSelectList.length > 0) {
		if (this.artistSelectIndex < 0) {
			return this.artistSelectList[0][1];
		} else {
			return this.artistSelectList[ this.artistSelectIndex ][1];
		}
	}
	return null;
}
ArtistSelect.prototype.getFirst = function() {
	if (this.artistSelectList.length > 0) {
		return this.artistSelectList[0][1];
	}
	return null;
}
ArtistSelect.prototype.get = function(i) {
	if (this.artistSelectList.length > 0 && i < this.artistSelectList.length) {
		return this.artistSelectList[i][1];
	}
	return null;
}
ArtistSelect.prototype.onBlur = function(field, event) {
	setTimeout("setVisibility('"+this.name+"box', 0);", 200);
}
ArtistSelect.prototype.onArtistReceive = function(plain,xml) {
	this.artistSelectList = new Array();
	this.artistSelectIndex = -1;

	var a = xml.getElementsByTagName("artist");
	for (i = 0; i < a.length; i++) {
		var id = a[i].childNodes[0].childNodes[0].nodeValue;
		var artist = a[i].childNodes[1].childNodes[0].nodeValue;
		this.artistSelectList[i] = new Array(id, artist);
	}
	this.draw();
}

ArtistSelect.prototype.selectUp = function() {
	this.artistSelectIndex = (this.artistSelectList.length + this.artistSelectIndex-1);
	this.artistSelectIndex %= this.artistSelectList.length;
	this.draw();
}
ArtistSelect.prototype.selectDown = function () {
	this.artistSelectIndex += 1;
	this.artistSelectIndex %= this.artistSelectList.length;
	this.draw();
}
ArtistSelect.prototype.isVisible = function() {
	var s = getElementStyle(this.name+"box");
	return (s.visibility == "visible");
}
ArtistSelect.prototype.isEmpty = function() {
	return (this.artistSelectList.length == 0);
}

ArtistSelect.prototype.show = function(artist) {
	if (artist.length > 0) {
		// request artist names here
		u = "js/artistselect.php?name="+uEnc(artist);
		getURL(u, this.onArtistReceive, this);
	} else {
		setVisibility(this.name+"box", 0);
	}
}
ArtistSelect.prototype.hide = function() {
	this.artistSelectList = new Array();
	this.artistSelectIndex = -1;
	setVisibility(this.name+"box", 0);
}

/********************************************************
 * DOM modifiers
 */
ArtistSelect.prototype.draw = function () {
	e = document.getElementById(this.name+"box");
	
	est = getElementStyle(this.name+"box");
	
	tableClear(this.name);
	
	for (i = 0; i < this.artistSelectList.length; i++) {
		this.addRow(i, this.artistSelectList[i][1], i == this.artistSelectIndex);
	}

	if (opera) e.className = ""; // opera redraw hack
	if (this.artistSelectList.length > 0) {
		setVisibility(this.name+"box", 1);
		var tmp = e.offsetWidth;
		if (opera) e.className = "selectbox";	 // opera redraw hack
	} else {
		this.artistSelectIndex = -1;
		setVisibility(this.name+"box", 0);
	}
}

ArtistSelect.prototype.addRow = function(id,artist, highlight) {
	var table = document.getElementById(this.name);
	var newItemNumber = table.rows.length;
	var col = document.createElement('td');
	col.className = (highlight ? "blueselect":"");
	// needs refinement
	col.innerHTML = "<a href=\"javascript:addArtist("+id+")\">"+artist+"</a>";
	var row = table.insertRow(newItemNumber);
	row.appendChild(col);
}


