$(document).ready(function()
	{


		// Product image slideshow
		$('div.productImages div.productImage img').hide();
		$('div.productImages div.productImage img:first-child').show();
		$('ul.productThumbs li:first-child a').addClass('active');
		
		$('ul.productThumbs a').click(function()
			{
				$('ul.productThumbs a').removeClass('active');
				$(this).addClass('active');

				var thisTarget = $(this).attr('href');

				$('div.productImages div.productImage img').hide();
				$(thisTarget).fadeIn();
				
				// Let image thumbnails clicks  select corresponding attributes
				var value = thisTarget.substring(7);
				if ( $('div.attr input[value="' + value + '"]').length > 0 ) {
					$('div.attr input[value="' + value + '"]').attr("checked", "checked");
				}
				
				this.blur();
				return false;
			}
		);
		
		
		// Let attribute clicks select corresponding thumbnails
		$('div.attr input').click(function()
			{
				var value = $(this).attr('value');
				
				if ( $('ul.productThumbs a[href="#image-' + value + '"]').length > 0 ) {				
					$('ul.productThumbs a').removeClass('active');
					$('ul.productThumbs a[href="#image-' + value + '"]').addClass('active');
	
					$('div.productImages div.productImage img').hide();
					$('img#image-' + value).fadeIn();
				}
			}
		);
		
		
		// Hide child category lists in the sidebar, unless they're active
		$('#sidebar ul.shopCats li ul').hide();
		$('#sidebar ul.shopCats li ul:has(li.current)').show();
		
		// Then make them show/hide on click
		$('#sidebar ul.shopCats li:has(ul) a.parent').click(function()
		{
			var me = $(this).parent();
			$('#sidebar ul.shopCats li ul:visible').slideToggle();
			$('ul', me).slideToggle();
			return false;
		});
		
		
		// Hide/Show additional email fields on "Send Registry" form
		$('div.registryNotify div.row:has(input)').hide();
		$('div.registryNotify div.row:has(input):first-child').show();
		
		$('div.registryNotify label a').click(function()
		{
			var thisDiv = $(this).parents("div:first");
			$(this).fadeOut();
			thisDiv.next().slideDown();
			return false;
		});
		
		
		// Fix bad HTML on coupon code and gift certificate fields
		$('fieldset#coupons input[type="text"]').addClass("formText");
		
		
		// Hide/show billing address
		
		sameAddr = "false";
		$('label#sameAddrLabel').show();
		$('input#sameAddress').click(function()
		{
			if ( sameAddr == "true" ) {
				sameAddr = "false";
				$('#billing input, #billing select').each(function()
				{
					$(this).removeAttr("disabled", "disabled");
				});
			} else {
				sameAddr = "true";
				$('#billing input, #billing select').each(function()
				{
					$(this).attr("disabled", "disabled");
				});
			}
			$('#billing').slideToggle();
			
		});
		
		if ( $('#billing:has("div.error")').length == 0 && $('#billing input#BillAddress').val() == "" ) {
			$('#billing').hide();
			$('input#sameAddress').attr("checked", "checked");
			sameAddr = "true";
		}
		
		
//		// Order review
//		$('div#review').hide();
//		$('h3.review a').click(function()
//		{
//			$('div#review').slideToggle();
//			return false;
//		});
		
		
		
		
//		// Dynamic price display for gift certificates
//		if ( $('form#productAdd.product-Certificate').length > 0 )  {
//		
//			price = $('form#productAdd.product-Certificate option:selected').text();
//			$('p.productPrice').text(price);
//				
//			$('form#productAdd.product-Certificate select').change(function()
//			{
//				price = $('option:selected', this).text();
//				$('p.productPrice').text(price);
//			});
//		}
		

	}
);