﻿(function($) {
  var scrolling = false;
  var firstTime = true;

  function scroll(container, itemWidth, speed, direction, opts) {
    if (!scrolling) {
      scrolling = true;
      firstLI = container.find('li').first();
      lastLI = container.find('li').last();
      //firstLI.find('a').css('border-color', '#b0052c');
      //firstLI.find('a').css('border-width', '4px');
      //lastLI.find('a').css('border-color', '#b0052c');
      //lastLI.find('a').css('border-width', '4px');

      if (direction == 'left') {
        var firstClone = container.find('li').first().clone();
        firstClone.appendTo(container);

        firstClone.find('a').click(function(event) {
          event.preventDefault();
          opts.onElemClick.call(this);
        });
        firstClone.find('a').mouseenter(function() {
          opts.onElemMouseOver.call(this);
        });
        firstClone.find('a').mouseleave(function() {
          opts.onElemMouseOut.call(this);
        });

        container.animate({
          left: '-=' + itemWidth
        }, speed, function() {
          container.find('li').first().remove();
          //container.find('li').eq(0).find('a').css('border-color', '#03608c');
          //container.find('li').eq(0).find('a').css('border-width', '5px');
          container.css('left', 0);
          changeImage(container, opts);
          scrolling = false;
        });
      } else if (direction == 'right') {
        container.css('left', -itemWidth);
        var lastClone = container.find('li').last().clone();

        lastClone.find('a').click(function(event) {
          event.preventDefault();
          opts.onElemClick.call(this);
        });
        lastClone.find('a').mouseenter(function() {
          opts.onElemMouseOver.call(this);
        });
        lastClone.find('a').mouseleave(function() {
          opts.onElemMouseOut.call(this);
        });

        lastClone.prependTo(container);

        container.animate({
          left: '+=' + itemWidth
        }, speed, function() {
          container.find('li').last().remove();
          //container.find('li').eq(0).find('a').css('border-color', '#03608c');
          //container.find('li').eq(0).find('a').css('border-width', '5px');
          container.css('left', 0);
          changeImage(container, opts);
          scrolling = false;
        });
      }
    }
  }

  function autoScroll(container, itemWidth, speed, direction, opts) {
    scroll(container, itemWidth, opts.scrollSpeed * 2, 'left', opts);
  }

  function changeImage(container, opts) {
    var theClass = container.find('li').eq(0).find('a').attr('class');
    var theElem = container.find('.' + theClass);
    opts.onElemClick.call(theElem);
  }

  // Binds this plugin to the specified control
  // opts are the options specified by the user
  $.fn.pbsCarousel = function(opts) {
    new $jc(this, opts);
  };

  // Constructor
  // sender is the object this plugin is bound to
  // opts are the options passed by the initialiser
  $.pbsCarousel = function(sender, opts) {
    var defaults = {
      itemsUntilAnimate: 5,
      scrollSpeed: 1000,
      auto: 0,
      autoSpeed: 6000,
      onElemClick: function() { },
      onElemMouseOver: function() { },
      onElemMouseOut: function() { }
    };
    opts = $.extend({}, defaults, opts);

    var leftScroller = sender.find('div').eq(0);
    var container = sender.find('div').eq(1).find('ul');
    var rightScroller = sender.find('div').eq(2);
    var itemWidth = container.find('li').width();

    $.each(container.find('li'), function() {
      $(this).find('a').click(function(event) {
        event.preventDefault();
        opts.onElemClick.call(this);
      });

      $(this).find('a').mouseenter(function() {
        opts.onElemMouseOver.call(this);
      });

      $(this).find('a').mouseleave(function() {
        opts.onElemMouseOut.call(this);
      });
    });

    //container.find('li').eq(0).find('a').css('border-color', '#03608c');
    //container.find('li').eq(0).find('a').css('border-width', '5px');
    var interval = null;
    if (opts.auto == 1) {
      interval = setInterval(function() {
        autoScroll(container, itemWidth, opts.scrollSpeed * 2, 'left', opts);
      }, opts.autoSpeed);
    }

    if (interval != null) {
      container.parent().parent().parent().hover(function() {
        clearInterval(interval);
      }, function() {
        interval = setInterval(function() {
          autoScroll(container, itemWidth, opts.scrollSpeed * 2, 'left', opts);
        }, opts.autoSpeed);
      });
    }

    leftScroller.click(function() {
      if (!scrolling) {
        scroll(container, itemWidth, opts.scrollSpeed, 'left', opts);
      }
    });

    rightScroller.click(function() {
      if (!scrolling) {
        scroll(container, itemWidth, opts.scrollSpeed, 'right', opts);
      }
    });
  };

  // Internal reference to the constructor, just for ease
  var $jc = $.pbsCarousel;
})(jQuery);
