$(document).ready(function() {

	$("body").addClass("js");
	
	// cufon
	Cufon.replace('#header li a, #food li a, .button, .feature h2, #nav h2', { fontFamily: 'Cauldron', hover: true, textShadow: '#ffe450 0px 0px' });
	Cufon.replace('#hero h1', { fontFamily: 'James Fajardo', textShadow: '#000 2px 2px' });
	Cufon.replace('#maincontent h1, .h2 #maincontent h2', { fontFamily: 'James Fajardo', textShadow: 'rgba(0,0,0,0.2) 1px 1px' });
	Cufon.replace('.story #maincontent h2, .story #maincontent a', { fontFamily: 'James Fajardo', hover: true });
	
	// search boxes - clear fields on focus
	$("#search input").focus(function() {
		if($(this).val()=="Search the site") {
			$(this).val("");
		}
	});
	$("#recipe-search input").focus(function() {
		if($(this).val()=="Recipe search") {
			$(this).val("");
		}
	});
	
	// clipped images - fade if more than one image
	$(".clip-image").cycle({
		timeout: 5000,
		speed: 2000,
		slideExpr: "img"
	});
	
	// fancybox
	$(".fancybox").fancybox({
		overlayColor: "#000000",
		overlayOpacity: 0.5,
		autoScale: false
	});
	
	// comments - show/hide
	$("#comments li:even").addClass("odd");
	$("#comments").hide();
	$(".comments").click(function() {
		if($("#comments").hasClass("open")) {
			$("#comments").slideToggle("slow", "easeOutCirc").removeClass("open");
			$(".comments-link").html("View comments");
		} else {
			$("#comments").slideToggle("slow", "easeOutCirc").addClass("open");
			$(".comments-link").html("Hide comments");
		}
		$(this).blur();
		return false;
	})
	
	// comments - clear fields on focus
	$("#comments input, #comments textarea").focus(function() {
		$(this).val("").css("color", "#000000");
	});
	
	// comments - submit
	$("#comment-submit").bind('click', function() {
	  $.post("/ajax-proxy.cfm", $("#comment-form").serialize());
	  $("#comment-form").fadeOut();
	  $(this).closest('h3').html("Thank You for your comment");
	  $("#comment-thankyou").fadeIn();
	  return false;	  
	});
		
	
	// add print links
	$(".stats-print").append('<li class="print"><span>Print this page</span></li>').find(".print").click(function() { window.print(); });
	
	// items per page form - hide non-JS submit button
	$("#content-tab form input.submit").hide();
	$("#content-tab form select").change(function() {
		$("#content-tab form").submit();
	});
	
	// our range - hover flags
	$("#range area").hover(function() {
		$("#range img").addClass($(this).attr("class"));
	}, function() {
		$("#range img").removeClass();
	});
	
	// star rating
	$(".star-rating a").click(function() {
		// prevent multiple votes
		if(!$(this).parent().parent().hasClass("star-rating-voted")) {
			// get star clicked
			rating = parseInt($(this).text());
			// do ajax post here
			$.post("/ajax-proxy.cfm", {proxymethod:'AddRecipeRating',id:$(this).closest('ul').attr('id'), rating:rating});
			// update with clicked score
			$(this).parent().parent().removeClass().addClass("star-rating star-rating-voted star-rating-voted-" + rating).find(".current-rating").removeClass().addClass("current-rating current-rating-" + rating);
		}
		return false;
	});
	
	// food menu - add glow on hover
	if(!$("body").hasClass("home")) {
		$("#food li").hover(function() {
			$(this).toggleClass("on");
		});
	}
	
	// product selector scrolling panel
	$("#product-selector").jScrollPane({
		dragMinHeight: 51,
		dragMaxHeight: 51,
		scrollbarWidth: 25
	}).parent().hide();
	$("#product-selector-handle").html("Select a product").click(function() {
		$(this).toggleClass("product-selector-handle-open");
		$(this).parent().find(".jScrollPaneContainer").slideToggle("fast");
		$(this).blur();
		return false;
	});

	// carousel
	var carouselShow = 3;
	var carouselItems = $("#carousel li").length;
	var carouselLength = carouselItems + (2*carouselShow); /* 3 added either side for circular wrapping */
	var carouselCurrent = carouselShow + 2; /* start at second (middle) item, once 3 have been added before items */	
	$("#carousel .next").click(function() {
		if(!running) {
			carouselCurrent++;
			if(carouselCurrent == carouselLength) {
				carouselCurrent = (2*carouselShow);
				carouselShrink(carouselCurrent - 2, true);
				carouselShrink(carouselLength-2, false);
			}
			carouselShrink(carouselCurrent - 2, false);
			carouselShrink(carouselCurrent + 1, false);
			carouselGrow(carouselCurrent - 1);
		}
	});
	$("#carousel .prev").click(function() {
		if(!running) {
			carouselAnimating = true;
			carouselCurrent--;
			if(carouselCurrent == 1) {
				carouselCurrent = carouselItems + 1;
				carouselShrink(carouselCurrent, true);
				carouselShrink(1, false);
			}
			carouselShrink(carouselCurrent - 3, false);
			carouselShrink(carouselCurrent, false);
			carouselGrow(carouselCurrent - 1);
		}
	});
	
	var carouselShrink = function(index, growFirst) {
		if(growFirst) {
			$("#carousel li").eq(index).css("opacity", "1").css("width", "110px").find("img").addClass("on").css("width", "100px").css("height", "117px").css("margin-top", "-59px").css("margin-left", "-51px");
		}
		if($.browser.msie) {
			topMargin = "-29px";
		} else {
			topMargin = "-25px";
		}
		$("#carousel li").eq(index).css("width", "60px").find("img").removeClass("on").animate({
			width: "50px",
			height: "63px",
			marginTop: topMargin,
			marginLeft: "-25px",
			opacity: "0.6"
		}, 500);
	}
	var carouselGrow = function(index) {
		$("#carousel li").eq(index).css("width", "110px").find("img").addClass("on").animate({
			width: "100px",
			height: "117px",
			marginTop: "-55px",
			marginLeft: "-51px",
			opacity: "1"
		}, 500);
		active = $("#carousel li").eq(index);
		$("#carousel-item").html('<a href="' + active.find("a").attr("href") + '">' + active.find("img").attr("alt") + '</a>');
	}
	
	$("#carousel").jCarouselLite({
		btnNext: "#carousel .next",
	    btnPrev: "#carousel .prev",
		circular: true
	});
	carouselGrow(carouselCurrent-1);
	
});
