function Lineage(container)
{
	this.container = container;
	return this;
}

Lineage.prototype.url = "mvlineage.cgi";
Lineage.prototype.linkURL = "http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=";
Lineage.prototype.images = new Object();
Lineage.prototype.index0 = 0;
Lineage.prototype.index1 = 1;

Lineage.prototype.init = function(taxid)
{
	this.taxid = taxid;
	this.preloadImages();
	this.container.innerHTML = "<p class='load'>Loading ...</p>";
	Utils.sendHTTPRequest(this.url, {taxid:this.taxid}, this, this.loadLineage, this.showError);
	return this;
}

Lineage.prototype.shouldHide = function(index)
{
	return index >= this.index0 && index < this.index1;
}

Lineage.prototype.loadLineage = function(root)
{
	var list0 = $element("ul");
	var list = list0;
	var l = null;
	var index = -1;
	for(var n=0;n<root.childNodes.length;n++){
		var node = root.childNodes[n];
		if(node.nodeType == 1){
			if(l){
				list = $element("ul");
				l.appendChild(list);
				l.list = list;
				if(this.shouldHide(index))
					list.style.display = "none";
			}
			index += 1;
			l = $element("li");
			var img = $element("img");
			img.line = l;
			if(this.shouldHide(index)){
				img.src = this.images.arrowRight.src;
				img.title = this.images.arrowRight.title;
			}else{
				img.src = this.images.arrowDown.src;
				img.title = this.images.arrowDown.title;
			}
			if(img.addEventListener)
				img.addEventListener("click", this.toggleLine, false);
			else if(img.attachEvent)
				img.attachEvent("onclick", this.toggleLine);
			l.appendChild(img);
			l.marker = img;
			var anchor = $element("a");
			anchor.href = this.linkURL + node.getAttribute("taxid");
			anchor.appendChild($text(node.getAttribute("name")));
			l.appendChild(anchor);
			list.appendChild(l);
		}
	}
	if(l && l.marker){
		l.style.paddingLeft = "12px";
		l.marker.style.display = "none";
	}
	this.container.innerHTML = "";
	this.container.appendChild(list0);
}

Lineage.prototype.toggleLine = function(eventObj)
{
	if(!eventObj)
		eventObj = event;
	var image = eventObj.target? eventObj.target : eventObj.srcElement;
	if(image.line && image.line.list){
		var list = image.line.list;
		if(list.style.display == "none"){
			list.style.display = "";
			image.src = Lineage.prototype.images.arrowDown.src;
			image.title = Lineage.prototype.images.arrowDown.title;
		}else{
			list.style.display = "none";
			image.src = Lineage.prototype.images.arrowRight.src;
			image.title = Lineage.prototype.images.arrowRight.title;
		}
	}
}

Lineage.prototype.showError = function(err, arg)
{
	var msg;
	switch(err){
		case Utils.ParserError:
			msg = "XML parser error:<br>" + arg;
			break;
		case Utils.LoadError:
			msg = "Load error: " + arg;
			break;
		case Utils.ServerError:
			msg = "Server error: server returned status " + arg;
			break;
		case Utils.RequestError:
			msg = "Request error: " + arg;
			break;
	}
	this.container.innerHTML = "<p class='error'>" + msg + "</p>";
}

Lineage.prototype.preloadImages = function()
{
	var def = [
		['arrowRight', '/projects/mapview/static/images/right-h.png', 'Click to expand'],
		['arrowDown', '/projects/mapview/static/images/down-h.png', 'Click to collapse']
	];
	for(var n=0;n<def.length;n++){
		this.images[def[n][0]] = $element('img');
		this.images[def[n][0]].src = def[n][1];
		this.images[def[n][0]].title = def[n][2];
	}
}
