Team:Freiburg/js/homepage.js

From 2014.igem.org

var DEBUG = false;

var Slideshow = (function(){

 var _open=false,
     _slides=[],
     _current_slide = 0,
     _slides_list_entries,
     _animating=false;
 // Eventhandler
 var registerEventhandler = function(){
   if (DEBUG) console.log("func: registerEventhandler");
   $('.slideshow .controls .button.show-list').click(toggleSlideListOpen);
   $('.slideshow .controls .button.slide-up').click(previousSlide);
   $('.slideshow .controls .button.slide-down').click(nextSlide);
   // slides list
   $('.slideshow .slides-list .entry a').click(selectSlide);
   // IE9, Chrome, Safari, Opera
   $(window).on("mousewheel", wheel);
   // Firefox
   $(window).on("DOMMouseScroll", wheel);
 };
 var enableButton = function(direction) {
   $('.slideshow .controls .button.slide-' + direction)
     .removeClass('disabled');
 };
 var disableButton = function(direction) {
   $('.slideshow .controls .button.slide-' + direction)
     .addClass('disabled');
 }
 var nextSlide = function(){
   if (DEBUG) console.log("func: nextSlide");
   if (!_open && _slides.length && _current_slide < _slides.length - 1){
     // open next slide
     goToSlide(_current_slide + 1);
   }
 };
 var previousSlide = function(){
   if (DEBUG) console.log("func: previousSlide");
   if (!_open && _slides.length && _current_slide > 0){
     // open previous slide
     goToSlide(_current_slide - 1);
   }
 };
 var toggleSlideListOpen = function(){
   if (DEBUG) console.log("func: toggleSlideListOpen");
   $('.slideshow').toggleClass('open');
   _open = !_open;
   if (_open){
     disableButton('up');
     disableButton('down');
   } else {
     if (_current_slide !== 0)
       enableButton('up');
     if (_current_slide !== _slides.length - 1)
       enableButton('down');
   }
 };
 function preventDefault(e) {
   e = e || window.event;
   if (e.preventDefault)
       e.preventDefault();
   e.returnValue = false;  
 }
 function wheel(e) {
   var evnt = e.originalEvent;
   var direction = (evnt.wheelDeltaY ? evnt.wheelDeltaY : evnt.wheelDelta) || -evnt.detail;
   var currentWheelDelta = Math.abs(direction);
   if (_animating) return;
   if (direction > 0) {
       // scrolled up
       previousSlide();
   } else if (direction < 0) {
       // scrolled down
       nextSlide();
   }
 }
 var goToSlide = function(idx) {
   if (DEBUG) console.log("func: goToSlide");
   if (idx < 0 || 
       _slides.length === undefined || 
       idx > _slides.length - 1) return;
   // TODO: go to slide with index idx
   if (DEBUG) console.log("func: goToSlide, set active");
   for (var _i=0;_i<_slides.length;_i++){
     if (_i < idx) {
       _slides[_i].addClass('top');
     } else {
       _slides[_i].removeClass('top');
     }
   }
   _slides[_current_slide].removeClass('active');
   _slides[idx].addClass('active');
   _animating = true;
   setTimeout(function(){_animating = false;}, 200);
   if (DEBUG) console.log("func: goToSlide, currentSlide: "+ idx);


   // check if at the end or beginning of slideshow
   if (_current_slide === 0 && idx !== 0)
     enableButton('up');
   if (_current_slide === _slides.length - 1 && idx !== _slides.length - 1)
     enableButton('down');
   if (idx === 0) 
     disableButton('up');
   if (idx === _slides.length - 1)
     disableButton('down');
   _current_slide = idx;
 };
 var selectSlide = function(e) {
   e.preventDefault();
   var entry = $(this).parent();
   if (!_slides_list_entries) {
     _slides_list_entries = $('.slideshow .slides-list .entry');
   }
   if (DEBUG) {
     console.log("Selection of slide from slides list:");
     console.dir(entry);
     console.dir(_slides_list_entries);
     console.log(_slides_list_entries.index(entry));
   }
   // close the list and then go to the selected slide
   if (_open) {
     toggleSlideListOpen();
     setTimeout(function(){
       goToSlide(_slides_list_entries.index(entry));
     }, 400);
   } else {
     goToSlide(_slides_list_entries.index(entry));
   }
 };
 var resizeSlideshow = function (){
   var slideshow = $('.slideshow');
   var windowHeight = $(window).height();
   var slideshowOffset = slideshow.offset();
   if (windowHeight > slideshowOffset.top + 100) {
     slideshow.height(windowHeight - slideshowOffset.top);
   }
 };
 var init = function(){
   if (DEBUG) console.log("func: init");
   $('.slideshow .slides .slide').each(function(){
     _slides.push($(this));
   });
   // give every slide a z-index
   for (var _i=0; _i < _slides.length; ++_i){
     _slides[_i].css('z-index', _slides.length - _i);
   }
   registerEventhandler();
   disableButton('up');
   if ($('.slideshow').hasClass('open')){
     _open = true;
     disableButton('down');
   }
   $(window).resize(resizeSlideshow);
   $(window).load(resizeSlideshow);
   $(document).load(resizeSlideshow);
   $(document).ready(resizeSlideshow);
   $(window).load(function(){
     // unhide other slides
     $('.slide:not(:first-child) .left').show();
   })
 };
 init();
 return {
   nextSlide: nextSlide,
   previousSlide: previousSlide,
   toggleSlideListOpen: toggleSlideListOpen,
   resizeSlideshow: resizeSlideshow
 }

})();

Slideshow.resizeSlideshow();