$(document).ready(function() {
	
	$(".colors a").each(function() {
		var color = $(this).attr('rel');
		$(this).addClass('swatch').css('background-color', "#" + color);
	});
	
	//alert(dump(instock_tshirts));

	$(".section").each(function() {

		var whichoption = $(this);
		var size_field = $("input.size", this);
		var shirtcolor_field = $("input.shirtcolor", this);
		var inkcolor_field = $("input.inkcolor", this);

		// SHIRT SIZE
		$(".shirtsizes a", this).click(function() {
		
			// Remove selected class from all others
			$(".shirtsizes a", whichoption).each(function() { $(this).removeClass('selected'); });
		
			// Add selected class
			$(this).addClass('selected');
		
			// Get new size from clicked link
			var newsize_val = $(this).text();
		
			// Update Shirt Color input field
			$(size_field).val(newsize_val);
			
			return false;
		});

		// SHIRT COLORS
		$(".shirtcolors a", this).click(function() {
		
			// Remove selected class from all others
			$(".shirtcolors a", whichoption).each(function() { $(this).removeClass('selected'); });
		
			// Add selected class
			$(this).addClass('selected');
		
			// Get new color from clicked link
			var newcolor = $(this).attr('rel');
			var newcolor_val = $(this).text();
		
			// Change the color
			$(".shirtpreview div", whichoption).css({'background-color' : "#" + newcolor});
		
			// Update Shirt Color input field
			$(shirtcolor_field).val(newcolor_val);
			
			return false;
		});
	
		// INK COLORS
		$(".inkcolors a", this).click(function() {
		
			// Remove selected class from all others
			$(".inkcolors a", whichoption).each(function() { $(this).removeClass('selected'); });
		
			// Add selected class
			$(this).addClass('selected');
		
			// Get new color from clicked link
			var newcolor = $(this).attr('rel');
			var newcolor_val = $(this).text();
		
			// Change the color
			$(".inkpreview", whichoption).attr('src', "phpThumb/phpThumb.php?src=../lake.png&f=png&w=200&h=200&zc=1&fltr[]=clr|100|" + newcolor);
			
			// Update Shirt Color input field
			$(inkcolor_field).val(newcolor_val);
			
			return false;
		});
	
	});

});


function updateField(whichoption) {
	var shirtcolor = $(".shirtcolors a.selected", whichoption).attr('rel');
	var inkcolor = $(".inkcolors a.selected", whichoption).attr('rel');
	
	$('input.combo', whichoption).val("shirt" + shirtcolor + "_ink" + inkcolor);
}