// FPD Application-specific Javascript Library

var Annotation = {
	generalAjaxFailure: function(transport) {
		$("annotation_errors_text").innerHTML = transport.responseText;
		$("annotation_errors").show();
	},
	
	groupElementId: function(gid, el) {
		return "groups" + "_" + gid + "_" + el;
	},
	featureElementId: function(gid, fid, el) {
		return this.groupElementId(gid, "features") + "_" + fid + "_" + el;
	},
	qualifierElementId: function(gid, fid, qid, el) {
		return this.featureElementId(gid, fid, "qualifiers") + "_" + qid + "_" + el;
	},
	groupVersionElementId: function(gid, version, el) {
		return this.groupElementId(gid, "versions") + "_" + version + "_" + el;
	},
	
	collectParams: function(params) {
		params["fref"] = $F("fref");
	},
	collectGroupParams: function(params, gid) {
		this.collectParams(params);
		params["gid"] = gid;
		params["gclass"] = $F(this.groupElementId(gid, "gclass"));
	},
	collectFeatureParams: function(params, gid, fid) {
		this.collectGroupParams(params, gid);
		params["fid"] = fid;
		params["feature_key"] = $F(this.featureElementId(gid, fid, "feature_key"));
	},
	
	groupChangeMode: function(gid, mode) {
		new Ajax.Updater({success: this.groupElementId(gid, "panel")}, g_cgi_uri + "/_annotation_group.pl", {
			parameters: { gid: gid, mode: mode },
			onFailure: this.generalAjaxFailure
		});
	},
	groupAdd: function() {
		params = {};
		this.collectParams(params);
		var count = this.incrementNewCounter("new_group_count");
		params["gid"] = -1 * count;
		new Ajax.Updater({success: "annotation_groups"}, g_cgi_uri + "/_annotation_group_add.pl", {
			parameters: params,
			insertion: Insertion.Bottom,
			onFailure: this.generalAjaxFailure
		});
	},
	groupSave: function(gid) {
		var params = Form.serialize(this.groupElementId(gid, "form"), true);
		this.collectGroupParams(params, gid);
		params["gid"] = gid;
		new Ajax.Request(g_cgi_uri + "/_annotation_group_save.pl", {
			parameters: params,
			onSuccess: function(transport) {
				$(Annotation.groupElementId(gid, "container")).replace(transport.responseText);
			},
			onFailure: function(transport) {
				$(Annotation.groupElementId(gid, "message")).replace(transport.responseText)
			}
		});
	},
	groupDiscard: function(gid) {
		if (gid > 0) return;
		$(this.groupElementId(gid, "container")).remove();
	},
	groupDelete: function(gid) {
		var answer = confirm("Delete this group?");
		if (!(answer)) return;
		var params = {};
		this.collectGroupParams(params, gid);
		new Ajax.Request(g_cgi_uri + "/_annotation_group_delete.pl", {
			parameters: params,
			onSuccess: function(transport) {
				$(Annotation.groupElementId(gid, "container")).remove();
				var deleted_groups = $("annotation_groups_deleted");
				deleted_groups.innerHTML += transport.responseText;
				deleted_groups.show();
			},
			onFailure: function(transport) {
				$(Annotation.groupElementId(gid, "message")).replace(transport.responseText);
			}
		});
	},
	groupUndelete: function(gid) {
		var answer = confirm("Undelete this group?");
		if (!(answer)) return;
		var params = {};
		this.collectGroupParams(params, gid);
		new Ajax.Request(g_cgi_uri + "/_annotation_group_undelete.pl", {
			parameters: params,
			onSuccess: function(transport) {
				$(Annotation.groupElementId(gid, "container")).remove();
				var groups = $("annotation_groups");
				groups.innerHTML += transport.responseText;
			},
			onFailure: function(transport) {
				$(Annotation.groupElementId(gid, "message")).replace(transport.responseText);
			}
		});
	},
	groupClone: function(gid) {
		var params = {};
		this.collectGroupParams(params, gid);
		var count = this.incrementNewCounter("new_group_count");
		params["new_gid"] = -1 * count;
		new Ajax.Updater({success: "annotation_groups"}, g_cgi_uri + "/_annotation_group_clone.pl", {
			parameters: params,
			insertion: Insertion.Bottom,
			onFailure: this.generalAjaxFailure
		});
	},
	groupStatusChanged: function(status_el, gid) {
		var el = $(this.groupElementId(gid, "panel"));
		if (parseInt(status_el.getValue()) == 1) {
			el.addClassName("annotation_inactive");
		} else {
			el.removeClassName("annotation_inactive");
		}
	},
	groupCollapse: function(gid) {
		$(this.groupElementId(gid, "expand")).show();
		$(this.groupElementId(gid, "collapse")).hide();
		$(this.groupElementId(gid, "details")).hide();
	},
	groupVersionsExpand: function(gid) {
		$(this.groupElementId(gid, "versions_expand")).hide();
		$(this.groupElementId(gid, "versions_collapse")).show();
		$(this.groupElementId(gid, "versions")).show();
	},
	groupVersionsCollapse: function(gid) {
		$(this.groupElementId(gid, "versions_expand")).show();
		$(this.groupElementId(gid, "versions_collapse")).hide();
		$(this.groupElementId(gid, "versions")).hide();
	},
	
	featureAdd: function(gid) {
		var params = {};
		params["feature_key"] = $F(this.groupElementId(gid, "new_feature_key"));
		if (params["feature_key"].length == 0) {
			alert("Select a feature key");
			return;
		}
		var count = this.incrementNewCounter(this.groupElementId(gid, "new_feature_count"));
		params["fid"] = -1 * count;
		this.collectGroupParams(params, gid);
		
		new Ajax.Updater({success: this.groupElementId(gid, "features")}, g_cgi_uri + "/_annotation_feature_add.pl", {
			parameters: params,
			insertion: Insertion.Bottom,
			onFailure: this.generalAjaxFailure
		});
	},
	featureDelete: function(gid, fid) {
		if (fid > 0) this.pushList(this.groupElementId(gid, "deleted_features"), fid);
		$(this.featureElementId(gid, fid, "panel")).remove();
	},
	
	qualifierAdd: function(gid, fid) {
		var params = {};
		params["qualifier_key"] = $F(this.featureElementId(gid, fid, "new_qualifier_key"));
		if (params["qualifier_key"].length == 0) {
			alert("Select a qualifier key");
			return;
		}
		var count = this.incrementNewCounter(this.featureElementId(gid, fid, "new_qualifier_count"));
		params["qid"] = -1* count;
		this.collectFeatureParams(params, gid, fid);
		new Ajax.Updater({success: this.featureElementId(gid, fid, "qualifiers")}, g_cgi_uri + "/_annotation_qualifier_add.pl", {
			parameters: params,
			insertion: Insertion.Bottom,
			onFailure: this.generalAjaxFailure
		});
	},
	qualifierDelete: function(gid, fid, qid) {
		if (qid > 0) this.pushList(this.featureElementId(gid, fid, "deleted_qualifiers"), qid);
		$(this.qualifierElementId(gid, fid, qid, "panel")).remove();
	},
	
	groupVersionExpand: function(gid, version) {
		new Ajax.Request(g_cgi_uri + "/_annotation_group_version.pl", {
			parameters: { gid: gid, version: version },
			onSuccess: function (transport) {
				$(Annotation.groupVersionElementId(gid, version, "panel")).update(transport.responseText);
				$(Annotation.groupVersionElementId(gid, version, "expand")).hide();
				$(Annotation.groupVersionElementId(gid, version, "collapse")).show();
				$(Annotation.groupVersionElementId(gid, version, "panel")).show();
			},
			onFailure: this.generalAjaxFailure
		});
	},
	groupVersionCollapse: function(gid, version) {
		$(this.groupVersionElementId(gid, version, "expand")).show();
		$(this.groupVersionElementId(gid, version, "collapse")).hide();
		$(this.groupVersionElementId(gid, version, "panel")).hide();
	},
	groupVersionRollback: function(gid, version) {
		var answer = confirm("Rollback to version " + version + "?");
		if (!(answer)) return;
		var params = {gid: gid, version: version};
		new Ajax.Request(g_cgi_uri + "/_annotation_group_version_rollback.pl", {
			parameters: params,
			onSuccess: function(transport) {
				$(Annotation.groupElementId(gid, "container")).replace(transport.responseText);
			},
			onFailure: function(transport) {
				$(Annotation.groupElementId(gid, "message")).replace(transport.responseText)
			}
		});
	},
	
	incrementNewCounter: function(el) {
		el = $(el);
		var count = parseInt(el.getValue()) + 1;
		el.value = count;
		return count;
	},
	pushList: function(el, item) {
		el = $(el);
		var val = el.getValue();
		if (val.length > 0) val += ",";
		val += item;
		el.value = val;
		return val;
	}
};

