$(document).ready(function(){
	$index = [0,0,0];
	$images = [];
	$i = 0;
	$sponsors = $(".topimage ul");
	
	$sponsors.each(function (i) {
		$j = 0;
		$images[$i] = $(this).children();
		
		$images[$i].each(function (j) {
			$images[$i][$j] = {}; 
			$images[$i][$j] = $(this); 
			$j++; });
		$index[$i] = $images[$i].length-1;
		$i++;
	});
	
	changeSponsors(true); 
});
 
function changeSponsors( $firstRun ) {	
	for ( $i = 0; $i < $images.length; $i++ ) {
		if( $images[$i].length > 1 ) {
			for ( $j = 0; $j < $images[$i].length; $j++ ) {
				if( $j != $index[$i] )
					if( $firstRun )
						$images[$i][$j].hide();
					else
						$images[$i][$j].fadeOut(2000);
			};
			
			for ( $j = 0; $j < $images[$i].length; $j++ ) {
				if( $j == $index[$i] )
					$images[$i][$j].fadeIn(2000);
			};
			
			$newIndex = Math.round(Math.random()*($images[$i].length-1));
			while( $index[$i] == $newIndex )
				$newIndex = Math.round(Math.random()*($images[$i].length-1));
			$index[$i] = $newIndex;
		};
	};
	
	setTimeout("changeSponsors(false)", 10000); 
};
