var MAX_SUBIMAGES = 11;

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}


function prepareGallery() {
  // Check that we can find the thumbnails
  if ($("#imageGalleryThumbs").length == 0) {
    return false;
  }


  // Add the onclick callback for all thumbnail links
  $("#imageGalleryThumbs a").click(function() {
      return showImage($(this));
    });
  $("#imageGalleryThumbs a").keypress(function() {
      return showImage($(this));
    });


  // Add the onclick callback for all pagination links
  $("#galleryPagination li a").click(function() {
      return showSubImage($(this));
    });
  $("#galleryPagination li a").keypress(function() {
      return showSubImage($(this));
    });


  // Set all pagination link URLs to "#" to avoid confusion
  // since this script does not update the URLs when the
  // destination images change.
  $("#galleryPagination li a").attr('href', '#');
}


function showImage(newPic) {
  var paginationEnabled = ($("#galleryPagination").length > 0);

  // Load the image(s)
  newPic.parent().find(".images img").each(function(i, image) {
      var url = $(image).attr('src');
      var description = $(image).attr('title');
      var placeholder_id = ( i == 0 ? "#placeholder" : "#placeholder-sub-"+(i-1) );
      var description_id = ( i == 0 ? "#imageDescription" : "#imageDescription-sub-"+(i-1) );

      $(placeholder_id).show().attr("src", url);
      $(description_id).show().html(description);
    });


  // Hide the subimage placeholders that are not being used
  var numSubimages = newPic.parent().find(".images img").length - 1;
  var firstToHide = paginationEnabled ? 0 : numSubimages;
  for (var i=firstToHide; i<MAX_SUBIMAGES; i++) {
    $("#placeholder-sub-"+i).hide();
    $("#imageDescription-sub-"+i).hide();
  }


  // Load the new image title
  if ($("#imageTitle").length) {
    var text = newPic.attr("title");
    $("#imageTitle").html(text);
  }


  // If pagination is enabled, update the number of pagination links shown
  if (paginationEnabled) {
    for (var i=0; i<=MAX_SUBIMAGES; i++) {
      var elem = $("#galleryPagination li").eq(i);

      // Remove current class from all elements
      elem.removeClass("current");

      // We want to display numSubimages+1 links
      // Unless there are no subimages to select, then show no links
      if (i <= numSubimages && numSubimages > 0) {
        elem.show();
      } else {
        elem.hide();
      }
    }

    // Set the first item as selected
    $("#galleryPagination li:first").addClass("current");
  }


  return false;
}


function showSubImage(link) {
  // Get the number of the link that was clicked on
  var numClicked = parseInt(link.html()) - 1;

  // Hide/show each placeholder image and description
  for (var i=0; i<=MAX_SUBIMAGES; i++) {
    var placeholder_id = ( i == 0 ? "#placeholder" : "#placeholder-sub-"+(i-1) );
    var description_id = ( i == 0 ? "#imageDescription" : "#imageDescription-sub-"+(i-1) );
    var pagination_link = $("#galleryPagination li").eq(i);

    if (i == numClicked) {
      $(placeholder_id).show();
      $(description_id).show();
      pagination_link.addClass("current");
    } else {
      $(placeholder_id).hide();
      $(description_id).hide();
      pagination_link.removeClass("current");
    }
  }


  return false;
}


$(document).ready(function() {
    prepareGallery();
  });

