Use the new flex-box et border-sizing.
[hacks/simpleWebSlides.git] / simpleWebSlides.js
1 /*
2   Namespace object.
3 */
4
5 var SWS = SWS || {};
6
7
8
9 SWS.Utils = new function () {
10     var self = this;
11
12     self.isUndefined = function (o) { return typeof o == "undefined"; };
13     self.push2 = function (t, i, v) {
14         if ((typeof t[i]) == 'undefined') {
15             t[i] = new Array();
16         };
17         var l = t[i].length;
18         t[i][l] = v;
19     };
20
21     self.isEmpty = function (o) {
22         for(var _ in o) return false;
23         return true;
24     };
25
26     self.parseFrameSpec = function (s) {
27         var elems = s.split("_");
28         var result = {};
29         var min_last = 10000;
30         var max_value = -1;
31         for(var i = 0; i < elems.length; i++){
32             var bounds = elems[i].split("-");
33             if (bounds.length > 2 || bounds.length == 0) return {};
34             if (bounds.length == 1) bounds[1] = bounds[0];
35             var a = parseInt(bounds[0]);
36             var b = parseInt(bounds[1])
37             if (!isFinite(a) || !isFinite(b)) return {};
38             a = Math.min(a, 1000); // don't allow more than 1000 frames/slide
39             b = Math.min(b, 1000);
40             if (b > max_value) max_value = b;
41             for (var j = a; j <= b; j++)
42                 result[j] = true;
43         };
44         return result;
45     };
46
47     self.getParameterByName = function (name) {
48         name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
49         var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
50         results = regex.exec(location.search);
51         return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
52     }
53 };
54
55
56
57
58 SWS.Templates = new function () {
59     var self = this;
60     self.controlPanel = "<div id='sws-control-panel-canvas'><div id='sws-control-panel'>\
61 <div id='sws-control-panel-title-bar'>\
62 <a title='Toggle fullscreen' id='sws-control-panel-fullscreen' class='sws-symbol' onclick='SWS.Presentation.toggleFullScreen();'></a>\
63 <a title='Close panel' id='sws-control-panel-close' onclick='$(\"#sws-control-panel-canvas\").toggle();'>&#x2716;</a>\
64 </div>\
65 <div id='sws-control-panel-options'>\
66 <span title='Change the aspect ratio' class='sws-symbol' >&#x1f4bb;</span><select id='sws-aspect-select' onchange='SWS.Presentation.changeAspect();'>\
67 <option value='sws-aspect-4-3'>4:3</option>\
68 <option value='sws-aspect-16-9'>16:9</option>\
69 <option value='sws-aspect-16-10'>16:10</option>\
70 </select>\
71 <span title='Change the theme' class='sws-symbol'>&#x1f3a8;</span><select id='sws-theme-select' onchange='SWS.Presentation.changeTheme();'></select>\
72 <a onclick='SWS.Presentation.openPrint()' ><span title='Open Print-Out' class='sws-symbol'>&#59158;</span></a>\
73 </div>\
74 <div id='sws-control-panel-navigation'>\
75 <a title='First slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.firstSlide());' >&#x23ee;</a>\
76 <a title='Previous slide' class='sws-symbol' onclick='SWS.Presentation.previousSlide();SWS.Presentation.refresh();'>&#x23ea;</a>\
77 <a title='Previous step' class='sws-symbol' style='-webkit-transform: scaleX(-1);' onclick='SWS.Presentation.previous();SWS.Presentation.refresh();'>&#x25b6;</a>\
78 <a title='Next step' class='sws-symbol' onclick='SWS.Presentation.next();SWS.Presentation.refresh();'>&#x25b6;</a>\
79 <a title='Next slide' class='sws-symbol' onclick='SWS.Presentation.nextSlide();SWS.Presentation.refresh();'>&#x23e9;</a>\
80 <a title='Last slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.lastSlide());'>&#x23ed;</a>\
81 <br/>\
82 <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>\
83 <input type='range' title='Navigate the presentation' id='sws-control-panel-navigation-bar' onchange='SWS.Presentation.navigate();' step='1'></input>\
84 </div>\
85 </div>\
86 </div>";
87     self.slideActivate = function (o) {
88         if (!(o.hasClass("sws-active-slide"))){
89             o.removeClass("sws-inactive-slide").addClass("sws-active-slide");
90         };
91     };
92
93     self.slideDeactivate = function (o) {
94         if (!(o.hasClass("sws-inactive-slide"))){
95             o.removeClass("sws-active-slide").addClass("sws-inactive-slide");
96         };
97     };
98
99     self.slideChange = function (from, to) {
100         var canvas = $(".sws-canvas");
101         self.slideDeactivate($(canvas[from]));
102         self.slideActivate($(canvas[to]));
103     };
104
105     self.objectActivate = function (o) {
106         if (!(o.hasClass("sws-active-object"))){
107             o.removeClass("sws-inactive-object").addClass("sws-active-object");
108             return true;
109         };
110         return false;
111     };
112
113     self.objectDeactivate = function (o) {
114         if (!(o.hasClass("sws-inactive-object"))){
115             o.addClass("sws-inactive-object").removeClass("sws-active-object");
116             return true;
117         };
118         return false;
119     };
120
121     self.updateFooter = function (o) {
122         var footer = o.find(".sws-footer");
123         if (footer.length && (footer.children("*").length == 0)) {
124             var i = SWS.Presentation.getCurrentSlide();
125             var cur = $( "<span class='sws-current-slide-number'>"
126                          + (i + 1)
127                          +"</span>");
128             var sep = $( "<span class='sws-slide-num-sep' />");
129             var tot = $( "<span class='sws-last-slide-number'>"
130                          + (SWS.Presentation.getNumSlides())
131                          +"</span>");
132             footer.append(cur).append(sep).append(tot);
133         };
134     };
135     self.updateHeader = function (o) {};
136 };
137 SWS.ConfigBuilder = function () {
138     var self = this;
139     self['sws-object-activate'] = SWS.Templates.objectActivate;
140     self['sws-object-deactivate'] = SWS.Templates.objectDeactivate;
141     self['sws-slide-change'] = SWS.Templates.slideChange;
142     self['sws-update-footer'] = SWS.Templates.updateFooter;
143     self['sws-update-header'] = SWS.Templates.updateHeader;
144 };
145
146 SWS.Defaults = new SWS.ConfigBuilder ();
147
148 SWS.Config = new SWS.ConfigBuilder ();
149
150
151 SWS.Effects = new function () {
152     var self = this;
153
154     self.objectDeactivateFadeOut = function (o) {
155         o.animate({'opacity': '0'}, 200,
156                   function () {
157
158                       SWS.Templates.objectDeactivate(o);
159                   });
160     };
161
162     self.objectActivateFadeIn = function (o) {
163
164         if (SWS.Templates.objectActivate(o)){
165             o.animate({'opacity': '1' }, 200);
166         };
167
168     };
169
170     self.slideChangeHorizontalFlip = function (from, to){
171         var f = SWS.Presentation.getSlide(from);
172         var t = SWS.Presentation.getSlide(to);
173         f.animate({ 'left': '50%', 'width': '0pt', 'opacity':'0' }, 150,
174                   function  () {
175                       SWS.Templates.slideDeactivate(f);
176                       f.css({'left':'0%', 'width': '100%'});
177                       t.css({ 'left': '50%', 'width': '0pt','opacity':'0' });
178                       SWS.Templates.slideActivate(t);
179                       t.animate({'left':'0%', 'width': '100%','opacity':'1'});
180                   });
181     };
182     self.slideChangeFadeOutIn = function (from, to) {
183         var f = SWS.Presentation.getSlide(from);
184         var t = SWS.Presentation.getSlide(to);
185         f.animate({ 'opacity': '0'}, 150,
186                   function () { SWS.Templates.slideDeactivate(f);
187                                 SWS.Templates.slideActivate(t);
188                                 t.css('opacity', '0');
189                                 t.animate({ 'opacity': '1'}, 150);
190                               });
191     };
192     self.slideChangeHorizontalSlide = function (from, to) {
193         var f = SWS.Presentation.getSlide(from);
194         var t = SWS.Presentation.getSlide(to);
195         if (from < to) {
196             t.css('left', '100%');
197             t.css('opacity', '1');
198             SWS.Templates.slideActivate(t);
199             f.animate({ 'left': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f);
200                                                               f.css('opacity', '0');
201                                                               t.animate({ 'left': '0%' }, 250);
202                                                             });
203         } else {
204             t.css('left', '-100%');
205             SWS.Templates.slideActivate(t);
206             f.animate({ 'left': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f);
207                                                              f.css('opacity', '0');
208                                                            });
209             t.css('opacity', '1');
210             t.animate({ 'left': '0%' }, 250);
211         };
212     };
213
214
215     self.slideChangeVerticalSlide = function (from, to) {
216         var f = SWS.Presentation.getSlide(from);
217         var t = SWS.Presentation.getSlide(to);
218         if (from < to) {
219             t.css('top', '100%');
220             SWS.Templates.slideActivate(t);
221             f.animate({ 'top': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
222             t.animate({ 'top': '0%' }, 250);
223         } else {
224             t.css('top', '-100%');
225             SWS.Templates.slideActivate(t);
226             f.animate({ 'top': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
227             t.animate({ 'top': '0%' }, 250);
228         };
229     };
230
231 };
232
233 SWS.Fullscreen = new function () {
234     var self = this;
235
236     if (SWS.Utils.isUndefined(document.fullScreen)) {
237         if (SWS.Utils.isUndefined(document.mozfullScreen)) {
238             self.status = function () { return document.webkitIsFullScreen; };
239             self.enter = function(e) {
240                 e.webkitRequestFullScreen();
241             };
242             self.exit = function () {
243                 document.webkitCancelFullScreen();
244             };
245
246         } else {
247             self.status = function () { return document.mozfullScreen; };
248             self.enter = function(e) {
249                 e.mozRequestFullScreen();
250             };
251             self.exit = function () {
252                 document.mozCancelFullScreen();
253             };
254
255         };
256     } else {
257             self.status = function () { return document.fullScreen; };
258             self.enter = function(e) {
259                 e.requestFullScreen();
260             };
261             self.exit = function () {
262                 document.cancelFullScreen();
263             };
264
265     };
266
267
268 };
269
270 SWS.Presentation = new function () {
271
272
273     var self = this;
274
275     //TODO move outside of the Presentation object
276
277
278     var _total_slides;
279     var _initialized = false;
280     var _disable_input_events = false;
281     var _print_mode = false;
282     var _slide_callbacks = new Array ();
283     var _total_steps = -1;
284     var _current_theme = "";
285
286     self.getNumSlides = function () { return _total_slides; };
287
288     self.getSlide = function(i) {
289         return $($(".sws-canvas")[i]);
290     };
291
292     self.registerCallback = function (i, f) {
293         if (_initialized) return;
294         //jQuery does not seem to work well
295         //on a partial DOM
296
297         var slide_num = $(".sws-slide").length - 1;
298
299         SWS.Utils.push2(_slide_callbacks, slide_num,{ 'fn': f, 'frame': i });
300
301     };
302
303     if (typeof(Storage)!=="undefined"){
304         self.getCurrentSlide = function () {
305             //unary + casts to integer
306             var i = +(sessionStorage.getItem("current_slide"));
307             if (!(i >= 0 && i < self.getNumSlides())){
308                 return 0;
309             } else {
310                 return i;
311             };
312         };
313
314         self.setCurrentSlide = function (i) {
315             sessionStorage.setItem("current_slide", i);
316         };
317
318     } else {
319         var _current_slide = 0;
320         self.getCurrentSlide = function () { return _current_slide; };
321         self.setCurrentSlide = function (i) { _current_slide = i; };
322
323     };
324     self.firstSlide = function () { return 0; };
325     self.lastSlide = function () { return self.getNumSlides() - 1; };
326     self.refresh = function () {
327         /* block upcoming input event until all animations are finished */
328         _disable_input_events = true;
329
330         var canvas = $(".sws-canvas");
331         var from_slide_num = canvas.index($(".sws-active-slide"));
332         var to_slide_num = self.getCurrentSlide();
333         var watch_slide_anim = false;
334         var to_slide = $(canvas[to_slide_num]);
335         var from_slide = $(canvas[from_slide_num]);
336         var slide_change = (from_slide_num != to_slide_num);
337
338
339         var info = to_slide.data("sws-frame-info");
340         SWS.Config['sws-update-header'](to_slide);
341         SWS.Config['sws-update-footer'](to_slide);
342
343         if (slide_change) {
344             //Launch a slide transition:
345             SWS.Config['sws-slide-change'](from_slide_num, to_slide_num);
346             watch_slide_anim = true;
347             for (var i = 0; i < info.callbacks.at_slide.length;i++){
348                 info.callbacks.at_slide[i](to_slide);
349             };
350         };
351
352
353         var cur = info.current;
354         var custom = info.custom;
355         var real_slide = to_slide.find(".sws-slide");
356
357         real_slide.find("*").andSelf().each(function (i){
358             var frameset = $(this).data("sws-frame-set") || {};
359             if (frameset[cur])
360                 SWS.Config['sws-object-activate']($(this));
361             else
362                 SWS.Config['sws-object-deactivate']($(this));
363         });
364
365         var callbacks;
366         if (callbacks = info.callbacks.at_frame[self.getCurrentFrame()]){
367             for (var k = 0; k < callbacks.length; k++)
368                 callbacks[k]($(to_slide));
369         };
370
371         var all = $(from_slide).add(to_slide);
372         all.find("*").addBack().promise().done(function() {
373             _disable_input_events = false;
374         });
375     };
376
377     self.nextSlide = function () {
378         self.setCurrentSlide(Math.min(self.getCurrentSlide()+1,
379                                       self.lastSlide()));
380         self.setCurrentFrame(self.firstFrame());
381     };
382
383     self.previousSlide = function () {
384         self.setCurrentSlide(Math.max(self.getCurrentSlide()-1,
385                                       self.firstSlide()));
386         self.setCurrentFrame(self.firstFrame());
387     };
388
389     self.getFrameInfo = function () {
390
391         var i = self.getCurrentSlide();
392         var canvas = $($(".sws-canvas")[i]);
393         var infos = canvas.data("sws-frame-info");
394         return infos;
395     };
396
397     self.getTotalSteps = function () {
398         if (_total_steps >= 0) return _total_steps;
399         _total_steps = 0;
400         $(".sws-canvas").each(function(i) {
401             var canvas = $($(".sws-canvas")[i]);
402             var infos = canvas.data("sws-frame-info");
403             _total_steps += infos.last + 1;
404         });
405         return _total_steps;
406     };
407
408     self.getCurrentFrame = function () { return self.getFrameInfo().current; };
409     self.setCurrentFrame = function (i) { self.getFrameInfo().current = i; };
410     self.firstFrame = function () { return 0; };
411     self.lastFrame = function () { return self.getFrameInfo().last; };
412
413     self.nextFrame = function () {
414         self.setCurrentFrame(Math.min(self.getCurrentFrame()+1,
415                                       self.lastFrame()));
416
417     };
418     self.previousFrame = function () {
419         self.setCurrentFrame(Math.max(self.getCurrentFrame()-1,
420                                        self.firstFrame()));
421     };
422
423     self.next = function () {
424         var i = self.getCurrentFrame();
425         if (i == self.lastFrame()) {
426             self.nextSlide();
427             self.setCurrentFrame(self.firstFrame());
428         } else
429             self.nextFrame();
430     };
431
432     self.previous = function () {
433         var i = self.getCurrentFrame();
434         if (i == self.firstFrame()){
435             self.previousSlide();
436             self.setCurrentFrame(self.lastFrame());
437         }
438         else
439             self.previousFrame();
440     };
441
442     self.goToSlide = function (s, f) {
443         if (SWS.Utils.isUndefined(f))
444             f = 0;
445         if (!(s >= self.firstSlide() && s <= self.lastSlide())) return;
446         self.setCurrentSlide(s);
447         if (!(f >= self.firstFrame() && f <= self.lastFrame())) f = 0;
448         self.setCurrentFrame(f);
449         self.refresh();
450     };
451
452     self.cycleStyle = function() {
453         var styles = $("head").children('link[rel$="stylesheet"][title]');
454         var j = styles.index(styles.filter(':not(:disabled)'));
455         styles[j].disabled = true;
456         if (++j == styles.length) j = 0;
457         styles[j].disabled = false;
458     };
459
460
461     self.printMode = function () {
462         _print_mode = true;
463         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>");
464         $("body").append(progress);
465
466         $("html").removeClass("sws-display").addClass("sws-print");
467         self.goToSlide(0,0);
468         var steps = self.getTotalSteps();
469         var total_steps = steps;
470         var loop;
471         loop = function () {
472             if (steps >= 0) {
473                 //Crazy workaround for chromium
474                 ($("link.sws-theme[rel='stylesheet']")[0]).disabled = false;
475                 $(".sws-canvas").find("*").addBack().promise().done(function() {
476                     var percent = ((total_steps - steps) / total_steps) * 100;
477                     console.log(percent);
478                     $("#sws-percent-progress").text(Math.round(percent));
479                     SWS.Config['sws-slide-change'] = SWS.Templates.slideChange;
480                     self.refresh();
481                     $($(".sws-canvas")[self.getCurrentSlide()]).css('opacity', 1 );
482                     self.next();
483                     steps--;
484                     loop();
485                     })
486             } else {
487                 $("#sws-percent-progress").text(100);
488                 progress.remove();
489             }
490         };
491         loop();
492
493     }
494
495     self.inputHandler = function (event) {
496         if (_disable_input_events || _print_mode ) return;
497
498         switch (event.which) {
499         case 36:/* Home */
500             self.setCurrentSlide(self.firstSlide());
501             break;
502
503         case 35:/* End */
504             self.setCurrentSlide(self.lastSlide());
505             break;
506         case 32: /* space */
507         case 34: /* PgDown */
508         case 39: /* -> */
509             if (self.getCurrentSlide() == self.lastSlide()
510                 && self.getCurrentFrame() == self.lastFrame()) return;
511             self.next();
512             break;
513         case 78: /* n */
514             self.nextSlide();
515             break;
516         case 8: /* backspace */
517         case 33: /* PgUp */
518         case 37: /* <-   */
519             self.previous();
520             break;
521         case 80: /* p */
522             self.previousSlide();
523             break;
524         case 83: /* s */
525             self.cycleStyle();
526             return;
527         case 67: /* c */
528             $("#sws-control-panel-canvas").toggle();
529             
530         default:
531             return;
532         };
533         self.refresh();
534     };
535
536
537
538     function init_canvas(canvas, custom) {
539         var cur_frame = 0;
540         var last_frame = canvas.find(".sws-pause").length;
541         //Add all regular elements to the frame list
542         var slide = $(canvas.find(".sws-slide")[0]);
543
544         var callbacks = { at_slide : new Array(),
545                           at_frame : new Array() }
546
547         if (SWS.Utils.isUndefined(custom)) {
548             custom = new Array ();
549         };
550
551         for (var i = 0; i < custom.length; i++) {
552             if (isFinite(custom[i].frame)){
553                 var num = +(custom[i].frame);
554                 if (num > last_frame) last_frame = num;
555                 SWS.Utils.push2(callbacks.at_frame, num, custom[i].fn);
556             } else if (custom[i].frame == "slide")
557                 callbacks.at_slide.push(custom[i].fn);
558             else {
559                 var frame_set = SWS.Utils.parseFrameSpec(custom[i].frame);
560                 for(var f in frame_set){
561                     if (f > last_frame) last_frame = f;
562                     SWS.Utils.push2(callbacks.at_frame, +(f), custom[i].fn);
563                 };
564             }
565         };
566
567         var specials = null;
568
569         slide.find('*[class*="sws-onframe-"]').each(function(_){
570             var cls = $(this).attr('class');
571             var idx = cls.indexOf("sws-onframe-");
572             if (idx >= 0) {
573                 var end = cls.indexOf(" ", idx);
574                 end = (end == -1) ? cls.length : end;
575                 var spec = cls.substring(idx+12, end);
576                 var o = SWS.Utils.parseFrameSpec(spec);
577                 for(var f in o)
578                     if (f > last_frame) last_frame = f;
579                 $(this).find("*").andSelf().each(function(_){
580                     if (!SWS.Utils.isEmpty(o))
581                         $(this).data("sws-frame-set", o);
582                     if (specials)
583                         specials.add($(this));
584                     else
585                         specials = $(this);
586                 });
587             };
588         });
589
590         slide.find("*").andSelf().not(specials).each(function(i) {
591             if ($(this).hasClass("sws-pause"))  cur_frame++;
592             var o = {};
593             for (var j = cur_frame; j <= last_frame; j++)
594                 o[ j ] = true;
595             if (!SWS.Utils.isEmpty(o))
596                 $(this).data("sws-frame-set", o);
597         });
598
599         canvas.data("sws-frame-info", { current: 0,
600                                         last: (last_frame - 0), // force cast to integer
601                                         callbacks: callbacks
602                                       });
603
604     };
605
606     /* Forces redrawing the page without reloading */
607     self.redraw = function (f) {
608         if (SWS.Utils.isUndefined(f))
609             $("body").hide().show(400, function () {
610                 $("body").css("display","block");
611                 if (!SWS.Utils.isUndefined(f))
612                     f();
613             });
614     };
615     self.changeAspect = function() {
616         $("html").removeClass("sws-aspect-4-3")
617             .removeClass("sws-aspect-16-9")
618             .removeClass("sws-aspect-16-10")
619             .addClass($("#sws-aspect-select").val());
620         self.redraw();
621     };
622
623     self.getCurrentTheme = function () {
624         var l = $("link.sws-theme[rel='stylesheet']")[0];
625
626         if (l) {
627             return  l.title;
628         } else
629             return ""
630     };
631
632     self.changeTheme = function (name) {
633         var theme_name;
634         if (typeof name === 'undefined')
635             theme_name = $("#sws-theme-select").val()
636         else
637             theme_name = name;
638
639         _current_theme = theme_name;
640         $("link.sws-theme").each (function (i) {
641             var e = this;
642             var title =  e.title;
643             if (title == theme_name) {
644                 e.rel = "stylesheet";
645                 e.disabled = false;
646                 e.media="all";
647             } else {
648                 e.rel = "alternate stylesheet";
649                 e.disabled = true;
650                 e.media="all";
651             };
652         });
653         self.redraw();
654
655     };
656
657     self.openPrint = function () {
658         window.open ("?mode=print&theme=" + self.getCurrentTheme());
659     }
660     var _fullscreen_icon_on = "&#xe746;";
661     var _fullscreen_icon_off = "&#xe744;";
662
663     self.toggleFullScreen = function () {
664         if (SWS.Fullscreen.status()) {
665             SWS.Fullscreen.exit();
666             $("a#sws-control-panel-fullscreen")
667                 .html(_fullscreen_icon_off);
668
669
670
671         } else {
672             SWS.Fullscreen.enter($("body")[0]);
673             $("a#sws-control-panel-fullscreen")
674                 .html(_fullscreen_icon_on);
675         };
676     };
677     function _update_ui() {
678         var nav = $('#sws-control-panel-navigation-bar');
679         nav.val(SWS.Presentation.getCurrentSlide() + 1);
680         $('#sws-control-panel-slide-input').val(nav.val());
681     }
682     self.navigate = function () {
683         self.goToSlide($("#sws-control-panel-navigation-bar").val()-1);
684         _update_ui();
685     };
686
687
688     self.init = function () {
689
690
691         $("html").addClass("sws-display");
692         //$(window).resize(self.redraw);
693
694
695
696         _total_slides = $(".sws-slide").length;
697
698         var cur = self.getCurrentSlide();
699
700         $(".sws-slide").each(function (i) {
701
702             var par = $(this).parent();
703
704
705             $(this).remove();
706             var canvas = $('<div class="sws-canvas"/>');
707
708             if (!($(this).hasClass("sws-option-noheader"))) {
709                 canvas.append($('<div class="sws-header"/>'));
710             };
711             if (!$(this).hasClass("sws-cover")) {
712                 var title = $($(this).find("h1")[0]);
713                 var title_div = $('<div class="sws-title" />');
714                 title_div.append(title);
715                 canvas.append(title_div);
716             }
717             var inner = $('<div class="sws-inner-canvas"/>');
718             var content = $('<div class="sws-content"/>');
719             $(this).find('script[type="text/javascript"]').remove();
720             content.append($(this));
721             inner.append(content);
722             canvas.append(inner);
723
724             if (!($(this).hasClass("sws-option-nofooter"))) {
725                 canvas.append($('<div class="sws-footer"/>'));
726             };
727
728             par.append(canvas);
729
730             if (i == cur) {
731                 canvas
732                     .addClass("sws-active-slide")
733                     .removeClass("sws-inactive-slide");
734             } else {
735                 canvas
736                     .addClass("sws-inactive-slide")
737                     .removeClass("sws-active-slide");
738             };
739             init_canvas(canvas,_slide_callbacks[i]);
740
741         });
742
743         // Initialize the control panel
744         $("body").append($(SWS.Templates.controlPanel));
745         // Fill the theme switcher
746         $("link.sws-theme").each (function (i) {
747             var e = $(this);
748             var opt = "<option value='";
749             opt += e.attr("title");
750             opt += "' ";
751             if (e.attr("rel") == "stylesheet") {
752                 opt+= "selected='selected'";
753             };
754             opt += ">" + e.attr("title") + "</option>";
755             $("#sws-theme-select").append($(opt));
756         });
757         // Set the fullscreen icon
758         if (SWS.Fullscreen.status()) {
759             $("a#sws-control-panel-fullscreen")
760                 .html(_fullscreen_icon_on);
761         } else {
762             $("a#sws-control-panel-fullscreen")
763                 .html(_fullscreen_icon_off);
764         };
765         // Put the navigation range at the correct position
766         var nav = $('#sws-control-panel-navigation-bar');
767         nav.attr("min", SWS.Presentation.firstSlide() + 1);
768         nav.attr("max", SWS.Presentation.lastSlide() + 1);
769         $('#sws-control-panel-total-slides').text('/' + SWS.Presentation.getNumSlides());
770         _update_ui();
771
772         _slide_callbacks = null; /* avoid a leak */
773         var passed_theme = SWS.Utils.getParameterByName("theme");
774         //workaround weird chrome CSS loading bug
775         var f =
776             function () {
777                 if (passed_theme == "")
778                     self.changeTheme();
779                 else
780                     self.changeTheme(passed_theme);
781                 if (SWS.Utils.getParameterByName("mode") == "print") {
782                     self.printMode();
783                 }
784                 else
785                     self.refresh();
786                 $(document).keydown(self.inputHandler);
787
788                 _initialized = true;
789             };
790         setTimeout(f, 100);
791     };
792
793 };