/* jQuery slider for CVSCaremark Homepage */
$(document).ready(function(){
//--------SETUP-----------//
	//initial variables
	//var slideDelay = 6000; // time of pause on each slide TO-DO: use this variable in timers
	var slideWidth = $('#slideshow-outer').width(); //width of slide - takes value of the width of the container div ('#slideshow-outer')
	var slideCount = $('#slideshow-wrapper div.slide').length; //counts the number of slides in the HTML

	var init; //defines initial timer variable
	var repeatRight; //defines repeating timer variable
	var repeatLeft; //defines repeating timer variable
	var captionStart; //defines caption timer variable
	var captionEnd; // defines caption end timer variable

	//set width of wrapper based on number of slides
	var wrapperWidth = slideCount * slideWidth;
	$('#slideshow-wrapper').css({'width' : wrapperWidth})

	//number slides
	for (var i = 0; i < slideCount; i++){
		$('#slideshow-wrapper div.slide').eq(i).attr('id','slide-'+i);
	}
	
//--------FUNCTIONS-----------//
	//define leftTurn function
	leftTurn = function ($newPage){
		
		//removes event handlers to prevent duplication
		$('#controls #prev a').unbind();
		$('#controls #next a').unbind();
		$('#dots img').unbind();
		
		//prevents default on link at all stages of animation
		$('#controls #prev a').bind('click', function(event){
			event.preventDefault();
		});
		
		indexCurrent = $('#slideshow-wrapper div.slide.current').prevAll.length; //determines index of current slide
		var originalPosition = $('#slideshow-wrapper').css('left'); //determines current left position of wrapper
		var originalPositionInt = parseInt(originalPosition.replace('px','')); //converts originalPosition to an integer
		var newPosition = (originalPositionInt - (slideWidth * $newPage)); //sets variable for new position
		
		//TO-DO only shuffle if the current slide is the first in the list
		
		//shuffle slides
		for (var j = 0; j< $newPage; j++){ //runs once for every slide that needs to move
			$('#slideshow-wrapper div.slide:last').insertBefore('#slideshow-wrapper div.slide:first');
		}
				
		//move container to keep current slide in same position
		$('#slideshow-wrapper').css({'left': newPosition});
		
		//reset 'current' slide
		$('#slideshow-wrapper div.slide.current').removeClass('current');
		$('#slideshow-wrapper div.slide:first').addClass('current'); //set first slide to current
		
		//animate slide
		$('#slideshow-wrapper').animate({left: originalPositionInt}, 700, function(){
			
			//determine index of current slide
			var idCurrent = $('#slideshow-wrapper div.slide.current').attr('id'); //determines id of current slide
			indexCurrent = parseInt(idCurrent.replace('slide-','')); //converts originalPosition to an integer - note: var already defined globally
			
			//reset active dot image
			$('#controls #dots img').attr('src','/sites/all/themes/cvscaremark_rev3/images/slideshow/dot.gif'); //removes active from all dots from last step
			$('#controls #dots img').eq(indexCurrent).attr('src','/sites/all/themes/cvscaremark_rev3/images/slideshow/dot-active.gif'); //sets current dot to active
			
			//run caption slide function
			slideCaption();
			
			//once animation is complete, rebind event handlers to allow functions to run again
			$('#controls #prev a').bind('click', function(event){ //rebinds prev event handler
				event.preventDefault(); //prevents page reload
				window.clearTimeout(repeatLeft); //clears timeouts to prevent duplication
				window.clearTimeout(repeatRight); //clears timeouts to prevent duplication
				window.clearTimeout(captionStart); //clears timeouts to prevent duplication
				window.clearTimeout(captionEnd); //clears timeouts to prevent duplication
				closeCaption(); // closes the current caption
				leftTurn(1);
				}
			);
			$('#controls #next a').bind('click', function(event){  //rebinds next event handler
				event.preventDefault(); //prevents page reload
				window.clearTimeout(repeatLeft); //clears timeouts to prevent duplication
				window.clearTimeout(repeatRight); //clears timeouts to prevent duplication
				window.clearTimeout(captionStart); //clears timeouts to prevent duplication
				window.clearTimeout(captionEnd); //clears timeouts to prevent duplication
				closeCaption(); // closes the current caption
				rightTurn(1);
				}
			);
			$('#dots img').bind('click', function(event){ //rebinds dots event handler
				closeCaption(); // closes the current caption
				var dotClicked = $(this).prevAll().length; //detect index of dot clicked
				dotNavigate(dotClicked); // calls dotNavigate function, passing dotClicked as an argument
				}
			);
			
			//run again on timer
			repeatLeft = window.setTimeout(function(){
				rightTurn(1);
			}, 10000);
		});
	};
	
	//define rightTurn function
	rightTurn = function ($newPage){
		//ensures $newPage variable is a positive number
		if ($newPage < 1){ 
			$newPage = 0 -$newPage; 
		}
		
		//removes event handlers to prevent duplication
		$('#controls #prev a').unbind();
		$('#controls #next a').unbind();
		$('#dots img').unbind();
		
		//prevents default on link at all stages of animation
		$('#controls #next a').bind('click', function(event){
			event.preventDefault();
		});
		
		var originalPosition = $('#slideshow-wrapper').css('left'); //determines current left position of wrapper
		var originalPositionInt = parseInt(originalPosition.replace('px','')); //converts originalPosition to an integer
		var newPosition = (originalPositionInt - (slideWidth * $newPage)); //sets variable for new position
		
		//animate slide
		$('#slideshow-wrapper').animate({left: newPosition}, 700, function(){
			
			//shuffle slides
			for (var j = 0; j< $newPage; j++){ //runs once for every slide that needs to move
				$('#slideshow-wrapper div.slide:first').insertAfter('#slideshow-wrapper div.slide:last');
			}
			$('#slideshow-wrapper').css({left: originalPositionInt}); //resets position of wrapper
			
			//reset 'current' slide
			$('#slideshow-wrapper div.slide.current').removeClass('current');
			$('#slideshow-wrapper div.slide:first').addClass('current'); //set first slide to current
			
			//determine index of current slide
			var idCurrent = $('#slideshow-wrapper div.slide.current').attr('id'); //determines id of current slide
			indexCurrent = parseInt(idCurrent.replace('slide-','')); //converts originalPosition to an integer - note: var already defined globally
			
			//reset active dot image
			$('#controls #dots img').attr('src','/sites/all/themes/cvscaremark_rev3/images/slideshow/dot.gif'); //removes active from all dots from last step
			$('#controls #dots img').eq(indexCurrent).attr('src','/sites/all/themes/cvscaremark_rev3/images/slideshow/dot-active.gif'); //sets current dot to active
			
			//run caption slide function
			slideCaption();
			
			//once animation is complete, rebinds event handlers to allow function to run again
			$('#controls #prev a').bind('click', function(event){ //rebinds prev event handler
				event.preventDefault(); //prevents page reload
				window.clearTimeout(repeatLeft); //clears timeouts to prevent duplication
				window.clearTimeout(repeatRight); //clears timeouts to prevent duplication
				window.clearTimeout(captionStart); //clears timeouts to prevent duplication
				window.clearTimeout(captionEnd); //clears timeouts to prevent duplication
				closeCaption(); // closes the current caption
				leftTurn(1);
				}
			);			
			$('#controls #next a').bind('click', function(event){  //rebinds next event handler
				event.preventDefault(); //prevents page reload
				window.clearTimeout(repeatLeft); //clears timeouts to prevent duplication
				window.clearTimeout(repeatRight); //clears timeouts to prevent duplication
				window.clearTimeout(captionStart); //clears timeouts to prevent duplication
				window.clearTimeout(captionEnd); //clears timeouts to prevent duplication
				closeCaption(); // closes the current caption
				rightTurn(1);
				}
			);
			$('#dots img').bind('click', function(event){  //rebinds dots event handler
				closeCaption(); // closes the current caption
				var dotClicked = $(this).prevAll().length; //detect index of dot clicked
				dotNavigate(dotClicked); // calls dotNavigate function, passing dotClicked as an argument
				}
			);
			
			//run again on timer
			repeatRight = window.setTimeout(function(){
				rightTurn(1);
			}, 10000);
		});
	};
	
	//define caption slide function
	slideCaption = function(){
		captionStart = window.setTimeout(function(){ //timer to open caption
			$('#slideshow-wrapper div.slide.current div.caption').animate({right: 0}, 500);
		}, 500);
		
		captionEnd = window.setTimeout(function(){ //timer to close caption
			$('#slideshow-wrapper div.slide.current div.caption').animate({right: -550}, 500);
		}, 9500);
	};
	
	//define close caption function
	closeCaption = function(){
		$('#slideshow-wrapper div.slide div.caption').css({right: -550});
	}
	
	//define dot navigation function
	dotNavigate = function($dotClicked){
		//determines number of slides to move
		var idCurrent = $('#slideshow-wrapper div.slide.current').attr('id'); //determines id of current slide
		indexCurrent = parseInt(idCurrent.replace('slide-','')); //converts originalPosition to an integer - note: var already defined globally
		var dotClicked = $dotClicked;
		var distance = (indexCurrent - dotClicked); //distance integer passed to "turn" function
		
		//TO-DO: shuffle slides appropriately
		
		//determines which way to turn
		if(distance > 0){
			//clear existing timeouts
			window.clearTimeout(init);
			window.clearTimeout(repeatLeft);
			window.clearTimeout(repeatRight);
			window.clearTimeout(captionStart);
			window.clearTimeout(captionEnd);			
			closeCaption(); // closes the current caption
			
			//run function
			leftTurn(distance);	
		}
		else if (distance < 0){
			//clear existing timeouts
			window.clearTimeout(init);
			window.clearTimeout(repeatLeft);
			window.clearTimeout(repeatRight);
			window.clearTimeout(captionStart);
			window.clearTimeout(captionEnd);
			closeCaption(); // closes the current caption
			
			//run function
			rightTurn(distance);
		}
		else{
			return false;
		}
		
	};
	
//--------INITIALIZATION-----------//
	//start on random slide
		var startSlide = Math.floor(Math.random()*slideCount); //random slide to start on
		//shuffle slides
		for (var k = 0; k< startSlide; k++){ //runs once for every slide that needs to move
			$('#slideshow-wrapper div.slide:first').insertAfter('#slideshow-wrapper div.slide:last');
		}

		//set variables based on current slide
		$('#slideshow-wrapper div.slide:first').addClass('current'); //set first slide to current
		var indexCurrent = startSlide; //sets index of current slide to startSlide var

		//generate dots
		for (var h = 0; h < slideCount; h++){
			$('#controls #dots').append('<img src="/sites/all/themes/cvscaremark_rev3/images/slideshow/dot.gif" alt="progress dot indicator for slide show">');
		}
		$('#controls #dots img').eq(startSlide).attr('src','/sites/all/themes/cvscaremark_rev3/images/slideshow/dot-active.gif'); //sets current dot to active

		//run caption slide function
		slideCaption();

	//run initial function on timer
	init = window.setTimeout(function(){
		rightTurn(1);
	}, 10000);
	
//--------EVENT HANDLERS-----------//   
	//$('#controls #prev a').click(leftTurn(0));
	$('#controls #prev a').bind('click', function(event){
		event.preventDefault(); //prevents page reload
		window.clearTimeout(init); //stops the initial timer
		window.clearTimeout(captionStart); //stops timer
		window.clearTimeout(captionEnd); //stops timer
		closeCaption(); // closes the current caption
		leftTurn(1);
		}
	);
	$('#controls #next a').bind('click', function(event){
		event.preventDefault(); //prevents page reload
		window.clearTimeout(init); //stops the initial timer
		window.clearTimeout(captionStart); //stops timer
		window.clearTimeout(captionEnd); //stops timer
		closeCaption(); // closes the current caption
		rightTurn(1);
		}
	);
	$('#dots img').bind('click', function(event){
		var dotClicked = $(this).prevAll().length; //detect index of dot clicked
		dotNavigate(dotClicked); // calls dotNavigate function, passing dotClicked as an argument NOTE - inside function clears timeouts
		}
	);
	

});
