﻿$(document).ready(function () {
    
    $("div#slideshowcontainer .pages").show();
    $("div#slideshowcontainer .pages a:first").addClass("active");

    var slideWidth = $("div#slideshowcontainer div.window").width();
    var slideCount = $("div#slideshowcontainer div.reel div").size();     
    var reelWidth = slideWidth * slideCount;   

    //Adjust the image reel to its new size:
    $("div#slideshowcontainer div.reel").css({ 'width': reelWidth });

    //Paging + Slider Function
    rotate = function () {
        var triggerID = $active.attr("rel") - 1; 
        var reelPosition = triggerID * slideWidth; 

        $("div#slideshowcontainer .pages a").removeClass('active'); 
        $active.addClass('active'); 

        //Slider Animation
        $("div#slideshowcontainer div.reel").animate({
            left: -reelPosition
        }, 1800);

    };

    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { 
            $active = $("div#slideshowcontainer .pages a.active").next();
            if ($active.length === 0) { 
                $active = $("div#slideshowcontainer .pages a:first"); 
            }
            rotate(); 
        }, 5000); 
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    $("div#slideshowcontainer div.reel div").hover(function () {
        clearInterval(play); //Stop the rotation
    }, function () {
        rotateSwitch(); //Resume rotation
    });

    //On Page Click:
    $("div#slideshowcontainer .pages 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
    });

    $("div#slideshowcontainer div.left a").click(function () {

        clearInterval(play); //Stop the rotation
        $active = $("div#slideshowcontainer .pages a.active").prev();
        if ($active.length === 0) {
            $active = $("div#slideshowcontainer .pages a:last"); 
        }
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation

        return false; 
    });

    $("div#slideshowcontainer div.right a").click(function () {

        clearInterval(play); //Stop the rotation
        $active = $("div#slideshowcontainer .pages a.active").next();
        if ($active.length === 0) { 
            $active = $("div#slideshowcontainer .pages a:first"); 
        }
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation

        return false; 
    });
});
