DetailsPane = function () {};

Object.extend(DetailsPane.prototype, {
	
	internalLinkClickEvent : new YAHOO.util.CustomEvent("InternalLinkClickEvent", this),
	
	loadDetails: function(dbid) {
		var sUrl = '/cgi-bin/entitylevelview/details?DB=' + this.db + "&FOCUS_SPECIES_ID=" + 
			this.focus_species_id + "&ID=" + dbid;
		var details = this.getDetailsDiv();
	    var ol = overlayElementWithTimeIndicator(details, 40);
		var callback = {
			scope: this,
			customevents:{
				onSuccess: function(eventType, args) {
					var o = eval('(' + args[0].responseText + ')');
					this.processDetails(o.details);
					this.current_instance_id = dbid;
				}, 
				onFailure: function(eventType, args) {
					$('details').innerHTML = "<PRE>There was a problem loading the details of instance with DB_ID " +dbid+ ":\n" + args[0].statusText + "</PRE>";
				},
				onComplete: function(eventType, args) {
					if (ol) ol.remove();
				}
			}
		};
		YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
	},
	tweakInternalLinks: function(links) {
	    var re = new RegExp("ID=(\\d+)");
	    for (var i = 0, l = links.length; i < l; ++i) {
			var href = links[i].readAttribute('href');
			if (href) {
		    	var matches = href.match(re);
		    	if (matches) {
					links[i].setAttribute("href","javascript:void(0)");
					Event.observe(links[i], "click", this.fireInternalLinkClickEvent.bind(this, matches[1]));
		    	}
			}
	    }
	},
	processDetails: function(details) {
		//log("processDetails");
	    var ds = this.getDetailsDiv();
	    if (details) {
			var html = decodeURIComponent(details[0]);
			var script = decodeURIComponent(details[1]);
			ds.innerHTML = html.replace(/<script.*?>(\n|\r|.)*?<\/script>/,eval(script));
			var links = ds.getElementsBySelector('a');
			this.tweakInternalLinks(links);
	    } else {
			ds.innerHTML = "";
	    }
	},
	getDetailsDiv: function() {
		return $("details");
	},
	fireInternalLinkClickEvent : function (dbId) {
		this.internalLinkClickEvent.fire(dbId);
	}
});