/*
 * jQuery infinitecarousel plugin
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 1.2.2
 * @date August 31, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 admin@catchmyfame.com (www.catchmyfame.com)
 * @license CC Attribution-Share Alike 3.0 - http://creativecommons.org/licenses/by-sa/3.0/
 */

(function ($) {
  $.fn.extend({
    infiniteCarousel: function (options) {
      var defaults =
			{
			  transitionSpeed: 1500,
			  displayTime: 6000,
			  displayThumbnails: 1
			};
      var options = $.extend(defaults, options);

      return this.each(function () {
        var randID = Math.round(Math.random() * 100000000);
        var o = options;
        var obj = $(this);
        var curr = 1;

        var numImages = $('li', obj).length; // Number of images
        var imgHeight = 253;
        var imgWidth = 675;
        var autopilot = 1;

        $(obj).width(imgWidth).height(imgHeight);

        $('ul', obj).css('left', -imgWidth + 'px');
        $('ul', obj).width(9999);


        // Build thumbnail viewer and thumbnail divs
        $('<div class="SlideshowControls" id="thumbs' + randID + '"></div>').insertAfter('#Slideshow');
        for (i = 0; i <= numImages - 1; i++) {
          $('#thumbs' + randID).append('<div class="SlideNav" id="thumb' + randID + '_' + (i + 1) + '">' + (i + 1) + '</div>');
          if (i == 0) $('#thumb' + randID + '_1').css({ 'background-position:': '0px -14px' });
        }
        $('#thumbs' + randID + ' div.SlideNav:not(:first)').css({ 'background-position': '0px 0px' });
        // Assign click handler for the thumbnails. Normally the format $('.thumb') would work but since it's outside of our object (obj) it would get called multiple times
        $('#thumbs' + randID + ' div').bind('click', thumbclick); // We use bind instead of just plain click so that we can repeatedly remove and reattach the handler


        function thumbclick(event) {
          target_num = this.id.split('_'); // we want target_num[1]
          if (curr != target_num[1]) {
            $('#thumb' + randID + '_' + curr).css({ 'background-position': '0px 0px' });
            clearTimeout(clearInt);
            $('#thumbs' + randID + ' div').css({ 'cursor': 'default' }).unbind('click'); // Unbind the thumbnail click event until the transition has ended
            autopilot = 0;
          }
          if (target_num[1] > curr) {
            diff = target_num[1] - curr;
            anim('next', diff);
          }
          if (target_num[1] < curr) {
            diff = curr - target_num[1];
            anim('prev', diff);
          }
        }

        function borderpatrol(elem) {
          $('#thumbs' + randID + ' div').css({ 'background-position': '0px 0px' });
          setTimeout(function () { elem.css({ 'background-position': '0px -14px' }); }, o.transitionSpeed);
        }

        function anim(direction, dist) {
          if (direction == "next") {
            if (curr == numImages) curr = 0;
            if (dist > 1) {
              borderpatrol($('#thumb' + randID + '_' + (curr + dist)));
              $('li:lt(2)', obj).clone().insertAfter($('li:last', obj));
              $('ul', obj).animate({ left: -imgWidth * (dist + 1) }, o.transitionSpeed, function () {
                $('li:lt(2)', obj).remove();
                for (j = 1; j <= dist - 2; j++) {
                  $('li:first', obj).clone().insertAfter($('li:last', obj));
                  $('li:first', obj).remove();
                }
                $(this).css({ 'left': -imgWidth });
                curr = curr + dist;
                $('#thumbs' + randID + ' div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
              });
            }
            else {
              borderpatrol($('#thumb' + randID + '_' + (curr + 1)));
              $('#thumbs' + randID + ' div').css({ 'cursor': 'default' }).unbind('click'); // Unbind the thumbnail click event until the transition has ended
              // Copy leftmost (first) li and insert it after the last li
              $('li:first', obj).clone().insertAfter($('li:last', obj));
              // Update width and left position of ul and animate ul to the left
              $('ul', obj)
								.animate({ left: -imgWidth * 2 }, o.transitionSpeed, function () {
								  $('li:first', obj).remove();
								  $('ul', obj).css('left', -imgWidth + 'px');
								  curr = curr + 1;
								  $('#thumbs' + randID + ' div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
								});
            }
          }
          if (direction == "prev") {
            if (dist > 1) {
              borderpatrol($('#thumb' + randID + '_' + (curr - dist)));
              $('li:gt(' + (numImages - (dist + 1)) + ')', obj).clone().insertBefore($('li:first', obj));
              $('ul', obj).css({ 'left': (-imgWidth * (dist + 1)) }).animate({ left: -imgWidth }, o.transitionSpeed, function () {
                $('li:gt(' + (numImages - 1) + ')', obj).remove();
                curr = curr - dist;
                $('#thumbs' + randID + ' div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
              });
            }
            else {
              borderpatrol($('#thumb' + randID + '_' + (curr - 1)));
              $('#thumbs' + randID + ' div').css({ 'cursor': 'default' }).unbind('click'); // Unbind the thumbnail click event until the transition has ended
              // Copy rightmost (last) li and insert it after the first li
              $('li:last', obj).clone().insertBefore($('li:first', obj));
              // Update width and left position of ul and animate ul to the right
              $('ul', obj)
								.css('left', -imgWidth * 2 + 'px')
								.animate({ left: -imgWidth }, o.transitionSpeed, function () {
								  $('li:last', obj).remove();
								  curr = curr - 1;
								  if (curr == 0) curr = numImages;
								  $('#thumbs' + randID + ' div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
								});
            }
          }
        }

        var clearInt = setInterval(function () { anim('next'); }, o.displayTime + o.transitionSpeed);
      });
    }
  });
})(jQuery);
