$(document).ready(
	function()
	{

		//Add a class of "printabletip" to shopping list tagged post.
		$('.post.tag-shopping-list').addClass('printabletip');

		// Hide the "Create Shopping List" link on actual shopping list posts.
		//$('.post.tag-shopping-list img[class='shoppinglist1']').parent(a).hide();
		$(".post.tag-shopping-list img[title='shoppinglist1']").parent("a").hide();

		//Prepend a checkbox to the first non-empty em tag inside each li in each ul inside the shopping list tagged post.
		$('.post.tag-shopping-list ul li em:not(:empty)').prepend('<input type="checkbox">');


		//Add the Form that sends the data to the printable list before the sociable widget (end of post)
		var listname = $('.post-title h3 a').text();

		//Below, James added the .sociable div to flag where the Create box belongs
		$('.post.tag-shopping-list .sociable').before('<div class="createlist"><h4>Create a Printable Shopping List</h4><p>Just check the items you plan to buy &amp; click the &ldquo;Create List&rdquo; button below.</p><form id="sendlist" action="/shoppinglist/" method="post" target="_blank"><input type="hidden" name="listname" value="'+listname+'" /><input type="button" value="Create List" id="createlist" /><input type="checkbox" name="showcoupons" id="showcoupons" checked="checked" /><label for="nocoupons">Include Coupon Matchups?</label><fieldset id="checkeditems"></fieldset></form></div');


		// Process the form submission.
		$('.post.tag-shopping-list #createlist').click(function(){
			$('#checkeditems').empty();
			var showcoupons = true;
			if($('#showcoupons').not(':checked')){showcoupons = false;}
			//Looping through each UL in the post.
			$('.post.tag-shopping-list ul').each(function(){
				// Get the category name for the list from the previous H3 tag.
				var itemname = $(this).prev("h3").text();
				// Strip all spaces from the category name for us as an input name.
				var itemcat = itemname.replace(/ /g,'');
				// If the list has an h3 before it 
				var allchecked = "";
				if (itemcat){
					// For each checked input box, add the contents of the list item to a string called allchecked.
					$(this).find('input:checked').each(function(){
						allchecked += '<li>' + $(this).parent('em').parent('li').html() + '</li>';
					});
					// As long as we have some checked input boxes...
					if (allchecked != ""){
						// Add the category name to an h2 for use in the printable list and append it to the checked list items.
						allchecked = '<h2>' + itemname + '</h2></li>' + allchecked;
						// Create a hidden input in the form and add the contents of the allchecked string.
						$('#checkeditems').append('<input type="hidden" name="' + itemcat + '" id="' + itemcat + '" value="" />');
						$('input#' + itemcat).attr('value',allchecked);
					}
				}
			});
			// Finally, submit the form.
			$('form#sendlist').submit();
		});


		// Create SkipTo Links and Back ToTop anchors for Printable lists.
		// This will create anchor links to skip down to each section of the list and links to return to the top.
		var skipto = "";
		// For each h3 in shopping-list tagged posts that is not the post title...
		$('.post.tag-shopping-list h3:not(.post-title h3)').each(function(i){
			// The link name field is the text of the h3 without any spaces or colons.
			var aname = $(this).text().replace(/ /g,'').replace(':','');
			// The link text should just be the text of the h3 without colons.
			var atext = $(this).text().replace(':','');
			// If the h3 had text...
			if (aname){
				// Append an anchor tag before the h3
				$(this).before('<a name="skipto-'+aname+'"></a>');
				// Add to the list of anchor links we need to create at the top.
				skipto += '<a href="#skipto-'+aname+'">'+atext+'</a>, ';
			}
			// After each unorder list, add a link back to the top of the page.
			$(this).next('ul').after('<a href="#header" class="totop">Return to Top</a>');
		});
		// If there are any h3 tags on the page that we added to the list...
		if(skipto != ""){
			// Remove the comma and space from the last link.
			skipto = skipto.substring(0, skipto.length - 2);
			// Put all the links into a div with an id of skiptop...
			skipto = '<div id="skipto">Skip to: ' + skipto + '</div>';
			// Add the skiptop div we created above after the .wrap div which contains the title.
			$('.post.tag-shopping-list .wrap').after(skipto);
		}
	

		// Smooth scroll to anchor links using ScrollTo Plugin.
		$.localScroll();
		


	}
);
