/**
 * Part of jsGallery package.
 *
 * @package jsGallery
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @version 1.0.0 build 080812
 * @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 integer current image
  */
  var nowShowing = 0;
  
 /**
  * @var string id of container element for list of selection links
  */
  var selectId = "#piclist";
  
  
 /**
  * @var string id of container element for captions
  */
  var captionId = "#picturecaption";
  
 /**
  * @var string id of container div for displayed gallery images
  */
  var viewerId = "#display";
  
 /**
  * @var string id of gallery image element
  */
  var showImgId = "#current";
  
 /**
  * @var string id of back button (need not be present)
  */
  var backButton = "#back";

 /**
  * @var string id of next button (need not be present)
  */
  var nextButton = "#next";
  
 
  
 /**
  * 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;
    $("#nPics").html(nImages);
    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);
  });

 /**
  * 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()
    {
      removeCurrentPicClass();
      $(this).parent().addClass("currentpic");
      $(this).parent().next().addClass("currentpic");
      var imageId = $(this).attr("id");
      showImage(imageId.substring(3));
      return false;
    });
  });
  
/**
  * Onclick for back button
  *
  * Checks to see if button exists
  * Removes current image class from all links and then adds class to current pic.
  * 
  * @return false to prevent normal link action
  */
  $(function()
  {
    if ($(backButton).length == 0) return;
    $(backButton).click(function()
    {
      if (nowShowing <= 0)
      {
        return false;
      }
      nowShowing --;
      setButtonClass(); 
      showImage(nowShowing);
      removeCurrentPicClass();
      nsFormatted = (nowShowing <= 8) ? '0' + (nowShowing + 1) : nowShowing + 1;
      $("#currentRef").html(nsFormatted);
      $("#pic" + nowShowing).parent().addClass("currentpic");
      $("#pic" + nowShowing).parent().next().addClass("currentpic");     
      return false;
    });
  });
  
/**
  * Onclick for next button
  *
  * Checks to see if button exists
  * 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()
  {
    if ($(nextButton).length == 0) return;
    $(nextButton).click(function()
    {
      if (nowShowing >= nImages - 1)
      {
        return false;
      }
      nowShowing ++;
      setButtonClass();
      showImage(nowShowing);
      removeCurrentPicClass();
      nsFormatted = (nowShowing <= 8) ? '0' + (nowShowing + 1) : nowShowing + 1;
      $("#currentRef").html(nsFormatted);
      $("#pic" + nowShowing).parent().addClass("currentpic");
      $("#pic" + nowShowing).parent().next().addClass("currentpic");
      return false;
    });
  });
  
/**
 * Remove currentpic class from all dt and dd
 */
 function removeCurrentPicClass()
 {
   $(selectId + " dt").each(function()
      {
        $(this).removeClass("currentpic");
        $(this).next().removeClass("currentpic");
      });
 }
 
/**
 * Add buttonon and buttonoff classes as appropriate
 *
 */
 function setButtonClass()
 {
   if (nowShowing == 0)
   {
     $(backButton).removeClass("buttonon");
     $(backButton).addClass("buttonoff");
   }
   if (nowShowing >= 1)
   {
     $(backButton).removeClass("buttonoff");
     $(backButton).addClass("buttonon");
   }
   if (nowShowing <= nImages - 2)
   {
     $(nextButton).removeClass("buttonoff");
     $(nextButton).addClass("buttonon");
   }
   if (nowShowing == nImages - 1)
   {
     $(nextButton).removeClass("buttonon");
     $(nextButton).addClass("buttonoff");
   }
 }

/**
 * 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
      //$("#piclist #pic" + i).addClass("loaded");
      if (i+1 < nImages)
      {
        preLoad(i+1);
      }
    };
    imageArray[i].src = hrefArray[i];
  }

