//MH
(function( $ ){
	$.fn.imagefader = function( options ) {  
		var settings = {
			'speed' : 600,
			'wait'	: 2000,
			'transition' : { 'hidden':{'opacity':0},'visible':{'opacity':1} },
			'start':'random',
			'easing':false
		};
		
		return this.each(function() {        
			if ( options ) { 
				$.extend( settings, options );
			}
			//PEBKAC proofing:
			settings.speed=parseInt(settings.speed);
			settings.wait=parseInt(settings.wait);
			
			var $this=$(this);

			if ( $this.children().length < 2  ) { return ; }
			
			function fade(){
				var $active=$this.children('.active');
				$active
					.removeClass('active')
					.animate(settings.transition.hidden,{
						'duration':settings.speed,
						'easing':settings.easing?settings.easing:undefined
					});
				if ( $active.next().length == 1 ) {
					$active=$active.next();
				}else{
					$active=$this.children(':first');
				}
				$active
					.addClass('active')
					.animate(settings.transition.visible,{
						'duration':settings.speed,
						'easing':settings.easing?settings.easing:undefined,
						'complete' : function(){window.setTimeout(function(){fade()},settings.wait);}
					});
			}
			

			$this
				.css({'position':'relative'})
				.children()
					.css({'position':'absolute','top':0,'left':0})
					.css(settings.transition.hidden)
				;
			if ( settings.start === 'random' ){
				settings.start=(Math.random()*$this.children().length)+1;
			}			
			settings.start=parseInt(settings.start);
			$this
				.children(':nth-child('+settings.start+')')
				.show()
				.addClass('active')
				.css(settings.transition.visible);
			
			window.setTimeout(function(){
				fade()
			},settings.wait);
			
		});
	};
})( jQuery );

