//IMG FADER
$(document).ready(function() {
    $('#slideshow').cycle({
		fx: 'fade', // choose your transition type, ex: blindX, blindY,blindZ, cover, curtainX,curtainY,fade,fadeZoom,growX,growY,scrollUp,scrollDown,scrollLeft,scrollRight,scrollHorz,scrollVert,shuffle,slideX,slideY,toss,turnUp,turnDown,turnLeft,turnRight,uncover,wipe,zoom
		speed: 1200, //The speed option defines the number of milliseconds it will take to transition from one slide to the next.
	    timeout: 7000, //The timeout option specifies how many milliseconds will elapse between the start of each transition.
		pause:  1, //The pause option causes the slideshow to pause when the mouse hovers over the slide.
		random:  0, //The random option causes the slides to be shown in random order, rather than sequential.
		//l'ozione randon da problemi con internet explorer... quindi meglio non usarla
		next:   '#nextimg', //The prev and next options are used to identify the elements which should be the triggers for prev/next transitions. When used in conjuction with timeout = 0 the effect is a manual slideshow. The values for prev and next can be a DOM element or any valid jQuery selection string.
	    prev:   '#previmg'
	});
});


$(document).ready(function() {
 
	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
		
	//Get size of divs, how many there are, then determin the size of the div reel.
	var divWidth = $(".window").width();
	var divSum = $(".div_reel div").size();
	var divReelWidth = divWidth * divSum;
	
	//Adjust the div reel to its new size
	$(".div_reel").css({'width' : divReelWidth});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var div_reelPosition = triggerID * divWidth; //Determines the distance the div reel needs to slide
 
		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".div_reel").animate({ 
			left: -div_reelPosition
		}, 500 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".div_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging 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
	});	
	
});
