/**
 * Gallery with sliding thumbs
 *
 * http://www.whiteoctober.co.uk
 * Dave Fletcher
 *
 * Licensed under the MIT (MIT-LICENSE.txt)
 *
 *
 */

jQuery.iSlidingGallery = {
	
	// properties
	imageContainer: null,
	thumbSlider: null,
	thumbArray: null,
	thumb: null,
	imagePreloader: null,

	/**
	 * selects a thumbnail
	 */	
	selectThumb: function() {
		
		$(this.mainImage).hide();
		this.gallery.imagePreloader.src = this.href;	
		this.gallery.currentThumb = this;
		return false;
	},
	
	/**
  * Preloads the image
  */
	centerThumbs: function(gallery) {
		
		// we have the current thumb, so if it's not first, or last, we need to make sure it's
		// in the middle.  This is about counting how far in it is basically.
		
		// count how far in it is!
		if (gallery.currentThumb != null)
		{
			thumbIndex = 0;
			var numThumbs = gallery.thumbs.length;
			thumbIndex = gallery.thumbs.index(gallery.currentThumb);
			
			if (numThumbs> gallery.galleryOptions.thumbCount)
			{
				if (thumbIndex <= Math.floor(gallery.galleryOptions.thumbCount/2)) 
				{
					var intMove = 0;
				}
				else if (thumbIndex > (numThumbs - gallery.galleryOptions.thumbCount + 1) && numThumbs > gallery.galleryOptions.thumbCount)  
				{
					var intMove = ((numThumbs-gallery.galleryOptions.thumbCount)*gallery.galleryOptions.thumbWidth);
				}
				else 
				{
					var intMove = ((thumbIndex-Math.floor(gallery.galleryOptions.thumbCount/2))*gallery.galleryOptions.thumbWidth);
				}
				
				gallery.thumbslider.animate({left: -intMove}, "fast");
				
			}
			gallery.whichpage.text((thumbIndex+1));
			
			
		}

	},

	/**
  * Performs all the actions once we've preloaded the image
  */
	nextThumb: function() {
		
		jQuery.iSlidingGallery.moveThumb(this.gallery, 1);
		return false;
	},	

	/**
  * Performs all the actions once we've preloaded the image
  */
	prevThumb: function() {
		
		jQuery.iSlidingGallery.moveThumb(this.gallery, -1);
		return false;
		
	},	

	moveThumb: function (gallery, intMove)
	{
		if (gallery.currentThumb != null)
		{
			// get the thumb index
			var numThumbs = gallery.thumbs.length;
			thumbindex = gallery.thumbs.index(gallery.currentThumb);
			newIndex = thumbindex + intMove;
			if (newIndex >= 0 && newIndex < numThumbs)
			{
				gallery.mainImage.hide();
				gallery.imagePreloader.src = gallery.thumbs.get(newIndex).href;
				gallery.currentThumb = gallery.thumbs.get(newIndex);
			}
			
		}
		else if (intMove > 0)
		{
			gallery.mainImage.hide();
			gallery.imagePreloader.src = gallery.thumbs.get(1).href;
			gallery.currentThumb = gallery.thumbs.get(1);
		}
	},

	/**
  * Performs all the actions once we've preloaded the image
  */
	replaceImage: function() {

		this.gallery.mainImage.attr('src', this.src);
		
		jQuery.iSlidingGallery.centerThumbs(this.gallery);
		jQuery.iSlidingGallery.resizeImageContainer(this.gallery, this.height);
		
	},	

	/**
  * Resizes the image container
  */
	resizeImageContainer: function(gallery, heightNew) {

		gallery.mainpiccontainer.animate({height: heightNew},400,'linear',function()
		{
			gallery.mainImage.fadeIn();
		});
		
	},

	
	/**
	 * Builds the gallery
	 */
	innit: function(options) {
		if (!options) {
			return;
		}
		
		return this.each(
			function() {
				
				el = this;
				el.galleryOptions = options;
				
				// bind the click event to the selectThumb function on each thumbnail
				$(this).find('.thumbslider a').bind('click', jQuery.iSlidingGallery.selectThumb).each( function()
				{
					this.gallery = el;
					this.mainImage = $(el).find('.mainpiccontainer img');

					//this.imageContainer = $(el).find('.mainpiccontainer');
					//this.thumbSlider = $(el).find('.thumbslider');
				
				});
				this.thumbs = $(this).find('.thumbslider a');
				this.thumbslider = $(this).find('.thumbslider');
				this.mainpiccontainer = $(this).find('.mainpiccontainer');
				this.mainImage = $(this).find('.mainpiccontainer img');
				this.whichpage = $(this).find('.whichpage .thispage');
				this.imagePreloader = new Image();
				this.imagePreloader.gallery = this;
				
				$(this.imagePreloader).bind('load', jQuery.iSlidingGallery.replaceImage);
				
				
				$(this).find('a.prevthumbscontrolbutton').bind('click', jQuery.iSlidingGallery.prevThumb).each( function()
				{
					this.gallery = el;
				});
				
				$(this).find('a.nextthumbscontrolbutton').bind('click', jQuery.iSlidingGallery.nextThumb).each( function()
				{
					this.gallery = el;
				});
				
				this.imagePreloader.src = this.mainImage.attr('src');	
				//if (this.mainImage.height() > 0) this.mainpiccontainer.height(this.mainImage.height());
				
			}
		);
	}

};

jQuery.fn.extend ({
	
	SlidingGallery: jQuery.iSlidingGallery.innit
	
});