 		else
 			css_href = document.location.origin + url
 
+		# console.info("CSSLoader: Loading:", css_href)
+
 		# console.error "parsing location", url, CUI.getPathToScript(), css_href
 
 		cssNode = CUI.dom.element "LINK",
 			rel: "stylesheet"
 			charset: "utf-8"
 			name: @__cssName
-			loading: "1"
-			theme: name
+			"data-cui-loading": "1"
+			"data-cui-theme": opts.theme
+			"data-cui-url": opts.url
 			href: css_href
 
-
-		CUI.dom.data(cssNode, "css-loader-deferred", dfr)
-
 		dfr.always =>
-			CUI.dom.removeData(cssNode, "css-loader-deferred")
-			CUI.dom.removeAttribute(cssNode, "loading")
+			CUI.dom.removeAttribute(cssNode, "data-cui-loading")
 
 		dfr.fail (css_href) =>
 			console.error("CSSLoader: Loading failed, removing node.", css_href)

 * https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###


class CUI.CSSLoader extends CUI.Element
	initOpts: ->
		super()
		@addOpts
			group:
				mandatory: true
				default: "main"
				check: String

	readOpts: ->
		super()
		@__cssName = "cui-css-"+@_group
		@__themes = {}

	__getCSSNodes: ->
		CUI.dom.matchSelector(document.documentElement, "link[name=\""+@__cssName+"\"]")

	getActiveTheme: ->
		for cssNode in @__getCSSNodes()
			if not CUI.dom.getAttribute(cssNode, "loading")
				name = CUI.dom.getAttribute(cssNode, "theme")
				active_theme = @__themes[name]
				break

		if active_theme
			return active_theme

		return null

	getThemes: ->
		@__themes

	registerTheme: (_opts) ->
		opts = CUI.Element.readOpts _opts, "CSS.registerTheme",
			name:
				mandatory: true
				check: String
			url:
				mandatory: true
				check: String

		@__themes[opts.name] = opts

	forceReloadTheme: ->
		theme = @getActiveTheme()
		if not active_theme
			return CUI.rejectedPromise()

		@loadTheme(theme, true)


	loadTheme: (name, overload_url = null) ->
		url = @__themes[name]?.url
		CUI.util.assert(url, "CSSLoader.loadTheme", "Theme not found.", name: name, themes: @__themes)

		if overload_url
			url = overload_url

		# console.error "nodes:", @__getCSSNodes()

		for oldCssNode in @__getCSSNodes()
			same_theme = CUI.dom.getAttribute(oldCssNode, "theme") == name
			same_url = CUI.dom.getAttribute(oldCssNode, "href") == url
			is_loading = CUI.dom.getAttribute(oldCssNode, "loading")

			# console.debug same_theme, same_url, is_loading

			if is_loading
				loader_deferred = CUI.dom.data(oldCssNode, "css-loader-deferred")
				if same_theme and same_url
					console.warn("CSSLoader.loadTheme:", name, ". Theme already loading, returning Promise.")
					return loader_deferred.promise()

				# console.warn("CSSLoader.loadTheme:", name, ". Theme still loading, but a different one, aborting other load.")
				# reject loading, this removed the old node
				load_deferred.reject()
			else
				if same_theme and same_url
					# all good
					# console.warn("CSSLoader.loadTheme:", name, ". Theme already loaded.")
					return CUI.resolvedPromise()

				# in all other cases we simply "overload"

		# console.info("CSSLoader: Loading:", url)

		dfr = new CUI.Deferred()


						CUI.dom.remove(css_node)
						old_css_nodes.push(css_node)

				CUI.dom.setAttribute(document.body, "data-cui-theme", opts.theme)

				# console.info("CSSLoader.loadTheme: Loading went fine: ", url, "Removing the old CSS node: ",  old_css_nodes)
				CUI.Events.trigger
