/**
 * jQuery.ScrollShow - Scrolling Slideshow.
 * Copyright (c) 2007 Ariel Flesler - aflesler(at)gmail(dot)com
 * Licensed under GPL license (http://www.opensource.org/licenses/gpl-license.php).
 * Date: 10/30/2007
 * @author Ariel Flesler
 * @modified mngr, 2009
 *
 * @id jQuery.fn.scrollShow
 * @param {Object} settings Hash of settings (detailed below).
 * @return {jQuery} Returns the same jQuery object, for chaining.
 **/
(function( $ ){
	
	var defaults = {
		elements:'img',//elements selector (relative to view)
		view:null,//container of the elements, the one to fix the width.
		navigators:null,//selector to the elements to navigate the slideshow.
		speed:600,//speed of transition, 1 for no-animation
		disabledClass:false,//style class for the disabled navigator (false if none)
		easing:'linear',//easing equation
		axis:'x',//axis to displace the slideshow
		margin:true,//take margin in account?
		setWidth:false,//whether to calculate and set, the overall width of the slideshow.
		setHeight:false,//whether to calculate and set, the height of view.
		trimLast:0,//pixels, if can trim last photo by width
		wrapper:null//some global container of gallery
	};
	
	$.fn.scrollShow = function( settings ){
		settings = $.extend( {}, defaults, settings );
		
		return this.each(function(){
			var 
				$view	  = $(settings.view),
				$elements = $(settings.elements, $view),
				$wrapper = $(settings.wrapper),
				limit	  = $elements.length-1,
				active	  = 0;
				disabled = false;
			
			$(settings.navigators).eq(0).bind('click', { dir: -1 }, sequential );
			$(settings.navigators).eq(1).bind('click', { dir: +1 }, sequential );

			if (settings.setWidth){
				var w = 0;
				container = $view.children();
				$elements.each(function(){
					w+=$(this).outerWidth({margin:true});
				});
				container.width(w);
			}
			
			function toggleDisabledClass(){
				$(settings.navigators)
					.eq(0).toggleClass(settings.disabledClass,((disabled)||(active==0))).end()
					.eq(1).toggleClass(settings.disabledClass,((disabled)||(active==limit) || ($view.children().width()-$view.attr('scrollLeft')-$wrapper.width()<settings.trimLast)));
			}
			
			function needNavigation(){
				disabled = ($view.children().width()<=$view.width());
				toggleDisabledClass();
			}

			needNavigation();
			$(window).resize(needNavigation);
			
			if (settings.setHeight){
				var h = 0;
				$elements.each(function(){
					h=Math.max(h,$(this).outerHeight({margin:true}));
				});
				$view.height(h);
			}
			
			function sequential( event ){
				if (!disabled){
					var pos = active + event.data.dir;
					if ((pos<0) || (pos>limit) || ((pos==limit)&&($view.children().width()-$view.attr('scrollLeft')-$wrapper.width()<settings.trimLast))) return false;
					else {
						disabled = true;
						$view.scrollTo( $elements[pos], $.extend(settings,{
							onAfter: function(){
								disabled=false;
								toggleDisabledClass();
							}
						}));
						toggleDisabledClass();
						active = pos;
					}
				}
				return false;
			};
		});
	};
		  
})( jQuery );