/**
 * jQuery-Plugin "photoSlider"
 * 
 * @version: 1.0, 11.12.2009
 * 
 * @author: Stijn Van Minnebruggen
 *          stijn@donotfold.be
 *          http://www.donotfold.be
 * 
 * @example: $('selector').photoSlider();
 * @example: $('selector').photoSlider({ animationSpeed: 800 });
 * 
 */

(function($) {

$.fn.photoSlider = function(settings) {
	
	/**
	 * Settings
	 * 
	 */
	
	settings = $.extend({
		
		animationSpeed: 800,
		easingEffect: "easeInOutCubic",
		disabledClass: "disabled",
		
		thumbWidth: 47,
		thumbsWidth: 235,
		imageWidth: 281
		
	}, settings);
	
	
	/**
	 * loop each photoslider
	 * 
	 */
	
	return $(this).each(function() {
		
		/**
		 * Set elements
		 * 
		 */
		
		var el = $(this);
		var photos = el.children('.photoSliderImgs').children('ul');
		var thumbs = el.children('.photoSliderThumbs').children('ul');
		
		
		/**
		 * Add prev and next buttons
		 * 
		 */
		
		var buttons = '<a href="#" class="prev"></a><a href="#" class="next"></a>';
		el.prepend(buttons);
		var prev = el.children('a.prev');
		var next = el.children('a.next');
		
		
		/**
		 * Button actions
		 * 
		 */
		
		prev.click(function() {
			thumbGoPrev();
			return false;
		});
		
		next.click(function() {
			thumbGoNext();
			return false;
		});
		
		
		/**
		 * Thumbnail actions
		 * 
		 */
		
		thumbs.children('li').each(function(i) {
			$(this).click(function() {
				showImg(i);
			});
		});
		
		
		/**
		 * Go to previous thumbnail page
		 * 
		 */
		
		var thumbGoPrev = function() {
			
			// get new position
				var currX = parseFloat(thumbs.css('marginLeft'));
				var newX = currX + settings.thumbsWidth;
			
			// move the thumbs
				if(newX <= 0) thumbs.animate({ "marginLeft": newX }, settings.animationSpeed, settings.easingEffect, function() { enableDisableButtons(); });
			
		};
		
		
		/**
		 * Go to next thumbnail page
		 * 
		 */
		
		var thumbGoNext = function() {
			
			// get new position
				var currX = parseFloat(thumbs.css('marginLeft'));
				var newX = currX + -( settings.thumbsWidth );
			
			// move the thumbs
				if(newX > -( thumbs.width() )) thumbs.animate({ "marginLeft": newX }, settings.animationSpeed, settings.easingEffect, function() { enableDisableButtons(); });
			
		};
		
		
		/**
		 * Show a specific image
		 * 
		 */
		
		var showImg = function(i) {
			var newX = -(i * settings.imageWidth);
			photos.animate({ "marginLeft": newX }, { "duration": settings.animationSpeed, "easing": settings.easingEffect });
		};
		
		
		/**
		 * Enable or disable the buttons
		 * 
		 */
		
		var enableDisableButtons = function() {
			
			// reset previous values
				next.removeClass(settings.disabledClass);
				prev.removeClass(settings.disabledClass);
			
			// disable if theres no need to scroll
				if(settings.thumbsWidth > newW) {
					next.addClass(settings.disabledClass);
					prev.addClass(settings.disabledClass);
				}
			
			// check thumb position
				var xPos = parseFloat(thumbs.css('marginLeft'));
				if(xPos == 0) prev.addClass(settings.disabledClass);
				if(xPos <= -( thumbs.width() - settings.thumbsWidth )) next.addClass(settings.disabledClass);
			
		};
		
		
		/**
		 * Set thumbs width and disable buttons if needed
		 * 
		 */
		
		var newW = thumbs.children('li').size() * settings.thumbWidth;
		thumbs.css('width', newW);
		enableDisableButtons();
		
		
	});
	
};

})(jQuery);
