$(document).ready(function () {

  /* Featured content rotator */
    cache = {
      home : {
        c_nav 		: $('.slides-nav a'),
        c_target	: $('.teaser-carousel')
      }
    };

    cache.home.c_nav.each(function(ix) {
      var self	= $(this);
      var target	= $(cache.home.c_target.find('li')[ix]);

      self.attr('rel', (ix+1));
    });

    var carousel_itemVisibleInCallbackBeforeAnimation = function(carousel, item, ix, state)
    {
      var total	= cache.home.c_nav.length;
      var current	= (ix % cache.home.c_nav.length);

      if (current==0)	{ current = total; }
      if (current<0)	{ current = total+current; }

      var container	= $(cache.home.c_nav).parent(0);
      var self		= container.find('a[rel="'+current+'"]');
      var counter		= container.parent(0).parent(0).siblings('div.slides-nav-counter-wrapper').find('.slides-nav-counter');

      cache.home.c_nav.removeClass('active');
      self.addClass('active');

      counter.html(current+' of '+container.length);

      return false;
    };

    var carousel_initCallback = function(carousel)
    {
      cache.home.c_nav.bind('click', function(evt) {
        evt.preventDefault();

        var ix = $(this).attr('rel');

        carousel.scroll($.jcarousel.intval(ix));
        carousel.startAuto(0); // Disable autoscrolling
      });

      /*
      cache.home.c_target.find('a.next:first').bind('click', function(evt) {
        evt.preventDefault();

        carousel.startAuto(0); // Disable autoscrolling
        carousel.next();
      });

      cache.home.c_target.find('a.prev:first').bind('click', function(evt) {
        evt.preventDefault();

        carousel.startAuto(0); // Disable autoscrolling
        carousel.prev();
      });
      */

        // Pause autoscrolling if user mouseovers current clip
        carousel.clip.hover(
          function() { carousel.stopAuto(); },
          function() { carousel.stopAuto(); }
          //function() { carousel.startAuto(); }
        );

    };

    if (cache.home.c_target.length)
    {
      cache.home.c_target.jcarousel({
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
        scroll: 1,
        auto: 8,
        wrap: 'last',
        initCallback: carousel_initCallback,
        itemVisibleInCallback: {
          onBeforeAnimation: carousel_itemVisibleInCallbackBeforeAnimation
          //onAfterAnimation:  carousel_itemVisibleInCallbackAfterAnimation
        }
      });
    }


});

