var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$.fn.bannerFlow = function(o) {
  var Flow, defaults;
  defaults = {
    selectors: {
      left: '.b-clients-arrow-left',
      right: '.b-clients-arrow-right',
      list: '.b-clients-list',
      images: '.b-clients-list li'
    }
  };
  Flow = (function() {
    function Flow(container, options) {
      var key, value, _ref;
      this.container = container;
      this.options = options;
      this.moveDirection = null;
      this._moveTimer = null;
      this.elements = {};
      _ref = this.options.selectors;
      for (key in _ref) {
        value = _ref[key];
        this.elements[key] = this.container.find(value);
      }
      this.containerWidth = parseInt(this.container.css("width"), 10);
      this.rightButtonOffset = this.elements.right.prop('offsetLeft');
      this.elements.lastImage = this.elements.images.last();
      this.bindControls();
    }
    Flow.prototype.bindControls = function() {
      this.elements.left.mousedown(__bind(function() {
        return this.makeMove("left");
      }, this)).mouseup(__bind(function() {
        return this.makeMove();
      }, this));
      return this.elements.right.mousedown(__bind(function() {
        return this.makeMove("right");
      }, this)).mouseup(__bind(function() {
        return this.makeMove();
      }, this));
    };
    Flow.prototype.makeMove = function(direction) {
      if (direction == null) {
        direction = null;
      }
      this.moveDirection = direction;
      clearTimeout(this._moveTimer);
      if (!direction) {
        console.log("stop moving");
        return;
      }
      console.log("move" + direction);
      return this.move();
    };
    Flow.prototype.move = function() {
      var exceedLeftBorder, exceedRightBorder, margin, step;
      step = 8;
      step = this.moveDirection === "right" ? -step : step;
      margin = (parseInt(this.elements.list.css("margin-left"), 10)) + step;
      exceedRightBorder = this.moveDirection === "right" && (this.rightButtonOffset > this.elements.lastImage.prop('offsetLeft'));
      exceedLeftBorder = this.moveDirection === "left" && (0 < margin);
      if (exceedLeftBorder || exceedRightBorder) {
        return;
      }
      this.elements.list.css("margin-left", margin + "px");
      return this._moveTimer = setTimeout((__bind(function() {
        return this.move();
      }, this)), 40);
    };
    return Flow;
  })();
  return this.each(function() {
    return $(this).data('bannerFlow', new Flow($(this), $.extend({}, defaults, o)));
  });
};
$.fn.pageGallery = function(o) {
  var Gallery, defaults;
  defaults = {
    selectors: {
      bigImage: '.b-page-gallery-big',
      links: '.b-page-gallery-link',
      thumbs: '.b-page-gallery-list li a'
    }
  };
  Gallery = (function() {
    function Gallery(container, options) {
      var key, value, _ref;
      this.container = container;
      this.options = options;
      this.elements = {};
      _ref = this.options.selectors;
      for (key in _ref) {
        value = _ref[key];
        this.elements[key] = this.container.find(value);
      }
      this.bindControls();
    }
    Gallery.prototype.bindControls = function() {
      var elements;
      elements = this.elements;
      return this.elements.thumbs.each(function() {
        return $(this).click(function(ev) {
          var fname;
          ev.preventDefault();
          fname = $(this).data('fname');
          elements.links.hide().filter('[data-fname="' + fname + '"]').show();
          elements.thumbs.removeClass('active');
          return $(this).addClass('active');
        });
      });
    };
    return Gallery;
  })();
  return this.each(function() {
    return $(this).data('gallery', new Gallery($(this), $.extend({}, defaults, o)));
  });
};
$.fn.slideShow = function(o) {
  var SlideShow, defaults;
  defaults = {
    images: '.b-past-event-list img',
    dots: '.b-past-event-dot',
    dotSelected: 'b-past-event-dot-selected',
    startDelay: 5000,
    normalDelay: 5000,
    clickDelay: 15000
  };
  SlideShow = (function() {
    function SlideShow(container, options) {
      var i, _ref;
      this.container = container;
      this.options = options;
      this.images = this.container.find(options.images);
      this.dots = this.container.find(options.dots);
      this._currentDot = null;
      this._currentImage = null;
      this._currentIdx = 0;
      this._timer = null;
      if (this.images.length !== this.dots.length) {
        console.log('number of dots and image is not equal!');
      }
      for (i = 0, _ref = this.images.length - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
        $(this.dots.get(i)).data('imgNum', i);
      }
      this.bindEvents();
      setTimeout((__bind(function() {
        return this.init();
      }, this)), 1000);
    }
    SlideShow.prototype.bindEvents = function() {
      var self;
      self = this;
      return this.dots.click(function(ev) {
        var $this, idx;
        $this = $(this);
        idx = $this.data('imgNum');
        return self.toggleImage(idx, self.options.clickDelay, true);
      });
    };
    SlideShow.prototype.init = function() {
      var height, parent, width;
      parent = this.images.closest('li');
      width = parent.width();
      height = parent.height();
      parent.css({
        width: width + "px",
        height: height + "px",
        position: "relative"
      });
      this.images.css({
        position: "absolute",
        left: "0px",
        top: "0px"
      });
      return this.toggleImage(0, this.options.startDelay);
    };
    SlideShow.prototype.toggleImage = function(idx, delay, disableAnimation) {
      var newDot, newImage;
      if (disableAnimation == null) {
        disableAnimation = false;
      }
      clearTimeout(this._timer);
      if (idx >= this.images.length) {
        idx = 0;
      }
      if (!this._currentImage) {
        this._currentImage = $(this.images.get(this._currentIdx));
        this._currentDot = $(this.dots.get(this._currentIdx));
      }
      if (idx !== this._currentIdx) {
        this._currentDot.removeClass(this.options.dotSelected);
        this._currentIdx = idx;
        newImage = $(this.images.get(idx));
        newDot = $(this.dots.get(idx));
        if (disableAnimation) {
          this._currentImage.hide();
          newImage.show();
          newDot.addClass(this.options.dotSelected);
          this._updateTimer(idx, delay);
        } else {
          this._currentImage.fadeOut(500);
          newImage.fadeIn(500, __bind(function() {
            this._currentDot.addClass(this.options.dotSelected);
            return this._updateTimer(idx, delay);
          }, this));
        }
        this._currentImage = newImage;
        return this._currentDot = newDot;
      } else {
        return this._updateTimer(idx, delay);
      }
    };
    SlideShow.prototype._updateTimer = function(idx, delay) {
      return this._timer = setTimeout((__bind(function() {
        return this.toggleImage(++idx, this.options.normalDelay);
      }, this)), delay);
    };
    return SlideShow;
  })();
  return this.each(function() {
    return $(this).data('slideshow', new SlideShow($(this), $.extend({}, defaults, o)));
  });
};
jQuery(function($) {
  var $bg, aspectRatio, content, resizeBg, theWindow;
  $('.b-past-event').slideShow().find('.b-past-event-image').fancybox({
    hideOnContentClick: true,
    cyclic: true
  });
  $('.m-single-page').pageGallery();
  $('.b-clients').bannerFlow();
  content = $('#video-content');
  if ($('#video-content').length) {
    if (content.hasClass('youtube')) {
      $('.video-link').click(function(ev) {
        $.fancybox({
          type: 'iframe',
          href: this.href.replace('youtu.be', 'youtube.com/embed'),
          width: 640,
          height: 360,
          autoScale: false,
          autoDimensions: false
        });
        return false;
      });
    } else {
      flowplayer("video-content", "/flowplayer-3.2.7.swf", {
        clip: {
          autoPlay: false
        }
      });
      $('.video-link').fancybox({
        content: content,
        width: parseInt(content.css('width'), 10) + 5,
        height: parseInt(content.css('height'), 10) + 5,
        autoScale: false,
        autoDimensions: false
      });
    }
  }
  theWindow = $(window);
  $bg = $("#bg");
  aspectRatio = $bg.width() / $bg.height();
  resizeBg = function() {
    if ((theWindow.width() / theWindow.height()) < aspectRatio) {
      return $bg.removeClass().addClass('bgheight');
    } else {
      return $bg.removeClass().addClass('bgwidth');
    }
  };
  theWindow.resize(resizeBg).trigger("resize");
  $('.b-topmenu-nav').spasticNav();
  return $('.b-page-gallery-link').fancybox();
});
