$(document).ready(function() {
	
	/* CUFON */
	Cufon.set('fontFamily', 'Audebaud' ).replace('a.flourish', { hover: true })('form.search label')('h2')('h3')('body.recipe div#uses p');
	Cufon.set('fontFamily', 'Tungsten Black' ).replace('body.our-heritage div.grid-2 h3')('body.awards div.grid-2 h3')('h3#preparation')('h3#serves')('h3#ingredients')('h3#method')('h3#alternative')('body.contact-us h3')('body.recipe div#uses span.cheese')('h3.cheese-platter')('div.cheese-platter h3');
	
	/* NAVIGATION */
	$('ul#navigation ul li:first-child').css('padding-top', '4px');
	$('ul#navigation ul li:last-child').css('padding-bottom', '5px');
	$('ul#navigation > li').hover(function() {
		$(this).find('ul:hidden').slideDown();
		$(this).find('a.flourish').css('color', '#00703c');
		$(this).find('span').animate({
   			opacity: 1
		}, 'fast', function(){$(this).clearQueue();});
	}, function(){
		$(this).find('ul').slideUp(function(){
			$(this).clearQueue();
		});
		if (($(this).hasClass('current-menu-item') == false) && ($(this).hasClass('current-menu-ancestor') == false)) {
			$(this).find('a.flourish').css('color', '#606060');
			$(this).find('span').animate({
	   			opacity: 0
			}, 'slow', function(){$(this).clearQueue();});
			$(this).find('a.flourish').each(function() {
				Cufon.set('fontFamily', 'audebaud' );
				Cufon.replace(this, { color: '#606060'});
			});
		}
	});

	
	/* IMAGE HOVERS */
	$('div#content div.image, div#content a').hover(function(){
		$(this).nextAll().find('.flourish').addClass('hover');
		var colorHover = $(this).nextAll().find('.flourish').css('color');
		$(this).nextAll().find('.flourish').each(function() {
			Cufon.set('fontFamily', 'audebaud' );
			Cufon.replace(this, { color: colorHover});
		});
	},
	function(){
		$(this).nextAll().find('.flourish').removeClass('hover');
		var colorLink = $(this).nextAll().find('.flourish').css('color');
		$(this).nextAll().find('.flourish').each(function() {
			Cufon.set('fontFamily', 'audebaud' );
			Cufon.replace(this, { color: colorLink});
		});
	});
	
	/* IMAGE HOVERS */
	$('body.main div#content div.image').hover(function(){
		$(this).parent().find('.flourish').addClass('hover');
		var colorHover = $(this).parent().find('.flourish').css('color');
		$(this).parent().find('.flourish').each(function() {
			Cufon.set('fontFamily', 'audebaud' );
			Cufon.replace(this, { color: colorHover});
		});
	},
	function(){
		$(this).parent().find('.flourish').removeClass('hover');
		var colorLink = $(this).parent().find('.flourish').css('color');
		$(this).parent().find('.flourish').each(function() {
			Cufon.set('fontFamily', 'audebaud' );
			Cufon.replace(this, { color: colorLink});
		});
	});
	
	/* IMAGE HOVERS */
	$('body.product a, body.recipe a').hover(function(){
		$(this).next('.flourish').addClass('hover');
		var colorHover = $(this).next('.flourish').css('color');
		$(this).next('.flourish').each(function() {
			Cufon.set('fontFamily', 'audebaud' );
			Cufon.replace(this, { color: colorHover});
		});
	},
	function(){
		$(this).next('.flourish').removeClass('hover');
		var colorLink = $(this).next('.flourish').css('color');
		$(this).next('.flourish').each(function() {
			Cufon.set('fontFamily', 'audebaud' );
			Cufon.replace(this, { color: colorLink});
		});
	});
	
	/* SLIDESHOW */
	$('#slideshow').cycle({ 
	    fx:      'fade', 
	    speed:    'slow', 
	    timeout:  5000,
	    next: '#next',
	    prev: '#previous',
	    pager: 'span#pager'
	});
	var howmanyslides = $('#slideshow img').length;
	$('span#pager').css({
		width: howmanyslides*23+'px', 
		marginLeft: '-'+(howmanyslides*23-14)/2+'px'
	
	});
	
	/* VERTICALLY ALign uses cheese */
	var uses = $('div#uses').length;
	if (uses) {
		var usesHeight = $('div#uses').height();
		var usesRightHeight = $('div#uses div.right').height();
		var margins = 
		$('div#uses div.right').css('margin-top', (usesHeight-usesRightHeight)/2+'px');
	}
	
	
	/* go ROllovers */
	$('input[src="images/btn-go.gif"]').hover(function(){
		$(this).attr('src', 'images/btn-go-over.gif');
	}, function(){
		$(this).attr('src', 'images/btn-go.gif');
	});
	
	
	/* CONTACT US */
	$('#contact-submit').hover(function() {
		$(this).css('background-image', 'url(images/contact-submit-over.gif)');
	}, function() {
		$(this).css('background-image', 'url(images/contact-submit.gif)');
	});
	
	jQuery.validator.addMethod('accept', function(value, element, param) {
		return value.match(new RegExp('^' + param + '$'));
	});
	
	$('form', '#contact-us').validate({
		errorElement: 'span',
		errorContainer: $('#contact-errors'),
		errorPlacement: function(error, element) {
			error.appendTo( element.parent('li').prev('li').find('span') );
		},
		rules: {
			'ml[name]'          : {
				required: true
			},
			'ml[email_address]' : {
				required: true,
				email: true
			},
			'ml[message]'       : 'required',
			'ml[comment_type]'  : 'required'
		},
		messages: {
			'ml[name]'          : {
				required: '* Required'
			},
			'ml[email_address]' : {
				required: '* Required',
				email: 'Your email address is not valid'
			},
			'ml[message]'       : '* Required',
			'ml[comment_type]'  : '* Required'
		}
	});
	
	// ACCORDION MENU
	$('li', 'ol.accordion').each(function() {
		$('h3', this).bind('click', function() {
			$(this).next().slideToggle('fast', function() {
				$('span', $(this).parent().find('h3')).css('background-position', $(this).is(':visible') ? '0 -11px' : '0 0');
			});	
		}).append('<span></span>');
	});
	
});


/* HACKS FOR IE NAV HOVER */
$(window).load(function() {
	$('ul#navigation li a.flourish span').css({
		'opacity': 0,
		'display': 'inline-block'
	});
	$('ul#navigation li.current-menu-item a.flourish span, ul#navigation li.current-menu-ancestor a.flourish span').css('opacity', 1);
});
