.
[hacks/simpleWebSlides.git] / simpleWebSlides.js
index 3b96882..59c5a3f 100644 (file)
@@ -44,7 +44,12 @@ SWS.Utils = new function () {
         return result;
     };
 
-
+    self.getParameterByName = function (name) {
+        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
+        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
+        results = regex.exec(location.search);
+        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
+    }
 };
 
 
@@ -52,13 +57,45 @@ SWS.Utils = new function () {
 
 SWS.Templates = new function () {
     var self = this;
-    self.controlPanel = "<div id='sws-control-panel'>\
-<a onclick='SWS.Presentation.goToSlide(SWS.Presentation.firstSlide());'>◀◀◀</a>\
-<a onclick='SWS.Presentation.previousSlide();SWS.Presentation.refresh();'>◀◀ </a>\
-<a onclick='SWS.Presentation.previous();SWS.Presentation.refresh();'>◀</a>\
-<a onclick='SWS.Presentation.next();SWS.Presentation.refresh();'>▶</a>\
-<a onclick='SWS.Presentation.nextSlide();SWS.Presentation.refresh();'>▶▶</a>\
-<a rel='Last slide' onclick='SWS.Presentation.goToSlide(SWS.Presentation.lastSlide());'>▶▶▶</a>\
+    self.helpPanel = "<div id='sws-help-panel-canvas'>\
+<h1>Keyboard shortcuts</h1>\
+<table>\
+<tr ><td style='color:#f55;'>h</td><td style='color:#f55;'> toggle help</td></tr>\
+<tr><td>c</td><td> toggle the control panel</td></tr>\
+<tr><td>Left, PgUp,swipe left</td><td> previous step</td></tr>\
+<tr><td>Right, PgDown, Space, swipe right</td><td> next step</td></tr>\
+<tr><td>p</td><td> previous slide</td></tr>\
+<tr><td>n</td><td> next slide</td></tr>\
+<tr><td>Home</td><td> first slide</td></tr>\
+<tr><td>End</td><td> last slide</td></tr>\
+</table>\
+</div>";
+    self.controlPanel = "<div id='sws-control-panel-canvas'><div id='sws-control-panel'>\
+<div id='sws-control-panel-title-bar'>\
+<a title='Toggle fullscreen' id='sws-control-panel-fullscreen' class='sws-symbol' onclick='SWS.Presentation.toggleFullScreen();'></a>\
+<a title='Close panel' id='sws-control-panel-close' onclick='$(\"#sws-control-panel-canvas\").toggle();'>&#x2716;</a>\
+</div>\
+<div id='sws-control-panel-options'>\
+<span title='Change the aspect ratio' class='sws-symbol' >&#x1f4bb;</span><select id='sws-aspect-select' onchange='SWS.Presentation.changeAspect();'>\
+<option value='sws-aspect-4-3' selected='selected'>4:3</option>\
+<option value='sws-aspect-16-9'>16:9</option>\
+<option value='sws-aspect-16-10'>16:10</option>\
+</select>\
+<span title='Change the theme' class='sws-symbol'>&#x1f3a8;</span><select id='sws-theme-select' onchange='SWS.Presentation.changeTheme();'></select>\
+<a onclick='SWS.Presentation.openPrint()' ><span title='Open Print-Out' class='sws-symbol'>&#59158;</span></a>\
+</div>\
+<div id='sws-control-panel-navigation'>\
+<a title='First slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.firstSlide());' >&#x23ee;</a>\
+<a title='Previous slide' class='sws-symbol' onclick='SWS.Presentation.previousSlide();SWS.Presentation.refresh();'>&#x23ea;</a>\
+<a title='Previous step' class='sws-symbol' style='-webkit-transform: scaleX(-1);' onclick='SWS.Presentation.previous();SWS.Presentation.refresh();'>&#x25b6;</a>\
+<a title='Next step' class='sws-symbol' onclick='SWS.Presentation.next();SWS.Presentation.refresh();'>&#x25b6;</a>\
+<a title='Next slide' class='sws-symbol' onclick='SWS.Presentation.nextSlide();SWS.Presentation.refresh();'>&#x23e9;</a>\
+<a title='Last slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.lastSlide());'>&#x23ed;</a>\
+<br/>\
+<span class='sws-symbol'>&#xe4ae;</span><input type='text' id='sws-control-panel-slide-input' oninput='SWS.Presentation.goToSlide($(\"#sws-control-panel-slide-input\").val()-1);'></input><span id='sws-control-panel-total-slides'></span>\
+<input type='range' title='Navigate the presentation' id='sws-control-panel-navigation-bar' onchange='SWS.Presentation.navigate();' step='1'></input>\
+</div>\
+</div>\
 </div>";
     self.slideActivate = function (o) {
         if (!(o.hasClass("sws-active-slide"))){
@@ -81,7 +118,6 @@ SWS.Templates = new function () {
     self.objectActivate = function (o) {
         if (!(o.hasClass("sws-active-object"))){
             o.removeClass("sws-inactive-object").addClass("sws-active-object");
-            o.css({'visibility':'visible'});
             return true;
         };
         return false;
@@ -127,10 +163,14 @@ SWS.Config = new SWS.ConfigBuilder ();
 
 SWS.Effects = new function () {
     var self = this;
-
+    
     self.objectDeactivateFadeOut = function (o) {
+       if (o.is("embed")) return;
         o.animate({'opacity': '0'}, 200,
-                  function () { SWS.Templates.objectDeactivate(o)});
+                  function () {
+
+                      SWS.Templates.objectDeactivate(o);
+                  });
     };
 
     self.objectActivateFadeIn = function (o) {
@@ -168,13 +208,19 @@ SWS.Effects = new function () {
         var t = SWS.Presentation.getSlide(to);
         if (from < to) {
             t.css('left', '100%');
+            t.css('opacity', '1');
             SWS.Templates.slideActivate(t);
-            f.animate({ 'left': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
-            t.animate({ 'left': '0%' }, 250);
+            f.animate({ 'left': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f);
+                                                              f.css('opacity', '0');
+                                                              t.animate({ 'left': '0%' }, 250);
+                                                            });
         } else {
             t.css('left', '-100%');
             SWS.Templates.slideActivate(t);
-            f.animate({ 'left': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
+            f.animate({ 'left': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f);
+                                                             f.css('opacity', '0');
+                                                           });
+            t.css('opacity', '1');
             t.animate({ 'left': '0%' }, 250);
         };
     };
@@ -198,6 +244,43 @@ SWS.Effects = new function () {
 
 };
 
+SWS.Fullscreen = new function () {
+    var self = this;
+
+    if (SWS.Utils.isUndefined(document.fullScreen)) {
+        if (SWS.Utils.isUndefined(document.mozfullScreen)) {
+            self.status = function () { return document.webkitIsFullScreen; };
+            self.enter = function(e) {
+                e.webkitRequestFullScreen();
+            };
+            self.exit = function () {
+                document.webkitCancelFullScreen();
+            };
+
+        } else {
+            self.status = function () { return document.mozfullScreen; };
+            self.enter = function(e) {
+                e.mozRequestFullScreen();
+            };
+            self.exit = function () {
+                document.mozCancelFullScreen();
+            };
+
+        };
+    } else {
+            self.status = function () { return document.fullScreen; };
+            self.enter = function(e) {
+                e.requestFullScreen();
+            };
+            self.exit = function () {
+                document.cancelFullScreen();
+            };
+
+    };
+
+
+};
+
 SWS.Presentation = new function () {
 
 
@@ -209,9 +292,10 @@ SWS.Presentation = new function () {
     var _total_slides;
     var _initialized = false;
     var _disable_input_events = false;
-
+    var _print_mode = false;
     var _slide_callbacks = new Array ();
-
+    var _total_steps = -1;
+    var _current_theme = "";
 
     self.getNumSlides = function () { return _total_slides; };
 
@@ -223,9 +307,9 @@ SWS.Presentation = new function () {
         if (_initialized) return;
         //jQuery does not seem to work well
         //on a partial DOM
-
-        var slide_num = $(".sws-slide").length - 1;
-
+        var slides = $(".sws-slide");
+        var h1s = $("body").children("h1");
+        var slide_num = slides.add(h1s).length - 1;
         SWS.Utils.push2(_slide_callbacks, slide_num,{ 'fn': f, 'frame': i });
 
     };
@@ -245,11 +329,18 @@ SWS.Presentation = new function () {
             sessionStorage.setItem("current_slide", i);
         };
 
+       self.showHelpAtStartup = function () {
+           var r = sessionStorage.getItem("show_help");
+           if (r == "hide") return false;
+           sessionStorage.setItem("show_help", "hide");
+           return true;
+       };
+
     } else {
         var _current_slide = 0;
         self.getCurrentSlide = function () { return _current_slide; };
         self.setCurrentSlide = function (i) { _current_slide = i; };
-
+       self.showHelpAtStartup = function () { return false; };
     };
     self.firstSlide = function () { return 0; };
     self.lastSlide = function () { return self.getNumSlides() - 1; };
@@ -265,6 +356,7 @@ SWS.Presentation = new function () {
         var from_slide = $(canvas[from_slide_num]);
         var slide_change = (from_slide_num != to_slide_num);
 
+
         var info = to_slide.data("sws-frame-info");
         SWS.Config['sws-update-header'](to_slide);
         SWS.Config['sws-update-footer'](to_slide);
@@ -282,8 +374,8 @@ SWS.Presentation = new function () {
         var cur = info.current;
         var custom = info.custom;
         var real_slide = to_slide.find(".sws-slide");
-
-        real_slide.find("*").andSelf().each(function (i){
+        var dont_touch = real_slide.find("sws-protect").find("*").addBack();
+        real_slide.find("*").addBack().not(dont_touch).each(function (i){
             var frameset = $(this).data("sws-frame-set") || {};
             if (frameset[cur])
                 SWS.Config['sws-object-activate']($(this));
@@ -291,15 +383,20 @@ SWS.Presentation = new function () {
                 SWS.Config['sws-object-deactivate']($(this));
         });
 
-        var callbacks;
-        if (callbacks = info.callbacks.at_frame[self.getCurrentFrame()]){
-            for (var k = 0; k < callbacks.length; k++)
-                callbacks[k]($(to_slide));
-        };
 
         var all = $(from_slide).add(to_slide);
         all.find("*").addBack().promise().done(function() {
-            _disable_input_events = false;
+            var callbacks;
+            //execute callbacks when all elements are finished transitioning
+            if (callbacks = info.callbacks.at_frame[self.getCurrentFrame()]){
+                for (var k = 0; k < callbacks.length; k++)
+                    callbacks[k]($(to_slide));
+            };
+            all.find("*").addBack().promise().done(function() {
+                //wait for all elements to finish transitionning, in case a callback animate something
+                //and enable _input_events again.
+                _disable_input_events = false;
+            });
         });
     };
 
@@ -322,6 +419,18 @@ SWS.Presentation = new function () {
         var infos = canvas.data("sws-frame-info");
         return infos;
     };
+
+    self.getTotalSteps = function () {
+        if (_total_steps >= 0) return _total_steps;
+        _total_steps = 0;
+        $(".sws-canvas").each(function(i) {
+            var canvas = $($(".sws-canvas")[i]);
+            var infos = canvas.data("sws-frame-info");
+            _total_steps += infos.last + 1;
+        });
+        return _total_steps;
+    };
+
     self.getCurrentFrame = function () { return self.getFrameInfo().current; };
     self.setCurrentFrame = function (i) { self.getFrameInfo().current = i; };
     self.firstFrame = function () { return 0; };
@@ -374,9 +483,129 @@ SWS.Presentation = new function () {
         styles[j].disabled = false;
     };
 
+
+    self.printMode = function () {
+        _print_mode = true;
+
+       var old_fx_status = $.fx.off;
+       //disable animation while printing.
+
+       $.fx.off = true;
+        var progress = $("<div style='position:fixed;top:0pt;left:0pt;background:white;color:black;width:100%;height:100vh;z-index:200;' id='sws-print-progress'>Rendering presentation: <span id='sws-percent-progress'></span>%</div>");
+        $("body").append(progress);
+
+        $("html").removeClass("sws-display").addClass("sws-print");
+        self.goToSlide(0,0);
+        var steps = self.getTotalSteps();
+        var total_steps = steps;
+        var loop;
+        loop = function () {
+            if (steps >= 0) {
+                //Crazy workaround for chromium
+                ($("link.sws-theme[rel='stylesheet']")[0]).disabled = false;
+                $(".sws-canvas").find("*").addBack().promise().done(function() {
+                    var percent = ((total_steps - steps) / total_steps) * 100;
+                    $("#sws-percent-progress").text(Math.round(percent));
+                    SWS.Config['sws-slide-change'] = SWS.Templates.slideChange;
+                    self.refresh();
+                    $($(".sws-canvas")[self.getCurrentSlide()]).css('opacity', 1 );
+                    self.next();
+                    steps--;
+                    loop();
+                    })
+            } else {
+                $("#sws-percent-progress").text(100);
+                progress.remove();
+               window.status = 'Ready';
+               $.fx.off = old_fx_status;
+
+            }
+        };
+        loop();
+
+    }
+
+    self.buildFullTOC = function () {
+
+        var build_sections = function (doc) {
+            var res = [];
+            var h1s = doc.find("body").first().children("h1");
+            var slides = doc.find("body").first().children(".sws-slide");
+            var slide_num = 1;
+            var collection = h1s.add(slides);
+            collection.each (function () {
+                if ($(this).is("h1")) {
+                    res.push({ 'title' : $(this).text(),
+                               'slide' : slide_num });
+                } else {
+                    slide_num++;
+                }
+            });
+            return res;
+        };
+
+        var toc = [];
+
+        var append = function (a,e) {
+            return a.push(e);
+        };
+        var prepend = function (a,e) {
+            return a.unshift(e);
+        };
+
+        var loop = function (doc, dir, add, ignoreFirst) {
+            if (ignoreFirst !== true) {
+                var this_toc = { 'title' : doc.find("title").first().text()
+                                .replace ("&", "&amp;")
+                                .replace("'","&apos;")
+                                .replace('"', "&quot;")
+                                .replace("<", "&lt;")
+                                .replace(">", "&gt;"),
+                                 'sections' : build_sections(doc) };
+                add(toc, this_toc);
+            };
+            var url = doc.find(dir).first().attr("href");
+            if (!SWS.Utils.isUndefined(url) && url != "") {
+                $.ajax({ 'url' : url, 'async' : false ,'success' : function (page) {
+                    loop ($(page), dir, add, false);
+                }});
+            };
+        };
+        loop ($(document), ".sws-previous", prepend, false);
+        return toc;
+
+    };
+
+
+
+    var _startTouch = null;
+
     self.inputHandler = function (event) {
-        if (_disable_input_events) return;
-        switch (event.which) {
+        if (_disable_input_events || _print_mode) return;
+        var code = 0;
+        switch (event.type) {
+        case 'touchstart':
+           _startTouch = event.changedTouches[0];
+            return;
+        case 'touchend':
+           if (!_startTouch) return;
+           var _endTouch = event.changedTouches[0];
+           var Xdist = _endTouch.clientX - _startTouch.clientX;
+           var Ydist = _endTouch.clientY - _startTouch.clientY;
+           if (Xdist > 40) code = 39
+           else if (Xdist < -40) code = 37
+            else if (Ydist > 20 && !$("#sws-control-panel-canvas").is(":visible")) code = 67;
+           else if (Ydist < -20  && $("#sws-control-panel-canvas").is(":visible")) code = 67;
+           else code = 39;
+            break;
+        case 'keydown':
+            code = event.which;
+            break;
+        default:
+            return;
+        };
+
+        switch (code) {
         case 36:/* Home */
             self.setCurrentSlide(self.firstSlide());
             break;
@@ -384,34 +613,41 @@ SWS.Presentation = new function () {
         case 35:/* End */
             self.setCurrentSlide(self.lastSlide());
             break;
-
         case 32: /* space */
+        case 34: /* PgDown */
         case 39: /* -> */
-
+       case 176: /* Multimedia skip forward */
+       case 179: /* Multimedia play/pause */
+            if (self.getCurrentSlide() == self.lastSlide()
+                && self.getCurrentFrame() == self.lastFrame()) return;
             self.next();
             break;
-        case 34: /* PgDown */
         case 78: /* n */
             self.nextSlide();
             break;
         case 8: /* backspace */
+        case 33: /* PgUp */
         case 37: /* <-   */
+       case 177: /* Multimedia skip backward */
             self.previous();
             break;
-        case 33: /* PgUp */
         case 80: /* p */
             self.previousSlide();
             break;
         case 83: /* s */
-            self.cycleStyle();
+                self.cycleStyle();
             return;
         case 67: /* c */
-            $("#sws-control-panel").toggle();
+            $("#sws-control-panel-canvas").toggle();
+           return;
+        case 72: /* h */
+            $("#sws-help-panel-canvas").toggle();
+
         default:
             return;
         };
         self.refresh();
-    };
+};
 
 
 
@@ -444,7 +680,7 @@ SWS.Presentation = new function () {
             }
         };
 
-        var specials = null;
+        var specials = $([]);
 
         slide.find('*[class*="sws-onframe-"]').each(function(_){
             var cls = $(this).attr('class');
@@ -457,12 +693,10 @@ SWS.Presentation = new function () {
                 for(var f in o)
                     if (f > last_frame) last_frame = f;
                 $(this).find("*").andSelf().each(function(_){
-                    if (!SWS.Utils.isEmpty(o))
+                    if (!SWS.Utils.isEmpty(o)){
                         $(this).data("sws-frame-set", o);
-                    if (specials)
-                        specials.add($(this));
-                    else
-                        specials = $(this);
+                    }
+                    specials = specials.add($(this));
                 });
             };
         });
@@ -477,55 +711,183 @@ SWS.Presentation = new function () {
         });
 
         canvas.data("sws-frame-info", { current: 0,
-                                        last: last_frame,
+                                        last: (last_frame - 0), // force cast to integer
                                         callbacks: callbacks
                                       });
 
     };
 
-    self.init = function () {
-        console.log("inited");
-        $(window).bind('storage', function (e) {
-            console.log(e);
+    /* Forces redrawing the page without reloading */
+    self.redraw = function (f) {
+        if (SWS.Utils.isUndefined(f))
+            $("body").hide().show(400, function () {
+                $("body").css("display","block");
+                if (!SWS.Utils.isUndefined(f))
+                    f();
+            });
+    };
+    self.changeAspect = function() {
+       if (_print_mode) return;
+       var newClass = $("#sws-aspect-select").val();
+       var args = newClass.split("-");
+       var targetRatio = (args[2] - 0) / (args[3] - 0);
+       var realRatio = window.innerWidth / window.innerHeight;
+       var byClass = (targetRatio > realRatio ) ? "sws-by-height" : "sws-by-width";
+       if ($("html").hasClass(newClass)
+           && $("html").hasClass(byClass))
+           return;
+
+        $("html").removeClass("sws-aspect-4-3")
+            .removeClass("sws-aspect-16-9")
+            .removeClass("sws-aspect-16-10")
+           .removeClass("sws-by-width")
+           .removeClass("sws-by-height")
+            .addClass(newClass).addClass(byClass);
+        self.redraw();
+    };
+
+    self.getCurrentTheme = function () {
+        var l = $("link.sws-theme[rel='stylesheet']")[0];
+
+        if (l) {
+            return  l.title;
+        } else
+            return ""
+    };
+
+    self.changeTheme = function (name) {
+        var theme_name;
+        if (typeof name === 'undefined')
+            theme_name = $("#sws-theme-select").val()
+        else
+            theme_name = name;
+
+        _current_theme = theme_name;
+        $("link.sws-theme").each (function (i) {
+            var e = this;
+            var title =  e.title;
+            if (title == theme_name) {
+                e.rel = "stylesheet";
+                e.disabled = false;
+                e.media="all";
+            } else {
+                e.rel = "alternate stylesheet";
+                e.disabled = true;
+                e.media="all";
+            };
         });
+        self.redraw();
 
-        _total_slides = $(".sws-slide").length;
+    };
+
+    self.openPrint = function () {
+        window.open ("?mode=print&theme=" + self.getCurrentTheme());
+    }
+    var _fullscreen_icon_on = "&#xe746;";
+    var _fullscreen_icon_off = "&#xe744;";
+
+    self.toggleFullScreen = function () {
+        if (SWS.Fullscreen.status()) {
+            SWS.Fullscreen.exit();
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_off);
+
+
+
+        } else {
+            SWS.Fullscreen.enter($("body")[0]);
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_on);
+        };
+    };
+    function _update_ui() {
+        var nav = $('#sws-control-panel-navigation-bar');
+        nav.val(SWS.Presentation.getCurrentSlide() + 1);
+        $('#sws-control-panel-slide-input').val(nav.val());
+    }
+    self.navigate = function () {
+        self.goToSlide($("#sws-control-panel-navigation-bar").val()-1);
+        _update_ui();
+    };
+
+
+    self.init = function () {
+
+
+        $("html").addClass("sws-display");
+        //$(window).resize(self.redraw);
+
+        var slides = $(".sws-slide");
+        var h1s = $("body").children("h1");
+        var slide_num = slides.add(h1s).length - 1;
+
+        _total_slides = $(".sws-slide").add($("body").children("h1")).length;
 
-        $(document).keydown(self.inputHandler);
-        $("body").append($(SWS.Templates.controlPanel));
         var cur = self.getCurrentSlide();
-        $(".sws-slide").each (function (i) {
+        var toc = self.buildFullTOC();
+        var common_html = "<div class='sws-slide sws-toc'><h1>Plan</h1><ul style='list-style-type:none'>";
+        var i;
+        for (i= 0; i < toc.length - 1; i++)
+            common_html += "<li class='done'>" + (i+1) +
+            ' ' + toc[i].title + "</li>";
+
+        common_html += "<li>" + toc.length + ' ' + toc[toc.length - 1].title;
+        common_html += "<ul style='list-style-type:none' >";
+        var sections = toc[toc.length - 1].sections;
+        $("body").children("h1").each(function (i) {
+            var this_html = common_html;
+            var j;
+            var secnum = toc.length + '.';
+            for (j = 0; j < i; ++j)
+                this_html += "<li class='done'>" + secnum + (j+1) + ' ' +
+                sections[j].title + "</li>";
+            this_html += "<li class='hl'>" + secnum + (i+1) + ' ' +
+                sections[i].title + "</li>";
+            for (j = i+1; j < sections.length; j++)
+                this_html += "<li>" + secnum + (j+1) + ' '
+                +sections[j].title + "</li>";
+            this_html += "</ul></li></ul></div>";
+            $(this).after(this_html);
+        });
+
+        $(".sws-slide").each(function (i) {
+
             var par = $(this).parent();
 
+
             $(this).remove();
             var canvas = $('<div class="sws-canvas"/>');
 
             if (!($(this).hasClass("sws-option-noheader"))) {
                 canvas.append($('<div class="sws-header"/>'));
             };
+
+            if (!$(this).hasClass("sws-cover")) {
+                var title = $($(this).find("h1")[0]);
+                var title_div = $('<div class="sws-title" />');
+                title_div.append(title);
+                canvas.append(title_div);
+            };
+
             var inner = $('<div class="sws-inner-canvas"/>');
-            var h1 = $(this).children("h1");
-            if (h1) {
-              var title = $('<div class="sws-title"/>');
-              h1.detach();
-              title.append(h1);
-              inner.append(title);
-            }
+            var content = $('<div class="sws-content"/>');
             $(this).find('script[type="text/javascript"]').remove();
-            if (!($(this).hasClass("sws-option-nofiller"))) {
-                inner.append('<div class="sws-filler"/>');
-                inner.append('<div class="sws-filler"/>');
-            };
-            inner.append($(this));
-            if (!($(this).hasClass("sws-option-nofiller"))) {
-                inner.append('<div class="sws-filler"/>');
-                inner.append('<div class="sws-filler"/>');
-            };
-            inner.append('<div class="sws-filler"/>');
+            content.append($(this));
+            inner.append(content);
             canvas.append(inner);
+           var that = this;
+           [ "sws-cover", "sws-toc" ].forEach(
+               function(v) {
+                   if ($(that).hasClass(v)) {
+                       inner.addClass(v);
+                       $(that).removeClass(v);
+                   }
+               });
+
             if (!($(this).hasClass("sws-option-nofooter"))) {
                 canvas.append($('<div class="sws-footer"/>'));
             };
+
             par.append(canvas);
 
             if (i == cur) {
@@ -540,10 +902,63 @@ SWS.Presentation = new function () {
             init_canvas(canvas,_slide_callbacks[i]);
 
         });
-        _slide_callbacks = null; /* avoid a leak */
-        self.refresh();
-        _initialized = true;
 
+        // Initialize the control panel
+        $("body").append($(SWS.Templates.controlPanel));
+        $("body").append($(SWS.Templates.helpPanel));
+        // Fill the theme switcher
+        $("link.sws-theme").each (function (i) {
+            var e = $(this);
+            var opt = "<option value='";
+            opt += e.attr("title");
+            opt += "' ";
+            if (e.attr("rel") == "stylesheet") {
+                opt+= "selected='selected'";
+            };
+            opt += ">" + e.attr("title") + "</option>";
+            $("#sws-theme-select").append($(opt));
+        });
+        // Set the fullscreen icon
+        if (SWS.Fullscreen.status()) {
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_on);
+        } else {
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_off);
+        };
+        // Put the navigation range at the correct position
+        var nav = $('#sws-control-panel-navigation-bar');
+        nav.attr("min", SWS.Presentation.firstSlide() + 1);
+        nav.attr("max", SWS.Presentation.lastSlide() + 1);
+        $('#sws-control-panel-total-slides').text('/' + SWS.Presentation.getNumSlides());
+        _update_ui();
+        _slide_callbacks = null; /* avoid a leak */
+        var passed_theme = SWS.Utils.getParameterByName("theme");
+
+
+        //workaround weird chrome CSS loading bug
+        var f =
+            function () {
+                if (passed_theme == "")
+                    self.changeTheme();
+                else
+                    self.changeTheme(passed_theme);
+                if (SWS.Utils.getParameterByName("mode") == "print") {
+                    self.printMode();
+                }
+                else {
+                   if (self.showHelpAtStartup()) $("#sws-help-panel-canvas").show().delay(5000).hide();
+                   self.changeAspect();
+                    self.refresh();
+               };
+                $(document).keydown(self.inputHandler);
+                document.body.addEventListener('touchstart',self.inputHandler, false);
+                document.body.addEventListener('touchend',self.inputHandler, false);
+               $(window).resize(self.changeAspect);
+                _initialized = true;
+            };
+        //setTimeout(f, 100);
+       f();
     };
 
 };