Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (d, w) {
- // user defines
- var
- swapHours = 0,
- swapMinutes = 0,
- swapSeconds = 5,
- swapTotal = (swapHours * 60 + swapMinutes) * 60 + swapSeconds,
- loopSlideShow = true,
- sint,
- isPaused = true;
- // some handy helper functions
- function classExists(e, className) {
- return RegExp('(\\s|^)' + className + '(\\s|$)').test(e.className);
- }
- function classAdd(e, className) {
- if (classExists(e, className))
- return false;
- e.className += (e.className ? ' ' : '') + className;
- return true;
- }
- function classRemove(e, className) {
- if (!classExists(e, className))
- return false;
- e.className = e.className.replace(
- new RegExp('(\\s|^)' + className + '(\\s|$)'), ' '
- ).replace(/^\s+|\s+$/g, '');
- return true;
- }
- function nodeFirst(e) {
- e = e.firstChild;
- while (e && e.nodeType != 1)
- e = e.nextSibling;
- return e;
- }
- function nodeLast(e) {
- e = e.lastChild;
- while (e && e.nodeType != 1)
- e = e.previousSibling;
- return e;
- }
- function nodeNext(e) {
- while (e = e.nextSibling)
- if (e.nodeType == 1)
- return e;
- return null;
- }
- function nodePrev(e) {
- while (e = e.previousSibling)
- if (e.nodeType == 1)
- return e;
- return null;
- }
- function nodeFlush(e) {
- while (e.firstChild)
- e.removeChild(e.firstChild);
- }
- function nodeReplace(e, newNode) {
- nodeFlush(e);
- e.appendChild(
- typeof newNode == 'object' ? newNode : d.createTextNode(newNode)
- );
- }
- function make(tagName, child, attribs, parent) {
- var e = d.createElement(tagName);
- if (child)
- e.appendChild(
- typeof child == 'object' ? child : d.createTextNode(child)
- );
- if (attribs)
- for (var i in attribs)
- e[i] = attribs[i];
- if (parent)
- parent.appendChild(e);
- return e;
- }
- function prevent(e, deselect) {
- e.cancelBubble = true;
- if (e.stopPropagation)
- e.stopPropagation();
- if (e.preventDefault)
- e.preventDefault();
- e.returnValue = false;
- if (deselect) {
- if (w.getSelection)
- w.getSelection().removeAllRanges();
- if (d.selection)
- d.selection.empty();
- }
- }
- function controlEvent(e, handler) {
- handler();
- e = e || window.event;
- prevent(e, true);
- }
- function clockFormat(value) {
- value = String(Math.floor(value));
- while (value.length < 2)
- value = '0' + value;
- return value;
- }
- // slideShow functions
- function showCounter() {
- nodeReplace(slideCounter,
- clockFormat(swapCounter / 3600) + ':' +
- clockFormat((swapCounter / 60) % 60) + ':' +
- clockFormat(swapCounter % 60)
- );
- }
- function resetCounter() {
- swapCounter = swapTotal;
- showCounter();
- }
- function makeSlide(newSlide) {
- classRemove(currentSlide, 'ss_show');
- currentSlide = newSlide;
- classAdd(currentSlide, 'ss_show');
- }
- function nextSlide() {
- resetCounter();
- var newSlide = nodeNext(currentSlide);
- if (newSlide)
- makeSlide(newSlide);
- else if (loopSlideShow)
- makeSlide(firstSlide);
- }
- function prevSlide() {
- resetCounter();
- var newSlide = nodePrev(currentSlide);
- if (newSlide)
- makeSlide(newSlide);
- else if (loopSlideShow)
- makeSlide(lastSlide);
- }
- function slideUpdate() {
- if (swapCounter--)
- showCounter();
- else
- nextSlide();
- }
- var prevClicked = false;
- function pauseSlide() {
- if(!prevClicked){
- clearInterval(sint);
- prevClicked = true;
- }else{
- setInterval(slideUpdate, 1000); paused(clearInterval);
- prevClicked = false
- }
- }
- //function pauseSlide() {
- //clearInterval(sint); // This line pause/stop the setInterval
- //sleep(5000); // for testing this does freez everything for 5 seconds then continue
- //setInterval(slideUpdate, 1000); // this does continue from last point it was paused(clearInterval)
- // To find how to use the pause clearInterval in a switch button. How to make a switch button to work.
- //}
- // This function sleep n seconds before continue
- function sleep(milliseconds) {
- var start = new Date().getTime();
- for (var i = 0; i < 1e7; i++) {
- if ((new Date().getTime() - start) > milliseconds){
- break;
- }
- }
- }
- function startSlideShow() {
- resetCounter();
- sint = setInterval(slideUpdate, 1000);
- }
- // slideShow setup
- var
- slideShow = d.getElementById('slideShow'),
- slideCounter = make('div', false, {id: 'slideCounter'}),
- slideControls = make('div', false, {id: 'slideControls'}),
- slidePrev = make('a', 'Previous Slide', {
- onclick: function (e) {
- controlEvent(e, prevSlide);
- },
- className: 'previous',
- href: '#'
- }, slideControls),
- slideNext = make('a', 'Next Slide', {
- onclick: function (e) {
- controlEvent(e, nextSlide);
- },
- className: 'next',
- href: '#'
- }, slideControls),
- slidePause = make('a', 'Pause Slide', {
- onclick: function (e) {
- controlEvent(e, pauseSlide);
- },
- className: 'pause',
- href: '#'
- }, slideControls),
- firstSlide = nodeFirst(slideShow),
- lastSlide = nodeLast(slideShow),
- currentSlide = firstSlide,
- swapCounter;
- slideShow.parentNode.insertBefore(slideCounter, slideShow);
- slideShow.parentNode.insertBefore(slideControls, slideShow.nextSibling);
- classAdd(slideShow, 'ss_scripted');
- classAdd(currentSlide, 'ss_show');
- // wait for onload to actually start the countdown
- if (w.addEventListener)
- w.addEventListener('load', startSlideShow, false);
- else
- w.attachEvent('onload', startSlideShow);
- })(document, window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement