$(document).ready(function(){

	/* Single Field Forms */

	if($("form.singlefield").length) {

		$('form.singlefield input[type="text"]').bind({
			'focus': function() {
				var value = $(this).val();
				var title = $(this).attr('title');
				if(value == title) $(this).val('');
				$(this).addClass('userinput');
			},
			'blur': function() {
				var value = $(this).val();
				var title = $(this).attr('title');
				if(value == '') {
					$(this).removeClass('userinput');
					$(this).val(title);
				}
			}
		});

	}

	/* Fragments */

	if($(".table-of-contents").length) {

		$(".table-of-contents li > a.on").show_fragment();

		$(".table-of-contents li a").click(function(e){
			e.preventDefault();
			var parent = $(this).parents(".table-of-contents");
			$("li > a.on", parent).hide_fragment();
			$(this).show_fragment();
			return false;
		});

	}

	/* Stickers */

	if($(".sticker").length) {

		$(".sticker").each(function(){
			var offset = $(".sticker").offset();
			$(this).data("offset-top-init", offset.top);
		});

		$(window).bind('scroll', function() {

			var top = $(this).scrollTop();

			$(".sticker").each(function(){

				if(top > $(this).data("offset-top-init")) {
					$(this).addClass("fixed");
				} else {
					$(this).removeClass("fixed");
				}

			});

		});

	}

	/* Carousels */

	if($("#carousel1").length) {

		$('#carousel1 ul.roundabout').roundabout({
			shape: 'theJuggler',
			minOpacity: 1,
			reflect: true,
			focusBearing: 2.1,
			btnPrev: '#carousel1-prev',
			btnNext: '#carousel1-next'
		});
	}

	if($("#carousel2").length) {

		$('#carousel2 ul.roundabout').roundabout({
			shape: 'theJuggler',
			minOpacity: 1,
			focusBearing: 7.3,
			reflect: true,
			btnPrev: '#carousel2-prev',
			btnNext: '#carousel2-next'
		});

	}

});

$.fn.show_fragment = function() {
	var fragment = $(this).attr("href");
	$(this).addClass("on");
	$(fragment).show();
}

$.fn.hide_fragment = function() {
	var fragment = $(this).attr("href");
	$(this).removeClass("on");
	$(fragment).hide();
}
