From e510010e0a7ec1ccf1595d946d88b543ada973bb Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 11 Jan 2012 18:43:01 -0500
Subject: [PATCH] Issue #1401890: Set the correct pager page when a custom
 start position is used.

---
 js/jcarousel.js |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/js/jcarousel.js b/js/jcarousel.js
index ac1a5b8..23c7e91 100644
--- a/js/jcarousel.js
+++ b/js/jcarousel.js
@@ -51,7 +51,7 @@ Drupal.behaviors.jcarousel.attach = function(context, settings) {
       options.setupCallback = function(carousel) {
         Drupal.jcarousel.setupCarousel(carousel);
         if (options.navigation) {
-          Drupal.jcarousel.addNavigation(carousel, options.navigation);
+          Drupal.jcarousel.addNavigation(carousel, options.navigation, options.start);
         }
       };
       if (options.navigation && !options.itemVisibleInCallback) {
@@ -160,7 +160,7 @@ Drupal.jcarousel.setupCarousel = function(carousel) {
 /**
  * Setup callback for jCarousel. Adds the navigation to the carousel if enabled.
  */
-Drupal.jcarousel.addNavigation = function(carousel, position) {
+Drupal.jcarousel.addNavigation = function(carousel, position, start) {
   // Don't add a pager if there's only one page of results.
   if (carousel.pageCount <= 1) {
     return;
@@ -171,14 +171,28 @@ Drupal.jcarousel.addNavigation = function(carousel, position) {
 
   var navigation = $('<ul class="jcarousel-navigation"></ul>');
 
+  // If options.start is not 1, we need to calculate what page to make active.
+  // jCarousel is strange in that as soon as start is greater than 1, it makes
+  // the start index be on the second page. But, this doesn't propagate through
+  // to subsequent pages. That means that if the original index of start is
+  // on the first or second pages, it will always be on the second page.
+  // Subsequent start positions will be on additional pages.
+  var active_page = Math.ceil(start / carousel.pageSize) + 1;
+
   for (var i = 1; i <= carousel.pageCount; i++) {
     var pagerItem = $(Drupal.theme('jCarouselPageLink', i));
     var listItem = $('<li></li>').attr('jcarousel-page', i).append(pagerItem);
     navigation.append(listItem);
 
-    // Make the first page active by default.
-    if (i === 1) {
+    // Make the appropriate page active.
+    if (i === active_page) {
       listItem.addClass('active');
+      carousel.pageNumber = i;
     }
 
     // Scroll to the correct page when a pager is clicked.
-- 
1.7.7.4
