jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

(function($){
  $.fn.PhotoGallery = function(options) {
  
    options = jQuery.extend({
      idNumber:"#"
    }, options);
    var pageCount;
    var shellWidth;
    var shellHeight;
    var imgThumbs;
    var obj;
  
    return this.each(function() {
      obj = $(this);

      pageCount = $("div.imgThumbnails", obj).length;
      shellWidth = $("table.galleryImgFullShell td", obj).width();
      shellHeight = $("table.galleryImgFullShell td", obj).height();
      imgThumbs = $("a.galleryImgThumb", obj);
      
      //only apply slide effect if there is more than one page
      if (pageCount > 1)
      {
        //apply slide effect
        $("#widgetPhotoGallery"+options.idNumber).serialScroll({
          target:  "#imgThumbnails"+options.idNumber,
          items:  'div.imgThumbnails', // Selector to the items ( relative to the matched elements, '#sections' in this case )
          prev:  "#imgPreviousNextShell"+options.idNumber+" a.imgPrevious",// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
          next:"#imgPreviousNextShell"+options.idNumber+" a.imgNext",// Selector to the 'next' button (absolute too)
          axis:  "x",// The default is 'y' scroll on both ways
          navigation:  "#navigation li a",
          duration:700,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
          force:true // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
        });
      }
      else
      {
        //if only 1 page then hide 'previous' and 'next' links
        $("table.imgPreviousNextShell", obj).hide();
      }
      
      //apply click event to thumbnails
      $(imgThumbs).click(function(){
        $(imgThumbs).removeClass("selected");
        $(this).addClass("selected");
        LoadFullImage(shellWidth, shellHeight, this);
        return false;
      });     
      
      //load first image
      $("a.galleryImgThumb:first", obj).click();
    });
    
    function LoadFullImage(shellWidth, shellHeight, link)
    {
      var img = new Image();
	    img.onload = function() {
		    $(this).attr("id", "galleryImgFull");
		    FitImageToShell(shellWidth, shellHeight, this);
		    $("table.galleryImgFullShell td", obj).empty().append(this);
	    };
	    img.src = link.href;  
	    //set description of large image
	    if (link.title != "")
	      $("p.imgDescription", obj).text(link.title);
	    else
	      $("p.imgDescription", obj).text("\u00a0");//&nbsp;
    }//LoadFullImage

    function FitImageToShell(shellWidth, shellHeight, img)
    {
      var imgWidth = img.width;
      var imgHeight = img.height;
      
      if (imgWidth > shellWidth-10)
      {
        var sizeRatio = imgHeight/imgWidth;
        var newWidth = shellWidth - 10;//-10 just to make sure it fits
        var newHeight = newWidth * sizeRatio;
        $(img).attr("width", newWidth);
        $(img).attr("height", newHeight);
        if (newHeight > shellHeight)
          FitImageToShell(shellWidth, shellHeight, img);
      }
      else if (imgHeight > shellHeight-10)
      {
        var sizeRatio = imgWidth/imgHeight;
        var newHeight = shellHeight - 10;//-10 just to make sure it fits
        var newWidth = newHeight * sizeRatio;
        $(img).attr("width", newWidth);
        $(img).attr("height", newHeight);
        if (newWidth > shellWidth)
          FitImageToShell(shellWidth, shellHeight, img);
      }
    }//FitImageToShell
    
  };
})(jQuery);
