/*!
 * NB Brian - External reference: note that the variable $.ss.debug has been added to facilitate debugging. 
 * jQuery.SerialScroll
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 06/14/2009
 *
 * @projectDescription Animated scrolling of series.
 * @author Ariel Flesler
 * @version 1.2.2
 *
 * @id jQuery.serialScroll
 * @id jQuery.fn.serialScroll
 * @param {Object} settings Hash of settings, it is passed in to jQuery.ScrollTo, none is required.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @link {http://flesler.blogspot.com/2008/02/jqueryserialscroll.html Homepage}
 *
 * Notes:
 *	- The plugin requires jQuery.ScrollTo.
 *	- The hash of settings, is passed to jQuery.ScrollTo, so its settings can be used as well.
 */
;(function( $ ){

	var $serialScroll = $.serialScroll = function( settings ){
		return $(window).serialScroll( settings );
	};

	// Many of these defaults, belong to jQuery.ScrollTo, check it's demo for an example of each option.
	// @link {http://demos.flesler.com/jquery/scrollTo/ ScrollTo's Demo}
	$serialScroll.defaults = {// the defaults are public and can be overriden.
		duration:2000, // how long to animate.
		axis:'xy', // which of top and left should be scrolled
		event:'click', // on which event to react.
		start:0, // first element (zero-based index)
		step:1, // how many elements to scroll on each action
		lock:false,// ignore events if already animating
		cycle:true, // cycle endlessly ( constant velocity )
		constant:true, // use contant speed ?
		lazy:true
		/*
		items : "ul.active li" 
		navigation:null,// if specified, it's a selector a collection of items to navigate the container
		target:window, // if specified, it's a selector to the element to be scrolled.
		interval:0, // it's the number of milliseconds to automatically go to the next
		lazy:false,// go find the elements each time (allows AJAX or JS content, or reordering)
		stop:false, // stop any previous animations to avoid queueing
		force:false,// force the scroll to the first element on start ?
		jump: false,// if true, when the event is triggered on an element, the pane scrolls to it
		items:null, // selector to the items (relative to the matched elements)
		prev:null, // selector to the 'prev' button
		next:null, // selector to the 'next' button
		onBefore: function(){}, // function called before scrolling, if it returns false, the event is ignored
		exclude:0 // exclude the last x elements, so we cannot scroll past the end
		*/
	};

	$.fn.serialScroll = function( options ){
		return this.each(function(){
			//alert("in serial scroll init "+options.target);
			var 
				settings = $.extend( {}, $serialScroll.defaults, options ),
				event = settings.event, // this one is just to get shorter code when compressed
				step = settings.step, // ditto
				lazy = settings.lazy, // ditto
				context = settings.target ? this : document, // if a target is specified, then everything's relative to 'this'.
				$pane = $(settings.target || this, context),// the element to be scrolled (will carry all the events)
				pane = $pane[0], // will be reused, save it into a variable
				items = settings.items, // will hold a lazy list of elements
				active = settings.start, // active index
				auto = settings.interval, // boolean, do auto or not
				nav = settings.navigation, // save it now to make the code shorter
				timer; // holds the interval id

			//alert("initialization of serial scroll "+settings.target+" pane-"+$pane+" "+$($pane).attr("id"));

			if( !lazy )// if not lazy, save the items now
				items = getItems();



			if( settings.force )
				jump( {}, active );// generate an initial call

			// Button binding, optional
			$(settings.prev||[], context).bind( event, -step, move );
			$(settings.next||[], context).bind( event, step, move );
			
			
			// Custom events bound to the container
			// NB Brian Note that for the poetry slide show i'm not using the next and previous bindings.
			// I write my own next and previous.
			if (!pane){
				//alert("pane is undefined. this shouldn't happen in serial scroll but does in chrome and opera");
				//$pane=this;
				//pane = $pane[0];
				//pane=$("#poems-inner");
			}
			if( !pane.ssbound )// don't bind more than once
				$pane
					.bind('prev.serialScroll', -step, move ) // you can trigger with just 'prev'
					.bind('next.serialScroll', step, move ) // f.e: $(container).trigger('next');
					.bind('goto.serialScroll', jump ) // f.e: $(container).trigger('goto', 4 );
					.bind('setactive.serialScroll', setactive ) // f.e: $(container).trigger('setactive', 1); //added by brian macmillan
					.bind('getactive.serialScroll', getactive ) // f.e: $(container).trigger('getactive'); //added by brian macmillan
					.bind('setduration.serialScroll', setduration ); // f.e: $(container).trigger('setactive', 1); //added by brian macmillan

			if( auto )
				$pane
					.bind('start.serialScroll', function(e){
						if( !auto ){
							clear();
							auto = true;
							next();
						}
					 })
					.bind('stop.serialScroll', function(){// stop a current animation
						clear();
						auto = false;
					});

			$pane.bind('notify.serialScroll', function(e, elem){// let serialScroll know that the index changed externally
				//alert("in serialscroll notify");
				var i = index(elem);
				if( i > -1 )
					active = i;
				//alert("in notify active -"+active);
			});

			pane.ssbound = true;// avoid many bindings

			if( settings.jump )// can't use jump if using lazy items and a non-bubbling event
				(lazy ? $pane : getItems()).bind( event, function( e ){
					jump( e, index(e.target) );
				});

			if( nav )
				nav = $(nav, context).bind(event, function( e ){
					e.data = Math.round(getItems().length / nav.length) * nav.index(this);
					jump( e, this );
				});
			//setActive added by Brian MacMillan. Can use notify instead - $(selector).trigger('notify.serialScroll',[iActiveSlide]);
			function setactive(e,psIndex){
				//alert("in serial scroll setactive");
				active = psIndex;
				//e.data = psIndex;
			};
			function getactive(){
				//alert("in serial scroll getactive "+active);
				//NB Brian can't get active to return as a integer even though the alert displays it as an integer
				return active;
				//e.data = psIndex;
			};
			//setduration added by Brian Macmillan to handle wrap issues. see bm_poems.js
			function setduration(e,piValue){
				settings.duration = piValue;
			};			
			function move( e ){
				e.data += active;
				jump( e, this );
			};
			function jump( e, button ){
				/* NB Brian - !isNaN(button) caused an issue with all browsers but firefox, when using next and prev buttons*/
				//when this function is called internally, button has no value, when called externally
				//button is the current slideshow container object.
				//alert("in ss jump "+$(button).attr("id"));
				if (typeof button == "number"){ //!isNaN(button) ){// initial or special call from the outside $(container).trigger('goto',[index]);
					e.data = button;
					button = pane;
				}
				
				var
					pos = e.data, n,
					real = e.type, // is a real event triggering ?
					$items = settings.exclude ? getItems().slice(0,-settings.exclude) : getItems(),// handle a possible exclude
					limit = $items.length,
					elem = $items[pos],
					duration = settings.duration;
			
				//alert("in jump button-"+$(button).attr("id")+"; current index-"+active+"; next index-"+pos+"; real-"+real+"; elem id "+$(elem).attr("id")+"; duration-" +duration);


				if( real )// real event object
					e.preventDefault();

				if( auto ){
					clear();// clear any possible automatic scrolling.
					timer = setTimeout( next, settings.interval ); 
				}

				if( !elem ){ // exceeded the limits
					//NB Brian - alter this so it loops
					n = pos < 0 ? 0 : limit - 1;
					if( active != n )// we exceeded for the first time
						pos = n;
					else if( !settings.cycle )// this is a bad case
						return;
					else
						pos = limit - n - 1;// invert, go to the other side
					elem = $items[pos];
				}
				//NB Brian - added by brian macmillan 2010/08/27
				$(elem).addClass("active");
				
				//if ($.ss.debug){alert("in serial scroll; animated-"+$pane.is(':animated')+" elem-"+!elem+" duration-"+duration+" lock-"+settings.lock+" real-"+real)};
				//alert("in serial scroll; current pos-"+active+" next pos-"+pos+"animated-"+$pane.is(':animated')+"elem-"+!elem+" duration-"+duration+" lock-"+settings.lock+" real-"+real);
				
				//alert("in serial scroll; animated-"+$pane.is(':animated')+" elem-"+!elem+" duration-"+duration+" lock-"+settings.lock+" real-"+real);
				if( !elem || settings.lock && $pane.is(':animated') || // no animations while busy
					real && settings.onBefore &&
					settings.onBefore(e, elem, $pane, getItems(), pos) === false ) return;

				if( settings.stop )
				{
					//alert("in ss stop");
					$pane.queue('fx',[]).stop();// remove all its animations
				}
				if( settings.constant )
				{
					//alert("ss settings are constant");
					duration = Math.abs(duration/step * (active - pos ));// keep constant velocity
				}
				//if ($.ss.debug){
					//alert("before scroll -items-"+$(items).size()+" elem-"+$(elem).attr("id")+" duration-"+duration+" "+settings.target+" next position-"+pos+" current position-"+active+" $pane "+$($pane).attr("id"));
				//};
				$pane
					.scrollTo( elem, duration, settings )// do scroll
					.trigger('notify.serialScroll',[pos]);// in case serialScroll was called on this elem more than once.
				
			};

			function next(){// I'll use the namespace to avoid conflicts
				$pane.trigger('next.serialScroll');
				//alert("in next");
			};

			function clear(){
				clearTimeout(timer);
			};

			function getItems(){
				//alert("getitems "+$(items).attr("id"));
				//alert("items and pane "+items+" "+pane));
				//alert("pane "+$( pane ).attr("id"));
				//alert("items "+$($cs.)
				//alert($(items).eq($(items).length-1).attr("id"));
				return $(items,pane);
				//return $( items, $($(pane).attr("id")+ " ul.active"));
			};

			function index( elem ){
				//alert("index");
				if( !isNaN(elem) ) return elem;// number
				var $items = getItems(), i;
				while(( i = $items.index(elem)) == -1 && elem != pane )// see if it matches or one of its ancestors
					elem = elem.parentNode;
				return i;
			};
		});
	};

})( jQuery );
