function validate_tips_search_fields(event)
{
	var keyword = jQuery('#keyword').val();
	var cat = jQuery('#cat').val();
	var sub_cat = jQuery('#sub_cat').val();

	var key_valid = (jQuery.trim(keyword).length > 0);
	var cat_valid = (cat != -1);
	var sub_cat_valid = (sub_cat != -1);

	// Check whether nothing selected
/*	if (!key_valid && !cat_valid && !sub_cat_valid)
	{
		return false;
	}*/

	// Check if keywords entered OR category / sub category selected
	var valid = (key_valid ^ (cat_valid || sub_cat_valid));
	if (!valid)
	{
		event.preventDefault();
		alert('Either enter keywords OR select a category and an optional sub-category');
	}

	return valid;
}

function validate_tips_share_fields(event)
{
	jQuery('#tips_share .form_field').removeClass('invalid');

	var cat = jQuery('#share_tip_cat');
	var title = jQuery('#share_tip_title');
	var desc = jQuery('#share_tip_desc');

	var cat_valid = (cat.val() != -1);
	var title_valid = (jQuery.trim(title.val()).length > 0);
	var desc_valid = (jQuery.trim(desc.val()).length > 0);

	if (!cat_valid)
	{
		cat.parent().addClass('invalid');
	}

	if (!title_valid)
	{
		title.parent().addClass('invalid');
	}

	if (!desc_valid)
	{
		desc.parent().addClass('invalid');
	}

	var valid = (cat_valid && title_valid && desc_valid);
	if (!valid)
	{
		event.preventDefault();
	}
}

function validate_articles_search_fields(event)
{
	var keyword = jQuery('#keyword');
	var cat = jQuery('#cat');

	var key_valid = (jQuery.trim(keyword.val()).length > 0);
	var cat_valid = (cat.val() != -1);

	// Check if keywords entered OR category / sub category selected
	var valid = (key_valid ^ cat_valid);
	if (!valid)
	{
		event.preventDefault();
		alert('Either enter keywords OR select a category');
	}
}

function validate_swaps_search_fields(event)
{
	var keyword = jQuery('#keyword').val();
	var cat = jQuery('#cat').val();
	var sub_cat = jQuery('#sub_cat').val();

	var key_valid = (jQuery.trim(keyword).length > 0);
	var cat_valid = (cat != -1) && (sub_cat != -1);
	var single_cat = (cat != -1) ^ (sub_cat != -1);

	// Check if keywords entered OR category AND sub category selected
	if ((key_valid && cat_valid) ||
		(!key_valid && !cat_valid) ||
		(single_cat))
	{
		event.preventDefault();
		alert('Either enter keywords OR select the gender and a category for the items');
	}

	return valid;
}

function validate_swaps_share_fields(event)
{
	jQuery('#swaps_share .form_field').removeClass('invalid');

	var msg = '';
	var cat = jQuery('#share_swap_cat');
	var sub_cat = jQuery('#share_swap_sub_cat');
	var title = jQuery('#share_swap_title');
	var desc = jQuery('#share_swap_desc');
	var sell = jQuery('#sell:checked');
	var swap = jQuery('#swap:checked');
	var sellswap_value = jQuery('#share_swap_value');
	var photo = jQuery('#photo');
	var disclaimer = jQuery('#disclaimer:checked');

	var cat_valid = (cat.val() != -1);
	if (!cat_valid)
	{
		cat.parent().addClass('invalid');
	}

	var sub_cat_valid = (sub_cat.val() != -1);
	if (!sub_cat_valid)
	{
		sub_cat.parent().addClass('invalid');
	}

	var title_valid = (jQuery.trim(title.val()).length > 0);
	if (!title_valid)
	{
		title.parent().addClass('invalid');
	}

	var desc_valid = (jQuery.trim(desc.val()).length > 0);
	if (!desc_valid)
	{
		desc.parent().addClass('invalid');
	}

	var sellswap_valid = ((sell.length > 0) || (swap.length > 0));
	if (!sellswap_valid)
	{
		jQuery('#share_swap_sellswap').addClass('invalid');
	}

	var sellswap_value_valid = (jQuery.trim(sellswap_value.val()).length > 0);
	if (!sellswap_value_valid)
	{
		jQuery('#share_swap_sellswap').addClass('invalid');
	}

	var photo_valid = false;
	if (jQuery.trim(photo.val()).length > 0)
	{
		var file_parts = photo.val().split('.');
		var ext = file_parts[file_parts.length - 1].toLowerCase();
		photo_valid = ((ext == 'gif') || (ext == 'jpg') || (ext == 'jpeg') || (ext == 'png'));
	}
	else
	{
		photo_valid = true;
	}

	if (!photo_valid)
	{
		photo.parent().addClass('invalid');
		msg = 'Only photographs in JPEG, GIF, or PNG format can be uploaded';
	}

	var disclaimer_valid = true;
	if (photo.val().length > 0)
	{
		if (disclaimer.length != 1)
		{
			disclaimer_valid = false;
			jQuery('#disclaimer').parent().addClass('invalid');
			if (msg != '')
			{
				msg = msg + '. You must also ';
			}
			else
			{
				msg = msg + 'You must ';
			}

			msg = msg + 'check the photo disclaimer checkbox to indicate that you have read and understood the terms and conditions of uploading photographs';
		}
	}

	var valid = (cat_valid && sub_cat_valid && title_valid && desc_valid &&
				 sellswap_valid && sellswap_value_valid && photo_valid &&
				 disclaimer_valid);
	if (!valid)
	{
		event.preventDefault();
		if (msg != '')
		{
			alert(msg);
		}
	}
}

function validate_tips_comment_fields()
{
	var comment = jQuery('#comment').val();

	var valid = (jQuery.trim(comment).length > 0);
	if (!valid)
	{
		alert('Enter some text for your comment');
	}

	return valid;
}

function tips_search(event)
{
	// Prevent the form from being submitted
	event.preventDefault();

	// Make sure search fields filled in correctly
	if (!validate_tips_search_fields())
	{
		alert('Either enter keywords OR select a category and an optional sub-category');
		return;
	}

	// Perform an AJAX request to do the search
//	alert(jQuery('#search_tips').serialize());
	var tips_div = jQuery('#tips');
	tips_div.animate({ height: '48px' },
					 {
						 complete:
						 	function ()
							{
								tips_div.html('');
								tips_div.css('background',
											 'transparent url(wp-content/themes/2under3/images/loading.gif) 50% 0px no-repeat');
//								send_ajax_html_request();
								send_ajax_html_request(
									location.href, jQuery('#tips_search_form').serialize(),
									function (data, textStatus)
									{
										var tips_div = jQuery('#tips');
										tips_div.css('background-image', '');
										tips_div.append('<div id="hidden" style="visibility: hidden;"></div>');
										var dummy = jQuery('#hidden');
										dummy.html(data);
										var html_height = dummy.height();
										dummy.remove();
										tips_div.animate({ height: html_height },
														 {
															 complete:
																function ()
																{
																	tips_div.html(data);
																}
														 });
									},
									function (XMLHttpRequest, textStatus, errorThrown)
									{
										alert('opps');
									});
							}
					 });
}

function tips_share(event)
{
	// Prevent the form from being submitted
	event.preventDefault();

	// Make sure search fields filled in correctly
	// TODO: make validation function
	if (!validate_tips_share_fields())
	{
		alert('One or more of the required fields are not filled in');
		return;
	}

	// Perform an AJAX request to do the search
//	alert(jQuery('#search_tips').serialize());
	var tips_share_response = jQuery('#tips_share_response');
	tips_share_response.animate(
		{ height: '48px' },
		{
			complete:
				function ()
				{
					tips_share_response.html('');
					tips_share_response.css(
						'background',
						'transparent url(wp-content/themes/2under3/images/loading.gif) 50% 0px no-repeat');
					send_ajax_html_request(
						location.href, jQuery('#tips_share_form').serialize(),
						function (data, textStatus)
						{
							var tips_share_response = jQuery('#tips_share_response');
							tips_share_response.css('background-image', '');
//alert(data);
							tips_share_response.append('<div id="hidden2" style="visibility: hidden;"></div>');
							var dummy = jQuery('#hidden2');
							dummy.html(data);
							var html_height = dummy.height();
							dummy.remove();
							tips_share_response.animate(
								{ height: html_height },
								{
									complete:
										function ()
										{
											tips_share_response.html(data);
											jQuery('#tips_share_form')[0].reset();
//											clear_form_fields(jQuery('#tips_share_form'));
										}
								});
						},
						function (XMLHttpRequest, textStatus, errorThrown)
						{
							jQuery('#tips_share_form')[0].reset();
							alert('opps');
						});
				}
		});
}

// TODO: finish this
/*
function clear_form_fields(form)
{*/
/*	for (var i = 0; i < arguments.length; i++ )
	{
		var field = jQuery(arguments[i]);
//		alert("This accident was caused by " + arguments[i]);
	}*/
/*	var fields = form.find(':input');
//	fields.
}*/

function tips_add_comment(event)
{
	// Prevent the form from being submitted
	event.preventDefault();

	// Make sure search fields filled in correctly
	if (!validate_tips_comment_fields())
	{
		alert('Enter some text for your comment');
		return;
	}

	// Perform an AJAX request to do the search
//	alert(jQuery('#search_tips').serialize());
	var comment_form = jQuery('#tip_comment_form');
	var comment_form_data = comment_form.serialize();
	var url = comment_form.attr('action');
	var refresh_data = jQuery('#tip_refresh').val();
	refresh_data = refresh_data.slice(1);

	var tips_div = jQuery('#tips');
	tips_div.fadeOut('normal');
	tips_div.html('');
	tips_div.animate({ height: '48px' },
					 {
						 complete:
						 	function ()
							{
//								tips_div.html('');
								tips_div.css('background',
											 'transparent url(wp-content/themes/2under3/images/loading.gif) 50% 0px no-repeat');

								// Add the comment into WP
								send_ajax_html_request(
									url, comment_form_data,
									function (data, textStatus)
									{
										var tips_div = jQuery('#tips');
//										tips_div.css('background-image', '');
//										tips_div.append('<div id="hidden3" style="visibility: hidden;"></div>');
//										var dummy = jQuery('#hidden3');
//										dummy.html(data);
//										var html_height = dummy.height();
//										dummy.remove();
//										tips_div.animate({ height: html_height },
//														 {
//															 complete:
//																function ()
//																{
//										alert(data);
//										tips_div.html(data);
//																	jQuery('#tip_comment_form')[0].reset();
//																}
//														 });

										send_ajax_html_request(
											location.href, refresh_data,
											function (data, textStatus)
											{
												var tips_div = jQuery('#tips');
												tips_div.css('background-image', '');
												tips_div.append('<div id="hidden3" style="visibility: hidden;"></div>');
												var dummy = jQuery('#hidden3');
												dummy.html(data);
												var html_height = dummy.height();
												dummy.remove();
												tips_div.animate({ height: html_height },
																 {
																	 complete:
																		function ()
																		{
																			tips_div.html(data);
		//																	jQuery('#tip_comment_form')[0].reset();
																		}
																 });
											},
											function (XMLHttpRequest, textStatus, errorThrown)
											{
		//										jQuery('#tips_share_form')[0].reset();
												alert('opps');
											});
									},
									function (XMLHttpRequest, textStatus, errorThrown)
									{
//										jQuery('#tips_share_form')[0].reset();
										alert('opps');
									});
							}
					 });
}

function send_ajax_html_request(url, data, success_callback, failure_callback)
{
	jQuery.ajax(
	{
		url:		/*location.href*/ url,
		dataType:	'html',
		data:		/*jQuery('#tips_search_form').serialize()*/ data,
		type:		'POST',

		success:	success_callback,
/*			function (data, textStatus)
			{
				var tips_div = jQuery('#tips');
				tips_div.css('background-image', '');
				tips_div.append('<div id="hidden" style="visibility: hidden;"></div>');
				var dummy = jQuery('#hidden');
				dummy.html(data);
				var html_height = dummy.height();
				dummy.remove();
				tips_div.animate({ height: html_height },
								 {
									 complete:
									 	function ()
										{
											tips_div.html(data);
										}
								 });
			}*/

		error:		failure_callback
/*			function (XMLHttpRequest, textStatus, errorThrown)
			{
				alert('opps');
			}*/
	});
}

function tips_ajax_link_handler(event)
{
	// Prevent the form from being submitted
	event.preventDefault();

	// Get the information for the request from the link
	var alink = jQuery(this);
	var href = alink.attr('href');
	href = href.slice(1);
//	alert(href);

	var tips_div = jQuery('#tips');
	tips_div.animate({ height: '48px' },
					 {
						 complete:
						 	function ()
							{
								tips_div.html('');
								tips_div.css('background',
											 'transparent url(wp-content/themes/2under3/images/loading.gif) 50% 0px no-repeat');

								send_ajax_html_request(
									location.href, href,
									function (data, textStatus)
									{
										var tips_div = jQuery('#tips');
										tips_div.css('background-image', '');
										tips_div.append('<div id="hidden" style="visibility: hidden;"></div>');
										var dummy = jQuery('#hidden');
										dummy.html(data);
										var html_height = dummy.height();
										dummy.remove();
										tips_div.animate({ height: html_height },
														 {
															 complete:
																function ()
																{
																	tips_div.html(data);
																}
														 });
									},
									function (XMLHttpRequest, textStatus, errorThrown)
									{
										alert('opps');
									});
							}
					 });
}

function init_tips()
{
	// Generate the HTML for the tabs
/*	var tips_tabs = jQuery('#tips_tabs');
	if (tips_tabs)
	{
		var selected_tab = parseInt(jQuery('#selected_tab').val());
		tips_tabs.tabs({ selected: selected_tab });
	}*/

	// Bind an onSubmit handler for the tips search form to enable it to be done via AJAX
//	jQuery('#tips_search_form').submit(tips_search);
	jQuery('#tips_search').submit(validate_tips_search_fields);

	// Bind an onSubmit handler for the tips share form to enable it to be done via AJAX
//	jQuery('#tips_share_form').submit(tips_share);
	jQuery('#tips_share').submit(validate_tips_share_fields);

	// Bind an onClick handler to all the AJAX enabled links to handle the requests
//	jQuery('.ajax').live('click', tips_ajax_link_handler);

//	jQuery('#tip_comment_form').submit(tips_add_comment);
	jQuery('#tip_comment_form').submit(validate_tips_comment_fields);
//	jQuery('#tip_comment_form').live('submit', tips_add_comment);

//	var new_tip = jQuery('.new_tip');
//	if (new_tip)
//	{
//		new_tip.effect('pulsate');
//	}
//	jQuery('#post-new-tip').effect('highlight');
//	Effect.Pulsate('post-new-tip');

//	new Effect.Highlight('post-new-tip', { startcolor: '#ffff00', endcolor: '#ffffff', restorecolor: '#ffffff' });
//	new Effect.Highlight('post-new-tip', { startcolor: '#ffff00', endcolor: '#ffffff', restorecolor: '#ffffff', queue: 'end' });

//	new Effect.Highlight('post-new-tip', { startcolor: '#ffff00', endcolor: '#ffffff', restorecolor: '#ffffff', queue: 'end' });
}

function init_articles()
{
	// Bind an onSubmit handler for the articles search form to validate it
	jQuery('#articles_search').submit(validate_articles_search_fields);
}

/**********/

function validate_shoutbox_form_fields(event)
{
	jQuery('#shoutbox_form .form_field').removeClass('invalid');

	var msg = '';
	var title = jQuery('#title');//.val();
	var text = jQuery('#text');//.val();
	var disclaimer = jQuery('#disclaimer:checked');
	var photo = /*jQuery.trim(*/jQuery('#photo');//.val());

	var title_valid = (jQuery.trim(title.val()).length > 0);
	if (!title_valid)
	{
		title.parent().addClass('invalid');
	}

	var text_valid = (jQuery.trim(text.val()).length > 0);
	if (!text_valid)
	{
		text.parent().addClass('invalid');
	}

//	var disclaimer_valid = (disclaimer.length > 0);
	var photo_valid = false;
	if (jQuery.trim(photo.val()).length > 0)
	{
		var file_parts = photo.val().split('.');
		var ext = file_parts[file_parts.length - 1].toLowerCase();
		photo_valid = ((ext == 'gif') || (ext == 'jpg') || (ext == 'jpeg') || (ext == 'png'));
	}
	else
	{
		photo_valid = true;
	}

	if (!photo_valid)
	{
		photo.parent().addClass('invalid');
		msg = 'Only photographs in JPEG, GIF, or PNG format can be uploaded';
	}

//	if (!title_valid || !text_valid)
//	{
//		event.preventDefault();
//		alert('One or more of the required fields are not filled in');
//		return false;
//	}

//	if (!disclaimer_valid)
//	{
//		event.preventDefault();
//		alert('You must check the photo disclaimer checkbox to indicate that you have read and understood the terms and conditions of uploading photographs');
//		return false;
//	}

	var disclaimer_valid = true;
	if (photo.val().length > 0)
	{
		if (disclaimer.length != 1)
		{
			disclaimer_valid = false;
			jQuery('#disclaimer').parent().addClass('invalid');
			if (msg != '')
			{
				msg = msg + '. You must also ';
			}
			else
			{
				msg = msg + 'You must ';
			}

			msg = msg + 'check the photo disclaimer checkbox to indicate that you have read and understood the terms and conditions of uploading photographs';
		}
	}

	var valid = (title_valid && text_valid && photo_valid && disclaimer_valid);
	if (!valid)
	{
		event.preventDefault();
		if (msg != '')
		{
			alert(msg);
		}
	}
}

function show_shoutbox_form(alink)
{
//	var link_id = jQuery(this).attr('id');
//	var cntr_id = '#form_container_' + id.replace('#show_', '');

//	var alink = jQuery(id);
	var container = alink.parent().parent().find('.form_container');

	alink.fadeOut('slow');
	container.slideDown('slow');
}

function shoutbox_show_handler(event)
{
	event.preventDefault();
//	var link_id = '#' + jQuery(this).attr('id');
//	show_shoutbox_form(jQuery(this));
	var alink = jQuery('#show_link');
	var container = jQuery('#form_container');
	var buttons = jQuery('#buttons');

//	alink.css('display', 'none');
	alink.css('visibility', 'hidden');
	container.slideDown('slow');
//	buttons.fadeIn('slow');
}

function shoutbox_hide_handler(event)
{
	event.preventDefault();
//	var link_id = '#' + jQuery(this).attr('id');
//	show_shoutbox_form(jQuery(this));
	var alink = jQuery('#show_link');
	var container = jQuery('#form_container');
	var buttons = jQuery('#buttons');
	var form = container.find('form')[0];

//	container.slideUp('slow', function () { alink.css('display', 'inline'); });
	container.slideUp('slow', function () { alink.css('visibility', 'visible'); });
//	container.slideUp('slow');
	form.reset();
//	buttons.fadeIn('slow');
}

/*function reset_shoutbox_tab(tab)
{
//	var tab = jQuery(tab_id);
	var tab_link = tab.find('.show_link');
	var tab_form_container = tab.find('.form_container');
	var tab_form = tab_form_container.find('form')[0];

//alert(tab_link);
//alert(tab_form_container);
//alert(tab_form);

	if (tab_form)
	{
		tab_form.reset();
	}
//	var done_slideup = false;
//	var done_fadein = false;
//
//	tab_form_container.slideUp('slow', {} );
//	tab_link.fadeIn('slow', function () { done_fadein = true; });
//
//	while (!done_slideup || !done_fadein)
//	{
//	}
	tab_form_container.hide();
	tab_link.show();
}*/

//var current_tab_idx = 0;
//var tab_ids = ['#shoutbox_tab_giggle', '#shoutbox_tab_scream', '#shoutbox_tab_reflect'];
var tabs_urls;// = [jQuery('#url_giggle').val(), jQuery('#url_scream').val(), jQuery('#url_reflect').val()];

function shoutbox_tabs_select_handler(event, ui)
{
//	var selected_idx = ui.index;
//	var new_tab_id = tab_ids[selected_idx];

//	var current_tab_id = tab_ids[current_tab_idx];
//alert(jQuery(tab_ids[current_tab_idx]).attr('id'));
//	reset_shoutbox_tab(jQuery(tab_ids[current_tab_idx]));

//	current_tab_idx = selected_idx;

	var url = tabs_urls[ui.index];
	location.href = url;
	return false;
}

function init_shoutbox()
{
/*	tabs_urls = [jQuery('#url_giggle').val(), jQuery('#url_scream').val(), jQuery('#url_reflect').val()];

	// Generate the HTML for the tabs
	var shoutbox_tabs = jQuery('#shoutbox_tabs');
	if (shoutbox_tabs)
	{
		var selected_tab = parseInt(jQuery('#selected_tab').val());
//alert(selected_tab);
//		tips_tabs.tabs({ selected: selected_tab });
//		shoutbox_tabs.tabs();
		shoutbox_tabs.tabs({ selected: selected_tab, select: shoutbox_tabs_select_handler });
	}*/

	// Bind an onSubmit handler for the tips search form to enable it to be done via AJAX
	jQuery('#shoutbox_form').submit(validate_shoutbox_form_fields);

	jQuery('#show_link').click(shoutbox_show_handler);
	jQuery('#close_link').click(shoutbox_hide_handler);
}

function init_home()
{
/*	jQuery('#tip_carousel').jCarouselLite(
	{
		btnNext: ".tip.arrow .nxt",
		btnPrev: ".tip.arrow .prv",
		btnGo:
			['.tip.button.1', '.tip.button.2', '.tip.button.3', '.tip.button.4',
			 '.tip.button.5', '.tip.button.6', '.tip.button.7'],
		auto: 8000,
		visible: 1,
		scroll: 1,
		speed: 2000,
		initWidth: 376,
		initHeight: 123
	});

//	jQuery('#tip_carousel_container').animate({ visibility: visible }, '2000');
//	jQuery('#tip_carousel_container').fadeIn(1000);

	jQuery('#art_carousel').jCarouselLite(
	{
		btnNext: '.article.arrow .nxt',
		btnPrev: '.article.arrow .prv',
		btnGo:
			['.article.button.1', '.article.button.2', '.article.button.3', '.article.button.4',
			 '.article.button.5', '.article.button.6', '.article.button.7'],
		auto: 10000,
		visible: 1,
		scroll: 1,
		speed: 2000,
		initWidth: 617,
		initHeight: 158
	});

//	jQuery('#art_carousel_container').fadeIn(1000);

	jQuery('#recent_carousel').jCarouselLite(
	{
		btnNext: '.recent.arrow .nxt',
		btnPrev: '.recent.arrow .prv',
		btnGo:
			['.recent.button.1', '.recent.button.2', '.recent.button.3', '.recent.button.4',
			 '.recent.button.5', '.recent.button.6', '.recent.button.7'],
		auto: 9000,
		visible: 1,
		scroll: 1,
		speed: 2000,
		initWidth: 617,
		initHeight: 158
	});*/

	jQuery('#tip_carousel').queue(function ()
	{
		jQuery(this).jCarouselLite(
		{
			btnNext: ".tip.arrow .nxt",
			btnPrev: ".tip.arrow .prv",
			btnGo:
				['.tip.button.1', '.tip.button.2', '.tip.button.3', '.tip.button.4',
				 '.tip.button.5', '.tip.button.6', '.tip.button.7'],
/*			auto: 8000,*/
			auto: 10000,
			visible: 1,
			scroll: 1,
			speed: 2000,
			initWidth: 376,
			initHeight: 123
		});

		jQuery(this).dequeue();
	});

//	jQuery('#tip_carousel_container').animate({ visibility: visible }, '2000');
//	jQuery('#tip_carousel_container').fadeIn(1000);

	jQuery('#art_carousel').queue(function ()
	{
		jQuery(this).jCarouselLite(
		{
			btnNext: '.article.arrow .nxt',
			btnPrev: '.article.arrow .prv',
			btnGo:
				['.article.button.1', '.article.button.2', '.article.button.3', '.article.button.4',
				 '.article.button.5', '.article.button.6', '.article.button.7'],
			auto: 10000,
			visible: 1,
			scroll: 1,
			speed: 2000,
			initWidth: 617,
			initHeight: 158
		});

		jQuery(this).dequeue();
	});

//	jQuery('#art_carousel_container').fadeIn(1000);

	jQuery('#recent_carousel').queue(function ()
	{
		jQuery(this).jCarouselLite(
		{
			btnNext: '.recent.arrow .nxt',
			btnPrev: '.recent.arrow .prv',
			btnGo:
				['.recent.button.1', '.recent.button.2', '.recent.button.3', '.recent.button.4',
				 '.recent.button.5', '.recent.button.6', '.recent.button.7'],
/*			auto: 9000,*/
			auto: 10000,
			visible: 1,
			scroll: 1,
			speed: 2000,
			initWidth: 617,
			initHeight: 158
		});

		jQuery(this).dequeue();
	});

	jQuery('#recipe_carousel').queue(function ()
	{
		jQuery(this).jCarouselLite(
		{
			btnNext: '.recipe.arrow .nxt',
			btnPrev: '.recipe.arrow .prv',
			btnGo:
				['.recipe.button.1', '.recipe.button.2', '.recipe.button.3', '.recipe.button.4',
				 '.recipe.button.5', '.recipe.button.6', '.recipe.button.7'],
/*			auto: 7000,*/
			auto: 10000,
			visible: 1,
			scroll: 1,
			speed: 2000,
			initWidth: 617,
			initHeight: 158
		});

		jQuery(this).dequeue();
	});

	jQuery('#question_carousel').queue(function ()
	{
		jQuery(this).jCarouselLite(
		{
			btnNext: '.question.arrow .nxt',
			btnPrev: '.question.arrow .prv',
			btnGo:
				['.question.button.1', '.question.button.2', '.question.button.3', '.question.button.4',
				 '.question.button.5', '.question.button.6', '.question.button.7'],
/*			auto: 9000,*/
			auto: 10000,
			visible: 1,
			scroll: 1,
			speed: 2000,
			initWidth: 617,
/*			initHeight: 158*/
			initHeight: 100
		});

		jQuery(this).dequeue();
	});

	jQuery('#stories_carousel').queue(function ()
	{
		jQuery(this).jCarouselLite(
		{
			btnNext: '.stories.arrow .nxt',
			btnPrev: '.stories.arrow .prv',
			btnGo:
				['.stories.button.1', '.stories.button.2', '.stories.button.3', '.stories.button.4',
				 '.stories.button.5', '.stories.button.6', '.stories.button.7'],
/*			auto: 7000,*/
			auto: 10000,
			visible: 1,
			scroll: 1,
			speed: 2000,
			initWidth: 617,
			initHeight: 158
		});

		jQuery(this).dequeue();
	});

	jQuery('#directory_carousel').queue(function ()
	{
		jQuery(this).jCarouselLite(
		{
			btnNext: '.directory.arrow .nxt',
			btnPrev: '.directory.arrow .prv',
			btnGo:
				['.directory.button.1', '.directory.button.2', '.directory.button.3', '.directory.button.4',
				 '.directory.button.5', '.directory.button.6', '.directory.button.7'],
/*			auto: 7000,*/
			auto: 10000,
			visible: 1,
			scroll: 1,
			speed: 2000,
			initWidth: 617,
			initHeight: 158
		});

		jQuery(this).dequeue();
	});

//	jQuery('#recent_carousel_container').fadeIn(1000);
	jQuery('#tip_carousel_container').slideDown('slow');
	jQuery('#art_carousel_container').slideDown('slow');
	jQuery('#recent_carousel_container').slideDown('slow');
	jQuery('#recipe_carousel_container').slideDown('slow');
	jQuery('#question_carousel_container').slideDown('slow');
	jQuery('#stories_carousel_container').slideDown('slow');
	jQuery('#directory_carousel_container').slideDown('slow');
}

function init_swaps()
{
	// Bind an onSubmit handler for the swaps search form
	jQuery('#swaps_search').submit(validate_swaps_search_fields);

	// Bind an onSubmit handler for the swaps share form
	jQuery('#swaps_share').submit(validate_swaps_share_fields);

//	jQuery('#tip_comment_form').submit(validate_tips_comment_fields);
}

function init_stories()
{
//	// Bind an onSubmit handler for the tips search form to enable it to be done via AJAX
//	jQuery('#shoutbox_form').submit(validate_shoutbox_form_fields);

	jQuery('#show_link2').click(stories_show_handler);
	jQuery('#close_link').click(stories_hide_handler);
}

function stories_show_handler(event)
{
	event.preventDefault();
	var alink = jQuery('#show_link2').parent();
	var container = jQuery('#form_container');
	var buttons = jQuery('#buttons');

	alink.css('visibility', 'hidden');
	container.slideDown('slow');
}

function stories_hide_handler(event)
{
	event.preventDefault();
	var alink = jQuery('#show_link2').parent();
	var container = jQuery('#form_container');
	var buttons = jQuery('#buttons');
	var form = container.find('form')[0];

	container.slideUp('slow', function () { alink.css('visibility', 'visible'); });
	form.reset();
}

function validate_wisdom_pool_form_fields(event)
{
	jQuery('#wisdompool_form .form_field').removeClass('invalid');

	var title = jQuery('#title');
	var text = jQuery('#text');

	var title_valid = (jQuery.trim(title.val()).length > 0);
	var text_valid = (jQuery.trim(text.val()).length > 0);

	if (!title_valid)
	{
		title.parent().addClass('invalid');
	}

	if (!text_valid)
	{
		text.parent().addClass('invalid');
	}

	var valid = (title_valid && text_valid);
	if (!valid)
	{
		event.preventDefault();
	}
}

function init_wisdom_pool()
{
	// Bind an onSubmit handler for the wisdom pool search form to validate it
	jQuery('#wisdompool_form').submit(validate_wisdom_pool_form_fields);

	jQuery('#show_link3').click(wisdom_pool_show_handler);
	jQuery('#close_link').click(wisdom_pool_hide_handler);
}

function wisdom_pool_show_handler(event)
{
	event.preventDefault();
	var alink = jQuery('#show_link3').parent();
	var container = jQuery('#form_container');
	var buttons = jQuery('#buttons');

	alink.css('visibility', 'hidden');
	container.slideDown('slow');
}

function wisdom_pool_hide_handler(event)
{
	event.preventDefault();
	var alink = jQuery('#show_link3').parent();
	var container = jQuery('#form_container');
	var buttons = jQuery('#buttons');
	var form = container.find('form')[0];

	container.slideUp('slow', function () { alink.css('visibility', 'visible'); });
	form.reset();
}

function add_ingredient(event)
{
	event.preventDefault();
/*	var qty = jQuery('#qty');
	var qty_val = jQuery.trim(qty.val());
	var ing = jQuery('#ingredient');
	var ing_val = jQuery.trim(ing.val());

	if ((qty_val == '') || (ing_val == ''))
	{
		event.preventDefault();
		alert('You must specify an ingredient and a quantity for that ingredient');
		return;
	}

	var add_text = qty_val + ' ' + ing_val;
	var add_value = qty_val + '|' + ing_val;

	var $option = jQuery('<option></option>').text(add_text).val(add_value).attr("selected", true);
	jQuery('#share_recipe_ingredients').append($option).change();

	qty.val('');
	ing.val('');*/
/*	var newqty = jQuery('<div class="wrap_qty alignleft"><input name="share_recipe_quantities[]" class="qty" type="text" value="" /></div>');
	var newing = jQuery('<div class="wrap_ingredient alignleft"><input name="share_recipe_ingredients[]" class="ingredient" type="text" value="" /></div>');
	var newrem = jQuery('<div class="wrap_add alignleft"><input class="remove" type="submit" value="Remove" /></div>');
	var ings = jQuery('#share_recipe_ingredients');
	ings.append(newqty);
	ings.append(newing);
	ings.append(newrem);*/
	var ings = jQuery('#share_recipe_ingredients');
//	var count = ings.find('.ingredient_spacer').length + 1;
	var count = ings.find('.ingListItem').length;
//	var new_ing = jQuery('<div class="ingredient_spacer"><input name="share_recipe_remove[]" class="remove" type="checkbox" value="' + count +
//						 '" /><input name="share_recipe_quantities[]" class="qty" type="text" value="" />' +
//						 '<input name="share_recipe_ingredients[]" class="ingredient" type="text" value="" /></div>');
	var qty = jQuery('#new_quantity');
	var qty_val = jQuery.trim(qty.val());
	var ing = jQuery('#new_ingredient');
	var ing_val = jQuery.trim(ing.val());

	if ((qty_val != '') && (ing_val != ''))
	{
		var text = qty_val + ' ' + ing_val;
		var value = qty_val + '|' + ing_val;
		var new_ing = jQuery('<div class="ingListItem"><span class="ingListItemLabel">' + text + '</span><a href="#" class="ingListItemRemove">remove</a><input name="share_recipe_ingredients[]" type="hidden" value="' + value + '" /></div>');
		new_ing.find('a').click(remove_ingredient);
		ings.append(new_ing);

		qty.val('');
		ing.val('');
	}
}

function remove_ingredient(event)
{
	event.preventDefault();
//	var selected = jQuery('#share_recipe_ingredients input:checked').parent();
	var selected = jQuery(this).parent();
	selected.remove();
}

function init_cooking()
{
	jQuery('#recipe_search').submit(validate_recipes_search_fields);
	jQuery('#recipe_share').submit(validate_recipes_share_fields);
//	jQuery('#tip_comment_form').submit(validate_tips_comment_fields);
	jQuery('#share_recipe_cat').asmSelect();
//	jQuery('#share_recipe_ingredients').asmSelect({ selectClass: 'hidden' });
//	jQuery('#add').click(add_ingredient);
	jQuery('#add_new_ingredient').click(add_ingredient);
//	jQuery('#add').submit(add_ingredient);
//	jQuery('#remove').click(remove_ingredient);
	jQuery('#remove_ingredient').remove();
	jQuery('.ingListItemLabel').after(jQuery('<a href="#" class="ingListItemRemove">remove</a>').click(remove_ingredient));
//	jQuery('.ingListItemLabel').after(jQuery('<a href="#" class="ingListItemRemove">remove</a>').live('click', remove_ingredient));
	jQuery('.ingListItem :checkbox').remove();
}

function validate_recipes_search_fields(event)
{
	var keyword = jQuery('#keyword').val();
	var cat = jQuery('#cat').val();
//	var sub_cat = jQuery('#sub_cat').val();

	var key_valid = (jQuery.trim(keyword).length > 0);
	var cat_valid = (cat != -1);
//	var sub_cat_valid = (sub_cat != -1);

	// Check if keywords entered OR category / sub category selected
//	var valid = (key_valid ^ (cat_valid || sub_cat_valid));
	var valid = (key_valid ^ cat_valid);
	if (!valid)
	{
		event.preventDefault();
//		alert('Either enter keywords OR select a category and an optional sub-category');
		alert('Either enter keywords OR select a category');
	}

	return valid;
}

function validate_recipes_share_fields(event)
{
/*	var cat = jQuery('#share_tip_cat').val();
	var sub_cat = jQuery('#share_tip_sub_cat').val();
	var title = jQuery('#share_tip_title').val();
	var desc = jQuery('#share_tip_desc').val();

	var cat_valid = (cat != -1);
	var sub_cat_valid = true;
	var title_valid = (jQuery.trim(title).length > 0);
	var desc_valid = (jQuery.trim(desc).length > 0);

	var valid = (cat_valid && sub_cat_valid && title_valid && desc_valid);
	if (!valid)
	{
		alert('One or more of the required fields are not filled in');
	}

	return valid;*/
///////////////////////////////////////////////////
	jQuery('#recipe_share .form_field').removeClass('invalid');

	var msg = '';
	var title = jQuery('#share_recipe_title');
//	var cats = jQuery('#share_recipe_cat option[selected]');
	var cats = jQuery('#share_recipe_cat option:selected');
	var desc = jQuery('#share_recipe_desc');
	var serves = jQuery('#share_recipe_serves');
//	var makes = jQuery('#share_recipe_makes');
	var preptime = jQuery('#share_recipe_prep_time');
	var cooktime = jQuery('#share_recipe_cook_time');
//	var ings = jQuery('#share_recipe_ingredients option[selected]');
	var ings = jQuery('#share_recipe_ingredients .ingListItem');
	var instruct = jQuery('#share_recipe_instruct');
	var disclaimer = jQuery('#disclaimer:checked');
	var photo = jQuery('#photo');

	var title_valid = (jQuery.trim(title.val()).length > 0);
	if (!title_valid)
	{
		title.parent().addClass('invalid');
	}

	var cats_valid = (cats.length > 0);
	if (!cats_valid)
	{
		jQuery('#share_recipe_cat').parent().parent().addClass('invalid');
	}

	var desc_valid = (jQuery.trim(desc.val()).length > 0);
	if (!desc_valid)
	{
		desc.parent().addClass('invalid');
	}

	var serves_valid = (jQuery.trim(serves.val()).length > 0);
	if (!serves_valid)
	{
		serves.parent().addClass('invalid');
	}

/*	var makes_valid = (jQuery.trim(makes.val()).length > 0);
	if (!makes_valid)
	{
		makes.parent().addClass('invalid');
	}*/

	var preptime_valid = (jQuery.trim(preptime.val()).length > 0);
	if (!preptime_valid)
	{
		preptime.parent().addClass('invalid');
	}

	var cooktime_valid = (jQuery.trim(cooktime.val()).length > 0);
	if (!cooktime_valid)
	{
		cooktime.parent().addClass('invalid');
	}

	var ings_valid = (ings.length > 0);
	if (!ings_valid)
	{
//		jQuery('#share_recipe_ingredients').parent().parent().addClass('invalid');
		jQuery('#share_recipe_ingredients').parent().prev().addClass('invalid');
	}

	var instruct_valid = (jQuery.trim(instruct.val()).length > 0);
	if (!instruct_valid)
	{
		instruct.parent().addClass('invalid');
	}

	var photo_valid = false;
	if (jQuery.trim(photo.val()).length > 0)
	{
		var file_parts = photo.val().split('.');
		var ext = file_parts[file_parts.length - 1].toLowerCase();
		photo_valid = ((ext == 'gif') || (ext == 'jpg') || (ext == 'jpeg') || (ext == 'png'));
	}
	else
	{
		photo_valid = true;
	}

	if (!photo_valid)
	{
		photo.parent().addClass('invalid');
		msg = 'Only photographs in JPEG, GIF, or PNG format can be uploaded';
	}

	var disclaimer_valid = true;
	if (photo.val().length > 0)
	{
		if (disclaimer.length != 1)
		{
			disclaimer_valid = false;
			jQuery('#disclaimer').parent().addClass('invalid');
			if (msg != '')
			{
				msg = msg + '. You must also ';
			}
			else
			{
				msg = msg + 'You must ';
			}

			msg = msg + 'check the photo disclaimer checkbox to indicate that you have read and understood the terms and conditions of uploading photographs';
		}
	}

	var valid = (title_valid && cats_valid && desc_valid && serves_valid && /*makes_valid &&*/ preptime_valid && cooktime_valid && ings_valid &&
				 instruct_valid && photo_valid && disclaimer_valid);
	if (!valid)
	{
		event.preventDefault();
		if (msg != '')
		{
			alert(msg);
		}
	}
}

jQuery(document).ready(init_ads);
jQuery(document).ready(init_home);
jQuery(document).ready(init_tips);
jQuery(document).ready(init_articles);
jQuery(document).ready(init_shoutbox);
jQuery(document).ready(init_swaps);
jQuery(document).ready(init_stories);
jQuery(document).ready(init_wisdom_pool);
jQuery(document).ready(init_cooking);