$(document).ready(function () {

    //setttings        
    $(".carouselContainer .paging").hide();

    //Set Default State of each portfolio piece
    $(".carouselContainer .paging a:first").addClass("active");

    //Get size of images, how many there are, then determin the size of the image reel.
    var imageSum = $(".image_reel li").size();
    var imageWidth = $(".window").width();
    var imageReelWidth = (imageWidth * imageSum);

    //Create paging	
    
    for (var i = 0, im = imageSum; im > i; i++) {
        var item = i + 1
        $(".carouselContainer .paging").append("<a href='#' rel='" + item + "'><span>" + item + "</span></a>")
    }

    //Adjust the image reel to its new size
    $(".image_reel").css({ 'width': imageReelWidth });

    //Paging + Slider Function
    rotate = function () {
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
        $(".carouselContainer .paging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        //alert('animate, active is ' + triggerID);
        $(".image_reel").animate({
            left: -image_reelPosition
        }, 1000); // Note: time (in ms) between transitions
    };

    //Rotation + Timing Event
    rotateSwitch = function () {

        play = setInterval(function () {

            //Set timer - this will repeat itself every 3 seconds
            $active = $('.carouselContainer .paging a.active').next();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('.carouselContainer .paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function

        }, 5000); // Note: Timer speed in milliseconds (3 seconds)
    };

    rotateSwitch(); //Run function on launch

    //    On Hover
    $(".image_reel li").hover(function () {
        clearInterval(play); //Stop the rotation
    }, function () {
        rotateSwitch(); //Resume rotation
    });

    //On Click
    $(".carouselContainer .paging a").click(function () {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor

    });

    $(".carouselButtons .previousBtn").click(function () {
        Step(-1);
    });

    $(".carouselButtons .nextBtn").click(function () {
        Step(1);
    });


    //go to next or previous, direction is 1 or -1
    Step = function (direction) {

        if (direction > 0) {
            $active = $('.carouselContainer .paging a.active').next();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('.carouselContainer .paging a:first'); //go back to first
            }
        } else {
            $active = $('.carouselContainer .paging a.active').prev();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('.carouselContainer .paging a:last'); //go back to first
            }
        }

        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor

    };

});
