///////////////////////////////////////////////////////////////////////
// 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:	jQuery( "#div" ).imageScroller( {options} )
//					
//					options:	speed (millisecond)
//								loading (text)
//								direction (left, right, top, bottom)
//
// New in release 1.1:
//		- bug fix for preload images
///////////////////////////////////////////////////////////////////////

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