function chamaileonPluginLoader(config) {
	config = config || {};
	var plugindivs = document.querySelectorAll("[data-chamaileonplugin]");

	var url = config.url || "";

	var pluginIframes = [];

	for (var i = 0; i < plugindivs.length; i = i + 1) {
		var plugindiv = plugindivs[i];
		var dataPluginCode = plugindiv.getAttribute("data-chamaileonplugin");
		var dataPage = plugindiv.getAttribute("data-page");
		var dataLocation = plugindiv.getAttribute("data-location");

		var src = url + "/";

		switch (dataPluginCode) {
			case "subscribe-wide": {
				src = src + "subscribe-wide.html";
				break;
			}
			default: {
				src = src + dataPluginCode;
			}
		}

		if (dataPage) {
			src = src + "?page=" + dataPage;
		}

		if (dataLocation) {
			src = src + "&location=" + dataLocation;
		}

		var iframe = document.createElement("iframe");

		iframe.style.border = "0";
		iframe.style.width = "100%";
		iframe.scrolling = "no";
		iframe.className = "responsive-iframe";

		iframe.setAttribute("src", src);
		plugindiv.appendChild(iframe);
		pluginIframes.push(iframe.contentWindow);
	}


	window.addEventListener("message", handleMessage, false);

	function handleMessage(e) {
		if (pluginIframes.indexOf(e.source) === -1) {
			return;
		}
		
		var data = e.data;

		try {
			data = JSON.parse(data);
		} catch (e) {

		}

		switch (data.action) {
			case "iframeHeight": {
				var sourceFrame = null; // this is the IFRAME which send the postMessage
				var myFrames = document.getElementsByTagName("IFRAME");
				var eventSource = event.source; // event is the event raised by the postMessage
				var eventOrigin = event.origin; // origin domain, e.g. http://example.com

				// detect the source for IFRAMEs with same-origin URL
				for (var i = 0; i < myFrames.length; i += 1) {
					var f = myFrames[i];

					if (f.contentWindow === eventSource || // for absolute URLs
						f.contentWindow === eventSource.parent) { // for relative URLs
						sourceFrame = f;
						break;
					}
				}

				// detect the source for IFRAMEs with cross-origin URL (because accessing/comparing event.source properties is not allowed for cross-origin URL)
				if (sourceFrame === null) {
					for (var i = 0; i < myFrames.length; i += 1) {
						if (myFrames[i].src.indexOf(eventOrigin) === 0) {
							sourceFrame = myFrames[i];
							break;
						}
					}
				}

				sourceFrame.style.height = data.height + "px";

				break;
			}
		}

		// console.log("SUBSCRIBE WIDE EVENT:", data.action);
	}
}
