﻿(function($) {
  $.fn.leadFeature = function() {

    var id;
    var articleCol;
    var countRef, totalRef;
    var oneCurrent = 0, twoCurrent = 0, threeCurrent = 0;
    var oneTotal, twoTotal, threeTotal;
    var current;
    var timeoutVar;

    function nextClicked() {
      setTimeout("", 5000);
      clearTimeout(timeoutVar);
      showNext(current);
      return false;
    }

    function prevClicked() {
      setTimeout("", 5000);
      clearTimeout(timeoutVar);
      showPrev(current);
      return false;
    }

    function tabOneClicked() {
      oneTotal = displaySet(this, 'lfTabOne', oneCurrent);
      return false;
    }

    function tabTwoClicked() {
      twoTotal = displaySet(this, 'lfTabTwo', twoCurrent);
      return false;
    }

    function tabThreeClicked() {
      threeTotal = displaySet(this, 'lfTabThree', threeCurrent);
      return false;
    }

    function switchTabClass(onItem) {
      $(id + ' .tabOne').removeClass('on');
      $(id + ' .tabTwo').removeClass('on');
      $(id + ' .tabThree').removeClass('on');
      $(onItem).addClass('on');
    }

    function displaySet(tab, markerClass, iCur) {
      switchTabClass(tab);
      $(articleCol).addClass('hide');
      $(articleCol).filter('.' + markerClass).eq(iCur).removeClass('hide');
      $(countRef).text(iCur + 1);
      var count = $(articleCol).filter('.' + markerClass).size();
      $(totalRef).text(count.toString());
      current = markerClass;
      return count;
    }

    function showNext(markerClass) {
      if (markerClass == 'lfTabOne') {
        oneCurrent = oneCurrent + 1 > (oneTotal - 1) ? 0 : oneCurrent + 1;
        $(articleCol).addClass('hide');
        $(articleCol).filter('.' + markerClass).eq(oneCurrent).removeClass('hide');
        $(countRef).text(oneCurrent + 1);
      } else if (markerClass == 'lfTabTwo') {
        twoCurrent = twoCurrent + 1 > (twoTotal - 1) ? 0 : twoCurrent + 1;
        $(articleCol).addClass('hide');
        $(articleCol).filter('.' + markerClass).eq(twoCurrent).removeClass('hide');
        $(countRef).text(twoCurrent + 1);
      } else if (markerClass == 'lfTabThree') {
        threeCurrent = threeCurrent + 1 > (threeTotal - 1) ? 0 : threeCurrent + 1;
        $(articleCol).addClass('hide');
        $(articleCol).filter('.' + markerClass).eq(threeCurrent).removeClass('hide');
        $(countRef).text(threeCurrent + 1);
      }
      timeoutVar = setTimeout(function() {
        runNext();
      }, 5000);
    }

    function showPrev(markerClass) {
      if (markerClass == 'lfTabOne') {
        oneCurrent = oneCurrent - 1 < 0 ? oneTotal - 1 : oneCurrent - 1;
        $(articleCol).addClass('hide');
        $(articleCol).filter('.' + markerClass).eq(oneCurrent).removeClass('hide');
        $(countRef).text(oneCurrent + 1);
      } else if (markerClass == 'lfTabTwo') {
        twoCurrent = twoCurrent - 1 < 0 ? twoTotal - 1 : twoCurrent - 1;
        $(articleCol).addClass('hide');
        $(articleCol).filter('.' + markerClass).eq(twoCurrent).removeClass('hide');
        $(countRef).text(twoCurrent + 1);
      } else if (markerClass == 'lfTabThree') {
        threeCurrent = threeCurrent - 1 < 0 ? threeTotal - 1 : threeCurrent - 1;
        $(articleCol).addClass('hide');
        $(articleCol).filter('.' + markerClass).eq(threeCurrent).removeClass('hide');
        $(countRef).text(threeCurrent + 1);
      }
      timeoutVar = setTimeout(function() {
        runNext();
      }, 5000);
    }

    this.each(function() {
      id = '#' + $(this).attr('id');
      $(id + ' .tabOne').addClass('on');

      articleCol = $(id + " .tabHolder");

      countRef = $(id + ' .count2');
      totalRef = $(id + ' .total');

      $(id + ' .lfNext').click(nextClicked);
      $(id + ' .lfPrev').click(prevClicked);

      $(id + ' .tabOne').click(tabOneClicked);
      $(id + ' .tabTwo').click(tabTwoClicked);
      $(id + ' .tabThree').click(tabThreeClicked);

      $(id + ' .tabOne').trigger('click');
    });
    
    function runNext() {
      $(id + ' .lfNext:first').trigger('click');
    }
    
    timeoutVar = setTimeout(function() {
      runNext();
    }, 5000);
    
    return this;
  };
})(jQuery);