﻿$(document).ready(function() {

    var $slidymidythabio = $('.obituaries-slideshow ul').obituriesSlideShow({
		'speed':1000,
        'pause':3000
	});
	
    $('#moveleft', this).click(function () {
        $slidymidythabio.trigger('prev');
    });
    $('#moveright', this).click(function () {
        $slidymidythabio.trigger('next');
    });

});
/*
 * jQuery plug-in space
 */
(function($) {
	/*
	 * Sets the element to a relative position (if not done using CSS)
	 * Sets the children to absolute positioning
	 * Transitions visibility between the children using a animated effect 
	 * */
	$.fn.obituriesSlideShow = function(params) {
		var defaults = {
			speed:1000,
			pause:3000,
			startIndex:0,
			transitionCallback:function (index, $curr) {}
		};
		var t =  $.extend(defaults, params);
			
		return this.each(function () {
			
			var $this = $(this);
			var $children = $this.children();
			var length = $children.length;
			
			if ( length !== 1 ) {
				
				var slideInterval;
				var index = t.startIndex;
				
				var transition = function () {
					var $prev = $($children.get( (length + index-1)%length ));
					var $curr = $($children.get( index ));
					var $next = $($children.get( (index+1)%length ));
					
					$children.fadeOut(t.speed, function () {});
					$curr.fadeIn(t.speed,function () {});
					t.transitionCallback(index, $curr);
					index = (index + 1)%length;
				};
				
				slideInterval = setInterval ( transition, t.speed + t.pause );
				transition();
				
				$this.bind('next', function () {
					clearInterval( slideInterval );
					
					index = (index)%length;
					transition();
					slideInterval = setInterval( transition, t.speed + t.pause );
					
				}).bind('prev', function () {
					clearInterval( slideInterval );
					
					index = (length + index - 2)%length;
					transition();
					slideInterval = setInterval( transition, t.speed + t.pause );
					
				}).bind('jump', function (e, position) {
					clearInterval( slideInterval );
					
					index = position;
					transition();
					slideInterval = setInterval( transition, t.speed + t.pause );
				});
			} else {
				$children.show();
			}
			
			$this.css({
				'position':'relative'
			});
			$children.css({
				'position':'absolute',
				'overflow':'hidden',
				'width': $this.width() + 'px',
				'height': $this.height() + 'px',
				'line-height': $this.height() + 'px'
			});
		});

	};
})(jQuery);
