$(document).ready (

	// Occurs when document loads
	function() {	
	
		// Variables
		var imageWidth = 896;
		var slider = $('#slideContainer');
		var sliderGroups = $('#slideContainer .footerGroup');
		var groups = $('#footerBannerGroups .footerGroup');
		var groupSize = 1;
		var groupCount = groups.length;
		var currentIndex = 0;
		var emptyGroup = "<div class='footerGroup'></div>";
		var aniDuration = "slow";
		
		// Setting initial image/group
		currentIndex = GetRandomNumber(groupCount);

		// Setting initial width of sliding container
		$(slider).css('width', groupCount * ( groupSize * imageWidth ) + 'px');
		
		// Inserting first group into sliding container
		$(sliderGroups[1]).replaceWith($(groups[currentIndex]).clone());
		$(slider).css('left', -1 * ( imageWidth * groupSize ) + 'px');
				
		$('#footerBannersPrev').click(Prev);
		$('#footerBannersNext').click(Next);
		
		function GetRandomNumber(max) {
				
			var last = $.cookie('currentIndex');
			var cur = Math.floor( Math.random() * max );
			
			while ( last == cur ) {				
				cur = Math.floor( Math.random() * max );
			}
			
			$.cookie('currentIndex', cur);
			
			return cur;
						
		}
		
		function Prev() {
											
			$('#footerBannersPrev').unbind('click');
			$('#footerBannersPrev').addClass('footerBannersPrevInactive');
			$('#footerBannersNext').unbind('click');
			$('#footerBannersNext').addClass('footerBannersNextInactive');
			
			sliderGroups = $('#slideContainer .footerGroup');	
			currentIndex = ( currentIndex == 0 ) ? ( groupCount - 1 ) : currentIndex -= 1;
			
			// Replacing previous element
			$(sliderGroups[0]).replaceWith( $(groups[currentIndex]) );
			
			// Sliding to the left
			$(slider).animate( {'left' : '0px'}, aniDuration, 'linear', function() { MoveToMiddle(true); } );
			
		}
		
		function Next() {

			$('#footerBannersPrev').unbind('click');
			$('#footerBannersPrev').addClass('footerBannersPrevInactive');
			$('#footerBannersNext').unbind('click');
			$('#footerBannersNext').addClass('footerBannersNextInactive');

			sliderGroups = $('#slideContainer .footerGroup');	
			currentIndex = ( currentIndex == (groupCount - 1) ) ? ( currentIndex = 0 ) : currentIndex += 1;
			
			// Replacing next element
			$(sliderGroups[2]).replaceWith( $(groups[currentIndex]) );
			
			// Sliding to the right
			$(slider).animate( {'left' : -2 * ( imageWidth * groupSize ) }, aniDuration, 'linear', function() { MoveToMiddle(false); } );
		
		}
		
		function MoveToMiddle(fromStart) {
					
			var sliderGroups = $('#slideContainer .footerGroup');

			img = $(groups[currentIndex]).children('img').attr('src');
			$('#footerBannerContainer').css('background-image', 'url(' + img + ')');
			
			$(slider).fadeOut(250, function() {

				if ( fromStart ) {						
				
					$(sliderGroups[1]).replaceWith( $(sliderGroups[0]).clone() );
					$(sliderGroups[0]).replaceWith(emptyGroup);							
					
				}
				
				else {
			
					$(sliderGroups[1]).replaceWith( $(sliderGroups[2]).clone() );
					$(sliderGroups[2]).replaceWith(emptyGroup);						
					
				}

				$(slider).css('left', -1 * ( imageWidth * groupSize ) + 'px');
				
				$(slider).fadeIn(100, function() {

					$('#footerBannersPrev').click(Prev);
					$('#footerBannersPrev').removeClass('footerBannersPrevInactive');
					$('#footerBannersNext').click(Next);
					$('#footerBannersNext').removeClass('footerBannersNextInactive');									

				});			
			
			});
																	
		}
						
	}
	
);