/**
 * Basic slideshow manager. Switches between child elements marked with "slideShow" class.
 * Child items with "slideShowNext", "slideShowPrev" and "slideShowPlayPause" classes
 * act as controls.
 * 
 * @author Julius Kybartas
 */
(function($) {
  function slideControls(ls, timer) {
    var that = this;
    var initSpeed = 800;
    var switchSpeed = 200;
    
    this.next = function(spd) {
      var curr = null;
      var next = ls.eq(0);
      
      if(typeof spd != 'number') spd = switchSpeed;
      
      ls.each(function() {
        if($(this).hasClass('slide_current')) {
          curr = this;
        }
        /* Match element right after current, else "next" will stay at default and thus "wrap around" */
        if(curr != null && curr != this) {
          next = this;
          return false;
        }
        
        return null;
      });
      
      if(curr) that.switchTo(curr, next, spd);
    }
    
    this.prev = function(spd) {
      var curr = null;
      var prev = ls.eq(ls.length - 1);
      
      if(typeof spd != 'number') spd = switchSpeed;
      
      ls.each(function() {
        if($(this).hasClass('slide_current')) {
          curr = this;
          return false;
        }
        else {
          prev = this;
        }
        
        return null;
      });
      
      if(curr) that.switchTo(curr, prev, spd);
    }
    
    this.switchTo = function(src, dst, speed) {
      if(src != null) {
        that.stop();
        $(src).fadeOut(speed, function() {
          $(dst).fadeIn(speed).addClass('slide_current');
          that.play();
        }).removeClass('slide_current');
      }
      else if(dst != null) {
        $(dst).fadeIn(speed).addClass('slide_current');
      }
    }
    
    this.play = function() {
      timer.play(function() {
        that.next(initSpeed);
      });
    }
    
    this.stop = function() {
      timer.stop();
    }
    
    this.playToggle = function() {
      /* Yeah, that's a hack. I don't care. */
      if(timer.stopped()) {
        that.play();
        this.src = 'images/slideshow/pause.png';
        this.alt = 'Pause';
      }
      else {
        that.stop();
        this.src = 'images/slideshow/play.png';
        this.slt = 'Spil';
      }
    }
    
    if(!ls.filter('.slide_current').length) {
      this.switchTo(null, ls.eq(0).addClass('slide_current'), 'slow');
      this.play();
    }
  };
  
  function slideTimer(interval) {
    var tId;
    
    this.play = function(callback) {
      if(!tId) {
        tId = window.setInterval(callback, interval);
      }
    }
    
    this.stop = function() {
      if(tId) {
        window.clearInterval(tId);
        tId = null;
      }
    }
    
    this.stopped = function() {
      return (tId == null);
    }
  };
  
  $.fn.slideShow = function() {
    this.each(function() {
      var timer = new slideTimer(11000);
      var ctrl = new slideControls($('.slideShow', this), timer);
      
      $(".slideShowNext", this).click(ctrl.next).css("cursor", "pointer");
      $(".slideShowPrev", this).click(ctrl.prev).css("cursor", "pointer");
      $(".slideShowPlayPause", this).click(ctrl.playToggle).css("cursor", "pointer");
    });
    
    return this;
  };
})(jQuery);
