js/index.js

[Ritorna]

/*
 * index.js
 *
 * Copyright (C) 2016 Ing. Stefano Salvi<stefano@salvi.mn.it>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

function openDialog() {
  var el = document.getElementById("isf_overlay");
  isf_oldX = window.pageXOffset;
  oldY = window.pageYOffset;
  el.style.visibility = "visible";
}

function closeDialog() {
  var el = document.getElementById("isf_overlay");
  el.style.visibility = "hidden";
  window.scrollTo(isf_oldX, oldY);
}

function addListener(element, eventName, handler) {
  if (element.addEventListener) {
    element.addEventListener(eventName, handler, false);
  }
  else if (element.attachEvent) {
    element.attachEvent('on' + eventName, handler);
  }
  else {
    element['on' + eventName] = handler;
  }
}

/* Classe Slideshow */
function Slideshow (divId, timeSlide, timeStay) {
  var element = document.getElementById(divId);
  this.timeSlide = timeSlide;
  this.timeStay = timeStay;
  this.slides = [];
  this.curslide = 0;
  this.curtimer = null;
  this.curstart = Date.now();
  this.timeelapsed = 0;
  this.isScrolling=false;
  this.framewidth = element.offsetWidth;
  var children = element.children
  var left = element.offsetLeft;
  for(var i = 0; i < children.length; i++) {
    var el = children[i].style;
    el.position = "absolute";
    el.left = left + "px";
    el.marginLeft = this.framewidth + "px";
    this.slides.push(el);
  }

  this.slides[this.curslide].marginLeft = 0;

  this.prepareSlide = function () {
    var element = this.slides[(this.curslide + 1) % this.slides.length];
    element.transition = "none";
    element.marginLeft = this.framewidth + "px";
    this.isScrolling=false;
    this.curstart = Date.now();
    this.curtimer = window.setTimeout(this.changeSlide.bind(this), this.timeStay);
  }

  this.changeSlide = function () {
    var element = this.slides[this.curslide];
    element.marginLeft = "-" + this.framewidth + "px";
    element.zIndex = 0;
    element.transition = "margin-left " + this.timeSlide + "ms linear";

    this.curslide = (this.curslide + 1) % this.slides.length;

    element = this.slides[this.curslide];
    element.transition = "margin-left " + this.timeSlide + "ms linear";
    element.marginLeft = 0;
    element.zIndex = 256;

    this.isScrolling=true;
    this.curstart = Date.now();
    this.curtimer = window.setTimeout(this.prepareSlide.bind(this), this.timeSlide);
  }

  this.mouseover = function () {
    this.timeelapsed = Date.now() - this.curstart;
    clearTimeout(this.curtimer);
  }

  this.mouseout = function () {
    this.curstart = Date.now();
    if (this.isScrolling) {
      this.curtimer = window.setTimeout(this.prepareSlide.bind(this), this.timeSlide - this.timeelapsed);
    } else {
      this.curtimer = window.setTimeout(this.changeSlide.bind(this), this.timeStay - this.timeelapsed);
    }
  }

  addListener(element, 'mouseover', this.mouseover.bind(this));
  addListener(element, 'mouseout', this.mouseout.bind(this));
  this.curtimer = window.setTimeout(this.changeSlide.bind(this), this.timeStay);
}