//adapted from Tutsvalley tutorial by Doug G., 12-09
//http://tutsvalley.com/tutorials/making-a-jquery-infinite-carousel-with-nice-features/

    //options(1 = ON, 0 = OFF)
    var auto_slide = 1; /* toggles whether items slide without user interaction */
    var hover_pause = 1; /* toggles whether auto_slide pauses when user hovers over carousel */
    var key_slide = 1; /* enables sliding with left and right arrow keys */
	
 	var display_interval = 13000; /* length of time each item displays before auto_sliding, in milliseconds */
 	var slide_duration = 500; /* speed at which items slide, in milliseconds */
	var slide_increment = 238; /* pixel width of items including margins/padding/borders, if any */

	jQuery(document).ready(function() {
		/* Reveals feature if javascript is running, otherwise CSS keeps it hidden */
		jQuery('#carouselMarquee').css('display', 'block');

        /* move the last list item before the first item, so the last item 
        will display if the user initially clicks to slide left. */
        jQuery('#carouselContent li:first').before(jQuery('#carouselContent li:last')); 

        //check if auto sliding is enabled
        if(auto_slide == 1){
            /* set the interval (loop) to call slide function with option 'right'
            and set the delay to the variable declared previously */
            var timer = setInterval('slide("right")', display_interval);

        }

        //check if hover pause is enabled
        if(hover_pause == 1){
            //when user hovers over the carousel...
            jQuery('#carouselWrapper').hover(function(){
                //...stop the interval...
                clearInterval(timer)
            },function(){
                //...and when user mouses out start it again
                timer = setInterval('slide("right")', display_interval);
				slide('right');
            });

        }

        //check if key sliding is enabled
        if(key_slide == 1){

            //binding keypress function
            jQuery(document).bind('keypress', function(e) {
                //keyCode for left arrow is 37, right is 39
                if(e.keyCode==37){
                        //initialize the slide to left function
                        slide('left');
                }else if(e.keyCode==39){
                        //initialize the slide to right function
                        slide('right');
                }
            });

        }

  });

//slide function
function slide(whichway){

            /* an 'if' statement + checking the 'whichway' variable determines whether the user 
            wants to slide left or right */
            if(whichway == 'left'){
                //...calculating the new left indent of the unordered list (ul) for left sliding
                var left_indent = parseInt(jQuery('#carouselContent').css('left')) + slide_increment;
            }else{
                //...calculating the new left indent of the unordered list (ul) for right sliding
                var left_indent = parseInt(jQuery('#carouselContent').css('left')) - slide_increment;

            }

            //sliding effect using jQuery's animate function... '
            jQuery('#carouselContent:not(:animated)').animate({'left' : left_indent},slide_duration,function(){    

                /* when the animation finishes use the if statement again, and create infinite
                loop by swapping the position of last or first item */
                if(whichway == 'left'){
                    //...and if we slid left we put the last item before the first item...
                    jQuery('#carouselContent li:first').before(jQuery('#carouselContent li:last'));
                }else{
                    //...and if we slid right we put the first item after the last item...
                    jQuery('#carouselContent li:last').after(jQuery('#carouselContent li:first'));
                }

                //...and then restore the default left indent
                jQuery('#carouselContent').css({'left' : '-' + slide_increment + 'px'});
            });

	}