///////////////////////////////////////////////////////////////////////
// imageScroller (1.1), Copyright (C) 2008 - 2009 Max Kiusso
//
// Autor :		Max Kiusso - kiussoATgmailDOTcom
// Date :		2008 12 01
// Modified:	2009 11 23
//
// REQUIRES jQuery 1.2+ <http://jquery.com/>
//
// Features:
// 		This software provide to create a multidirectional image
//		scroller with mouse events
//
// Configuration:	$J( "#div" ).imageScroller(  )
//					
//					options:	speed (millisecond)
//								loading (text)
//								direction (left, right, top, bottom)
//
// New in release 1.1:
//		- bug fix for preload images
///////////////////////////////////////////////////////////////////////

( function( $J ) {
	$J.fn.imageScroller = function ( options ) {
		return this.each( function() {
			var $Jthis = $J( this );
			var loadImgs = 0;
			
			var opt = $J.extend( 
				{ 
					  speed: "3000"
					, loading: "Bitte warten, Sponsoren werden geladen..." 
					, direction: "top"
				}
				, options || {}
			);
			
			$Jthis.children().hide();
			$Jthis.append(
				"<div style='clear:both; padding: 0px; margin: 0px;'>" + 
				"<div id='loading'>" + opt.loading + "</div>" + 
				"</div>"
			);
			
			$J( "img" , $Jthis ).each(
				function () {
					var img = new Image();
					var soc = $J( this ).attr( 'src' );
					
					$J( img ).load(
						function () {
							loadImgs++;
						}
					).attr( "src" , soc );
				}
			);
			
			var intVal = window.setInterval(
				function () {
					if ( loadImgs == $J( "img" , $Jthis ).length ) {
						window.clearInterval( intVal );
						$J( "#loading" ).remove();
						$Jthis.children().show();
						var totImg = 0;
			
						$J.each(
							  $Jthis.children( ":not(div)" )
							, function () {
								switch ( opt.direction ) {
									case 'left':
									case 'right':
										if ( $J( this ).children().length ) {
											$J( this ).width( $J( this ).children( ":eq(0)" ).width() );
										}
										totImg += $J( this ).width();
										break;
									case 'top':
									case 'bottom':
										$J( this ).css( "display" , "block" );
										if ( $J( this ).children().length ) {
											$J( this ).height( $J( this ).children( ":eq(0)" ).height() );
										}
										totImg += $J( this ).height();
										break;
								}
								
								$J( this ).css({
									  margin:  "0px"
									, padding: "0px"
									, clear:   "both"
								});
								
								$J( this ).bind(
									  "mouseover"
									, function () {
										$J( "div:eq(0)" , $Jthis ).stop();
									}
								).bind(
									  "mouseout"
									, function () {
										scrollStart( $J( "div:eq(0)" , $Jthis ) , opt );
									}
								);
								
								$J( "div:eq(0)" , $Jthis ).append( $J( this ) );
							}
						);
						
						switch ( opt.direction ) {
							case 'left':
								$J( "div:eq(0)" , $Jthis ).css( "width" , totImg + "px" );
								break;
							
							case 'right':
								$J( "div:eq(0)" , $Jthis ).css( "width" , totImg + "px" );
								$J( "div:eq(0)" , $Jthis ).css({
									marginLeft: -( totImg - $Jthis.width() ) + "px"
								});
								break;
								
							case 'top':
								$J( "div:eq(0)" , $Jthis ).css( "height" , totImg + "px" );
								break;
								
							case 'bottom':
								$J( "div:eq(0)" , $Jthis ).css( "height" , totImg + "px" );
								$J( "div:eq(0)" , $Jthis ).css({
									marginTop: -( totImg - $Jthis.height() ) + "px"
								});
								break;
						}
	
						scrollStart( $J( "div:eq(0)" , $Jthis ) , opt );
					}
				}
				, 100
			);
			
			function scrollStart ( $Jscroll , opt ) {
				switch ( opt.direction ) {
					case 'left':
						var pos = -( $Jscroll.children( ":eq(0)" ).width() );
						var spd = opt.speed - ( Math.abs ( parseInt( $Jscroll.css( "marginLeft" ) ) ) * ( opt.speed / $Jscroll.children( ":eq(0)" ).width() ) );
						break;
						
					case 'right':
						var pos = -( $Jscroll.width() - $Jscroll.parents( "div:eq(0)" ).width() ) + $Jscroll.children( ":last" ).width();
						var spd = opt.speed - ( ( $Jscroll.children( ":last" ).width() - ( Math.abs ( parseInt( $Jscroll.css( "marginLeft" ) ) ) - Math.abs ( pos ) ) ) * ( opt.speed / $Jscroll.children( ":last" ).width() ) );
						break;
						
					case 'top':
						var tos = -( $Jscroll.children( ":eq(0)" ).height() );
						var spd = opt.speed - ( Math.abs ( parseInt( $Jscroll.css( "marginTop" ) ) ) * ( opt.speed / $Jscroll.children( ":eq(0)" ).height() ) );
						break;
						
					case 'bottom':
						var tos = -( $Jscroll.height() - $Jscroll.parents( "div:eq(0)" ).height() ) + $Jscroll.children( ":last" ).height();
						var spd = opt.speed - ( ( $Jscroll.children( ":last" ).height() - ( Math.abs ( parseInt( $Jscroll.css( "marginTop" ) ) ) - Math.abs ( tos ) ) ) * ( opt.speed / $Jscroll.children( ":last" ).height() ) );
						break;
				}
				
				$Jscroll.animate(
					{
						  marginLeft: ( pos || "0" ) + "px"
						, marginTop: ( tos || "0" ) + "px"
					}
					, spd
					, "linear"
					, function () {
						switch ( opt.direction ) {
							case 'left':
								$Jscroll.append( $J( this ).children( ":eq(0)" ) );
								$Jscroll.css( "marginLeft" , "0px" );
								break;
								
							case 'right':
								$Jscroll.prepend( $J( this ).children( ":last" ) );
								$Jscroll.css( "marginLeft" , -( $Jscroll.width() - $Jscroll.parents( "div:eq(0)" ).width() ) + "px" );
								break;
								
							case 'top':
								$Jscroll.append( $J( this ).children( ":eq(0)" ) );
								$Jscroll.css( "marginTop" , "0px" );
								break;
								
							case 'bottom':
								$Jscroll.prepend( $J( this ).children( ":last" ) );
								$Jscroll.css( "marginTop" , -( $Jscroll.height() - $Jscroll.parents( "div:eq(0)" ).height() ) + "px" );
								break;
						}
						
						scrollStart( $Jscroll , opt );
					}
				);
			};
		});
	};
})(jQuery);
