$().ready(function() {


	$('.showMultiSlider-entries').each(function(intIndex) {

		// Assemble all the class names 
		// used to id the parts for this
		// instance of the slider.
		var id = $(this).attr('id');
		var entrySelector = '.'+id+'.showMultiSlider-entries';
		var entryPrev = '.'+id+'.showMultiSlider-prev';
		var entryNext = '.'+id+'.showMultiSlider-next';


		// Used to keep track of whether or not a navigation
		// click is being acted upon.
		// It stops clicks from being processed while
		// ones before it are not yet finished.
		$(entrySelector).data('inuse', false);


		// Previous Button
		//////////////////////////////////////////////////////
		var sTickerPrevButton = $(entryPrev);
		
		// Store all the list items so we
		// can access them easily.
		sTickerPrevButton.data(
			"config",
			{
				Collection: $(entrySelector+" li")
			}
		);

		// Now that we have our data bound, let's bind
		// the click event.
		sTickerPrevButton.click(
			function(objEvent) {
				var jThis = $(this);
				
				if (!$(entrySelector).data('inuse')) {
					// Make the navigation buttons unusable while
					// processing the action.
					$(entrySelector).data('inuse', true);

					// Get the config data out of the button.
					var objConfig = jThis.data("config");

					// See if the last item has been moved into
					// the viewing area.
					var firstItem = objConfig.Collection.filter(":first-child");
					var firstItemPos = firstItem.position();
					var reachedTheBeginning = false;
					if (firstItemPos.left >= 0)
						reachedTheBeginning = true;

					if (!reachedTheBeginning) {
						var spotlightStartPos = $('#spotlightStartPos');
						if(spotlightStartPos.length > 0) {
							spotlightStartPos.val(parseInt(spotlightStartPos.val(), 10) - 1);
						}

						// Move each item to the right.
						objConfig.Collection.filter( "li" ).each(function(intIndex) {
							var moveDist = Number($(this).css('width').replace('px', ''));
							var marginLeft = Number($(this).css('margin-left').replace('px', ''));
							var marginRight = Number($(this).css('margin-right').replace('px', ''));

							if (!isNaN(marginLeft))
								moveDist += marginLeft;
							if (!isNaN(marginRight))
								moveDist += marginRight;

							$(this).animate({"left": "+="+moveDist+"px"}, "fast", function() {
								// Set the navigation buttons as usable again.
								$(entrySelector).data('inuse', false);
								
								// Hide Prev Button if no more Previous
								firstItemPos = firstItem.position();
								if (firstItemPos.left >= 0) {
									$(sTickerPrevButton).css('visibility', 'hidden');
								}
							});
						});


						// Make sure the next image is shown
						$(entryNext).css('visibility', 'visible');
					}
				}
				
				// Prevent default event (form submit).
				objEvent.preventDefault();
				return(false);
			}
		);
		
		
		
		// Next Button
		//////////////////////////////////////////////////////	
		var sTickerNextButton = $(entryNext);
		
		// Store all the list items so we
		// can access them easily.
		sTickerNextButton.data(
			"config",
			{
				Collection: $(entrySelector+" li")
			}
		);

		// Now that we have our data bound, let's bind
		// the click event.
		sTickerNextButton.click(
			function(objEvent, times) {
				//alert('click next');
				var jThis = $(this);
				
				//if (times == undefined) times = 1;
				//$(entrySelector).data('times', times);
				times = $(entrySelector).data('times');
				if(times == null || times == '' || isNaN(times)) {
					times = 1;
					$(entrySelector).data('times', times);
				}
				
				clickAction();


				function clickAction() {
					
					if (!$(entrySelector).data('inuse')) {
						
						// Make the navigation buttons unusable while
						// processing the action.
						$(entrySelector).data('inuse', true);
		
						// Get the config data out of the button.
						var objConfig = jThis.data("config");

						// See if the last item has been moved into
						// the viewing area.
						var lastItem = objConfig.Collection.filter(":last-child");
						var lastItemPos = lastItem.position();
						var containerWidth = Number($(lastItem).parent().css('width').replace('px', ''));
						var reachedTheEnd = false;
						if (lastItemPos.left <= containerWidth)
							reachedTheEnd = true;


						if (!reachedTheEnd) {
							//alert("$(entrySelector).data('updateStartPos')="+$(entrySelector).data('updateStartPos'));
							if($(entrySelector).data('updateStartPos') == true) {
								var spotlightStartPos = $('#spotlightStartPos');
								if(spotlightStartPos.length > 0) {
									spotlightStartPos.val(parseInt(spotlightStartPos.val(), 10) + 1);
								}
							}

							// Move each item to the left.
					  		objConfig.Collection.filter( "li" ).each(function(intIndex) {
					  	        var moveDist = Number($(this).css('width').replace('px', ''));
					  	        var marginLeft = Number($(this).css('margin-left').replace('px', ''));
					  	        var marginRight = Number($(this).css('margin-right').replace('px', ''));

								if (!isNaN(marginLeft))
									moveDist += marginLeft;
								if (!isNaN(marginRight))
									moveDist += marginRight;
								
								moveDist *= times;
								$(entrySelector).data('times', 0);

								$(this).animate({"left": "-="+moveDist+"px"}, "fast", function() {
									// Set the navigation buttons as usable again.
									$(entrySelector).data('inuse', false);

									// Hide Next Button if no more Next
									lastItemPos = lastItem.position();
									if (lastItemPos.left <= containerWidth) {
										$(sTickerNextButton).css('visibility', 'hidden');
									}

									/*$(entrySelector).data('times', $(entrySelector).data('times') - 1);
									// Repeat this as many times as requested.
									if ($(entrySelector).data('times') > 0) {
										clickAction(times);
									}*/
								
								});
					  		});

							$(sTickerPrevButton).css('visibility', 'visible');
							//alert("clickAction: $('#spotlightStartPos').val="+$('#spotlightStartPos').val());
						}
					}
				}


				// Prevent default event (form submit).
				objEvent.preventDefault();
				return(false);
			}
		);
		
	});
	
});



function clickSlider(times) {
	$('.spotlight.showMultiSlider-next').trigger('click', times);
}
function moveSliderN1() { clickSlider(1); }
function moveSliderN2() { clickSlider(2); }
function moveSliderN3() { clickSlider(3); }
function moveSliderN4() { clickSlider(4); }
function moveSliderN5() { clickSlider(5); }
function moveSliderN6() { clickSlider(6); }
function moveSliderN7() { clickSlider(7); }
function moveSliderN8() { clickSlider(8); }
function moveSliderN9() { clickSlider(9); }
				
				
