$(document).ready(function() {

	// BEGIN COLLAGE ROTATOR // 

	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
		
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel .message").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image 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
		$(".image_reel").fadeOut(600);
		$(".image_reel").animate({ left: -image_reelPosition }, 0 );
		$(".image_reel").fadeIn(600);

	}; 
	
	//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
		}, 8000); //Timer speed in milliseconds (8 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel .message").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
	});	

	// END COLLAGE ROTATOR // 

	// BEGIN TESTIMONIALS // 

	//Set the display of all elements to 'none'
	$('div.testimonials blockquote').css({display: 'none'});
	
	//Get the first image and display it
	$('div.testimonials blockquote:first').css({display: 'block'});

	//Paging + Slider Function
	rotateTest = function(){	
		//Get the first image
		var current = ($('div.testimonials blockquote.show')) ? $('div.testimonials blockquote.show') : $('div.testimonials blockquote:first');
		
		nextTest = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.testimonials blockquote:first') : current.next()) : $('div.testimonials blockquote:first'));
		
		//Hide the current image
		current.fadeOut(1000).removeClass('show');

		//Set the fade in effect for the next image, the show class has higher z-index
		nextTest.delay(500).fadeIn(1000).addClass('show');				
	
	}; 
	
	//Rotation + Timing Event
	rotateTestSwitch = function(){		
		playTest = setInterval(function(){ //Set timer - this will repeat itself in the timer interval set
			rotateTest(0); //Trigger the paging and slider function
		}, 8*1000); //Timer speed in milliseconds (6 seconds)
	};
	
	rotateTestSwitch(); //Run function on launch

	// END TESTIMONIALS // 

	// BEGIN MAINMENU ROLLOVER // 

	$("#hd .menu_container li").prepend("<span></span>"); //Throws an empty span tag right before the a tag

	$("#hd .menu_container li").each(function() { //For each list item...
		var linkText = $(this).find("a").html(); //Find the text inside of the <a> tag
		$(this).find("span").show().html(linkText); //Add the text in the <span> tag
	}); 

	$("#hd .menu_container li").hover(function() {	//On hover...
		$(this).find("span").stop().animate({
			marginTop: "-32" //Find the <span> tag and move it up 32 pixels
		}, 250);
	} , function() { //On hover out...
		$(this).find("span").stop().animate({
			marginTop: "0"  //Move the <span> back to its original state (0px)
		}, 250);
	});

	// END MAINMENU ROLLOVER // 

	// BEGIN B&W IMAGE ROLLOVER //
	
	$("#ft .toolbar .actions li, #bd .services_icons li, #bd .latest_project .viewport, #hd .actions li").hover(function() { //On hover...

		var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'

		//Set a background image(thumbOver) on the <a> tag - Set position to bottom
		$(this).find("a.item").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});

		//Animate the image to 0 opacity (fade it out)
		$(this).find("span").stop().fadeTo('normal', 0 , function() {
			$(this).hide() //Hide the image after fade
		});
	} , function() { //on hover out...
		//Fade the image to full opacity 
		$(this).find("span").stop().fadeTo('normal', 1).show();
	});

	// END B&W IMAGE ROLLOVER //


});
