( function( $ ) {
	$.fn.imageScroller = function ( options ) {
		return this.each( function() {
			var $this = $( this );
			var loadImgs = 0;
			var loadTrys = 0;
			var IntE = (navigator.userAgent.toLowerCase().indexOf("msie") > -1);
			$( "img" , $this ).load(
				function () {
					loadImgs++;
				}
			);
			var opt = $.extend( 
				{ 
					  speed: "4000"
				}
				, options || {}
			);
			
			$this.children().hide();
			$this.append(
				"<div style='clear:both; padding: 0px; margin: 0px;'>" + 
				"<div id='loading'>Loading images...</div>" + 
				"</div>"
			);
			
			var intVal = window.setInterval(
				function () {
					loadTrys++;
					if ( loadImgs == $( "img" , $this ).length || loadTrys == 30 || IntE) {
						window.clearInterval( intVal );
						$( "#loading" ).remove();
						$this.children().show();
						var totImg = 0;
			
						$.each(
							  $this.children( ":not(div)" )
							, function () {
							
								$( this ).css( "display" , "block" );
								if ( $( this ).children().length ) {
									$( this ).height( $( this ).children( ":eq(0)" ).height() );
								}
								totImg += $( this ).height();
										
								
								$( this ).css({
									  margin:  "0px"
									, padding: "0px"
									, clear:   "both"
								});
								
								$( this ).bind(
									  "mouseover"
									, function () {
										$( "div:eq(0)" , $this ).stop();
									}
								).bind(
									  "mouseout"
									, function () {
										scrollStart( $( "div:eq(0)" , $this ) , opt );
									}
								);
								
								$( "div:eq(0)" , $this ).append( $( this ) );
							}
						);
						$( "div:eq(0)" , $this ).css( "height" , totImg + "px" );
						scrollStart( $( "div:eq(0)" , $this ) , opt );
					}
				}
				, 100
			);
			
			function scrollStart ( $scroll , opt ) {
				var pos = 0;
				var tos = -( $scroll.children( ":eq(0)" ).height() );
				var spd = opt.speed - ( Math.abs ( parseInt( $scroll.css( "marginTop" ) ) ) * ( opt.speed / $scroll.children( ":eq(0)" ).height() ) );
				
				$scroll.animate(
					{
						  marginLeft: ( pos || "0" ) + "px"
						, marginTop: ( tos || "0" ) + "px"
					}
					, spd
					, "linear"
					, function () {
						$scroll.append( $( this ).children( ":eq(0)" ) );
						$scroll.css( "marginTop" , "0px" );						
						scrollStart( $scroll , opt );
					}
				);
			};
		});
	};
})(jQuery);