/**
 * Part of jsGallery package.
 *
 * @package jsGallery
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @version 1.0.0 build 080710
 * @copyright Copyright (c) 2008, J. Hardie Associates
 */
 
 /**
  * @var array of image objects
  */
  var imageArray = new Array();
  
 /**
  * @var array string href attributes of image selector links
  */
  var hrefArray = new Array();
  
 /**
  * @var array string image captions
  */
  var captionArray = new Array();
  
 /**
  * @var array boolean set by preload callback
  */
  var isPreloaded = new Array();
  
 /**
  * @var integer number of images
  */
  var nImages;
  
 /**
  * @var string id of container (dl) element for list of titles and captions
  */
  var pictureTextId = "#picturetext";
  
 /**
  * @var string id of container element for list of selection links
  */
  var selectId = "#picselect";
  
  
 /**
  * @var string id of container element for captions
  */
  var captionId = "#picturecaption";
  
 /**
  * @var string id of gallery image element
  */
  var showImgId = "#current";
  
 
  
 /**
  * Populate imageLinks, hrefArray, captionArray and isPreloaded. Set-off the preload chain and show first image and caption.
  *
  * @return void
  */
  $(function()
  {
    var imageLinks = $(selectId + " a");
    nImages = imageLinks.length;
    imageLinks.each(function(i)
    {
      hrefArray[i] = $(this).attr("href");
      isPreloaded[i] = false;
    });
    var imageCaptionElements = $(selectId + " dd");
    imageCaptionElements.each(function(i)
    {
      captionArray[i] = $(this).html();
    });
    preLoad(0);
    showImage(0);
    $(imageLinks[0]).addClass("currentpic");
    $(captionId).html(captionArray[0]);
  });

 /**
  * Onclick for image selection links
  *
  * Removes current image class from all links and then adds class to clicked image.
  * Calculates numeric id by removing first three characters from the image id. Calls showImage()
  * @return false to prevent normal link action
  */
  $(function()
  {
    $(selectId + " a").click(function()
    {
      $(selectId + " a").each(function()
      {
        $(this).removeClass("currentpic");
      });
      $(this).addClass("currentpic");
      var imageId = $(this).attr("id");
      showImage(imageId.substring(3));
      return false;
    });
  });

/**
 * Show image
 * Checks if image is preloaded and sets new src attribute appropriately. Sets caption
 *
 * @param integer image number
 * @return void
 */
  function showImage(i)
  {
    if (isPreloaded[i])
    {
      src = imageArray[i].src;
    }
    else
    {
      src = hrefArray[i];
    }
    imageCaption = captionArray[i];
    $(showImgId).attr("src", src); 
    $(captionId).html(imageCaption);
  }

 /**
  * Recursive function to preload all images
  * Note that image object onload must be set before src
  *
  * @param integer image number
  * @return void
  */ 
  function preLoad(i)
  {
    // first check for no images
    if (i >= nImages) return;
    imageArray[i] = new Image();
    imageArray[i].onload = function()
    {
      isPreloaded[i] = true;
      // option to show style change when image is loaded
      //$("#picselect #pic" + i).addClass("loaded");
      if (i+1 < nImages)
      {
        preLoad(i+1);
      }
    };
    imageArray[i].src = hrefArray[i];
  }
