//
// Slider Content is a JQuery plugin that will allow an infinite number of div elements to play
// a slideshow.  This was developed for use at Intuit, Inc. for the Accounting Professionals Division
// but may be applied in other parts of the company.
//
// Copyright © 2009 Intuit, Inc.
// Developed By: Matthew Gisonno
// v 1.0 (It may have bugs)
//
(function($) {
	//
	// Global variables available to all functions
	//
	var itemWidth;
	var itemCount;
	var itemTotalWidth;
	var itemBegin;
	var itemEnd;
	var playSlides;
	var firstPlay;
	//
	// Main function for sliding the content
	//
	$.fn.slideContent = function(options) {
	 	//
		// default options settings
		//
		var options = $.extend( {
			slider_id: $(this).attr('id'), // ID of the current slider, automatically set... probably shouldn't ever be over-ridden
			show_numbers: true, // Show a number for each section
			show_controls: true, // Show next and previous buttons
			show_play: true, // Show the play / pause button
			controls_active: 'active', // Class name for the active control or number
			continue_animation: true, // If you want custom handlers to be give the active class, define their parent here
			fade: false, // Fade in and out rather than slide, not implemented yet
			slide: true, // Slide in and out, default
			auto_play: true, // Should we auto-play the slideshow
			slide_interval: '7000', // Interval between slide transitions
			nextButton_text: 'Next >>>', // Next button text
			prevButton_text: '<<< Previous', // Previous button text
			playButton_text: 'Play', // Play button text
			pauseButton_text: 'Pause', // Pause button text
			prevButton_class: 'prevButton', // Previous button class
			nextButton_class: 'nextButton', // Next button class
			playButton_class: 'playButton', // Play button class
			pauseButton_class: 'pauseButton' // Pause button class
		},options);
		//
		// Main function for sliding the content
		//
		return this.each(function() {
			//
			// Setup some default values to work with later
			//
			itemWidth = parseFloat($(this).find('div.section').outerWidth(true));
			itemCount = parseFloat($(this).find('div.section').length);
			itemTotalWidth = parseFloat(itemCount * itemWidth);
			itemBegin = parseFloat(0);
			itemEnd = parseFloat(itemTotalWidth - itemWidth);
			//
			// Draw the main content box... or not if all is off
			//
			if (options.show_numbers == false && options.show_controls == false && options.show_play == false) {
				// If all these options are false, don't write the controls box at all
			} else {
				$(this).after('<div class="' + options.slider_id + 'Controls"></div>');
			}
			//
			// If we want to show a number for each section, then show it
			//
			if (options.show_numbers) {
				$(this).find('div.section').each(function(i) {
					var numCount = i + 1;
					$(this).parent().parent().parent().find('div.' + options.slider_id + 'Controls').append('<a href="javascript:void(0);" class="slideNumber">' + numCount + '</a>');
				});
				$('.slideNumber:first').addClass('active');
			} else {
				$('.slideNumber:first').addClass('active');
			}
			//
			// If we want to show the controls to the user, then show it
			//
			if (options.show_controls) {
				$(this).parent().find('div.' + options.slider_id + 'Controls').prepend('<a href="javascript:void(0);" class="slideButton ' + options.prevButton_class + '">' + options.prevButton_text + '</a>');
			}
			if (options.show_play) {
				$(this).parent().find('div.' + options.slider_id + 'Controls').append('<a href="javascript:void(0);" class="slideButton ' + options.playButton_class + '">' + options.playButton_text + '</a>');
			}
			if (options.show_controls) {
				$(this).parent().find('div.' + options.slider_id + 'Controls').append('<a href="javascript:void(0);" class="slideButton ' + options.nextButton_class + '">' + options.nextButton_text + '</a>');
			}
			//
			// They want a slider, let's make them one...
			//
			if (options.slide) {
				//
				// Setup the initial slider box
				//
				$(this)
					.css({ position: 'relative', width: itemWidth+'px', overflow: 'hidden' })
					.scrollLeft(itemBegin)
					.children('div:first').css({ width: itemTotalWidth+'px' })
					.find('div.section').css({ float: 'left', width: itemWidth+'px' });
				
				//
				// Slide number click function to jump to a particular section
				//
				$('.slideNumber').click(function() {
					if (!options.continue_animation) {
						$('div#'+options.slider_id).slideContent.stopSlideshow();
					}
					$('*:animated').stop(true,true);
					var currentItem = parseFloat($('.slideNumber').index(this) + 1);
					var currentPosition = parseFloat(currentItem * itemWidth - itemWidth);
					$('div#' + options.slider_id).animate({ scrollLeft : currentPosition }, 1000);
					$('.slideNumber').removeClass('active');
					$(this).addClass('active');
				});
				//
				// Next button click function to jump 1 slide forward
				//
				$('.' + options.nextButton_class).click(function() {
					if (!options.continue_animation) {
						$('div#'+options.slider_id).slideContent.stopSlideshow();
					}
					$('*:animated').stop(true,true);
					var currentSlidePos = parseFloat($('div#' + options.slider_id).scrollLeft());
					var newSlidePos = parseFloat(currentSlidePos + itemWidth);
					
					if (currentSlidePos == itemEnd) {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active');
						$('.slideNumber:first').addClass('active');
						$('div#' + options.slider_id).animate({ scrollLeft : itemBegin }, 500);
					} else {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active').next().addClass('active');
						$('div#' + options.slider_id).animate({ scrollLeft : newSlidePos }, 1000);
					}
				});
				//
				// Previous button click function to jump 1 slide backward
				//
				$('.' + options.prevButton_class).click(function() {
					if (!options.continue_animation) {
						$('div#'+options.slider_id).slideContent.stopSlideshow();
					}
					$('*:animated').stop(true,true);
					var currentSlidePos = parseFloat($('div#' + options.slider_id).scrollLeft());
					var newSlidePos = parseFloat(currentSlidePos - itemWidth);
					if (currentSlidePos == itemBegin) {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active');
						$('.slideNumber:last').addClass('active');
						$('div#' + options.slider_id).animate({ scrollLeft : itemEnd }, 500);
					} else {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active').prev().addClass('active');
						$('div#' + options.slider_id).animate({ scrollLeft : newSlidePos }, 1000);
					}
				});
				//
				// Play button click function to start the slideshow
				//
				$('.' + options.playButton_class).click(function() {
					if ($(this).text() == options.playButton_text) {
						firstPlay = false;
						$('div#'+options.slider_id).slideContent.playSlideshow(options.slider_id ,parseFloat(options.slide_interval));
					} else {
						$('div#'+options.slider_id).slideContent.stopSlideshow();
					}
				});
				//
				// Function used to start playing the slideshow
				//
				$.fn.slideContent.playSlideshow = function(id, interval) {
					$('*:animated').stop(true,true);
					var currentSlidePos = parseFloat($('div#' + options.slider_id).scrollLeft());
					var newSlidePos = parseFloat(currentSlidePos + itemWidth);
					
					if (firstPlay == false) {
						if (currentSlidePos == itemEnd) {
							$('div.' + id + 'Controls > a.active').removeClass('active');
							$('.slideNumber:first').addClass('active');
							$('div#' + id).animate({ scrollLeft : itemBegin }, 500);
						} else {
							$('div.' + id + 'Controls > a.active').removeClass('active').next().addClass('active');
							$('div#' + id).animate({ scrollLeft : newSlidePos }, 1000);
						}
					} else {
						firstPlay = false;
					}
					if (options.show_controls) {
						$('.' + options.playButton_class).text(options.pauseButton_text);
					}
					playSlides = setTimeout("$('div#' + '" + id + "').slideContent.playSlideshow('" + id + "','" + interval + "')", interval);
				};
				//
				// Function used to stop playing the slideshow
				//
				$.fn.slideContent.stopSlideshow = function() {
					clearTimeout(playSlides);
					firstPlay = true;
					if (options.show_controls) {
						$('.' + options.playButton_class).text(options.playButton_text);
					}
				};
				//
				// Auto start playing the slideshow
				//
				if (options.auto_play) {
					$('div#'+options.slider_id).slideContent.playSlideshow(options.slider_id ,parseFloat(options.slide_interval));
				}
				//
				// Main div hover function.  When on, pause slideshow.  when off, play slideshow.
				//
				function stopSlideShowMouse() {
					$(this).slideContent.stopSlideshow();
				}
				function startSlideShowMouse() {
					$(this).slideContent.playSlideshow(options.slider_id ,parseFloat(options.slide_interval));
				}
				$('div#' + options.slider_id).bind('mouseleave', startSlideShowMouse);
				$('div#' + options.slider_id).bind('mouseenter', stopSlideShowMouse);
			//
			// They want a fader, let's make them one...
			//
			} else if (options.fade) {
				//
				// Setup the initial fade box
				//
				$(this)
					.css({ position: 'relative', width: itemWidth+'px', overflow: 'hidden' })
					.children('div:first').css({ width: itemTotalWidth+'px' })
					.find('div.section').css({ position: "absolute", top: 0, left: 0, width: itemWidth+'px' }).hide();
				$(this).children().find('div.section:first').show();
				//
				// Slide number click function to jump to a particular section
				//
				$('.slideNumber').click(function() {
					if (!options.continue_animation) {
						//$('div#'+options.slider_id).slideContent.stopSlideshow();
					}
					$('*:animated').stop(true,true);
					var currentItem = parseFloat($('.slideNumber').index(this));
					$('div.section:visible').fadeOut();
					$('div.section').eq(currentItem).fadeIn();
					$('.slideNumber').removeClass('active');
					$(this).addClass('active');
				});
				//
				// Next button click function to jump 1 slide forward
				//
				$('.' + options.nextButton_class).click(function() {
					if (!options.continue_animation) {
						//$('div#'+options.slider_id).slideContent.stopSlideshow();
					}
					$('*:animated').stop(true,true);
					var currentItem = parseFloat($('div.' + options.slider_id + 'Controls').children('a').index($('.active')));
					if (currentItem == itemCount) {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active');
						$('.slideNumber:first').addClass('active');
						$('div.section:visible').fadeOut();
						$('div.section:first').fadeIn();
					} else {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active').next().addClass('active');
						$('div.section:visible').fadeOut().next().fadeIn();
					}
				});
				//
				// Previous button click function to jump 1 slide backward
				//
				$('.' + options.prevButton_class).click(function() {
					if (!options.continue_animation) {
						//$('div#'+options.slider_id).slideContent.stopSlideshow();
					}
					$('*:animated').stop(true,true);
					var currentItem = parseFloat($('div.' + options.slider_id + 'Controls').children('a').index($('.active')));
					if (currentItem == 1) {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active');
						$('.slideNumber:last').addClass('active');
						$('div.section:visible').fadeOut();
						$('div.section:last').fadeIn();
					} else {
						$('div.' + options.slider_id + 'Controls > a.active').removeClass('active').prev().addClass('active');
						$('div.section:visible').fadeOut().prev().fadeIn();
					}
				});
				//
				// Function used to start playing the slideshow
				//
				$.fn.slideContent.playFadeshow = function(id, interval) {
					$('*:animated').stop(true,true);
					var currentItem = parseFloat($('div.' + options.slider_id + 'Controls').children('a').index($('.active')));
					
					if (firstPlay == false) {
						if (currentItem == itemCount) {
							$('div.' + options.slider_id + 'Controls > a.active').removeClass('active');
							$('.slideNumber:first').addClass('active');
							$('div.section:visible').fadeOut();
							$('div.section:first').fadeIn();
						} else {
							$('div.' + options.slider_id + 'Controls > a.active').removeClass('active').next().addClass('active');
							$('div.section:visible').fadeOut().next().fadeIn();
						}
					} else {
						firstPlay = false;
					}
					if (options.show_controls) {
						$('.' + options.playButton_class).text(options.pauseButton_text);
					}
					playSlides = setTimeout("$('div#' + '" + id + "').slideContent.playFadeshow('" + id + "','" + interval + "')", interval);
				};
				//
				// Function used to stop playing the slideshow
				//
				$.fn.slideContent.stopFadeshow = function() {
					clearTimeout(playSlides);
					firstPlay = true;
					if (options.show_controls) {
						$('.' + options.playButton_class).text(options.playButton_text);
					}
				};
				//
				// Auto start playing the slideshow
				//
				if (options.auto_play) {
					$('div#'+options.slider_id).slideContent.playFadeshow(options.slider_id ,parseFloat(options.slide_interval));
				}
				//
				// Main div hover function.  When on, pause slideshow.  when off, play slideshow.
				//
				function stopFadeShowMouse() {
					$(this).slideContent.stopFadeshow();
				}
				function startFadeShowMouse() {
					$(this).slideContent.playFadeshow(options.slider_id ,parseFloat(options.slide_interval));
				}
				$('div#' + options.slider_id).bind('mouseleave', startFadeShowMouse);
				$('div#' + options.slider_id).bind('mouseenter', stopFadeShowMouse);
			}
		});
	};
})(jQuery);
