$(document).ready(function(){
	//-------Swap out arrow images on rollover
		$("#spotlight-left-arrow").hover(function(){
			$(this).css('cursor', 'pointer');
			$(this).attr("src", "/apps/aba-web/docroot/images/global/arrow_left_roll.jpg");
		}, function(){
			$(this).attr("src", "/apps/aba-web/docroot/images/global/arrow_left.jpg");
		});
		$("#spotlight-right-arrow").hover(function(){
			$(this).css('cursor', 'pointer');
			$(this).attr("src", "/apps/aba-web/docroot/images/global/arrow_right_roll.jpg");
		}, function(){
			$(this).attr("src", "/apps/aba-web/docroot/images/global/arrow_right.jpg");
		});
		
		//--------Slide the content 
		$("#spotlight-left-arrow").click(function(){
			$("#spotlight-content").animate({"margin-left": "+="+spot_findSlides_Right()}, "slow");
		});
		$("#spotlight-right-arrow").click(function(){
 			 $("#spotlight-content").animate({"margin-left": "-="+spot_findSlides_Left()}, "slow", function(){
				//animation complete
				spot_clearSlides_Left();
			});
		});
		
		
		//--------Calculate how many slides to move
		var spot_slideWidth = $(".spotlight-entry").outerWidth();
		var spot_marginLeft=$("#spotlight-content").css("margin-left");

		function spot_findSlides_Left(){
				for(i=1; i<4; i++){
					/*spot_slidesRemaining++;
					if(spot_slidesRemaining>spot_totalSlides){
						spot_slidesRemaining=1;
					}
					$(["#entry"]+spot_slidesRemaining).clone().appendTo(".horizontal-list");
					$(["#entry"]+spot_slidesRemaining).clone().appendTo(".horizontal-list");*/
					
					// clone first three list items and append them on to the end of the unordered list
					$('.horizontal-list .spotlight-entry:nth-child(' + i + ')').clone().appendTo(".horizontal-list");
				}	
				// return number : slide container to the left by 3 list items at a time
				// animation function (on button) calls back spot_clearSlides_Left
				return (3*spot_slideWidth);
				//return 0;
			}
			
		function spot_clearSlides_Left(){
			// callback function that clears the cloned elements, and resets the container's left margin to 0
			for(j=3; j>0; j--){
				$('.horizontal-list .spotlight-entry:nth-child(' + j + ')').remove();
			}
			$("#spotlight-content").css("margin-left", "0");
		}
		
		function spot_findSlides_Right(){
			/*if ($("#spotlight-content").css("margin-left") == spot_marginLeft){
				return 0;
			} else {
			return (3*spot_slideWidth);
			}*/
			//for (ii=spot_totalSlides; ii>(spot_totalSlides-3); ii--){
			
			// clone LAST three list items and insert them to the beginning of the unordered list, afterward removing the cloned list item
			for (ii=0; ii<3; ii++){
				$('.horizontal-list .spotlight-entry:nth-child(' + spot_totalSlides + ')').clone().insertBefore(".horizontal-list .spotlight-entry:nth-child(1)");
				$('.horizontal-list .spotlight-entry:nth-child(' + (spot_totalSlides+1) + ')').remove();
			}
			// reset container's left margin to 0 and return number to move container 3 list items at a time
			$("#spotlight-content").css("margin-left", -3*spot_slideWidth);
			return (3*spot_slideWidth);
		}
		
		// Total number of slides in spotlight 
		var spot_totalSlides = $(".spotlight-entry").size();
		// Initial value for spot_slidesRemaining (including 3 already on board)
		var spot_slidesRemaining=0;
		// Turn off arrows
		/*$("#left-arrow").css("visibility", "hidden");
		if (spot_totalSlides < 3){
			$("#right-arrow").css("visibility", "hidden");
		}*/
	});
