/* Helper file to get the gallery setup for jCarousel,
   plus the loading of jCarousel and the setup of the
   overlaid captions */
$(document).ready(function() {
	initGallery();
});
function initGallery() {

  // insert previous and next buttons, using them to control
  // clicking of next and previous photos
  $("div.photo-gallery").append('<p id="photo-nav"><a id="prev" href="#">&lt;Previous</a> | <a id="next" href="#">Next&gt;</a></p>');
  
  $("#photo-nav a").click(function() {
    // which way are we going?
    var direction = $(this).attr("id");
    
    if (direction == "next") {
      if ($("div.photo-gallery ul li a.selected").parent("li").next("li").length == 0) {
        // loop back around to the last item
        $("div.photo-gallery ul li:first a").click();
      } else {
        $("div.photo-gallery ul li a.selected").parent("li").next("li").children("a").click();
      }
    } else {
      if ($("div.photo-gallery ul li a.selected").parent("li").prev("li").length == 0) {
        // loop back around to the first item
        $("div.photo-gallery ul li:last a").click();
      } else {
        $("div.photo-gallery ul li a.selected").parent("li").prev("li").children("a").click();
      }
    }
    return false;
  });

  // load the first caption if one exists
  var caption = $("div.photo-gallery ul li:first img").attr("alt");
  if ($.trim(caption) != "") {
    // display caption
    $("div.photo-gallery div.main-image").append('<p class="caption">' + $.trim(caption) + '</p>');
  }
  
  // mark the first photo as selected
  $("div.photo-gallery ul li:first a").addClass("selected");
  
  // capture links of the thumbnails to larger images
  // and instead display them in the gallery viewer
  $("div.photo-gallery ul li a").click(function() {
    var thumbImg = $(this);
    var thumbImgEl = this;
    $("div.photo-gallery ul li a").removeClass("selected");
    $("div.photo-gallery .main-image img").fadeOut("normal", function() {
      $(this).attr("src", thumbImg.attr("href")).fadeIn("normal", function() {
        var caption = thumbImg.children("img").attr("alt");
        if ($.trim(caption) != "") {
          if ($("div.photo-gallery p.caption").length != 0) {
            $("div.photo-gallery p.caption").text($.trim(caption)).slideDown();
          } else {
            // previous photo did not have an image, so let's recreate the caption
            $("div.photo-gallery div.main-image").append('<p class="caption" style="display: none;">' + $.trim(caption) + '</p>');
            $("div.photo-gallery p.caption").slideDown();
          }
        } else {
          // get rid of the caption from the DOM
          $("div.photo-gallery p.caption").remove();
        }
      });
    });
    
    // load up the caption, if there is one
    $("div.photo-gallery p.caption").slideUp();
    
    // and mark the item as selected, removing all other items from
    // being selected
    $("div.photo-gallery ul li a").each(function() {
      if (this != thumbImgEl) {
        $(this).children("img").fadeTo("normal", 0.35);
      } else {
        $(this).children("img").fadeTo("normal", 1.00);
        $(this).addClass("selected");
      }
    });
    
    return false;
  });
}