jQuery(document).ready(function () {
    //Set Default State of each portfolio piece
    jQuery(".sliderLinks").show();
    jQuery(".sliderLinks a:first").addClass("active");
    jQuery(".slider li:first").addClass("active");
    jQuery("li.active").fadeToggle(1000, "linear", function () {
    });

    //Get size of images, how many there are, then determin the size of the image reel.
    var panelWidth = jQuery(".panel").outerWidth();
    var windowWidth = jQuery(".window").width();
    var imageSum = jQuery(".slider li.panel").size();
    var imageReelWidth = panelWidth * imageSum;

    //Adjust the image reel to its new size
    jQuery(".slider").css({ 'width': imageReelWidth });

    //sliderLinks + Slider Function
    rotate = function () {
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        jQuery("li.active").fadeToggle(1000, "linear", function () {
        });
        jQuery(".sliderLinks a").removeClass('active'); //Remove all active class
        jQuery(".slider li").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)	
        $sliderActive.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)	
        jQuery("li.active").fadeToggle(1000, "linear", function () {
        });
    };

    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { //Set timer - this will repeat itself every 3 seconds
            $active = jQuery('.sliderLinks a.active').next();
            if ($active.length === 0) { //If sliderLinks reaches the end...
                $active = jQuery('.sliderLinks a:first'); //go back to first
            }
            $sliderActive = jQuery('.slider li.active').next();
            if ($sliderActive.length === 0) { //If sliderLinks reaches the end...
                $sliderActive = jQuery('.slider li:first'); //go back to first
            }
            rotate(); //Trigger the sliderLinks and slider function
        }, 12000); //Timer speed in milliseconds (3 seconds)
    };

    rotateSwitch(); //Run function on launch	

    //On Hover
    /*jQuery(".slider li").hover(function () {
    clearInterval(play); //Stop the rotation
    }, function() {
    rotateSwitch(); //Resume rotation
    });*/

    //On Click
    jQuery(".sliderLinks a").click(function () {
        $active = jQuery(this); //Activate the clicked sliderLinks
        var sliderTargetRel = $active.attr("rel");
        var sliderTarget = ".slider li:nth-child(" + sliderTargetRel + ")";
        $sliderActive = jQuery(sliderTarget);
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });
});
