/*
 * ace.js
 * Copyright (C) 2015 tox <tox@rootkit>
 *
 * Distributed under terms of the MIT license.
 */

var RevealAce = window.RevealAce || (function() {
	if(!Reveal.ace) Reveal.ace = {};
	function extend(o) {
		for(var i = 1; i < arguments.length; i++)
			for(var key in arguments[i])
				o[key] = arguments[i][key];
		return o;
	};
	function mkEditor(iframe) {
		var w = iframe.contentWindow, d = w.document;
		d.write("<!DOCTYPE html><html>"+
			"<head>"+
			"<script src='https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.9/ace.js' type='text/javascript' charset='utf-8'></script>"+
			"</head>"+
			"<body>"+
			"<div id='editor' style='position:absolute; left:0; top:0; bottom:0; right:0;'>"+
			iframe.innerHTML+ // innerHTML is already escaped
			"</div>"+
			"</body>"+
			"</html>");
		iframe.onload = function() {
			function slidechanged(event) {
				var e = iframe;
				for(var e = iframe; e && e != event.currentSlide; e = e.parentNode);
				if(!e)
					return;
				iframe.focus();
				editor.focus();
			}
			var editor = w.ace.edit(d.getElementById('editor'));
			var aceConf = extend({}, options, iframe.dataset);
			editor.setOptions({
				fontSize: "16pt"
			});

			// Configuration
			if(aceConf.theme)
				editor.setTheme(aceConf.theme);
			if(aceConf.mode)
				editor.getSession().setMode(aceConf.mode);
			if(aceConf.autoFocus) {
				Reveal.addEventListener('slidechanged', slidechanged);
				slidechanged({ currentSlide: Reveal.getCurrentSlide() })
			}

			// Events
			if(options.oninit)
				options.oninit.call(editor, editor);
			if(iframe.dataset.oninit)
				(new Function("editor", iframe.dataset.oninit)).call(editor, editor);
			if(options.onchange)
				editor.getSession().on('change', options.onchange);
			if(iframe.dataset.onchange) {
				var onchange = new Function("value", "editor", iframe.dataset.onchange);
				editor.getSession().on('change', function() {
					var value = editor.getValue();
					return onchange.call(editor, value, editor);
				});
			}
			if(iframe.id) {
				Reveal.ace[iframe.id] = editor;
			}
		};
		d.close();
	}

	var module = {};
	var config = Reveal.getConfig();
	var options = extend({
		className: "ace",
		autoFocus: false,
		onchange: null,
		oninit: null
	}, config.ace || {});

	var aces = document.getElementsByClassName(options.className);
	for(var i = 0; i < aces.length; i++) {
		if(!aces[i].contentWindow) {
			console.warn("ACE Editors must be embedded in an IFrame");
			continue;
		}
		mkEditor(aces[i]);
	}

	return module;
})();

@@ -408,16 +408,58 @@
 cument;%0A
+%09%09var mode = iframe.getAttribute(%22mode%22);%0A
 %09%09d.writ
@@ -564,11 +564,11 @@
 e/1.
-1.9
+2.6
 /ace
@@ -621,21 +621,194 @@
 script%3E%22
-+%0A%09%09%09
+)%0A%09%09if (mode) %7B%0A%09%09%09d.write(%22%3Cscript src='https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/mode-%22 + mode + %22.js' type='text/javascript' charset='utf-8'%3E%3C/script%3E%22);%0A%09%09%7D%0A%09%09d.write(
 %22%3C/head%3E
@@ -906,16 +906,17 @@
 ht:0;'%3E%22
+ 
 +%0A%09%09%09ifr
@@ -1393,16 +1393,148 @@
 %09%09%09%7D);%0A%0A
+%09%09%09if (mode) %7B%0A%09%09%09%09var fetched_mode = w.ace.require(%22ace/mode/%22 + mode).Mode;%0A%09%09%09%09editor.session.setMode(new fetched_mode());%0A%09%09%09%7D%0A%0A
 %09%09%09// Co

update ace and handle different modes by setting it as an html attr