module.exports = function () {

	var DEFAULT_STATE = false;

	var filter = {},
		nodes,
		properties,
		enabled = DEFAULT_STATE,
		filteredNodes,
		filteredProperties;


	filter.filter = function (untouchedNodes, untouchedProperties) {
		nodes = untouchedNodes;
		properties = untouchedProperties;

		var externalElements = filterExternalElements(nodes.concat(properties));

		if (enabled) {
			setColorsForExternals(externalElements);
		} else {
			resetBackgroundColors(externalElements);
		}

		filteredNodes = nodes;
		filteredProperties = properties;
	};

	function filterExternalElements(nodes) {
		return nodes.filter(function (node) {
			return node.visualAttributes().indexOf("external") >= 0;
		});
	}

	function setColorsForExternals(elements) {
		var iriMap = mapExternalsToBaseUri(elements);
		var entries = iriMap.entries();

		var colorScale = d3.scale.category10()
			.domain(iriMap.keys());

		if (entries.length > colorScale.range().length) {
			resetBackgroundColors();
			return;
		}

		for (var i = 0; i < entries.length; i++) {
			var baseIri = entries[i].key;
			var groupedElements = entries[i].value;

			setBackgroundColorForNodes(groupedElements, colorScale(baseIri));
		}
	}

	function mapExternalsToBaseUri(elements) {
		var map = d3.map();

		elements.forEach(function (element) {
			var baseIri = element.baseIri();

			if (!map.has(baseIri)) {
				map.set(baseIri, []);
			}
			map.get(baseIri).push(element);
		});

		return map;
	}

	function setBackgroundColorForNodes(elements, backgroundColor) {
		elements.forEach(function (element) {
			element.backgroundColor(backgroundColor);
		});
	}

	function resetBackgroundColors(elements) {
		elements.forEach(function (element) {
			element.backgroundColor(null);
		});
	}


	filter.enabled = function (p) {
		if (!arguments.length) return enabled;
		enabled = p;
		return filter;
	};

	filter.reset = function () {
		enabled = DEFAULT_STATE;
	};


	// Functions a filter must have
	filter.filteredNodes = function () {
		return filteredNodes;
	};

	filter.filteredProperties = function () {
		return filteredProperties;
	};


	return filter;
};

@@ -52,16 +52,106 @@
 = false;
+%0A%09var COLOR_RANGE = %5Bd3.rgb(%22#3366CC%22), d3.rgb(%22#EE2867%22)%5D; // taken from Linked Data VOWL
 %0A%0A%09var f
@@ -955,18 +955,14 @@
 ale.
-category10
+linear
 ()%0A%09
@@ -975,119 +975,91 @@
 ain(
-iriMap.keys());%0A%0A%09%09if (entries.length %3E colorScale.range().length) %7B%0A%09%09%09resetBackgroundColors();%0A%09%09%09return;%0A%09%09%7D
+%5B0, entries.length - 1%5D)%0A%09%09%09.range(COLOR_RANGE)%0A%09%09%09.interpolate(d3.interpolateHsl);
 %0A%0A%09%09
@@ -1105,41 +1105,8 @@
 ) %7B%0A
-%09%09%09var baseIri = entries%5Bi%5D.key;%0A
 %09%09%09v
@@ -1144,17 +1144,16 @@
 .value;%0A
-%0A
 %09%09%09setBa
@@ -1202,22 +1202,16 @@
 orScale(
-baseIr
 i));%0A%09%09%7D

Use other color range for coloring externals