jQuery(function() {
	// Remove the right border from the last item in the navigation menus.
	jQuery('.navigation').each(function() {
		jQuery('> ul > li:last', this).css('border-right', 'none');
	});
	jQuery('#top .navigation > ul > li:first, .footer .navigation > ul > li:first').addClass('first');
	
	// Go down to the ul.sub-menu level and back up to the parent (ie: only bind the hover event onto items with sub menus)
	jQuery('#top .navigation > ul > li > ul.sub-menu').parent().addClass('hasChild').hoverIntent({
		over: function() {
			jQuery(this).addClass('hover').find('> .sub-menu', this).slideDown('normal');
		},
		timeout:500,
		out: function() {
			jQuery(this).removeClass('hover').find('> .sub-menu', this).slideUp('normal');
		}
	});
	// add level classes to help with depth recognition in the css.
	jQuery('#top .navigation > ul > li > ul.sub-menu').addClass('level0').find('> li > ul.sub-menu').addClass('level1');
	jQuery('#top .navigation > ul > li:nth-child(2)').addClass('recipes');
	
	jQuery('.fancybox').fancybox({
		showNavArrows: false,
		titleShow: false,
		width:100
	});
	
	if (jQuery.uniform) {
		jQuery('form select, form input:checkbox').uniform();
		// Check the form for input and textareas only.
		jQuery('.uniform input, .uniform textarea').each(function() {
			var _rel = jQuery(this).attr('rel');
			if (_rel) { // we use the rel to store the suggest text.
				jQuery(this).addClass('suggest-text').val(_rel)
					.focus(function() {
						if (jQuery(this).val() == jQuery(this).attr('rel')) {
							jQuery(this).val("").removeClass('suggest-text');;
						}
					})
					.blur(function() {
						if (jQuery(this).val() == "") {
							jQuery(this).val(jQuery(this).attr('rel')).addClass('suggest-text');
						}
					});
			}
		}).parent('.uniform').submit(function() { // clear the suggest values on submit.
			jQuery('input:not([type=hidden]), textarea:not(.his-caption):not(.her-caption)', this).each(function() {
				if (jQuery(this).attr('rel') && (jQuery(this).val() == jQuery(this).attr('rel'))) {
					jQuery(this).val("");
				}
			});
			
			if (!validateform(this)) {
				jQuery('input:not([type=hidden]):not(.submit), textarea:not(.his-caption):not(.her-caption)', this).each(function() {
					var _rel = jQuery(this).attr('rel');
					if (_rel && jQuery(this).val() == "") {
						jQuery(this).addClass('suggest-text').val(_rel);
					}
				});
				return false;
			} else {
				return true;
			}
		});
	}
	
	/* Meet the Team Page */
	if (jQuery('#frame-wall').length > 0) {
		// Pagination
		jQuery('#frame-wall').quickPager({
			pageSize:10,
			currentPage:1,
			holder:"#pager .numbers"
		});
		jQuery('#pager a').each(function() {
			jQuery(this).addClass('page-'+jQuery(this).attr('rel'));
		});
		if (jQuery('#pager .numbers a').length > 0) {
			jQuery('#pager').prepend('<img src="/wp-content/themes/modularity/images/page.png" alt="page:" class="page-indicator" />');
		}
		
		// Meet the team tooltips
		jQuery('#frame-wall li').tooltip({
			bodyHandler:function() {
				return jQuery('.hover-content', this).html();
			},
			delay:0,
			fade:250,
			left:5,
			top:5,
			track:true,
			showURL:false
		}).find('img').attr('title', '');
	}
	
	/* Recipe Category Page */
	jQuery('.pack-container').hover(
		function() {
			jQuery(this).removeClass('top').addClass('bottom');
		},
		function() {
			jQuery(this).removeClass('bottom').addClass('top');
		}
	);
	
	/* Recipe Page Tabs */
	jQuery('.single-recipe #tabs > li').click(function() {
		if (!jQuery(this).hasClass('active')) {
			jQuery('#tabs > li.active').removeClass('active');
			jQuery(this).addClass('active');
			jQuery('#tab-info > li:visible').css({'display':'none'});
			jQuery('#tab-info > li[rel='+jQuery(this).attr('rel')+']').css({'display':'block'});
		}
	}).hover(
		function() {
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).removeClass('hover');
		}
	);
	// royal page
	jQuery('.results').click(
		function() {
		jQuery('#results').slideDown();
	});
	
	// Where an image has a link around it and is inside a frame (which is absolutely positioned above it), make the frame clickable.
	jQuery('.frame-container a').parent('.frame-container').css('cursor', 'pointer').click(function() {
		location.href = jQuery('a', this).attr('href');
	});
	jQuery('.frame-container[rel]').css('cursor', 'pointer').click(function() {
		var _link = jQuery(this).attr('rel');
		
		if (_link.indexOf('http://') != -1) {
			window.open(_link);
		} else {
			location.href = _link;
		}
	});
	
	/* FAQs Page */
	jQuery('.page-id-134 .page .col2 ul li p, ul.faqs li p, .page-id-107 .page .col2 ul li p').hide().siblings('h5').click(function() {
		var _el = this;
		jQuery('.page-id-134 .page .col2 ul li p:visible, ul.faqs li p:visible, .page-id-107 .page .col2 ul li p:visible').slideUp(function() {
			jQuery(_el).siblings('p').slideDown();
		});
	}).css('cursor', 'pointer');
	// If we're on the FAQs page, set a timeout to slide down the first item to show the user that this functionality exists.
	if (jQuery('.page-id-134, .page-id-107, ul.faqs').length > 0) {
		setTimeout("jQuery('.page-id-134 .page .col2 ul > li:first p, ul.faqs > li:first p, .page-id-107 .page .col2 ul > li:first p').slideDown()", 1000);
	}
	
	// Add a character limit to the his and her caption boxes
	jQuery('.her-caption, .his-caption').keyup(function(e) {
		/* 8 = Backspace, 46 = delete */
		if (this.value.length >= 75 && (e.keyCode != 8 || e.keyCode != 46)) {
			this.value = this.value.substring(0, 75);
		}
	});
	
	/* E-Card Page */
	// Click here for some inspiration functionality.
	var _inspiration = new Array();
	_inspiration.push({her:'No, you\'re thinking of lasagne.', his:'Our love is a many-layered thing.', ladiesFirst:false});
	_inspiration.push({her:'You\'re an idiot.', his:'Out of all the Italian renaissance artists, I think my favourite is Cannelloni.', ladiesFirst:false});
	_inspiration.push({her:'I\'d rather we just had a curry.', his:'Shall we have the neighbours for dinner?', ladiesFirst:false});
	_inspiration.push({her:'It\'s not 1971, Peter.', his:'This food is groo-vy!', ladiesFirst:false});
	_inspiration.push({her:'Some phrases just aren\'t designed for middle-aged men, Peter.', his:'This chicken is really rocking my world right now.', ladiesFirst:false});
	_inspiration.push({her:'Just think, 12 years ago today we were walking down the aisle.', his:'Trust you to remember our first shopping trip.', ladiesFirst:true});
	_inspiration.push({her:'Great dinner party last night. Sarah made her chicken soup.', his:'She\'s far too nice to those animals.', ladiesFirst:true});
	_inspiration.push({her:'This chicken\'s delicious. What\'s your impression of it?', his:'Buk buk buk.', ladiesFirst:true});
	_inspiration.push({her:'But marriage is a bit of an eye-opener?', his:'You know what they say, love is blind', ladiesFirst:false});
	_inspiration.push({her:'Go on, whisper those three words you know I love to hear.', his:'Here\'s... your... pie?', ladiesFirst:true});
	_inspiration.push({her:'Nice weather we\'re having, though.', his:'Isn\'t it sad when people run out of things to say to their partners?', ladiesFirst:false});
	_inspiration.push({her:'And I\'m supposed to be happy with fourth place?', his:'My three favourite things: steak, ale and pie.', ladiesFirst:false});
	_inspiration.push({her:'Amazing sense of irony for an 8-year-old.', his:'Apparently Mike\'s little boy said I\'m a "cool dad".', ladiesFirst:false});
	_inspiration.push({her:'When I married Mr. Right, I never realised his first name was "Always"!', his:'No, no, no, you pronounce it moo-sah-KAH.', ladiesFirst:false});
	_inspiration.push({her:'Notice it\'s got haddock AND salmon in it?', his:'Now you\'re just fishing for compliments.', ladiesFirst:true});
	_inspiration.push({her:'Don\'t tell me, too cheesy.', his:'I *was* going to tell you my macaroni joke.', ladiesFirst:false});
	// _inspiration.push({her:'It\'s easy to tell when you\'re not listening because you mysteriously start agreeing with everything I say.', his:'Yes, darling.', ladiesFirst:true}); // This one exceeds the 75 char limit for what she says.
	
	jQuery('a#inspiration').click(function() {
		var _rand = Math.floor(Math.random()*_inspiration.length);
		var _quote = _inspiration[_rand];
		var _rels = ['enter your caption here', '... and here'];
		
		if (_quote.ladiesFirst) {
			jQuery('.col2').addClass('ladiesFirst');
			jQuery('.col2 #ladiesFirst').attr('checked', true).parent('span').addClass('checked');
			jQuery('.her-caption').attr('rel', 'enter your caption here')
			jQuery('.his-caption').attr('rel', '... and here');
		} else {
			jQuery('.col2').removeClass('ladiesFirst');
			jQuery('.col2 #ladiesFirst').attr('checked', false).parent('span').removeClass('checked');
			jQuery('.her-caption').attr('rel', '... and here')
			jQuery('.his-caption').attr('rel', 'enter your caption here');
		}
		
		jQuery('.her-caption').val(_quote.her).removeClass('suggest-text');
		jQuery('.his-caption').val(_quote.his).removeClass('suggest-text');
		
		return false;
	});
	jQuery('#ladiesFirst').change(function() {
		// Empty the val if there was no user input.
		jQuery('.col2 textarea').each(function() {
			if (jQuery(this).val() == jQuery(this).attr('rel')) jQuery(this).val('');
		});
		if (this.checked) {
			// Switch around the textareas.
			jQuery('.col2').addClass('ladiesFirst');
			// Set the correct rel attributes.
			jQuery('.her-caption').attr('rel', 'enter your caption here')
			jQuery('.his-caption').attr('rel', '... and here');
		} else {
			// Switch the textareas back.
			jQuery('.col2').removeClass('ladiesFirst');
			// Set the correct rel attributes.
			jQuery('.her-caption').attr('rel', '... and here')
			jQuery('.his-caption').attr('rel', 'enter your caption here');
		}
		// Reset the suggestion text.
		jQuery('.col2 textarea').each(function() {
			if (jQuery(this).val() == '') jQuery(this).val(jQuery(this).attr('rel'));
		});
	});
	
	// Add a character limit to the message area on the e-card page
	jQuery('.e-card #message').keyup(function(e) {
		/* 8 = Backspace, 46 = delete */
		if (this.value.length >= 250 && (e.keyCode != 8 || e.keyCode != 46)) {
			this.value = this.value.substring(0, 250);
		}
	});
	jQuery('.e-card #is_invite').click(function() {
		if (this.checked) {
			jQuery('#invite-fields').slideDown();
		} else {
			jQuery('#invite-fields').slideUp();
		};
	});
	
	/* Contact Us Page */
	if (jQuery('.contact-us').length > 0) {
		jQuery('form:not(#form-select)').parent().parent().hide();
		jQuery('#form-select #type').change(function() {
			var _id = this.value;
			jQuery('form:not(#form-select)').not(jQuery('#'+_id)).parent().parent().hide();
			// Don't do anything if the form is already visible or if the 'please select' is selected.
			if (_id != "" ) {
				showContactForm(_id);
			}
		});
		
		var _i = location.href.indexOf('#');
		if (_i != -1) {
			var _jump = location.href.substring(_i+1);
			if (_jump == 'review') { _jump = 'feedback'; }
			jQuery('#form-select #type').val(_jump).trigger('change');
		}
	}
		// jQuery('#client2_form47_BMS_META_300, #client2_form59_BMS_META_294').change(function() {
		// 	if (jQuery(this).val() == 9) { // if 'other' was selected.
		// 		jQuery('.hear-other:hidden').slideDown();
		// 	} else {
		// 		jQuery('.hear-other:visible').slideUp().val("");
		// 	}
		// });
		// jQuery('#client2_form63_BMS_META_306').change(function() {
		// 	if (jQuery(this).val() == 4) { // if 'other' was selected.
		// 		jQuery('.hear-other:hidden').slideDown();
		// 	} else {
		// 		jQuery('.hear-other:visible').slideUp().val("");
		// 	}
		// });
});
jQuery(document).ready(function(){
	/* Contact Us Page */
	if (jQuery('.contact-us').length > 0) {
		jQuery('form:not(#form-select)').parent().parent().hide();
		jQuery('#form-select #type').change(function(e){
			var _id = this.value;
			// Don't do anything if the form is already visible or if the 'please select' is selected.
			if (_id != "" ){
				jQuery('#form-select #type').val("1");
				jQuery('form:not(#form-select)').not(jQuery('#'+_id)).parent().parent().hide();
				showContactForm(_id);
			}
		});

		var _i = location.href.indexOf('#');
		if (_i != -1) {
			var _jump = location.href.substring(_i+1);
			if (_jump == 'review') { _jump = 'feedback'; }
			jQuery('#form-select #type').val(_jump).trigger('change');
		}
	}

})
function showContactForm(id) {
	jQuery('input:not([type=hidden]):not([type=checkbox]), select, textarea').each(function() {
		//jQuery(this).val('');
	});
	jQuery('#'+id).show();
}
