﻿$(document).ready(function () {
    //Set Default State of each portfolio piece
    $(".sliderpaging").show();
    $(".sliderpaging a:first").addClass("active");

    //Get size of images, how many there are, then determin the size of the image reel.
    var sliderWidth = $("div#sliderwindow").width();

    var divCount = $("div#sliderreel div").size();     //Number of div elements
    var sliderReelWidth = sliderWidth * divCount;   //Total width of slider reel

    //Adjust the image reel to its new size:
    $("div#sliderreel").css({ 'width': sliderReelWidth });

    //Paging + Slider Function
    rotate = function () {
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var sliderReelPosition = triggerID * sliderWidth; //Determines the distance the image reel needs to slide

        $(".sliderpaging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        $("div#sliderreel").animate({
            left: -sliderReelPosition
        }, 1800);

    };

    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { //Set timer - this will repeat itself every x seconds
            $active = $('.sliderpaging a.active').next();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('.sliderpaging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 8000); //Timer speed in milliseconds (x seconds)
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    $("div#sliderreel div").hover(function () {
        clearInterval(play); //Stop the rotation
    }, function () {
        rotateSwitch(); //Resume rotation
    });

    //On Page Click:
    $(".sliderpaging 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
    });

    $(".sliderleftpage a").click(function () {

        //Slider Animation
        clearInterval(play); //Stop the rotation
        $active = $('.sliderpaging a.active').prev();
        if ($active.length === 0) { //If paging reaches the end...
            $active = $('.sliderpaging a:last'); //go back to first
        }
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });

    $(".sliderrightpage a").click(function () {

        //Slider Animation
        clearInterval(play); //Stop the rotation
        $active = $('.sliderpaging a.active').next();
        if ($active.length === 0) { //If paging reaches the end...
            $active = $('.sliderpaging a:first'); //go back to first
        }
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });
});
