var annotations=[];
var types2Annotations = {};
var selectedObjects = [];

//Ext.QuickTips.init();
//Ext.apply(Ext.QuickTips, {True:false,interceptTitles: true,autoHide:false,hideDelay:1500,hideOnClick:true});

function addAnnotation(type, obj) {
	var arr = types2Annotations[type];
	if (!arr) {
		arr = [];
		types2Annotations[type] = arr;
	}
	arr[arr.length]=obj;
}

function showAnnotation(type, flag, doc) {
	if (!doc) doc = document;
	var arr = types2Annotations[type];
	if (arr) {
		for (var k = 0; k < arr.length; ++k)
		{
			if (flag) {
				Ext.get(arr[k]).addClass("cls" + type);
				Ext.get(arr[k]).addClass("clsEntityShow");
			} else {
				Ext.get(arr[k]).removeClass("clsEntityShow");
				Ext.get(arr[k]).removeClass("cls" + type);
			}
		}
	}
}

function clearSelection(coll, cls) {
	if (!coll) coll = selectedObjects;
	if (!cls) cls = "clsSelected";
	for (var k = 0; k < coll.length; ++k)
	{
		var aNode = Ext.get(coll[k]);
		aNode.removeClass(cls);
		aNode.addClass("cls" + aNode.dom.getAttribute("type"));
	}
	if (coll == selectedObjects)
		selectedObjects = [];
}

function selectObject(type, objectid, cls, doc) {
	if (!doc) doc = document;
	var result = [];
	var arr = types2Annotations[type];
	if (arr) {
		for (var k = 0; k < arr.length; ++k)
		{
			if (arr[k].getAttribute("entityID") == objectid) {
				var aNode = Ext.get(arr[k]);
				aNode.removeClass("cls" + aNode.dom.getAttribute("type"));
				aNode.addClass(cls);
				selectedObjects[selectedObjects.length] = arr[k];
				result[result.length] = arr[k];
			}
		}
	}
	return result;
}

function isAnnotatedBy(type) {
	return !!types2Annotations[type];
}

function stylizeNode(aNode,a) {
	aNode.setAttribute("id",a.annotationID);
	aNode.setAttribute("entityID",a.entityID);
	aNode.setAttribute("entity",a.entityLabel);
	aNode.setAttribute("type",a.entityType);
	//aNode.style.borderBottom = "#2B2BFF 1px dashed";
	//aNode.style.backgroundColor = "#DDF5FF";
	//aNode.setAttribute("title",a.entityType);
	aNode.setAttribute("class", "cls" + a.entityType);
	aNode.className = "clsEntity cls" + a.entityType;
	
	addAnnotation(a.entityType, aNode);
	var listener = function(ev) {window.top._dispatcher.fireEvent('subjectSelected', {id: a.entityID, label: a.entityLabel, type: a.entityType});};
	if (document.all) {
		aNode.attachEvent("onclick", listener);
	} else {
		aNode.addEventListener("click", listener, false);
	}
	new ontos.widgets.EntityTooltip({target: aNode, entityID:a.entityID, entityLabel:a.entityLabel,entityType:a.entityType});
}

function bootstrapNodes(nds)
{
	for (var i = 0; i < nds.length; ++i)
	{
		var aNode = document.getElementById(nds[i]);
		if (!aNode) continue;
		addAnnotation(aNode.getAttribute("type"), aNode);
		var entityID = aNode.getAttribute("entityID");
		var arg = {id: entityID, label: aNode.getAttribute("entity"), type: aNode.getAttribute("type")};
		Ext.get(nds[i]).on("click", function() {
			window.top._dispatcher.fireEvent('subjectSelected', arguments[2].arg);
			arguments[0].stopEvent();
		}, this, {arg:arg});
		new ontos.widgets.EntityTooltip({target: aNode, entityID:entityID, entityLabel:aNode.getAttribute("entity"),entityType:aNode.getAttribute("type")});
	}
	window.top.loadcomplete();
}
