(function($) {
	$.fn.slider = function () {
		return this.each(function () {
		
		
			// some data used for the slider
			var sliderData = {
				container:		$('#spotlight'),
				totalSlides:	0,
				totalWidth:		0,
				slideWidth:		0,
				currentSlide:	0,
				autoPlay:		  0,
				timeoutVar:		null,
				moving:			false
			}
			
			/* assign unique IDs to each slide */
			$('.spotlightCell',sliderData.container).each(function (i) {
				$(this).data('id',i+1);
			});
			
			/* fill in some of the data */
			sliderData.totalSlides = $('.spotlightCell',sliderData.container).length;
			sliderData.slideWidth = 816;
			sliderData.totalWidth = 816 * sliderData.totalSlides;
			sliderData.currentSlide = 1;
			
			/* set the width of the slider - this will allow the slides to line up horizontally */
			$('#spotlightContent',sliderData.container).width(sliderData.totalWidth);
			
			/* functions to return the next and previous slide numbers based on the current slide */
			function getPreviousNum() {
				if (sliderData.currentSlide - 1 == 0)
					return sliderData.totalSlides;
				return sliderData.currentSlide - 1;
			}
			function getNextNum() {
				if (sliderData.currentSlide + 1 > sliderData.totalSlides)
					return 1;
				return sliderData.currentSlide + 1;
			}
			/* obtain a slide based on the given ID number */
			function getSlide(num) {
				var $slide = null;
				$('.spotlightCell',sliderData.container).each(function () {
					if ($(this).data('id') == num) {
						$slide = $(this);
						return;
					}
				});
				return $slide;
			}
			
			function autoPlay() {
				// clear the timeout var if it exists
				if (sliderData.timeoutVar != null) {
					sliderData.timeoutVar = clearTimeout(sliderData.timeoutVar);
				}
				// set interval for moving if autoplay is set
				if (sliderData.autoPlay != 0) {
					sliderData.timeoutVar = setTimeout(function () {
						move(true);
					}, sliderData.autoPlay);
				}
			}
			// call auto rotate right away
			autoPlay();
			
			/* this function is what makes the slider circular in nature 
			 * it will keep moving the first slide to the end or the last slide
			 * to the beginning depending on if the slide hit the front/end and
			 * the direction desired */
			function moveSlides(direction) {
				// Gather some information about the current slide and adjacent slide
				// *note: in this case the adjacent slide is the next one that should be served.
				// if there is none there, then we need to detach a slide and move it
				var $currentSlide = getSlide(sliderData.currentSlide);
				var nextSlideNum;
				var $adjacentSlide;
				if (direction == false) {
					nextSlideNum = getPreviousNum();
					$adjacentSlide = $currentSlide.prev();
				} else {
					nextSlideNum = getNextNum();
					$adjacentSlide = $currentSlide.next();
				}
				
				// If there is no slide where we are sliding to, we need to detach one from the end and
				// brint it to position
				if ($adjacentSlide.length == 0) {
					// Get the slide we are going to move
					var $slideToMove = getSlide(nextSlideNum);
					// Because we are moving slides around, as soon as they are actually repositioned, the position
					// of the slider needs to be adjusted to compensate for this so that when the slides are 
					// moved the slider remains showing the same slide
					if (direction == false) {
						// in this case we are taking a slide from the end and bringing to the front
						$('#spotlightContent',sliderData.container).css('marginLeft', function (index,val) {
							// so shift the margin to the left so the focused slide stays in view
							return parseInt(val) - sliderData.slideWidth;
						});
						// now that the slider has moved, we detach and move the slide
						// after both of these steps the slider will still show the same slide
						$slideToMove.detach().insertBefore($currentSlide);
					} else {
						// in this case we are taking a slide from the beginning and moving it to the end
						$('#spotlightContent',sliderData.container).css('marginLeft', function (index,val) {
							// so shift the margin of the slider to the right so the focused slide remains viewed
							return parseInt(val) + sliderData.slideWidth;
						});
						// see above
						$slideToMove.detach().insertAfter($currentSlide);
					}
				}
			}
			
			/** function called to move the slider in the given direction */
			function move(direction) {
				// Don't move the slider if it's already moving
				if (!sliderData.moving) {
		
					// move slides if necessary
					moveSlides(direction);
					// slider is now moving
					sliderData.moving = true;
			
					// determine if we add or subtract from the margin
					var operator;
					if (direction == false) {
						operator = '+=';
					} else {
						operator = '-=';
					}
					$('#spotlightContent',sliderData.container).animate(
						{
							// animate the margin
							marginLeft: operator+sliderData.slideWidth
						},
						function () {
							// call autoplay to reset timer
							autoPlay();
							// slider no longer moving...
							sliderData.moving = false;
							if (direction == false)
								sliderData.currentSlide = getPreviousNum();
							else
								sliderData.currentSlide = getNextNum();
						}
					);
				}
			}
			// arrow handlers
			$('.spotlightArrow-left',sliderData.container).click(function () {
				move(false);	
			});
			$('.spotlightArrow-right',sliderData.container).click(function () {
				move(true);	
			});
			// add pointer cursor to arrows
			$('.spotlightArrow',sliderData.container).css('cursor','pointer');
		});
	};
})(jQuery);

$jq = jQuery.noConflict(true);
$jq(document).ready(function () {
	$jq("#spotlight").slider();
});
