$(document).ready(function(){
 var loc = window.location.href;
 if(loc.indexOf("didyouknow") > 0) {
  var parts = loc.split("#");
  $("#didYouKnowContainer span[name='" + parts[1] + "'] ").toggleClass("no_display");
 }

  $('.didyouknow_unhide').mouseup(function(){
//    window.history.go(0);
    var helpTopic = $(this).attr("href").substr(1);
    $("#didYouKnowContainer span[name='" + helpTopic + "'] ").toggleClass("no_display");
  });
});

Vertis.slideshow = function (ul) {
  this.config = {
    fadetime: 1000,
    interval: 5000
  };

  this._container = $(ul);
  this._cache = {};

  var imgs  = this._container.find('li img'),
      limit = imgs.length - 1;

  if (limit > 0) {
    for (var i = 0; i < limit; i++) {
      var img = imgs.eq(i);
      this._cache[i] = img.attr('src');
      img.attr('id', 'cache_' + i).attr('src', '');
    }
  }

  // pokazanie pierwszego zdjecia
  this._container.find('li:last').show();
};

Vertis.slideshow.prototype = {
  move: function (dir) {
    var that = this;
    dir = dir || 0; // value if called by interval

    if(that._running) {
      return;
    }

    that.stop();

    var animationTime = (dir==0) ? that.config.interval : 0;

    if (dir != -1) {
      var banner = that._container.children(':last');
      var prevImg = banner.prev().find('img');

      var src = prevImg.attr('id').match(/^cache_(\d+)$/);
      if (src != null) {
        prevImg.removeAttr('id').attr('src', this._cache[src[1]]);
        delete this._cache[src[1]];
        
        var _time = new Date().getTime();
        prevImg.load(function () {
          var _restTime = animationTime - (new Date().getTime() - _time);
          if (_restTime > 0) {
            that._timeout = setTimeout(function () {
              that.fade(banner, dir);
            }, _restTime);
          } else
            that.fade(banner, dir);
        });
      } else {
        that._timeout = setTimeout(function () {
          that.fade(banner, dir)
        }, animationTime);
      }
    } else {
      var banner = that._container.children(':first').hide();
      var nextImg = banner.find('img');

      var src = nextImg.attr('id').match(/^cache_(\d+)$/);
      if (src != null) {
        nextImg.removeAttr('id').attr('src', this._cache[src[1]]);
        delete this._cache[src[1]];
        
        var _time = new Date().getTime();
        nextImg.load(function () {
          var _restTime = animationTime - (new Date().getTime() - _time);
          if (_restTime > 0)
            that._timeout = setTimeout(function () {
              that.fade(banner, dir);
            }, _restTime);
          else
            that.fade(banner, dir);
        });
      } else {
        that._timeout = setTimeout(function () {
          that.fade(banner, dir)
        }, animationTime);
      }
    }
  },

  fade: function (banner, dir) {
    var that = this;
    var speed = that.config.fadetime;

    that._running = true;
    that.stop();

    if (dir != -1) {
      banner.prev().show();
      banner.fadeTo(speed, 0, function () {
        banner.prependTo(that._container).fadeTo(0, 1);
        that.start();
      });
    } else {
      banner.appendTo(that._container);
      banner.fadeTo(speed, 1, function() {
        that.start();
      });
    }
  },

  start: function () {
    var that = this;
    that._running = false;
    that.move();
  },

  stop: function () {
    clearInterval(this._timeout);
  },

  next: function () {
    this.move(1);
  },

  pause: function () {
    this.stop();
  },

  prev: function () {
    this.move(-1);
  }
}

