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