$(document).ready(function() {



// Fixed Scrolling
	var top = $('#featured-links div').offset().top - parseFloat($('#featured-links div').css('marginTop').replace(/auto/, 0));
	$(window).scroll(function (event) {
		// what the y position of the scroll is
		var y = $(this).scrollTop();

		if (y >= top) {
			$('#featured-links div').addClass('fixed');
		} else {
			$('#featured-links div').removeClass('fixed');
		}
	});

// Infinite Carousel
	$('#featured-links').infiniteCarousel();

// Background
	$('#supersize').supersized();

// Navigation Hovers
	var colorsRotate = [
		"#a39f2a",
		"#faa91f",
		"#3e9bd5",
		"#e34138",
		"#69b0a4",
		"#d48f47"
	]
	var colorCounter = -1;
	function colorRotate() {
		if (colorCounter < (colorsRotate.length - 1)) {
			colorCounter++
		} else {
			colorCounter = 0;
		}
		return colorsRotate[colorCounter];
	}
	
	// We should really optimize this part.
	
	var sidenavli = $("#sidenav ul li a:not(#sidenav ul li ul li a, #sidenav ul li.active a)");
	
	sidenavli.hover(
	function() {
		$('.backblock',this).stop().css({backgroundColor:colorRotate()}).animate({opacity:1}, 250);
	},
	function() {
		$('.backblock',this).stop().animate({opacity:0}, 400);
	})
	.css('position','relative')
	.append( $('<span>')
		.attr('class', 'backblock')
		.css({
			opacity: 0,
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: -1,
			width: sidenavli.width(),
			height: sidenavli.height()
		})
	);
	
	// Featured link hovers
	
	var featuredlink = $("#featured-links ul li a");
	
	featuredlink.hover(
	function() {
		$('.backblock',this).stop().css({backgroundColor:colorRotate()}).animate({opacity:1}, 250);
	},
	function() {
		$('.backblock',this).stop().animate({opacity:0}, 400);
	})
	.css('position','relative')
	.append( $('<span>')
		.attr('class', 'backblock')
		.css({
			opacity: 0,
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: -1,
			width: featuredlink.outerWidth(),
			height: featuredlink.outerHeight()
		})
	);
	
	// Featured link arrows
	
	var arrowed = $("#featured-controls a");
	
	arrowed.hover(
	function() {
		$('.backblock',this).stop().css({backgroundColor:colorRotate()}).animate({opacity:1}, 250);
	},
	function() {
		$('.backblock',this).stop().animate({opacity:0}, 400);
	})
	.append( $('<span>')
		.attr('class', 'backblock')
		.css({
			opacity: 0,
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: -1,
			width: arrowed.width(),
			height: arrowed.height()
		})
	);
	


// Homepage scrolling
	
	
	
	var quoteWrapper = $('#quote-wrapper'),
		quoteDiv = $('#quotse').css({'overflow' : 'hidden'}),
		quoteScroller = $('#quotse ul'),
		quoteQuotes = $('#quotse ul > li');
	
	
	var quoteHeightAdjust = 0;
	quoteQuotes.each(function() {
		if ($(this).height() > quoteHeightAdjust) {
			quoteHeightAdjust = $(this).outerHeight();
		}
	}); quoteDiv.height(quoteHeightAdjust * 1.4);
	
	//quoteScroller.css('width', quoteQuotes[0].offsetWidth * quoteQuotes.length);
	$('<span id="quotse-controls"><a class="quotse-left">&laquo; Left</a><a class="quotse-right">Right &raquo;</a></span>').insertAfter(quoteDiv);

	$('#quote-wrapper').serialScroll({
		target: '#quotse',
		items: quoteQuotes,
		axis: 'x',
		duration: '400',
		force: true
	});
	
	var quoteeNames = $('#quotee ul > li');
	
	$('#quote-wrapper').serialScroll({
		target: '#quotee',
		items: quoteeNames,
		axis: 'y',
		duration: '400',
		force: true
	});
	
	$('a.quotse-right').click(function() {
		$('#quotee').trigger('next');
		$('#quotse').trigger('next');
	});
	
	$('a.quotse-left').click(function() {
		$('#quotee').trigger('prev');
		$('#quotse').trigger('prev');
	});
	
	// Featured link arrows
	/*
	var homepageArrows = $("#quotse-controls");
	
	homepageArrows.hover(
	function() {
		$('.backblock',this).stop().css({backgroundColor:colorRotate()}).animate({opacity:1}, 250);
	},
	function() {
		$('.backblock',this).stop().animate({opacity:0}, 400);
	})
	.append( $('<span>')
		.attr('class', 'backblock')
		.css({
			opacity: 0,
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: -1,
			width: homepageArrows.width(),
			height: homepageArrows.height()
		})
	);
	*/
	
	
	var clearMePrevious = '';

// clear input on focus
$('.clearMeFocus').focus(function() {
	if($(this).val()==$(this).attr('title')) {
		clearMePrevious = $(this).val();
		$(this).val('');
	}
});

	// if field is empty afterward, add text again
	$('.clearMeFocus').blur(function() {
		if($(this).val()=='') {
			$(this).val(clearMePrevious);
		}
	});

	
	
});

// Cufon... More like CuFUN!
Cufon.replace('h2.entry-header', { fontFamily: 'Leviathan Black' });
Cufon.replace('#quotse h3', { fontFamily: 'Leviathan Black' });
