///////////////////////////////////////////////////////////////////////
// imageScroller 
//
///////////////////////////////////////////////////////////////////////

var $j = jQuery.noConflict();


( function( $j ) {
	$j.fn.imageScroller = function ( options ) {
		return this.each( function() {
			var $this = $j( this );
			var loadImgs = 0;
			
			var opt = $j.extend( 
				{ 
					  speed: "2000"
					, loading: "Loading images..." 
					, direction: "left"
				}
				, options || {}
			);
			
			$this.children().hide();
			$this.append(
				"<div style='clear:both; padding: 0px; margin: 0px;'>" + 
				"<div id='loading'>" + opt.loading + "</div>" + 
				"</div>"
			);
			
			$j( "img" , $this ).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" , $this ).length ) {
						window.clearInterval( intVal );
						$j( "#loading" ).remove();
						$this.children().show();
						var totImg = 0;
			
						$j.each(
							  $this.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( $( 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)" , $this ).stop();
									}
								).bind(
									  "mouseout"
									, function () {
										scrollStart( $j( "div:eq(0)" , $this ) , opt );
									}
								);
								
								$j( "div:eq(0)" , $this ).append( $j( this ) );
							}
						);
						
						switch ( opt.direction ) {
							case 'left':
								$j( "div:eq(0)" , $this ).css( "width" , totImg + "px" );
								break;
							
							case 'right':
								$j( "div:eq(0)" , $this ).css( "width" , totImg + "px" );
								$j( "div:eq(0)" , $this ).css({
									marginLeft: -( totImg - $this.width() ) + "px"
								});
								break;
								
							case 'top':
								$j( "div:eq(0)" , $this ).css( "height" , totImg + "px" );
								break;
								
							case 'bottom':
								$j( "div:eq(0)" , $this ).css( "height" , totImg + "px" );
								$j( "div:eq(0)" , $this ).css({
									marginTop: -( totImg - $this.height() ) + "px"
								});
								break;
						}
	
						scrollStart( $j( "div:eq(0)" , $this ) , opt );
					}
				}
				, 100
			);
			
			function scrollStart ( $scroll , opt ) {
				switch ( opt.direction ) {
					case 'left':
						var pos = -( $scroll.children( ":eq(0)" ).width() );
						var spd = opt.speed - ( Math.abs ( parseInt( $scroll.css( "marginLeft" ) ) ) * ( opt.speed / $scroll.children( ":eq(0)" ).width() ) );
						break;
						
					case 'right':
						var pos = -( $scroll.width() - $scroll.parents( "div:eq(0)" ).width() ) + $scroll.children( ":last" ).width();
						var spd = opt.speed - ( ( $scroll.children( ":last" ).width() - ( Math.abs ( parseInt( $scroll.css( "marginLeft" ) ) ) - Math.abs ( pos ) ) ) * ( opt.speed / $scroll.children( ":last" ).width() ) );
						break;
						
					case 'top':
						var tos = -( $scroll.children( ":eq(0)" ).height() );
						var spd = opt.speed - ( Math.abs ( parseInt( $scroll.css( "marginTop" ) ) ) * ( opt.speed / $scroll.children( ":eq(0)" ).height() ) );
						break;
						
					case 'bottom':
						var tos = -( $scroll.height() - $scroll.parents( "div:eq(0)" ).height() ) + $scroll.children( ":last" ).height();
						var spd = opt.speed - ( ( $scroll.children( ":last" ).height() - ( Math.abs ( parseInt( $scroll.css( "marginTop" ) ) ) - Math.abs ( tos ) ) ) * ( opt.speed / $scroll.children( ":last" ).height() ) );
						break;
				}
				
				$scroll.animate(
					{
						  marginLeft: ( pos || "0" ) + "px"
						, marginTop: ( tos || "0" ) + "px"
					}
					, spd
					, "linear"
					, function () {
						switch ( opt.direction ) {
							case 'left':
								$scroll.append( $j( this ).children( ":eq(0)" ) );
								$scroll.css( "marginLeft" , "0px" );
								break;
								
							case 'right':
								$scroll.prepend( $j( this ).children( ":last" ) );
								$scroll.css( "marginLeft" , -( $scroll.width() - $scroll.parents( "div:eq(0)" ).width() ) + "px" );
								break;
								
							case 'top':
								$scroll.append( $j( this ).children( ":eq(0)" ) );
								$scroll.css( "marginTop" , "0px" );
								break;
								
							case 'bottom':
								$scroll.prepend( $j( this ).children( ":last" ) );
								$scroll.css( "marginTop" , -( $scroll.height() - $scroll.parents( "div:eq(0)" ).height() ) + "px" );
								break;
						}
						
						scrollStart( $scroll , opt );
					}
				);
			};
		});
	};
})(jQuery);


$j(
   function () {
      $j( "#imageScroll" ).imageScroller( {speed:'5000', loading:'Wait please...'} );
      $j( "#sponsors" ).imageScroller( {speed:'10000', loading:'Wait please...'} );
      $j( "#supporters" ).imageScroller( {speed:'10000', loading:'Wait please...'} );
   }
)