/**/
jQuery(document).ready(function() {
	/** - removed as it was causing fail. Wishlist not used in April 2010 Update
	// Wish List
	$('input[@type=submit][@value="Add to Cart"]').after("<input type=\"submit\" name=\"add_to_wish_list\" value=\"Add to Wish List\" />");
	$('input[@name=add_to_wish_list]').bind('click', function() {
		$('.productListing').parent().attr('action', '../wish_list/');
	});

	/**/
	if ($('#colour').val() != 0) {
		$('.colour').val($('#colour').val());
	}

	// Bind the function
	$("select.colour").bind("change",updateProductImage);

	// Hide the images
	if ($("input#colour_count").val() != 1) {
		$('div.product-detail-thumbnail a').hide();
	}

	$("select.colour").trigger('change');

	$("select.size").bind("change", function() {

		$(this).parent().parent().parent().find('input[@type=submit]').get(0).disabled = true;

		//alert("ProductId: " + $("div.product-detail-block input[@name=atc_id]").val() +
		//			"Size:" + $(this).val() +
		//			"Colour:" + $(this).parent().parent().find('select.colour').val());

		$(this).parent().parent().find('select.quantity').load('../request_handlers/quantities.php', {
			prod_id: $("div.product-detail-block input[@name=atc_id]").val(),
			size: $(this).val(),
			colour: $(this).parent().parent().find('select.colour').val()
		}, function(){

			$(this).parent().parent().find('select.quantity').get(0).selectedIndex = 0;

			// Fix IE 7 Problem...
			$(this).parent().parent().find('select.quantity').hide();
			$(this).parent().parent().find('select.quantity').show();

			quantity = $(this).parent().parent().find('select.quantity').val();

			if (quantity > 0) {
				$(this).parent().parent().parent().find('input[@type=submit]').get(0).disabled = false
			} else {
				$(this).parent().parent().parent().find('input[@type=submit]').get(0).disabled = true;
			}


		});
	});


	$("select.size").trigger('change');

	// New - Refresh Product Criteria when a category is selected.
	$("select#category").bind("change",categoryBehaviour);

	alignProductThumbnails();
	/**/
});/**/

function alignProductThumbnails() {

  $("div#product-thumbnails-block img").each(function(){

		//alert(image_height + " - " + top_margin);

		var height = 172;
		var width  = 178;

		var image_height = $(this).height();
		var image_width = $(this).width();

		var top_margin  = (height - image_height) / 2;
		var left_margin = (width - image_width) / 2;

		if (jQuery.browser.msie) {
		  if (parseInt(jQuery.browser.version) == 6) {
		  	left_margin = left_margin / 2;
		  }
		}

		$(this).css({"float" : "left", "margin-top" : top_margin, "margin-left" : left_margin});

  });

}

function categoryBehaviour() {

	$("select#type").attr("disabled","true");

	$("div#pcv").load('../request_handlers/criteria.php', {
			category: $("select#category").val()
		},
		function() {
			$("select#category").bind("change",categoryBehaviour);
		});

	$("select#type").attr("disabled","false");

}

function updateProductImage(event) {

	// Get the id of the image to show
	showId = getImageId(this).toUpperCase();

	// Hide all the images
	//$(this).parent().parent().parent().parent().find('div.productImage img').parent().hide();
	$('div.product-detail-thumbnail a').hide();

	// Show the selected image
	// $('img#' + showId).parent().show();
	$('div.product-detail-thumbnail a img#' + showId).parent().show();

	$(this).parent().parent().find('select.size').trigger('change');

}

function getImageId(obj) {
	id = $(obj).attr("id");
	//colour = $('select#' + id + ' option:selected').text();
	colour = $('select#' + id + ' option:selected').attr('id');
	extRef = id.split('_')[0];
	showId = extRef + "_" + colour;
	return showId;
}