jQuery(document).ready(function($) {
	
	var cycleFailed = false;
	
	// Set the menu's arrow to the correct starting position. Also set "current" class.
	$('#navigation li.current_page_item a, .current-page-ancestor a').addClass("current");
	
	var pageItemPosition = $('.current').position();
	var startPosition = $('.current').width()/2-7;
	
	$('#navigation .arrow').css(
		"left", pageItemPosition.left + startPosition
	);

	// Navigation hover fading.
	$("#navigation a").hover(
		function() {
			$(this).stop(true,true);
			$(this).animate({
				color: '#66a4d0'
			});
		},
		function() {
			$(this).stop(true,true);
			$(this).animate({
				color: '#75683c'
			});
		}
	);

	// Move arrow position on click.
	$('#navigation a').click(
		function() {
			$("#navigation a").removeClass("current");
			var classAdd = $(this);
			var navPosition = $(this).position();
			var navWidth = ($(this).width()/2) - 7;
			$('#navigation .arrow').animate({left: navPosition.left + navWidth}, 200, function(){
				// Change the "current" class to the newly selected item.
				classAdd.addClass("current");
			});
		}
	);
	
	// Create slideshow
	$('.container').cycle({
		fx: 'fade',
		pager: '#numbers div',
		speed: 400,
		timeout: 3000,
		onfail: function(){cycleFailed = true;},
		onNextSlide: function(index){  $("#numbers").scrollTo($("#numbers div a").eq(index)); }
	});
	
	// Create pause button and controls
	if(cycleFailed == false) {
		
		$('#numbers').before('<span id="leftscroll">&laquo;</span>');
		$('#numbers').after('<span id="rightscroll">&raquo;</span>');
		$('#slideshow').after('<div id="controls" class="clickable">Pause Slideshow</div>');
		$('.container').prepend('<div id="arrows"><div id="left_arrow">&laquo;</div><div id="right_arrow">&raquo;</div></div>');
		$('#arrows').hide();
		$('.container').hover(
			function () {
				$('#arrows').fadeIn('fast');
			}, 
			function () {
				$('#arrows').fadeOut('fast');
			}
		);
		$('#left_arrow').click(function() {
			$('.container').cycle('prev');
		});
		$('#right_arrow').click(function() {
			$('.container').cycle('next');
		});
		$('#controls').click(function() {
			if($(this).text() == "Pause Slideshow") {
				$('.container').cycle('pause');
				$(this).text('Resume Slideshow');
			} else {
				$('.container').cycle('resume');
				$(this).text('Pause Slideshow');
			}
			}
		);
	};
	
	// Number Scroll
	var sum = 0;
	
	$('#numbers a').each(function() {
		sum += parseFloat(
				($(this).width() + 20)
			);
	});
	
	$('#numbers div').css('width', (sum-19));

	$('#leftscroll').click(function() {
			$('#numbers').scrollTo("-=582px");
	});
	
	$('#rightscroll').click(function() {
			$('#numbers').scrollTo("+=582px");
	});

});
