(function( $ ){
	$.fn.spidey = function( options ) {
		
		var settings = {
			'pixels_per_second':		50,
			'delay_between_phases':		4.5,			//	in seconds
			'min_time_between_slides':	5,				//	in seconds
			'margin_of_forgiveness':	75,				//	if image is n px taller or shorter than canvas, no spidey, just stretch
			'minimum_slide_time':		5,				//	no matter how short the image, we'll stay on it for at least this many seconds
			'easing':					'easeOutCubic',	//	aleks prefers "easeOutCirc"
			'onMove':		function() {},
			'onFadeOut':	function() {},
			'onReset':		function() {}
		}
	
		var methods = {
			'kill':	function() {
				return this.each(function(index) {
					$(this).stop(true,false).fadeOut('fast',settings.onFadeOut.call(this));
					$(this).css('top',0);
				});
			},
			'reset': function() {
				return this.each(function(index) {
					$(this).css('top',0);
					$(this).stop(true,true).fadeIn('fast',settings.onReset.call(this));
				});
			},
			'getdims': function(o) {
				return {
					'imgheight':		o.height(),
					'containerheight':	o.closest('div').height(),
					'bottomposition':	o.closest('div').height() +- o.height()
				}
			},
			'init': function() {
				var props = methods.getdims($(this));
				//	overflow
				$(this).closest('div').css({ overflow: 'hidden' });
				$(this).css('position','relative');
				//	stagger
				$(this).filter(':even').css('top',0);
				$(this).filter(':odd').css('top',props.bottomposition);
				return this;
			},
			'pause': function() {
				$(this).stop();
			}
		}
	
	
		function move_it(o,props) {
			
			var	currentposition = 0,
				targetposition	= 0,
				speed			= parseInt( 1000 * ((props.imgheight +- props.containerheight) / settings.pixels_per_second) );
				delay 			= parseInt( 1000 * settings.delay_between_phases );;
			
			currentposition	= parseInt(o.css('top'));
			if (currentposition == 0) targetposition = props.bottomposition;
			
			if ( ((speed + delay) * 2) < (settings.min_time_between_slides * 1000)) {
				speed = parseInt( ((settings.min_time_between_slides * 1000) - delay) / 2 );
				console.log('image was so short that we had to change the value of `speed` according to `settings.min_time_between_slides`');
			}
			
			$(o).delay(delay).animate({top:targetposition}, {
				duration:	speed,
				easing:		settings.easing,
				complete:	function() {
					settings.onMove.call(this);
					move_it($(this),props);
				}
			});
		}	
	
		if ( typeof options === 'string' ) { 

			return methods[options].apply(this);
		}
		
		else {
		 
			return this.each(function(index) {
			
				var $this = $(this), props = methods.getdims($this);
				
				if (options) { $.extend(settings,options); }
				
				if ( props.bottomposition > settings.margin_of_forgiveness ) {
					//	image is way shorter than the canvas area
					//console.log('image is way shorter than the canvas area',props);
					$this.attr('height',props.containerheight);
					$this.css('height',props.containerheight);
				}
				
				else if (props.bottomposition > 0) {
					//	image is shorter than the canvas area, but not by much
					//console.log('image is shorter than the canvas area, but not by much',props);
					$this.attr('height',props.containerheight);
					$this.css('height',props.containerheight);
				}
				
				else if ( (props.bottomposition * -1) < settings.margin_of_forgiveness) {
					//	image is taller than canvas, but not forgivably
					//console.log('image is taller than canvas, but not forgivably',props);
				}
	
				else {
					//	spidey
					//console.log('spidey!',props);
					methods.init.call(this);
					move_it($this,props);
				}
			});
			
		}
	};
})( jQuery );
