﻿$(document).ready(function() {
	//Show the paging and activate its first link
    $(".paging_left").show();
    $(".paging_left a:first").addClass("active");

    //Get size of the image, how many images there are, then determin the size of the image reel.
    var imageWidth_l = $(".window_left").width();
    var imageSum_l = $(".image_reel_left img").size();
    var imageReelWidth_l = imageWidth_l * imageSum_l;

    //Adjust the image reel to its new size
    $(".image_reel_left").css({'width' : imageReelWidth_l});
    
    //Paging  and Slider Function
    rotate_l = function(){
        var triggerID_l = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition_l = triggerID_l * imageWidth_l; //Determines the distance the image reel needs to slide

        $(".paging_left a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        $(".image_reel_left").animate({
            left: -image_reelPosition_l
        }, 500 );

    }; 

    //Rotation  and Timing Event
    rotateSwitch_l = function(){
        play_l = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
            $active = $('.paging_left a.active').next(); //Move to the next paging
            if ( $active.length === 0) { //If paging reaches the end...
                $active = $('.paging_left a:first'); //go back to first
            }
            rotate_l(); //Trigger the paging and slider function
        }, 4000); //Timer speed in milliseconds (7 seconds)
    };

    rotateSwitch_l(); //Run function on launch
    
    //On Hover
    $(".image_reel_left a").hover(function() {
        clearInterval(play_l); //Stop the rotation
    }, function() {
        rotateSwitch_l(); //Resume rotation timer
    });	

    //On Click
    $(".paging_left a").click(function() {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        clearInterval(play_l); //Stop the rotation
        rotate_l(); //Trigger rotation immediately
        rotateSwitch_l(); // Resume rotation timer
        return false; //Prevent browser jump to link anchor
    });
});