Advertisement
Guest User

Untitled

a guest
Nov 8th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         (function (d, w) {
  2.  
  3.             // user defines
  4.  
  5.             var
  6.                     swapHours = 0,
  7.                     swapMinutes = 0,
  8.                     swapSeconds = 5,
  9.                     swapTotal = (swapHours * 60 + swapMinutes) * 60 + swapSeconds,
  10.                     loopSlideShow = true,
  11.                     sint,
  12.                     isPaused = true;
  13.  
  14.             // some handy helper functions
  15.  
  16.             function classExists(e, className) {
  17.                 return RegExp('(\\s|^)' + className + '(\\s|$)').test(e.className);
  18.             }
  19.  
  20.             function classAdd(e, className) {
  21.                 if (classExists(e, className))
  22.                     return false;
  23.                 e.className += (e.className ? ' ' : '') + className;
  24.                 return true;
  25.             }
  26.  
  27.             function classRemove(e, className) {
  28.                 if (!classExists(e, className))
  29.                     return false;
  30.                 e.className = e.className.replace(
  31.                         new RegExp('(\\s|^)' + className + '(\\s|$)'), ' '
  32.                         ).replace(/^\s+|\s+$/g, '');
  33.                 return true;
  34.             }
  35.  
  36.             function nodeFirst(e) {
  37.                 e = e.firstChild;
  38.                 while (e && e.nodeType != 1)
  39.                     e = e.nextSibling;
  40.                 return e;
  41.             }
  42.  
  43.             function nodeLast(e) {
  44.                 e = e.lastChild;
  45.                 while (e && e.nodeType != 1)
  46.                     e = e.previousSibling;
  47.                 return e;
  48.             }
  49.  
  50.             function nodeNext(e) {
  51.                 while (e = e.nextSibling)
  52.                     if (e.nodeType == 1)
  53.                         return e;
  54.                 return null;
  55.             }
  56.  
  57.             function nodePrev(e) {
  58.                 while (e = e.previousSibling)
  59.                     if (e.nodeType == 1)
  60.                         return e;
  61.                 return null;
  62.             }
  63.  
  64.             function nodeFlush(e) {
  65.                 while (e.firstChild)
  66.                     e.removeChild(e.firstChild);
  67.             }
  68.  
  69.             function nodeReplace(e, newNode) {
  70.                 nodeFlush(e);
  71.                 e.appendChild(
  72.                         typeof newNode == 'object' ? newNode : d.createTextNode(newNode)
  73.                         );
  74.             }
  75.  
  76.             function make(tagName, child, attribs, parent) {
  77.                 var e = d.createElement(tagName);
  78.                 if (child)
  79.                     e.appendChild(
  80.                             typeof child == 'object' ? child : d.createTextNode(child)
  81.                             );
  82.                 if (attribs)
  83.                     for (var i in attribs)
  84.                         e[i] = attribs[i];
  85.                 if (parent)
  86.                     parent.appendChild(e);
  87.                 return e;
  88.             }
  89.  
  90.             function prevent(e, deselect) {
  91.                 e.cancelBubble = true;
  92.                 if (e.stopPropagation)
  93.                     e.stopPropagation();
  94.                 if (e.preventDefault)
  95.                     e.preventDefault();
  96.                 e.returnValue = false;
  97.                 if (deselect) {
  98.                     if (w.getSelection)
  99.                         w.getSelection().removeAllRanges();
  100.                     if (d.selection)
  101.                         d.selection.empty();
  102.                 }
  103.             }
  104.  
  105.             function controlEvent(e, handler) {
  106.                 handler();
  107.                 e = e || window.event;
  108.                 prevent(e, true);
  109.             }
  110.  
  111.             function clockFormat(value) {
  112.                 value = String(Math.floor(value));
  113.                 while (value.length < 2)
  114.                     value = '0' + value;
  115.                 return value;
  116.             }
  117.  
  118.             // slideShow functions
  119.  
  120.             function showCounter() {
  121.                 nodeReplace(slideCounter,
  122.                         clockFormat(swapCounter / 3600) + ':' +
  123.                         clockFormat((swapCounter / 60) % 60) + ':' +
  124.                         clockFormat(swapCounter % 60)
  125.                         );
  126.             }
  127.  
  128.             function resetCounter() {
  129.                 swapCounter = swapTotal;
  130.                 showCounter();
  131.             }
  132.  
  133.             function makeSlide(newSlide) {
  134.                 classRemove(currentSlide, 'ss_show');
  135.                 currentSlide = newSlide;
  136.                 classAdd(currentSlide, 'ss_show');
  137.             }
  138.  
  139.             function nextSlide() {
  140.                 resetCounter();
  141.                 var newSlide = nodeNext(currentSlide);
  142.                 if (newSlide)
  143.                     makeSlide(newSlide);
  144.                 else if (loopSlideShow)
  145.                     makeSlide(firstSlide);
  146.             }
  147.  
  148.             function prevSlide() {
  149.                 resetCounter();
  150.                 var newSlide = nodePrev(currentSlide);
  151.                 if (newSlide)
  152.                     makeSlide(newSlide);
  153.                 else if (loopSlideShow)
  154.                     makeSlide(lastSlide);
  155.             }
  156.  
  157.             function slideUpdate() {
  158.                 if (swapCounter--)
  159.                     showCounter();
  160.                 else
  161.                     nextSlide();
  162.             }
  163.            
  164.            
  165.             var prevClicked = false;
  166. function pauseSlide() {
  167.             if(!prevClicked){
  168.                 clearInterval(sint);
  169.                 prevClicked = true;
  170.              }else{
  171.                 setInterval(slideUpdate, 1000); paused(clearInterval);
  172.                 prevClicked = false        
  173.              }
  174.  }
  175.            
  176.             //function pauseSlide() {
  177.                 //clearInterval(sint); // This line pause/stop the setInterval
  178.                 //sleep(5000); // for testing this does freez everything for 5 seconds then continue
  179.                 //setInterval(slideUpdate, 1000); // this does continue from last point it was paused(clearInterval)
  180.                 // To find how to use the pause clearInterval in a switch button. How to make a switch button to work.
  181.             //}
  182.            
  183.             // This function sleep n seconds before continue
  184.             function sleep(milliseconds) {
  185.                 var start = new Date().getTime();
  186.                 for (var i = 0; i < 1e7; i++) {
  187.                      if ((new Date().getTime() - start) > milliseconds){
  188.                           break;
  189.                 }
  190.                }
  191.               }
  192.  
  193.             function startSlideShow() {
  194.                 resetCounter();
  195.                 sint = setInterval(slideUpdate, 1000);
  196.             }
  197.  
  198.             // slideShow setup
  199.  
  200.             var
  201.                     slideShow = d.getElementById('slideShow'),
  202.                     slideCounter = make('div', false, {id: 'slideCounter'}),
  203.                     slideControls = make('div', false, {id: 'slideControls'}),
  204.                     slidePrev = make('a', 'Previous Slide', {
  205.                         onclick: function (e) {
  206.                             controlEvent(e, prevSlide);
  207.                         },
  208.                         className: 'previous',
  209.                         href: '#'
  210.                     }, slideControls),
  211.                     slideNext = make('a', 'Next Slide', {
  212.                         onclick: function (e) {
  213.                             controlEvent(e, nextSlide);
  214.                         },
  215.                         className: 'next',
  216.                         href: '#'
  217.                     }, slideControls),
  218.                     slidePause = make('a', 'Pause Slide', {
  219.                         onclick: function (e) {
  220.                             controlEvent(e, pauseSlide);
  221.                         },
  222.                         className: 'pause',
  223.                         href: '#'
  224.                     }, slideControls),
  225.                     firstSlide = nodeFirst(slideShow),
  226.                     lastSlide = nodeLast(slideShow),
  227.                     currentSlide = firstSlide,
  228.                     swapCounter;
  229.             slideShow.parentNode.insertBefore(slideCounter, slideShow);
  230.             slideShow.parentNode.insertBefore(slideControls, slideShow.nextSibling);
  231.  
  232.             classAdd(slideShow, 'ss_scripted');
  233.             classAdd(currentSlide, 'ss_show');
  234.  
  235.             // wait for onload to actually start the countdown
  236.  
  237.             if (w.addEventListener)
  238.                 w.addEventListener('load', startSlideShow, false);
  239.             else
  240.                 w.attachEvent('onload', startSlideShow);
  241.  
  242.  
  243.         })(document, window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement