$(document).ready(function() {

	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
		
var imageWidth = 940;
var numOfImages = 5;
var currentImageIndex = 1;
var nextImageIndex = 2;

var play = 0;
	
	rotate = function() { 
    	$(".paging a").removeClass('active'); //Remove all active class
        $(".paging a[name="+nextImageIndex+"]").addClass('active'); //Add active class 

//            Move images into position
	    $("div[name=img-"+nextImageIndex+"]").css("left", imageWidth+"px");
                               
     //Slider Animation
/*	 	$("div[name=img-"+currentImageIndex+"]").css("zIndex", "1");
		$("div[name=img-"+nextImageIndex+"]").css("zIndex", "2");*/
                               $("div[name=img-"+currentImageIndex+"]").animate({left: -imageWidth}, 500);
                                $("div[name=img-"+nextImageIndex+"]").animate({left: 0}, 500);
                                
                                currentImageIndex = nextImageIndex;
                };
	
	rotateSwitch = function(){                           
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds                                                
			nextImageIndex = Number(currentImageIndex) + 1;
            	if (nextImageIndex > numOfImages) {
					nextImageIndex = 1;
                                                }
                                                
						rotate(); //Trigger the paging and slider function
                        }, 7000); //Timer speed in milliseconds (3 seconds)
                };

	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel .slide a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	

	$(".paging a").click(function() {
    	var nextIndex = $(this).text();
                              
        if (nextIndex == currentImageIndex) return;
                                
        nextImageIndex = nextIndex;
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
                });
	
});

